The decision whether to use standard or enhanced NetBeans preference tree is left up to the developer.
NbPreferences.root()
returns root preference node. NbPreferences.forModule(class cls)
returns
preference node which absolute path
depends whether class provided as a parameter was loaded as a
part of any module or not. If so, then absolute path corresponds to
slashified code name base of module. If not, then absolute path
corresponds to class's package
config/Preferences/ `-- org |-- apache | `-- tools | `-- ant | `-- module.properties `-- netbeans `-- modules `-- derby.properties
prefs.properties is regular properties file. See:
#Thu Sep 28 18:15:22 CEST 2006
location=/tmp/location
systemHome=/tmp/systemHome
- <folder name="Services"/> - <file name="org-netbeans-modules-derby-DerbyOptions.settings" url="DerbyOptions.settings"/> - </folder>
- <folder name="Services"> - <folder name="IDEConfiguration"> - <folder name="ServerAndExternalToolSettings"> - <file name="org-netbeans-modules-derby-DerbyOptions.shadow"> - <attr name="originalFile" stringvalue="Services/org-netbeans-modules-derby-DerbyOptions.settings"/> - </file> - </folder> - </folder> - </folder>
-public class DerbyOptions extends SystemOption { - - private static final long serialVersionUID = 1101894610105398924L; +public class DerbyOptions { + private static DerbyOptions INSTANCE = new DerbyOptions(); public static DerbyOptions getDefault() { - return (DerbyOptions)SharedClassObject.findObject(DerbyOptions.class, true); + return INSTANCE; }
+ protected final String putProperty(String key, String value, boolean notify) { + String retval = getPreferences().get(key, null); + if (value != null) { + NbPreferences.forModule(DerbyOptions.class).put(key, value); + } else { + NbPreferences.forModule(DerbyOptions.class).remove(key); + } + return retval; + } + protected final String getProperty(String key) { + return NbPreferences.forModule(DerbyOptions.class).get(key, null); + }
+ public static BeanNode createViewNode() throws IntrospectionException { + return new BeanNode(DerbyOptions.getDefault()); + } - <file name="org-netbeans-modules-derby-DerbyOptions.shadow"> - <attr name="originalFile" stringvalue="Services/org-netbeans-modules-derby-DerbyOptions.settings"/> + <file name="DerbyOptionsNode.instance"> + <attr name="instanceCreate" methodvalue="org.netbeans.modules.derby.DerbyOptions.createViewNode"/>The other way means to go a little more farther and provide and install custom options panels/categories to Options Dialog according to the Options Dialog API. The module development support in NetBeans can help by generating boilerplate code and registering the option into your layer.
<answer id="resources-preferences"> <api group="preferences" name="org.netbeans.modules.derby" type="export" category="private" url=""> <table> <tbody> <tr> <th>key</th> <th>description</th> <th>read</th> <th>write</th> </tr> <tr> <td>location</td> <td>Derby location or an empty string if the Derby location is not set</td> <td>x</td> <td>x</td> </tr> <tr> <td>systemHome</td> <td>Derby system home or an empty string if the system home is not set</td> <td>x</td> <td>x</td> </tr> </tbody> </table> </api> </answer>
How to start? The best is to start with tests. But first you must have settings file that will be parsed and tested. Put this file among tests:
test/unit/src/org/netbeans/upgrade/systemoptions |-- BasicTestForImport.java |-- DerbyOptionsTest.java |-- org-netbeans-modules-derby-DerbyOptions.settingsThen write simple test that should contain assertions what actually will be parsed and imported. See:
public class DerbyOptionsTest extends BasicTestForImport { public DerbyOptionsTest(String testName) { super(testName, "org-netbeans-modules-derby-DerbyOptions.settings"); } public void testPreferencesNodePath() throws Exception { assertPreferencesNodePath("/org/netbeans/modules/derby"); } public void testPropertyNames() throws Exception { assertPropertyNames(new String[] { "systemHome", "location" }); } public void testSystemHome() throws Exception { assertPropertyType("systemHome","java.lang.String"); assertProperty("systemHome","/tmp/systemHome"); } public void testLocation() throws Exception { assertPropertyType("location","java.lang.String"); assertProperty("location","/tmp/location"); } }If the tests didn't pass then probably there is more complicated object graph serialized then you must subclass
PropertyProcessor
and put your
own code in.
(See as an example: TaskTagsProcessor
and here is a test).
java.util.prefs.Preferences
is installed in place of the platform-specific default implementation for
running tests which is in spirit of unit testing because individual tests
shouldn't interact. But if this behaviour isn't suitable then there is possible
to reinstall platform-specific default implementation again:
+ public void run(final TestResult result) { + //just initialize Preferences before code NbTestCase + Preferences.userRoot(); + super.run(result); + }