Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

Settings tutorial



Overview

For this tutorial, we will reuse what we have done for the configuration tutorial. In this previous tutorial, we add to create the panel for the GUI which allows to set the configuration properties.

In this tutorial, we will use the unified settings menu rather than creating the panel ourself.

We will learn to:
  • Use the settings GUI component to show and set the settings
Reminf that we have 3 configuration properties:
  • The lastDir property used to set the directory to use for the file chooser selecting an image
  • The limitWidth property will be a boolean which will specify if the width of images we open in the Plugin must be limited in the panel
  • The maxWidth property will be an int which will specify the value of this property

Creating the settings menu

We will modify the Menu factory to include a settings menu:
      public class SimpleMenuFactory extends AbstractMDIMenuFactory {
        private final JMenu filemenu = new JMenu("File");
        private final JMenu settingsmenu = new JMenu("Settings");
        private final JMenu openmenu = new JMenu("Open");
        private final JMenu helpmenu = new JMenu("Help");

        public SimpleMenuFactory() {
        }

        @Override
        protected void initMenus() {
          // settings creation
             settingsmenu.add(createSettingsMenuItem());      

          // the exit action exists already, we use it
          JMenuItem exitItem = new JMenuItem(getDefaultExitAction("Exit"));

          // Specific menu for the application. This menu is handled by the main application (not by a plugin)
          JMenuItem analyseImageItem = new JMenuItem(new AnalyseImageAction("Analyse"));

          staticMenuKeyMap.put(PluginElementTypes.OPEN_ACTIONS, openmenu);
      
          registerMenus();
          progress.setProgress(progress.getProgress() + 20, "Register Plugins Menus");

          // we get the default about menu for plugins, showing each loaded plugin, and allowing to unload them
          JMenuItem aboutPluginsItem = new JMenuItem(getPluginsMenuFactory().getAboutPluginsAction());
          helpmenu.add(aboutPluginsItem);

          // create main menus
          filemenu.add(openmenu);
          filemenu.add(analyseImageItem);
          filemenu.add(exitItem);

          this.getMDISettings().setDimension(new Dimension(500, 300));

          // create Menu bar
          Mbar.add(filemenu);
          Mbar.add(settingsmenu);
          Mbar.add(helpmenu);
        }
      }
We now have the following menu bar:
settingsTutorialSettingsMenu

Set the last directory property

We will allow to set the last directory property in the application. We will do it in the Menu factory:
      public class SimpleMenuFactory extends AbstractMDIMenuFactory {

        public SimpleMenuFactory() {
        }

        @Override
        protected void initMenus() {
          settingsmenu.add(createSettingsMenuItem());
          // settings creation      
             MDISettings settings = this.getMDISettings();
             JButton fileButton = new JButton("...");
             fileButton.addActionListener(new ActionListener() {
               @Override
               public void actionPerformed(ActionEvent e) {
                 selectDirectory();
               }
             });
      
             // general settings
             MDISettingsNode.Single node = new MDISettingsNode.Single(fileButton, "Directory");
             settings.addNode("General", node);         
        }
      }
We now have the following settings tree and the General settings we just created:
settingsTutorialGeneralSettings

Set the limitWidth and maxWidth properties in the Plugin

We will now set the lastDir and limitWidth properties in the plugin.

To do that, you must create a settings node for the Plugin.

Return the settings node

We will use the MDIPlugin.getStaticMenuElements(String) method to return the node for our Plugin settings:
      public class OpenImagePlugin extends AbstractMDIPlugin {
        private MDISettingsNode settingsNode;

        public OpenImagePlugin() {
        }

        @Override
        public Object getStaticMenuElements(String menu) {
          if (menu.equals(PluginElementTypes.OPEN_ACTIONS)) {
            return importImageAction;
          } else if (menu.equals(PluginElementTypes.SETTINGS)) {
            return settingsNode;
          } else {
            return null;
          }
        }
      }      

Creates the settings node

Now we must create our settingsNode:
      @Override
      public void register(MDIApplication app) throws Exception {
        super.register(app);
        importImageAction = new ImportImageAction("Image");

        maxWidthSpinner = new JSpinner(maxWidthModel);
        maxWidthSpinner.setEditor(new JSpinner.NumberEditor(maxWidthSpinner, "####"));
        maxWidthSpinner.setMaximumSize(maxWidthSpinner.getPreferredSize());
        maxWidthSpinner.addChangeListener((ChangeEvent e) -> {
          try {
            int value = ((Integer) ((JSpinner) e.getSource()).getValue());
            if (value < 10) {
              value = 10;
            } else if (value > 1000) {
              value = 1000;
            }
            pluginConf.maxWidth = value;
          } catch (ArithmeticException ex) {
          }
        });
        limitWidthCb.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            pluginConf.limitWidth = limitWidthCb.isSelected();
          }
        });
        MDISettingsNode.Single nodeLimitWidth = new MDISettingsNode.Single(maxWidthSpinner, "Max Width");
        MDISettingsNode.Single nodeMaxWidth = new MDISettingsNode.Single(limitWidthCb, "Limit Width");
        settingsNode = new MDISettingsNode.Multiple(nodeLimitWidth, nodeMaxWidth);
      }      
We did not create a label for our settingsNode, so we will use the Plugin name for the node in the tree:

We still need to set the GUI settings for the value of the unserialized configuration when starting the application, as for the configuration tutorial:
      @Override
      public void resetSettings() {
        maxWidthSpinner.setValue(pluginConf.maxWidth);
        limitWidthCb.setSelected(pluginConf.limitWidth);
      }      
We now have the following result:
settingsTutorialPluginSettings

See also


Categories: swing | tutorials

Copyright 2006-2023 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences