Creating options panels

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.

Getting ready

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.

How to do it...

First we need to create a Options Panel.

Right-click project node, select New and Other...:

  1. On New File dialog, choose File Type, under Categories select Module Development and under File Types select Options Panel and click Next >.
  2. Under Choose Panel Type: Click on Create Primary Panel.
  3. Under Category Label enter PacktPub.
  4. Under Icon (32x32) click on Browse... and select a desired image (for this recipe the PacktPub logo will be used).
  5. Under Keywords enter Packt Pub Primary Panel and click Next >.
    How to do it...
  6. And finally under Location: Leave all fields with the default values and click Finish.

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:

  • 3 Labels
  • 2 TextFields
  • 1 Button
  • 4 CheckBoxes

Organize and rename the text of the components in the following order:

How to do it...

Save it.

How it works...

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:

How it works...

At this point our example does not load/save parameters when entered, because our panel does not have any behavior.

There's more...

How to save and load parameters, creating sub-level entries in options menu and what keywords are used for.

Saving parameters

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.

Loading parameters

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.

Secondary panel

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:

  1. Right-click project node, select New and Other....
  2. On New File dialog, choose File Type. Under Categories select Module Development and under File Types select Options Panel and click Next >.
  3. Under Panel Type: Click on Create Secondary Panel.
  4. On Primary Panel leave Advanced as default option.
  5. Then enter Packt Pub Secondary Panel as Title.
  6. And under Keywords enter Packt Pub Secondary Panel and click Next.
  7. Under Location: Leave all fields with the default values and click Finish.

Then open the PacktPubSecondaryPanelPanel.java and add the following components:

  • 2 Labels
  • 2 TextFields

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:

Secondary panel

Keywords

Developer-defined keywords are used in conjunction with the quick search box.

By only typing in the quick search, for example Packt Pub, we will have the list of the matching keywords, in our case Packt Pub Primary Panel and Packt Pub Secondary Panel, as shown by the following screenshot:

Keywords
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.249.90