Preference (3) in eclipse workbench (end)

Source: Internet
Author: User

(Original ENGLISH)

Translated by Frank

Preference in eclipse workbench (3)

 

Bad words preference page

 

 

 

 

 

 

 

 

 

 

We have seen how to create the simple prefrence page of color and classify it. Now we will show you how to use a complex object as a preference page and store it in preference store so that it can be edited on the preference page. In this example, we add a bad words preference, which is an array of string members.

 

 

 

 

 

 

 

 

 

 

Because preferenceconverter does not provide an API to convert the string array, we must implement it in badwordcheckerplugin. As long as it is implemented in the plug-in, we can make the APIs used by preference visible to all objects so that these objects can access it. In general, we should use preferenceconverter to convert back and forth from the storage format.

 

 

 

 

 

 

 

 

 

 

First, you must define the method for obtaining the default preference and the method for obtaining and setting the preference. They are getbadwordsdefaultpreference (which returns an array of the string type), getbadwordspreference (which also returns an array of the string type), and setbadwordspreference (which uses an array of the string type as the parameter ). The string array is stored in the preference store as a separate string separated by delimiters. We select the semicolon (;) as the separator, because it is not used in the string and will never be confused with the content.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

/**
* Return the bad words preference default.
*/
Public String [] getdefaultbadwordspreference (){
Return convert (getpreferencestore (). getdefastring string (bad_words_preference ));
}

/**
* Returns the bad words preference.
*/
Public String [] getbadwordspreference (){
Return convert (getpreferencestore (). getstring (bad_words_preference ));
}

/**
* Converts preference_delimiter delimited string to a string array.
*/
Private string [] convert (string preferencevalue ){
Stringtokenizer tokenizer =
New stringtokenizer (preferencevalue, preference_delimiter );
Int tokencount = tokenizer. counttokens ();
String [] elements = new string [tokencount];
For (INT I = 0; I <tokencount; I ++ ){
Elements [I] = tokenizer. nexttoken ();
}

Return elements;
}

/**
* Sets the bad words preference.
*/
Public void setbadwordspreference (string [] elements ){
Stringbuffer buffer = new stringbuffer ();
For (INT I = 0; I <elements. length; I ++ ){
Buffer. append (elements [I]);
Buffer. append (preference_delimiter );
}
Getpreferencestore (). setvalue (bad_words_preference, buffer. tostring ());
}

 

 

There is no field editor in jface to edit the string array, so we will set a list to display the item, and use

To add or delete items. Our performok method will send the current content

Setbadwordspreference method. The modify mdefaults method resets the content in the list.

Getdefaultbadwordspreference method. Both methods are in badwordcheckerplugin

Definition. As a list component, it uses the string array as its content, so that we can use these

Useful methods deal with the methods defined for bad words preference in the plug-in. This

Preference's performok and performdefaults methods use these methods to update and reset the list component.

Preference value. See figure 3 bad words preference page.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


/**
* Sets the contents of the nameentry field to be the default
*/
Protected void extends mdefaults (){
Badwordlist. setitems (badwordcheckerplugin. getdefault (). getdefaultbadwordspreference ());
}
/**
* Saves the author name to the preference store.
*/
Public Boolean initialize Mok (){
Badwordcheckerplugin. getdefault (). setbadwordspreference (badwordlist. getitems ());
Return super. performok ();
}

Figure 3: Displays the preference dialog box on the bad words preference page.

 

 

Monitor the value of a variable using ipropertychangelistener

 

 

 

 

 

 

 

 

 

 

Preference is frequently used to set values of other objects, or may need to be provided to an open editor or view. You can use ipropertychangelistener to listen for these changes. ipropertychangelistener is a class used to add listener to ipropertystore, so that the listener can be notified at any time. Whenever the preference changes through setvalue (), the change will be notified. Obviously, this is the case when you press the OK or Apply button in preference or import an existing preference.

 

 

 

 

 

 

 

 

 

 

In this bad word check example, we have implemented a view to highlight bad words with the selected color (see the additional code). This view defines an ipropertychangelistener, when it changes ( . When this view is created, in the init (iviewsite) function (see ), It adds an ipropertychangelistener to the preference store. When we cancel it, we delete it as a listener in the preference store (see ). The init (iviewsite) method is defined in iviewpart. The deregistering method is defined in iworkbenchpart.

 

 

 

 

 

 

 

 

 

  
  New ipropertychangelistener (){
Public void propertychange (propertychangeevent event ){
If (event. getproperty (). Equals (badwordcheckerplugin. highlight_preference )){
// Update the colors by clearing the current color,
// Updating the view and then disposing the old color.
Color oldforeground = foreground;
Foreground = NULL;
Setbadwordhighlights (text. gettext ());
Oldforeground. Dispose ();
}
If (event. getproperty (). Equals (badwordcheckerplugin. bad_words_preference ))
// Only update the text if only the words have changed
Setbadwordhighlights (text. gettext ());
}
};
 

Public void Init (iviewsite site) throws partinitexception {
Super. INIT (SITE );
Site. getpage (). addselectionlistener (...);
Badwordcheckerplugin
. Getdefault ()
. Getpreferencestore ()
. Addpropertychangelistener (preferencelistener );}

 public void dispose() {
getSite().getPage().removeSelectionListener(...);
BadWordCheckerPlugin
.getDefault()
.getPreferenceStore()
.removePropertyChangeListener(preferenceListener);
if (foreground != null)
foreground.dispose();
super.dispose();
}
Import and Export preference settings

 

 

 

 

 

 

 

 

 

 

As eclipse 2.0, you can now Import and Export preferences so that you can read them again in a new workbench. To achieve this, you only need to use the import and export functions. The current preference settings are stored in the. EPF file. This file still does not contain the default value of preference. When you import an epreference from A. EPF file, the preference values are set to the stored values. Preference not stored in the. EPF file will not be affected.

If your preference is stored and retrieved using the preference store, you do not have to do more work when the preference is saved, they will be saved as part of the preference that can be imported and exported. If you want to perform more operations during the import, you need to use ipropertychangelistener.

Conclusion

 

 

 

 

 

 

 

 

 

 

In this article, we illustrate how to use the preferences store and preferences pages provided by eclipse to operate on the plug-in to maintain and update preferences, and import and export preference using the preference dialog box. By using preference store, use the prefrence dialog box and the provided field editor. Plug-in developers can quickly develop user interfaces for preference management. If you want to find more preference about eclipse, you can refer to the Platform Plug-in development wizard in the Help perspective. The help information is provided in the preference and properties sections of the programmer wizard.

The actual code of this example can be found in preferences.zip.

(Over)

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.