More often than not, when creating plugins that will enhance an IDE the developers decide to place the configuration panels in the IDE's own options panel.
In this recipe we will learn how to create panels that will be placed together with NetBeans own options panel, but in a separate container, and also include our own options together with NetBeans, meaning that one tab will be included with NetBeans options.
We will need to create a new project based on NetBeans Module. Follow the Getting Ready section of the previous recipe and when Project Name is asked enter NBCookbookOptionsPanel,
using the other values as shown in the recipe.
First we need to create a Options Panel.
Right-click project node, select New and Other...:
When NetBeans opens both PacktPubPanel.java
and PacktPubOptionsPanelController.java,
select PacktPubPanel.java
in the editor and drag the following components from the Swing Palette to the editor:
Organize and rename the text of the components in the following order:
Save it.
The example in this recipe can be executed by clicking on Run the Main Project. Once the new NetBeans instance is loaded click on the menu items Tools and then Options.
By selecting Create Primary Panel during creation we have indicated to NetBeans that we want a panel placed together with all other panel options that are already default by NetBeans, the other options being including our newly created panel in one of NetBeans panels.
On the top part of the Options dialog our Primary Panel entry is placed close to the Miscellaneous button as shown in the following screenshot:
At this point our example does not load/save parameters when entered, because our panel does not have any behavior.
How to save and load parameters, creating sub-level entries in options menu and what keywords are used for.
Saving parameters is the first step to making the code in this recipe 100 percent usable.
For this we will need to open PacktPubPanel.java
and change from Design mode to Source mode.
In Source mode, find the store()
method and replace the comments inside of it with the following code:
NbPreferences.forModule(PacktPubPanel.class).put(jLabel1.getText().toLowerCase(), jTextField2.getText()); NbPreferences.forModule(PacktPubPanel.class).put(jLabel2.getText().toLowerCase(), jTextField1.getText()); NbPreferences.forModule(PacktPubPanel.class).putBoolean(jCheckBox1.getText().toLowerCase(), jCheckBox1.isSelected()); NbPreferences.forModule(PacktPubPanel.class).putBoolean(jCheckBox2.getText().toLowerCase(), jCheckBox2.isSelected()); NbPreferences.forModule(PacktPubPanel.class).putBoolean(jCheckBox3.getText().toLowerCase(), jCheckBox3.isSelected()); NbPreferences.forModule(PacktPubPanel.class).putBoolean(jCheckBox4.getText().toLowerCase(), jCheckBox4.isSelected());
Then save it and run the example.
The information is saved to the following file:
NBCookbookOptionsPanel/build/testuserdir/config/Preferences/com/packtpub.properties
By opening it, it is possible to check how the data and the keys are displayed.
After saving the parameters the next logical step is to load them.
This can be easily achieved by following almost the same steps as in the previous saving parameters example:
Open PacktPubPanel.java
, find the load()
method, and replace the comments inside of the method body with the following code:
jTextField1.setText(NbPreferences.forModule(PacktPubPanel.class).get(jLabel2.getText().toLowerCase(), "")); jTextField2.setText(NbPreferences.forModule(PacktPubPanel.class).get(jLabel1.getText().toLowerCase(), "")); if(NbPreferences.forModule(PacktPubPanel.class).get(jCheckBox1.getText().toLowerCase(), "").equals("true")) jCheckBox1.setSelected(true); else jCheckBox1.setSelected(false); if(NbPreferences.forModule(PacktPubPanel.class).get(jCheckBox2.getText().toLowerCase(), "").equals("true")) jCheckBox2.setSelected(true); else jCheckBox2.setSelected(false); if(NbPreferences.forModule(PacktPubPanel.class).get(jCheckBox3.getText().toLowerCase(), "").equals("true")) jCheckBox3.setSelected(true); else jCheckBox3.setSelected(false); if(NbPreferences.forModule(PacktPubPanel.class).get(jCheckBox4.getText().toLowerCase(), "").equals("true")) jCheckBox4.setSelected(true); else jCheckBox4.setSelected(false);
Save the file.
Next time when the the IDE is restarted after parameters are inserted, the values will be retained.
The secondary panel is the one included in NetBeans own options instead of having its own tab.
To create a secondary panel follow the following instructions:
Then open the PacktPubSecondaryPanelPanel.java
and add the following components:
Save it and run the example by clicking on the Run Main Project icon.
After organizing the components and renaming them the new secondary options panel, already running in our example, should look more or less like this:
3.22.249.90