Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

Dynamic menus



Dynamic menus are menus which will only be present depending on the selected tab Metadatas.

Creating dynamic menus

Dynamic menus are menus which will only be present depending on the selected tab Metadatas. They are created by the MDIMenuFactory.addToDynamicMenuMap(String, JComponent) or MDIMenuFactory.addToDynamicMenuMap(String, AbstractAction) methods.

Creating dynamic menus with JMenus

The most straightforward way to do this is to just add a key for a JMenu, such as for example:
   public class MyMenuFactory extends AbstractMDIMenuFactory {
     protected void initMenus() {
       JMenu menu = new JMenu("Save File");
       addToDynamicMenuMap("save", menu);
     }
   }
The idea is to add elements in the menu ( AbstracAtion, JMenuItem, or JButton depending on the nature of the selected tab. If it is not possible to add any element under the menu, the menu will be disabled.

Creating dynamic menus with other components

It is also possible to create a dynamic menu with an AbstracAtion, JMenuItem, or JButton, such as for example:
   public class MyMenuFactory extends AbstractMDIMenuFactory {
     protected void initMenus() {
       JMenu menu = new JMenu("File");
       JMenuItem menuItem = new JMenuItem("Save");
       addToDynamicMenuMap("save", menuItem);
     }
   }
In that case, the element will be replaced by another one depending on the nature of the selected tab, or disabled if no element is available associated with the tab.

Using dynamic menus

The methods which can be used to add elements under the menu are:

Using dynamic menus with created JMenus

In this case the components returned by the method will be added under the menu specified by the key.

For example:
  public class MyMenuFactory extends AbstractMDIMenuFactory {
    protected void initMenus() {
      JMenu menu = new JMenu("Save File");
      addToDynamicMenuMap("save", menu);
    }
  }
And in a MDIPlugin:
   public class MyPlugin extends AbstractSwingMDIPlugin {
     private AbstractAction pluginAction = ...
     private MetaData meta = ...

     public void public Object getDynamicMenuElements(String menuKey, SwingFileProperties prop) {
       if (menuKey.equals("save") && prop.isCompatibleWith(meta)) {
         return pluginAction;
       } else {
         return null;
       }
     }
   }
In this case the action will be added in the menu only if the selected element is compatible with the MetaData.

Using dynamic menus with other components

In this case the result depends on the class of the component associated with the key, and what is returned by the getDynamicMenuElements method:
  • If the first result from the getDynamicMenuElements method is a boolean, the component associated with the key will be enabled or disabled depending on the value of the boolean
  • If the component associated with the key is a JMenuItem or JButton:
    • If the first result from the getDynamicMenuElements method is an AbstracAtion, this action will be associated with the button or menu item
    • If the first result from the getDynamicMenuElements method is a JMenuItem or JButton, it will replace the previous element


For example:
   public class MyMenuFactory extends AbstractMDIMenuFactory {
     protected void initMenus() {
       JMenu menu = new JMenu("File");
       JMenuItem item = new JMenu("Save As");
       addToDynamicMenuMap("save", item);
     }
   }
And in a MDIPlugin:
   public class MyPlugin extends AbstractSwingMDIPlugin {
     private AbstractAction saveAction = ...
     private MetaData meta = ...

     public void public Object getDynamicMenuElements(String menuKey, SwingFileProperties prop) {
       if (menuKey.equals("save") && prop.isCompatibleWith(meta)) {
         return pluginAction;
       } else {
         return null;
       }
     }
   }
In this case the action will be associated with the JMenuItem if the selected element is compatible with the MetaData.

See also


Categories: gui | swing

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