Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

About tutorial



Overview

For this tutorial, we will reuse what we have done for the basic tutorial.

Show an initial About dialog

To create an initial About dialog, we will just use the MDIMenuFactory.createDefaultMDIAbout(String, boolean) method in our Menu factory:
      public class SimpleMenuFactory extends AbstractMDIMenuFactory {
         private final JMenu filemenu = new JMenu("File");
         private final JMenu openmenu = new JMenu("Open");
         private final JMenu helpmenu = new JMenu("Help");

         public SimpleMenuFactory() {
         }

         @Override
         protected void initMenus() {
           JMenuItem exitItem = new JMenuItem(getDefaultExitAction("Exit"));
           staticMenuKeyMap.put(PluginElementTypes.OPEN_ACTIONS, openmenu);
           registerMenus();
           progress.setProgress(progress.getProgress() + 20, "Register Plugins Menus");

           JMenuItem aboutItem = new JMenuItem(createDefaultMDIAbout(null, false));
           helpmenu.add(aboutItem);

           filemenu.add(openmenu);
           filemenu.add(exitItem);

           Mbar.add(filemenu);
           Mbar.add(helpmenu);
         }
      }
We will have this menu if we click on the "About" menu item:
aboutTutorial1

Use the application description

In our "About" dialog, the application description is generated automatically with the application main class. The next step will be to replace it with a custom application description. We just change the beginning of our application code:
      public class AboutTutorialMDI extends AbstractMDIApplication {
         private Preferences pref = null;

         public AboutTutorialMDI() {
           super("MDI About Example");
           conf = AppliConfiguration.getInstance();
           ApplicationDesc desc = getApplicationDesc();
              desc.setBuildDate("08/10/2023");
              desc.setVersion("0.1");
              desc.setApplicationName("About Tutorial");
              desc.setAlternateText("This is a Tutorial about the About menu");
           ImageIcon splash = new ImageIcon(this.getClass().getResource("splash.png"));
           SplashScreen splashdialog = new SplashScreen(splash, getApplicationDesc(), true);
           ...
         }
      }
We now will have the following dialog:
aboutTutorial2

Get the application description from the configuration

The next step is to get the application description from a properties file, and get it through the configuration. Let's get it in the application:
      public class AboutTutorialMDI extends AbstractMDIApplication {
         private Preferences pref = null;

         public AboutTutorialMDI() {
           super("MDI About Example");
           conf = AppliConfiguration.getInstance();
              conf.setupApplicationDesc(getApplicationDesc());
           ImageIcon splash = new ImageIcon(this.getClass().getResource("splash.png"));
           SplashScreen splashdialog = new SplashScreen(splash, getApplicationDesc(), true);
           ...
      }
The Configuration.setupApplicationDesc(ApplicationDesc) method in the configuration allows the configuration to set the content of the application description. Let's create anapp.properties file with the description we will use in the same package as our Configuration class:
      version=0.1
      date=08/10/2023
      name=About Tutorial
      desc=This is a Tutorial about the About menu
And get the content of these properties in the setupApplicationDesc method[1]
We use the MDIConfigUtils class to get the values from the properties file
:
      public class AppliConfiguration implements Configuration {
         private static AppliConfiguration conf = null;
         public File lastDir = new File(System.getProperty("user.dir"));

         private AppliConfiguration() {
         }

         public static AppliConfiguration getInstance() {
           if (conf == null) {
           conf = new AppliConfiguration();
           }
           return conf;
         }

         @Override
         public void setupApplicationDesc(ApplicationDesc appDesc) {
           MDIConfigUtils.setupApplicationDesc(this, "app.properties", appDesc);
         }
      
      }
With this code, the ApplicationDesc of our application wil be set with the content of the properties file. We have the same result when we click on the "About" item:
aboutTutorial2

Get the application description from the Plugin configuration for the Plugin

We will do the same thing for the Plugin. First we remove the Plugin.getPluginProperty(String):
      public class OpenImagePlugin extends AbstractMDIPlugin {
        private AbstractAction importImageAction;

        public OpenImagePlugin() {
        }

        @Override
        public void register(MDIApplication app) throws Exception {
          super.register(app);
          importImageAction = new ImportImageAction("Image");
        }

        @Override
        public Object getStaticMenuElements(String menu) {
          if (menu.equals(PluginElementTypes.OPEN_ACTIONS)) {
            return importImageAction;
          } else {
            return null;
          }
        }
      }
Now we have the default description for the Plugin when we click on the "About" item:
aboutTutorial3
The description does not contain the version of the Plugin, nor the build date. We can provide it in the same way we did with the application. Let's create an openImagePlugin.properties file in the Plugin package:
      version=0.1
      date=08/10/2023
      desc=Open Images Plugin
Now we will create a configuration for the Plugin, and get the content of these properties in the setupApplicationDesc method, exactly as we did for the application[1]
We use the MDIConfigUtils class to get the values from the properties file
:
      public class OpenImagePluginConfiguration implements Configuration {
         private static OpenImagePluginConfiguration conf = null;

         private OpenImagePluginConfiguration() {
         }

         public static OpenImagePluginConfiguration getInstance() {
           if (conf == null) {
             conf = new OpenImagePluginConfiguration();
           }
           return conf;
         }

         @Override
            public void setupApplicationDesc(ApplicationDesc pluginDesc) {
              MDIConfigUtils.setupApplicationDesc(this, "openImagePlugin.properties", pluginDesc);
            }

         @Override
         public void putConfiguration(Preferences p, File file) {
         }

         @Override
         public void getConfiguration(Preferences p, File file) {
         }
      }
Now all we have to do is to get these in the main Plugin class:
      public class OpenImagePlugin extends AbstractMDIPlugin {
         private AbstractAction importImageAction;
         private final OpenImagePluginConfiguration pluginConf = OpenImagePluginConfiguration.getInstance();

         public OpenImagePlugin() {
           setupPluginDesc(pluginConf);
         }

         @Override
         public void register(MDIApplication app) throws Exception {
           super.register(app);
           importImageAction = new ImportImageAction("Image");
         }

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

         @Override
         public Configuration getPluginConfiguration() {
           return pluginConf;
         }
      }

Note that the getPluginConfiguration() is not really useful in this case because the Plugin does not maintain any parameters in its configuration, so you can forget it if you don't plan to add any configuration later.

Notes

  1. ^ [1] [2] We use the MDIConfigUtils class to get the values from the properties file

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