Eclipse plug-in development learning note "6"---add editor
We use the Myfirstplugin example, and we open three different editors at 111,222,333 clicks in viewlist.
First, we click Add in the Plugin.xml extension tab, select the Org.eclipse.ui.editors extension point, and click Finish.
Right-click the extension point to create a new two editor, altogether three editor, as shown in the properties:
Right-click on the SRC folder to create a new class, create 3 classes for Editor1,editor2,editor3, and inherit the EditorPart class.
Add the implementation code to the INIT and Createpartcontrol functions:
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 ("Editor 1");
Create a new Inputeditor1-3 3 class to implement the Ieditorinput interface. The Getimagedescriptor and GetName methods add the following code:
Return Workbenchimages.getimagedescriptor (Iworkbenchgraphicconstants.img_dtool_new_fastview);
Return "111";
Add the following code to the 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 Auto-generated method stubs
}
@Override
Public void mouseUp (MouseEvent e) {
TODO Auto-generated method stubs
}
});
Click Run, Effect
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 ();p rivate InputEditor2 input2 = new InputEditor2 ();p rivate InputEditor3 input3 = NE W 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 () {@Overridepublic void MouseDoubleClick (MouseEvent e) {ieditorinput input = Null String id = null;switch (list.getselection () [0]) {case "111": input = Input1;id = "Myfirstplugin.editors.Editor1"; 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) {Getsi Te (). GetPage (). Bringtotop (editor);} Else{try {getsite (). GetPage (). Openeditor (input, id),} catch (Exception E2) {e2.printstacktrace ();}}} @Overridepublic void MouseDown (MouseEvent e) {//Todo automatically generated method stub} @Overridepublic void MouseUp (MouseEvent e) {//TODO auto-generated party (French stub}}); } 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 {@Overridepublic void Dosave (Iprogressmonitor monitor) {Todo Auto-generated method stub} @Overridepublic void Dosaveas () {//TODO auto-generated method stub} @Overridepublic void Init (ieditorsite site, Ieditori Nput input) throws Partinitexception {setSite (site); setinput (input); Setpartname (Input.getname ()); Settitleimage ( Input.getimagedescriptor (). CreateImage ());} @Overridepublic Boolean IsDirty () {//TODO auto-generated method stub return false;} @Overridepublic Boolean issaveasallowed () {//TODO auto-generated method stub return false;} @Overridepublic void Createpartcontrol (Composite parent) {Composite topcom = new Composite (parent, SWT. NONE); Topcom.setlayout (new Filllayout ()); New Text (Topcom, SWT. MULTI). SetText ("Editor 1"); @Overridepublic 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.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{@ overridepublic void Dosave (Iprogressmonitor monitor) {//Todo automatically generated method stub} @Overridepublic void Dosaveas () {//Todo automatically generated method save root} @Overridepublic void init (ieditorsite site, ieditorinput input) throws Partinitexception {setSite (site); SetInput ( input); Setpartname (Input.getname ()); Settitleimage (Input.getimagedescriptor (). CreateImage ());} @Overridepublic Boolean IsDirty () {//TODO auto-generated method stub return false;} @Overridepublic Boolean issaveasallowed () {//TODO auto-generated method stub return false;} @Overridepublic void Createpartcontrol (Composite parent) {Composite topcom = new Composite (parent, SWT. NONE); Topcom.setlayout (new Filllayout ()); New Text (Topcom, SWT. MULTI). SetText ("Editor 2"); @Overridepublic 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 Editor3 extends editorpart{@Overridepublic void Dosave (Iprogressmonitor monitor) {//TODO auto-generated method stub} @Overridepublic void Dosaveas () {// TODO Auto-generated method stub} @Overridepublic void Init (ieditorsite site, ieditorinput input) throws Partinitexception {setSite (site); SetInput (input); Setpartname (Input.getname ()); Settitleimage (Input.getimagedescriptor (). CreateImage ());} @Overridepublic Boolean IsDirty () {//TODO auto-generated method stub return false;} @Overridepublic Boolean issaveasallowed () {//TODO auto-generated method stub return false;} @Overridepublic void Createpartcontrol (Composite parent) {Composite topcom = new Composite (parent, SWT. NONE); Topcom.setlayout (new Filllayout ()); New Text (Topcom, SWT. MULTI). SetText ("Editor 3"); @Overridepublic void SetFocus () {//TODO auto-generated method stub}}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 {@Overridepublic Object Getadapter (Class adapter) {//TODO auto-generated method stub return null;} @Overridepublic Boolean exists () {//TODO auto-generated method stub return true;} @Overridepublic imagedescriptor Getimagedescriptor () {//TODO auto-generated method stub return Workbenchimages.getimagedescriptor ( Iworkbenchgraphicconstants.img_dtool_new_fastview);} @Overridepublic String GetName () {//TODO auto-generated method stub return "111";} @Overridepublic ipersistableelement getpersistable () {//TODO auto-generated method stub return null;} @Overridepublic String GetToolTipText () {//TODO auto-generated method stub 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 {@Overridepublic Object getadapter (Class adapter) {//TODO auto-generated method stub return null;} @Overridepublic Boolean exists () {//TODO auto-generated method stub return true;} @Overridepublic imagedescriptor Getimagedescriptor () {//TODO auto-generated method stub return Workbenchimages.getimagedescriptor ( Iworkbenchgraphicconstants.img_etool_editor_trimpart);} @Overridepublic String GetName () {//TODO auto-generated method stub return "222";} @Overridepublic ipersistableelement getpersistable () {//TODO auto-generated method stub return null;} @Overridepublic String GetToolTipText () {//TODO auto-generated method stub 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 {@OverridepublicObject Getadapter (Class adapter) {//TODO auto-generated method stub return null;} @Overridepublic Boolean exists () {//TODO auto-generated method stub return true;} @Overridepublic imagedescriptor Getimagedescriptor () {//TODO auto-generated method stub return Workbenchimages.getimagedescriptor ( Iworkbenchgraphicconstants.img_etool_export_wiz);} @Overridepublic String GetName () {//TODO auto-generated method stub return "333";} @Overridepublic ipersistableelement getpersistable () {//TODO auto-generated method stub return null;} @Overridepublic String GetToolTipText () {//TODO auto-generated method stub return null;}}
Eclipse plug-in development learning note "6"---add editor