Eclipse programming tips (3)-implement preference

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.