lastDir
property used to set the directory to use for the file chooser selecting an imagelimitWidth
property will be a boolean
which will specify if the width of images we open in the Plugin must be limited in the panelmaxWidth
property will be an int
which will specify the value of this propertypublic 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:
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:
lastDir
and limitWidth
properties in the plugin.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; } } }
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:@Override public void resetSettings() { maxWidthSpinner.setValue(pluginConf.maxWidth); limitWidthCb.setSelected(pluginConf.limitWidth); }We now have the following result:
Copyright 2006-2023 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences