public class ImportImageAction extends AbstractMDIAction { public ImportImageAction(String name) { super(app, name); this.setDescription("Open Image", "Open Image"); } public void run() throws Exception { ((GUIApplication) app).noWriteMessages(); JFileChooser chooser = new JFileChooser("Open Image"); chooser.setDialogTitle("Open Image"); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); if (chooser.showOpenDialog(app.getApplicationWindow()) == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); BufferedImage image = ImageIO.read(file); if (image == null) { ((GUIApplication) app).getMessageArea().append("Error when Opening " + file.getName(), "red", MessageLogger.DEFAULT_FONT_SIZE, MessageLogger.BOLD); } else { JScrollPane pane = new JScrollPane(new ImagePanel(image)); gui.addTab(pane, image, file.getName()); ((GUIApplication) app).getMessageArea().append("Opened " + file.getName(), "green"); } } } }In this example, when we open an Image file, we have this message in the Message area:
public class ImportImageAction extends AbstractMDIAction { public ImportImageAction(String name) { super(app, name); this.setDescription("Open Image", "Open Image"); } public void run() throws Exception { ((GUIApplication) app).noWriteMessages(); JFileChooser chooser = new JFileChooser("Open Image"); chooser.setDialogTitle("Open Image"); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); if (chooser.showOpenDialog(app.getApplicationWindow()) == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); BufferedImage image = ImageIO.read(file); if (image == null) { ((GUIApplication) app).getMessageArea().append("Error when Opening " + file.getName(), "red", MessageLogger.DEFAULT_FONT_SIZE, MessageLogger.BOLD); } else { JScrollPane pane = new JScrollPane(new ImagePanel(image)); gui.addTab(pane, image, file.getName()); ((GUIApplication) app).getMessageArea().appendLink("Opened " + file.getName(), file.getName()); } } } }In this code:
public class ImageMessageAreaHandler implements MessageAreaHandler { @Override public void handleLinkSelection(String linkID) { MDIMessageDialogHelper.showMessageDialog("Left Click", MDIDialogBuilder.INFORMATION_MESSAGE, linkID); } }We also need to register our handler:
public class OpenImagePlugin extends AbstractMDIPlugin { public OpenImagePlugin() { } @Override public void initAfterGUI(MDIApplication app) { ((GUIApplication) app).getMessageArea().addHandler(new ImageMessageAreaHandler()); } }Here when the user selects a link, we will show a message dialog[1]
public class ImportImageAction extends AbstractMDIAction { public ImportImageAction(String name) { super(app, name); this.setDescription("Open Image", "Open Image"); } public void run() throws Exception { ((GUIApplication) app).noWriteMessages(); JFileChooser chooser = new JFileChooser("Open Image"); chooser.setDialogTitle("Open Image"); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); if (chooser.showOpenDialog(app.getApplicationWindow()) == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); BufferedImage image = ImageIO.read(file); if (image == null) { ((GUIApplication) app).getMessageArea().append("Error when Opening " + file.getName(), "red", MessageLogger.DEFAULT_FONT_SIZE, MessageLogger.BOLD); } else { JScrollPane pane = new JScrollPane(new ImagePanel(image)); gui.addTab(pane, image, file.getName()); ((GUIApplication) app).getMessageArea().appendLink("Opened " + file.getName(), file.getName(), image); } } } }Now the hyperlink also comes with the image Object.
public class MessageAreaTutorial2MDI extends AbstractMDIApplication { public MessageAreaTutorial2MDI() { super("MessageAreaTutorialMDI 2"); ... this.message.setHandleRightClick(true); }Now we will handle the right click on the link in our handler:
public class ImageMessageAreaHandler implements MessageAreaHandler { @Override public void handleLinkSelection(String linkID) { MDIMessageDialogHelper.showMessageDialog("Left Click", MDIDialogBuilder.INFORMATION_MESSAGE, linkID); } public void handleRightLinkSelection(String linkID, Object o) { if (o instanceof BufferedImage) { BufferedImage image = (BufferedImage) o; MDIMessageDialogHelper.showMessageDialog("Right Click", MDIDialogBuilder.INFORMATION_MESSAGE, "Width: " + image.getWidth(), "Height: " + image.getHeight()); } } }Now if we right-click on the same hyperlink, we will have:
Copyright 2006-2023 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences