我们使用myfirstplugin的示例,我们在ViewList中的111,222,333的点击时分别打开三个不同的编辑器。
首先,我们在plugin.xml的扩展选项卡中点击添加,选择org.eclipse.ui.editors扩展点,点击完成。
右击扩展点新建两个editor,一共三个editor,属性如下图所示:
右键src文件夹,点击新建类,新建3个类非别为Editor1,Editor2,Editor3,继承EditorPart类。
在init和createPartControl函数中加入实现代码:
setSite(site);
setInput(input);
setPartName(input.getName());
setTitleImage(input.getImageDescriptor().createImage());
Composite topCom = new Composite(parent, SWT.NONE);
topCom.setLayout(new FillLayout());
new Text(topCom, SWT.MULTI).setText("编辑器1");
新建InputEditor1-3 3个类,实现IEditorInput接口。getImageDescriptor和getName方法加上如下代码:
return WorkbenchImages.getImageDescriptor(IWorkbenchGraphicConstants.IMG_DTOOL_NEW_FASTVIEW);
return "111";
在ViewList中加上如下代码:
list.addMouseListener(new MouseListener() {
@Override
public void mouseDoubleClick(MouseEvent e) {
IEditorInput input = null;
String id = null;
switch (list.getSelection()[0]) {
case "111":
input = input1;
id = "myfirstplugin.editors.Editor1";
break;
case "222":
input = input2;
id = "myfirstplugin.editors.Editor2";
break;
case "333":
input = input3;
id = "myfirstplugin.editors.Editor3";
break;
}
if(input == null || id == null)
return;
IEditorPart editor = getSite().getPage().findEditor(input);
if(editor != null){
getSite().getPage().bringToTop(editor);
}
else{
try {
getSite().getPage().openEditor(input, id);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
@Override
public void mouseDown(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseUp(MouseEvent e) {
// TODO 自动生成的方法存根
}
});
点击运行,效果如图:
package myfirstplugin.views; import myfirstplugin.editors.InputEditor1; import myfirstplugin.editors.InputEditor2; import myfirstplugin.editors.InputEditor3; import org.eclipse.swt.SWT; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IViewPart; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.part.ViewPart; public class ViewList extends ViewPart { private InputEditor1 input1 = new InputEditor1(); private InputEditor2 input2 = new InputEditor2(); private InputEditor3 input3 = new InputEditor3(); public void createPartControl(Composite parent) { // TODO Auto-generated method stub Composite topCom = new Composite(parent,SWT.NONE); topCom.setLayout(new FillLayout()); final List list = new List(topCom,SWT.BORDER); list.add("111"); list.add("222"); list.add("333"); list.addSelectionListener(new SelectionAdapter(){ public void widgetSelected(SelectionEvent e) { IWorkbenchPage wbp = getViewSite().getPage(); IViewPart View1 = wbp.findView("myfirstplugin.views.ViewText"); ViewText v1 = (ViewText)View1; if(View1 == null)return; Text text = v1.getText(); text.setText(list.getSelection()[0] + "\n"); } }); list.addMouseListener(new MouseListener() { @Override public void mouseDoubleClick(MouseEvent e) { IEditorInput input = null; String id = null; switch (list.getSelection()[0]) { case "111": input = input1; id = "myfirstplugin.editors.Editor1"; break; case "222": input = input2; id = "myfirstplugin.editors.Editor2"; break; case "333": input = input3; id = "myfirstplugin.editors.Editor3"; break; } if(input == null || id == null) return; IEditorPart editor = getSite().getPage().findEditor(input); if(editor != null){ getSite().getPage().bringToTop(editor); } else{ try { getSite().getPage().openEditor(input, id); } catch (Exception e2) { e2.printStackTrace(); } } } @Override public void mouseDown(MouseEvent e) { // TODO 自动生成的方法存根 } @Override public void mouseUp(MouseEvent e) { // TODO 自动生成的方法存根 } }); } public void setFocus() { // TODO Auto-generated method stub } } package myfirstplugin.editors; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorSite; import org.eclipse.ui.PartInitException; import org.eclipse.ui.part.EditorPart; public class Editor1 extends EditorPart { @Override public void doSave(IProgressMonitor monitor) { // TODO 自动生成的方法存根 } @Override public void doSaveAs() { // TODO 自动生成的方法存根 } @Override public void init(IEditorSite site, IEditorInput input) throws PartInitException { setSite(site); setInput(input); setPartName(input.getName()); setTitleImage(input.getImageDescriptor().createImage()); } @Override public boolean isDirty() { // TODO 自动生成的方法存根 return false; } @Override public boolean isSaveAsAllowed() { // TODO 自动生成的方法存根 return false; } @Override public void createPartControl(Composite parent) { Composite topCom = new Composite(parent, SWT.NONE); topCom.setLayout(new FillLayout()); new Text(topCom, SWT.MULTI).setText("编辑器1"); } @Override public void setFocus() { // TODO 自动生成的方法存根 } } package myfirstplugin.editors; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.swt.SWT; import org.eclipse.swt.internal.win32.INPUT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorSite; import org.eclipse.ui.PartInitException; import org.eclipse.ui.part.EditorPart; public class Editor2 extends EditorPart{ @Override public void doSave(IProgressMonitor monitor) { // TODO 自动生成的方法存根 } @Override public void doSaveAs() { // TODO 自动生成的方法存根 } @Override public void init(IEditorSite site, IEditorInput input) throws PartInitException { setSite(site); setInput(input); setPartName(input.getName()); setTitleImage(input.getImageDescriptor().createImage()); } @Override public boolean isDirty() { // TODO 自动生成的方法存根 return false; } @Override public boolean isSaveAsAllowed() { // TODO 自动生成的方法存根 return false; } @Override public void createPartControl(Composite parent) { Composite topCom = new Composite(parent, SWT.NONE); topCom.setLayout(new FillLayout()); new Text(topCom, SWT.MULTI).setText("编辑器2"); } @Override public void setFocus() { // TODO 自动生成的方法存根 } } package myfirstplugin.editors; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorSite; import org.eclipse.ui.PartInitException; import org.eclipse.ui.part.EditorPart; public class Editor3 extends EditorPart{ @Override public void doSave(IProgressMonitor monitor) { // TODO 自动生成的方法存根 } @Override public void doSaveAs() { // TODO 自动生成的方法存根 } @Override public void init(IEditorSite site, IEditorInput input) throws PartInitException { setSite(site); setInput(input); setPartName(input.getName()); setTitleImage(input.getImageDescriptor().createImage()); } @Override public boolean isDirty() { // TODO 自动生成的方法存根 return false; } @Override public boolean isSaveAsAllowed() { // TODO 自动生成的方法存根 return false; } @Override public void createPartControl(Composite parent) { Composite topCom = new Composite(parent, SWT.NONE); topCom.setLayout(new FillLayout()); new Text(topCom, SWT.MULTI).setText("编辑器3"); } @Override public void setFocus() { // TODO 自动生成的方法存根 } } package myfirstplugin.editors; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IPersistableElement; import org.eclipse.ui.help.WorkbenchHelp; import org.eclipse.ui.internal.IWorkbenchGraphicConstants; import org.eclipse.ui.internal.WorkbenchImages; public class InputEditor1 implements IEditorInput { @Override public Object getAdapter(Class adapter) { // TODO 自动生成的方法存根 return null; } @Override public boolean exists() { // TODO 自动生成的方法存根 return true; } @Override public ImageDescriptor getImageDescriptor() { // TODO 自动生成的方法存根 return WorkbenchImages.getImageDescriptor(IWorkbenchGraphicConstants.IMG_DTOOL_NEW_FASTVIEW); } @Override public String getName() { // TODO 自动生成的方法存根 return "111"; } @Override public IPersistableElement getPersistable() { // TODO 自动生成的方法存根 return null; } @Override public String getToolTipText() { // TODO 自动生成的方法存根 return null; } } package myfirstplugin.editors; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IPersistableElement; import org.eclipse.ui.internal.IWorkbenchGraphicConstants; import org.eclipse.ui.internal.WorkbenchImages; public class InputEditor2 implements IEditorInput { @Override public Object getAdapter(Class adapter) { // TODO 自动生成的方法存根 return null; } @Override public boolean exists() { // TODO 自动生成的方法存根 return true; } @Override public ImageDescriptor getImageDescriptor() { // TODO 自动生成的方法存根 return WorkbenchImages.getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_EDITOR_TRIMPART); } @Override public String getName() { // TODO 自动生成的方法存根 return "222"; } @Override public IPersistableElement getPersistable() { // TODO 自动生成的方法存根 return null; } @Override public String getToolTipText() { // TODO 自动生成的方法存根 return null; } } package myfirstplugin.editors; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IPersistableElement; import org.eclipse.ui.internal.IWorkbenchGraphicConstants; import org.eclipse.ui.internal.WorkbenchImages; public class InputEditor3 implements IEditorInput { @Override public Object getAdapter(Class adapter) { // TODO 自动生成的方法存根 return null; } @Override public boolean exists() { // TODO 自动生成的方法存根 return true; } @Override public ImageDescriptor getImageDescriptor() { // TODO 自动生成的方法存根 return WorkbenchImages.getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_EXPORT_WIZ); } @Override public String getName() { // TODO 自动生成的方法存根 return "333"; } @Override public IPersistableElement getPersistable() { // TODO 自动生成的方法存根 return null; } @Override public String getToolTipText() { // TODO 自动生成的方法存根 return null; } }