Preferences API and NetBeans IDE (Part 2)

Yesterday, we were in the land of 5.5. Therefore, we 'only' were able to talk about java.util.prefs.Preferences. Today, however, is 24 hours later... and now we find ourselves in the land of 6.0. Yesterday, our preference was stored in the registry, or wherever the JDK stores it on your system. Today... because we're in the land of 6.0, and we now have org.openide.util.NbPreferences, the IDE magically stores our preference in a new folder called config/Preferences, located in the NetBeans user directory.

The cool part is that the 6.0 class NbPreferences takes the best of java.util.prefs.Preferences and replaces the worst of it. And what was the worst of it? Well, the worst of it (as seen in comments left at the end of yesterday's blog) was the fact that our preference wasn't stored where we, as NetBeans fans, would like it to be... in our NetBeans user directory. Since it is a JDK class, the JDK controls where the preference ends up. However, what is very nice, on the other hand, about java.util.prefs.Preferences, are its listeners (NodeChangeListener and PreferenceChangeListener). There's no point in creating new listeners for org.openide.util.NbPreferences while those provided by java.util.prefs.Preferences are doing a great job, right? So, org.openide.util.NbPreferences takes the best of both worlds, simplifying the implementation of java.util.prefs.Preferences and integrating with it where doing so is helpful.

Using the same example as yesterday, you'd simply replace this line in the visual Options component:

jTextField1.setText(Preferences.userNodeForPackage(MymodulePanel.class).get("SOME_KEY", ""));

...with this line:

jTextField1.setText(NbPreferences.forModule(TestmodulePanel.class).get("SOME_KEY", ""));

The only change between the two lines is in bold. Now, for storing the preference, you'd make the same change. So, yesterday you had this:

Preferences.userNodeForPackage(MymodulePanel.class).put("SOME_KEY", jTextField1.getText());

...and today you have this instead:

NbPreferences.forModule(TestmodulePanel.class).put("SOME_KEY", jTextField1.getText());

Again, the change is in bold. Almost no change at all, so migrating from the one to the other is going to be really simple. (Even search/replace would totally work.) Now, when you install the module and set the preference in the Options window, and click OK... you immediately find a new folder (if it didn't exist already) in the NetBeans user directory's config folder. The new folder is called Preferences. The folder contains a subfolder for each of the packages leading up to the package that contains the OptionsPanelController class, which is the class that calls the above two lines in the Options window. There, in the final subfolder, named after the final package, you see a file with the extension .properties. In my case, I ended up with a file called testmodule.properties, because my package structure was org.netbeans.modules.testmodule. And it has this content, because I typed "aaa" in the Options window:

#Tue Oct 31 12:27:10 CET 2006
SOME_KEY=aaa

Cool. So now I have stored my preference. (Or in fact, I haven't done anything at all. It was done for me by the NbPreferences class.) How do I get it into my code? I mean, there's no point in letting the user set a preference if you're not going to use it somewhere, right? For example, maybe the preference sets an executable or specifies a keyboard shortcut or something. Now, how would we get that preference into our code? The answer is almost identical to yesterday. If we wanted to put the preference into a TopComponent, for example, we would do it like this at the end of the constructor (the only difference from yesterday is the bit in bold):

Preferences pref = NbPreferences.forModule(TestmodulePanel.class);
String s = pref.get("SOME_KEY", "");

pref.addPreferenceChangeListener(new PreferenceChangeListener() {
    public void preferenceChange(PreferenceChangeEvent evt) {
        if (evt.getKey().equals("SOME_KEY")) {
            jTextField1.setText(evt.getNewValue());
        }
    }
});

jTextField1.setText(s);

And here you see the second cool thing about the NbPreferences class. (The first cool thing was, in case you forgot, that the preference ended up in my NetBeans user directory.) The second cool thing is that the NbPreferences class returns a normal java.util.pref.Preferences. So, we can now work with it exactly as we always did with java.util.pref.Preferences. Therefore, we just continue using the PreferenceChangeListener, so that when our preference changes in the Options window, the TopComponent is immediately updated. Cool, right?

For further reading, also for migrating a NetBeans System Option to an NbPreference, see Preferences API in NetBeans.

In other news. I've heard that there are problems downloading the 5.5 release, because of a demand that's even larger than had been thought. I've also heard that mirror sites are being set up. So, if things are downloading slowly or not at all, please be patient and please be aware that those who need to be aware of this are aware of this.

Comments:

Thanks for your blog Java is the best

Posted by Mahmoud Banna on October 30, 2006 at 11:12 PM PST #

Hi, Geertjan Am Samee, i started using netbeans from past 1 month and i was in search docs or tutorials to develop any plugin modules and a stand alone projects, i had collected the info regarding that from no. of sites i was unaware of ur Webblog :( Thanks for such an information on Netbeans, its really good ...........

Posted by Samee on March 01, 2007 at 07:31 PM PST #

This is the best place to find documentation Samee: http://platform.netbeans.org/tutorials. Another good place is the FAQ: http://wiki.netbeans.org/wiki/view/NetBeansDeveloperFAQ

Let me know if I can help you in any way. Drop me an e-mail anytime at geertjan.wielenga@sun.com.

Posted by Geertjan on March 01, 2007 at 09:47 PM PST #

Hi Geertjan,

I am working on a Netbeans based rich client which requires preferences to be stored remotely using a web service or EJB , so that a users preferences can be restored to any workstation after logon.

I was wandering if there is a way of overriding the normal NbPreferences behaviour or would it be best to define our own Preferences classes?

By the way i'm an avid reader of your blog, keep up the good work.

Posted by Andy Clark on October 03, 2007 at 05:10 PM PDT #

Hi Andy, first, thanks for the encouragement and support! Secondly, I'm not sure if NbPreferences is meant to be used in the way you describe, I would write to dev@openide.netbeans.org with this question, you're bound to find someone there who has implemented the same thing as what you're trying to create.

Posted by Geertjan on October 03, 2007 at 09:14 PM PDT #

Awesome blog!

I had yesterday, posted the following query -
"I was working on a plugin, and would like to know, if I could set a new property during runtime in Bundle.properties, by coding."

Someone replied to make use of NbPreferences, and today I saw your blog entry, it helped me a lot.

Thanks!

Posted by Varun on June 23, 2008 at 04:59 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today