Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

Usage of menus in Plugins



If you want to add menus and toolbar buttons in Plugins, you need to make them available by the application.

There are several ways to do that:
  • Either by adding a static menu
  • Either by setting a bookmark for a static menu (bookmark are another way of defining static menus)
  • Or by adding a dynamic menu
Note that if you don't want to make menus available in Plugins, you don't need to use these methods. The AbstractMDIMenuFactory.Mbar or the MDIMenuFactory.getMenuBar() method allows to get the menu bar.

Static menus

Static menus are menus which are always present in the application[1]
Which does not mean that elements in the menu are always enabled
.

Static menus are created by the AbstractMDIMenuFactory.addToStaticMenus(String, JMenu) method. MDIPlugins will be fired by the MDIPlugin.getStaticMenuElements(String) for each key defined in the factory.

Example

In this example we define an "Analyze" menu in the menu factory:
   protected void initMenus() {
     JMenubar mbar = getMenuBar();

     JMenu menu = new JMenu("Analyze");
     mbar.add(analyze);
     addToStaticMenu("analyze", menu);
   }
And we have an ImagePlugin which has an action allowing to analyse an image:
   public class ImagePlugin extends AbstractSwingMDIPlugin {
     private AbstractAction analyzeImageAction = ...

     public Object getStaticMenuElements(String menu) {
       if (menu.equals("analyze")) {
         return analyzeImageAction;
       } else {
         return null;
       }
      }
   }
The action will be added to the "Analyze" menu.

Bookmarked menus

Main Article: Bookmarked menus

Bookmarked menus are another (simpler) way to define static menus. Bookmarked menus are created by AbstractMDIMenuFactory.bookmarkMenu(String, JMenu).

Plugins will be able to add an element under this menu by MDIMenuFactory.addToBookmarkedMenu(String, JComponent) or MDIMenuFactory.addToBookmarkedMenu(String, AbstractAction)

Example

In this example we define an "Analyze" menu in the menu factory:
   protected void initMenus() {
     JMenubar mbar = getMenuBar();

     JMenu menu = new JMenu("Analyze");
     mbar.add(analyze);
     bookmarkMenu("analyze", menu);
   }
And we have an ImagePlugin which has an action allowing to analyse an image:
   public class ImagePlugin extends AbstractSwingMDIPlugin {
     private AbstractAction analyzeImageAction = ...

     public void register(MDIApplication app) throws Exception {
       super.register(app);
       MDIMenuFactory factory = ((GUIApplication)app).getMenuFactory();
       factory.addToBookmarkedMenu("Analyze", analyzeImageAction);
     }
   }
The action will be added to the "Analyze" menu.

Dynamic menus

Main Article: 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.

Custom Plugin actions

Main Article: Custom Plugin actions

Custom Plugin actions allow to use Plugins to act on Objects created by the application or other Plugins.

An example of usage could be to create a tree in the application and ask plugins to create menus on this tree.

Notes

  1. ^ Which does not mean that elements in the menu are always enabled

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