public class SimpleMenuFactory extends AbstractMDIMenuFactory { protected void initMenus() { JMenuItem exitItem = new JMenuItem(getDefaultExitAction("Exit")); filemenu.add(exitItem); openmenu = new JMenu("Open"); filemenu.add(openmenu); JMenuItem analyseImageItem = new JMenuItem(new AnalyseImageAction("Analyze")); filemenu.add(analyseImageItem); JMenuItem aboutPluginsItem = new JMenuItem(getPluginsMenuFactory().getAboutPluginsAction()); helpmenu.add(aboutPluginsItem); staticMenuKeyMap.put(PluginElementTypes.OPEN_ACTIONS, openmenu); getPluginsMenuFactory().registerMenus(staticMenuKeyMap, null); Mbar.add(filemenu); Mbar.add(helpmenu); } }Now we can add the code for the action in the Plugin:
public class OpenImagePlugin extends AbstractSwingMDIPlugin { public static final String OPEN_IMAGES = "OpenImages"; public static final String OPEN_IMAGES_DESC = "Open Images Plugin"; private AbstractAction importImageAction; private AbstractAction analyzeImageAction; public OpenImagePlugin() { } @Override public String getPluginName() { return OPEN_IMAGES; } @Override public Object getPluginProperty(String prop) { switch (prop) { case PluginElementTypes.PROPERTY_DATE: return "undef"; case PluginElementTypes.PROPERTY_DESC: return OPEN_IMAGES_DESC; case PluginElementTypes.PROPERTY_VERSION: return "0.1"; default: return null; } } @Override public void register(MDIApplication app) throws Exception { super.register(app); importImageAction = new ImportImageAction(app, "Image"); analyzeImageAction = new AnalyzeImageAction(app, "Analyze Image"); } @Override public Object getStaticMenuElements(String menu) { if (menu.equals(PluginElementTypes.OPEN_ACTIONS)) { return importImageAction; } else if (menu.equals("Tools")) { return analyzeImageAction; } else { return null; } } }And the code for the
AnalyzeImageAction
class which is a copy of the code which was in the basic tutorial:public class AnalyseImageAction extends AbstractMDIAction { public AnalyseImageAction(String name) { super(appli, name); this.setDescription("Analyse", "Analyse Image"); } @Override public void run() throws Exception { GUIApplication gui = (GUIApplication) app; if (gui.getSelectedProperties() != null) { BufferedImage image = (BufferedImage) gui.getSelectedProperties().getObject(); JDialog dialog = new JDialog(gui.getApplicationWindow(), gui.getSelectedProperties().getName(), false); Container pane = dialog.getContentPane(); pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); pane.add(new JLabel("Width: " + image.getWidth())); pane.add(new JLabel("Height: " + image.getHeight())); pane.add(new JLabel("Type: " + image.getType())); dialog.setSize(200, 100); dialog.setVisible(true); } } }Our code is very specific, and the dialog is really basic:
MDIDialogBuilder
interface:private class AnalyseImageMenuBuilder implements MDIDialogBuilder { private final BufferedImage image; public AnalyseImageMenuBuilder(BufferedImage image) { this.image = image; } @Override public JComponent createDialogContent() { JPanel pane = new JPanel(); pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); pane.add(Box.createVerticalStrut(5)); pane.add(new JLabel("Width: " + image.getWidth())); pane.add(Box.createVerticalStrut(5)); pane.add(new JLabel("Height: " + image.getHeight())); return pane; } }We can use this class in our
AnalyzeImageAction
:public class AnalyseImageAction extends AbstractMDIAction { public AnalyseImageAction(String name) { super(appli, name); this.setDescription("Analyse", "Analyse Image"); } @Override public void run() throws Exception { GUIApplication gui = (GUIApplication) app; if (gui.getSelectedProperties() != null) { BufferedImage image = (BufferedImage) gui.getSelectedProperties().getObject(); AnalyseImageMenuBuilder builder = new AnalyseImageMenuBuilder(image); gui.showDialog(builder, MDIDialogType.UNIQUE_INSTANCE); } } }We can also customize our builder to name the dialog header:
private class AnalyseImageMenuBuilder implements MDIDialogBuilder { private final BufferedImage image; public AnalyseImageMenuBuilder(BufferedImage image) { this.image = image; } @Override public String getDialogTitle() { return "Analyze Image"; } @Override public JComponent createDialogContent() { JPanel pane = new JPanel(); pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); pane.add(Box.createVerticalStrut(5)); pane.add(new JLabel("Width: " + image.getWidth())); pane.add(Box.createVerticalStrut(5)); pane.add(new JLabel("Height: " + image.getHeight())); return pane; } }We have the following result:
public class AnalyseImageAction extends AbstractMDIAction { public AnalyseImageAction(String name) { super(appli, name); this.setDescription("Analyse", "Analyse Image"); } @Override public void run() throws Exception { GUIApplication gui = (GUIApplication) app; if (gui.getSelectedProperties() != null) { BufferedImage image = (BufferedImage) gui.getSelectedProperties().getObject(); DefaultMDIDialogBuilder builder = new DefaultMDIDialogBuilder("Analyze Image"); builder.addVerticalDialogPart("Size", new JLabel("Width: " + image.getWidth()), new JLabel("Height: " + image.getHeight())); gui.showDialog(builder, MDIDialogType.UNIQUE_INSTANCE); } } }We have the following result:
public class AnalyseImageAction extends AbstractMDIAction { public AnalyseImageAction(String name) { super(appli, name); this.setDescription("Analyse", "Analyse Image"); } @Override public void run() throws Exception { GUIApplication gui = (GUIApplication) app; if (gui.getSelectedProperties() != null) { BufferedImage image = (BufferedImage) gui.getSelectedProperties().getObject(); gui.showMessageDialog("Size", MDIDialogBuilder.INFORMATION_MESSAGE, "Width: " + image.getWidth(), "Height: " + image.getHeight()); } } }We have the following result:
Copyright 2006-2023 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences