Preferences API and NetBeans IDE (Part 1)

In 6.0, NetBeans will come with its own Preferences API so that, for example, preferences will be stored in the NetBeans user directory. In 5.5, however, module developers must use the standard JDK Preferences API. (There are other approaches, such as the System Option class, which is deprecated in everything but name. But all other approaches are harder than using the Preferences API.) Today I learnt how to do that and it is really easy (especially for those who recall the tortuous approach described here).

Here's what you do.

  1. Create a module project and use the Options Panel wizard to generate an Options Panel extension. (Doesn't matter whether you choose "Primary Panel" or not, the procedure for adding options is the same.)

  2. Next, design the visual panel. This is what the user will see and what the user will interact with. Let's say, for example, that you need them to set the executable used by a tool that is integrated by your module. In this scenario, you might have a visual panel that looks like this:

  3. Now open the visual panel in the Source mode. Assuming your TextField is called jTextField1 and that your panel is called MymodulePanel, stick this into the load() method:

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

    Now put this in the store() method:

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

    For details on the above two lines of code, see the mountains of documentation on the Preferences API, all over the web. The OptionsPanelController class, one of the classes created by the Options Panel wizard, uses the update() and applyChanges() methods to call the load and store() methods in the visual component.

  4. Now create something that's going to use your persisted setting. For example, let's assume we have a TopComponent that lets the user do lots of stuff, including being able to see the selected executable:

  5. Now go to the Source view of the TopComponent. In the constructor, add these lines of code at the end:

    Preferences pref = Preferences.userNodeForPackage(MymodulePanel.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);

    Here, we use the same key as used in the Options window to find the related value. Then we put the value in the TextField. We also use a preferences change listener, and if the change comes from our key, we get the new value from the key.

  6. Install the module. Open the TopComponent. Go to the Options window and choose an executable. Close the Options window and notice that the specified executable appears in the TopComponent, thanks to the preferences change listener. Close the IDE and start it again. Notice that the executable is persisted and that it appears in the TopComponent and in the Options window.

  7. Now you can look at the preference, where it is stored in your system by the Preferences API. Under Windows, you can look at regedit:

    Notice that we have a node there called "SOME_KEY". That's the one we created in the Options window and worked with in the TopComponent. In 6.0, you won't need to go digging around in regedit (or wherever your system puts the preference). Instead, there'll be a new entry in config/preferences in your user directory.

Thanks to Radek and Martin for help with this topic.

Comments:

Hi,
Interesting blog, creating a preference is really simple.
But ....
The preference API might be easy to use, but in some corporate environment, client PCs are not allowed to access the registry at all.
So keep this in mind before using the Preference API.

Posted by bernhard huber on October 30, 2006 at 03:49 AM PST #

Another thing, for example, today we can copy a userdir for another machine and all things are similar as source IDE. There is a way, to copy or share this things with another machines or do backups?

[]'s

Posted by Michel Graciano on October 30, 2006 at 05:34 AM PST #

Thanks for the comments. Yes, there are disadvantages with this approach. See part 2, about the NbPreferences class, which solves this problem.

Posted by Geertjan on October 30, 2006 at 08:23 PM PST #

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