With the Chinaeditor class inheriting EditorPart abstract classes, only Init,createpartcontrol two methods are implemented, and this section will step through the use of the other 5 methods.
The execution of the EditorPart method.
To use good editorpart, you first need to understand the execution flow of its methods in various situations, and add a print statement to each method of the class. After running, look at the printout, you can get the following method to execute the process.
(1) When you double-click a list item to open the editor
Init--->isdirty--->createpartcontorl--->isdirty--->isdirty--->isdirty--->isdirty---> IsDirty--->setfocus--->isdirty--->issaveasallowed.
(2) When you close the editor, SetFocus--->isdirty--->issaveasallowed--->isdirty--->issaveasallowed--->setfocus--- >isdirty If you save the editor, the Dosave () method is also executed at the end.
(3) When you click the editor title: SetFocus
(4) When the editor loses focus: IsDirty--->issavedasallowed--->isdirty----->issaveasallowed.
(5) When the editor gets focus: setFocus---->isdirty---->issaveasallowed---->isdirty----->issaveasallowed.
(6) When the editor can be saved, select the main Menu "File---> Save" or press Ctrl+s shortcut key: IsDirty--->dosave.
The role and meaning of each method:
1.boolean IsDirty ()
This method learns whether the editor is dirty (so-called "dirty" means that the value in the editor has changed), true means dirty, and when it returns true, there are two effects: an "*" number appears in the title of the editor, and the "save" item in the main menu "file" is available.
It is important to note that the editor does not judge itself as dirty, it needs to be manually set up in the program, for example, by adding a keyboard listener event to the editor's text box, and setting the IsDirty method return value to True (dirty) when the character is entered in the text box.
You can see that the execution of this method is most frequent during method execution, so do not include too time-consuming execution statements in this method. This can affect program execution speed.
2.void Dosave ()
In this method, the code that saves the editor is written, and this method is also executed when the main menu "file----> Save" is selected. But because the Save menu and Ctrl+s shortcut keys are used when IsDirty returns True, the IsDirty method controls the execution of the Dosave method.
When the save succeeds, be careful to set the dirty state back to false and call the Firepropertychange method to update the editor's interface state (the "*" number before the editor title and the "Save" menu).
3.boolean issaveasallowed ()
Allows the editor to use the Save As feature, and if this returns FALSE, you cannot use the Save As feature, and the Save as item under File in the main menu is grayed out.
4.void Dosaveas ()
Similar to the role of Dosave, where you write the "Save as" function of the processing code.
5.void SetFocus ()
The method is executed when the editor obtains the focus.
An editor instance
In this example, when the text in the Chinaeditor editor is modified, "*" appears before the editor title and the main Menu "file----> Save" is available, when the editor is saved, "*" disappears and the Save menu is not available, when the editor is dirty, Closing the editor pops up a dialog box that prompts you to save.
Chinaeditor.java (The code I posted in front of me is all, it's pasted here again)
1 Public classChinaeditorextendsEditorPart {2 Private BooleanDirty =true;//whether the editor is a dirty identity3 4 //the initialization method of editor. The first two sentences of this method are fixed.5 Public voidInit (ieditorsite site, ieditorinput input)throwspartinitexception {6System.out.println ("Init");7 SetSite (site);8 setinput (input);9 //The next sentence sets the display name of the editor title bar, otherwise the name is used in the Plugin.xml propertyTen //Setpartname (Input.getname ()); One //The next sentence sets the icon for the editor title bar, otherwise it will automatically use a default icon A //Settitleimage (Input.getimagedescriptor (). CreateImage ()); - } - the //create an interface component in Editor in this method - Public voidCreatepartcontrol (Composite parent) { -System.out.println ("Createpartcontrol"); -Composite Topcomp =NewComposite (parent, SWT.) NONE); +Topcomp.setlayout (Newfilllayout ()); -Text Text =NewText (Topcomp, SWT. BORDER); +Text.settext ("Editor of China"); A atText.addkeylistener (NewKeyadapter () { - Public voidkeypressed (KeyEvent e) { - //If the editor is not dirty (that is, it is not modified), flag it dirty and refresh the interface state - if(!IsDirty ()) { -SetDirty (true); - Firepropertychange (ieditorpart.prop_dirty); in } - } to }); + } - the //saved processing code in this method, this method is executed when the Ctrl+s key is pressed. * //finally don't forget the flag is not dirty and refresh the interface state $ Public voidDosave (Iprogressmonitor monitor) {Panax Notoginseng if(IsDirty ()) { - //... Save Editor event handling code (omitted) theSetDirty (false); + Firepropertychange (ieditorpart.prop_dirty); A } the } + - //whether to allow "Save as", False does not allow $ Public Booleanissaveasallowed () { $ return false; - } - the //The "Save As" code is written here, this example does not implement it - Public voidDosaveas () {}Wuyi the //Dirty Identifies the set method, which sets the editor to be Dirty - Public voidSetDirty (BooleanDirty) { Wu This. Dirty =Dirty; - } About $ //the contents of the editor are dirty. True dirty, false not dirty - Public BooleanIsDirty () { - returnDirty; - } A + //This method is executed when the editor has the focus, and the empty implementation of this example the Public voidSetFocus () {} -}
Program Description: Firepropertychange (ieditorpart.prop_dirty) This sentence in addition to the interface state can be refreshed, if the Ieditorpart object adds the following listener, The PropertyChange event can also be triggered.
Chinaeditor.addpropertylistener (New Ipropertylistener () {
At this point, source is the Chinaeditor object, and Propid is the constant value of Ieditorpart.prop_dirty
public void propertychanged (Object source,int propid) {
//...... Event handling code, omitted here
}
})
About Eclipse plug-in development (v) instructions for using-----Editor class methods