1. An editor must correspond to an ieditorinput and editorpart. Iworkbenchpage obtains editorpart Based on ieditorinput.
In common applications, double-click a node in a table or tree and open the editor to edit the node. At this time, only one editor can appear at most for a node, that is, if
If an editor has been opened for a node, you should not open another editor. Instead, you should pin the editor that has been opened. "pin" indicates activating the editor here.
There are usually two ways to create a singleton for ieditorinput, so that there will be only one ieditorinput from start to end, so that there is only one editorpart.
Of course, this method is a bit strange. I personally like to use the second method, because it is more natural. Method 2: You can do this. When generating a tree or table, you can also set the ieditorinput of this node,
For the node type, add two methods, setinput (ieditorinput input) and getinput (). In this way, when you double-click a node, the input is directly obtained from the node. This ensures that only one
Editor.
2. Editor Loading
The editor is loaded through openeditor (ieditorinput input, string editorid) of iworkbenchpage.
3. Set the top of the editor. View Code directly
Iworkbenchpage WPG = getviewsite (). getpage ();
Ieditorpart editorpart = wpgfindediotr (entry. getinput (); // entry is the type of the node.
// If the editor already exists, set it to the current Editor (at the top). Otherwise, open an editor again.
If (editorpart! = NULL ){
WPG. bringtotop (editorpart );
} Else {
Try {
WPG. openeditor (entry. getinput (), myediotr. ID );
// If you need to perform some operations on the instance that obtains the editorpart, you can do so.
Myeditor myeidtor = (myeditor) WPG. openeditor (entry. getinput (), myediotr. ID); // pay attention to forced Transformation
// After obtaining the myeditor instance, you can perform various operations you want.
} Catch (partinitexception e ){
E. printstacktrace ();
}
}
5. ieditorinput method description
① Public Boolean exists (){
Return true; // if true is returned, the editor appears in the recently opened document column at the bottom of the eclipse Main Menu "File. If the return value is false, the error message is not displayed.
}
② Public imagedescriptor getimagedescriptor (){
Return workbenchimages. getimagedescriptor (iworkbenchgraphicconstans. img_etool_home_nav );
}
Editor title bar icon, but it also needs to be set in the editor using the settitleimage method to appear in the title bar
For example, settitleimage (input. getimagedescriptor (). createimage ());
③ Public String getname ();
The display name of the title bar of the editor. Like the above getimagedescriptor, you must set the setpartname () method in the editorpart to appear in the title bar, as shown in figure
Setpartname (input. getname ());
④ Public String gettooltiptext ()
Small yellow text in the title bar of the editor, which does not need to be set in editorpart like getname
⑤ Public ipersistableelement getpersistable ()
Returns an object that can be used to save the input data status of this editor.
⑥ Public object getadapter (class adapter)
Get an editor adapter as follows:
// Get an editor Adapter
// Iadaptable A = new chinaeditorinput (); // The reason why New chinaeditorpart can be directly assigned to iadapteble is that ieditorinput is extended from iadaptable
// Ifoo x = (ifoo) A. getadapter (ifoo. Class );
// If (X! = NULL) [Use X to do ifoo...]