Preference can be used to set system parameters so that users can define their own settings. For example, eclipse provides a lot of content that can be set, but for the editor, you can set the font and color. So how to implement a preference extension point?
It turned out to be simple. eclipse already provides a large number of classes for use in jface. You only need to extend fieldeditorpreferencepage. For example:
/**
*
*/
Public class imchatpreference extends fieldeditorpreferencepage implements
Iworkbenchpreferencepage {
/**
* @ Param Style
*/
Public imchatpreference (){
Super (GRID );
Setpreferencestore (implugin. getdefault (). getpreferencestore (); // set the location where you want to save the settings, which is obtained through plugin.
}
/**
* @ See org. Eclipse. UI. iworkbenchpreferencepage # Init (Org. Eclipse. UI. iworkbench)
*/
Public void Init (iworkbench Workbench ){
}
Private void createcolorcontrol (composite parent, string name,
String labeltext ){
Colorfieldeditor field = new colorfieldeditor (name, labeltext, parent); // This is a color setting component.
Field. setpage (this );
Field. setpreferencestore (getpreferencestore ());
Addfield (field );
}
Private void createfontcontrol (composite parent, string name,
String labeltext ){
Fontfieldeditor field = new fontfieldeditor (name, labeltext,
Parent); // This is a component used to set the font.
Field. setpage (this );
Field. setpreferencestore (getpreferencestore ());
Addfield (field );
}
Public Boolean initialize Mok (){
Implugin. getdefault (). savepluginpreferences (); // when you press the OK button, the configuration is saved.
Return super. performok ();
}
Public ipreferencestore dogetpreferencestore (){
Return implugin. getdefault (). getpreferencestore (); // obtain the store for storing the configuration.
}
/**
* @ See org. Eclipse. jface. Preference. fieldeditorpreferencepage # createfieldeditors ()
*/
@ Override
Public void createfieldeditors () {// This method is used to create an interface. Customize the interface, how to place controls, etc.
Final composite parent = getfieldeditorparent ();
Final composite container = new composite (parent, SWT. null );
Gridlayout layout = new gridlayout (1, false );
Layout. marginwidth = layout. marginheight = 0;
Container. setlayout (layout );
Container. setlayoutdata (New griddata (griddata. fill_both ));
Group = new group (container, SWT. null );
Group. setlayoutdata (New griddata (griddata. fill_horizontal ));
Group. settext (implugin. getresourcestring ("msgbox. Title "));
Group. settooltiptext (group. gettext ());
Createcolorcontrol (group, imconsts. key_chat_background_color, implugin
. Getresourcestring (imconsts. key_chat_background_color ));
Createcolorcontrol (group, imconsts. key_chat_foreground_color, implugin
. Getresourcestring (imconsts. key_chat_foreground_color ));
Createfontcontrol (group, imconsts. key_chat_font, implugin
. Getresourcestring (imconsts. key_chat_font ));
Group = new group (container, SWT. null );
Group. setlayoutdata (New griddata (griddata. fill_horizontal ));
Group. settext (implugin. getresourcestring ("msgsend. Title "));
Group. settooltiptext (group. gettext ());
Createcolorcontrol (group, imconsts. key_send_background_color, implugin
. Getresourcestring (imconsts. key_send_background_color ));
Createcolorcontrol (group, imconsts. key_send_foreground_color, implugin
. Getresourcestring (imconsts. key_send_foreground_color ));
Createfontcontrol (group, imconsts. key_send_font, implugin
. Getresourcestring (imconsts. key_send_font ));
}
}
Eclipse also provides stringfieldeditor and integerfieldeditor for ease of use.
The next step is to set the extension points. I will not go into details.
The above code is achieved by reading the preference of Eclipse. At the very beginning, I processed various inputs myself and set the color of the results. When I re-opened the interface, the appearance was incorrect because the interface could not be refreshed. Later, I repeatedly studied eclipse and finally found that it had been processed for a long time. Ah, a lot of time is wasted :(
Hope to help you.