In an earlier part of the tutorial we showed how to add a menu item to the help menu using the addMenu() method, and a "magic" number. In practice this method is only useful for trivial plugins. Real plugins are more likely to use an XML file to define new menu items, as described below.

First lets look at where the "magic" number 900100 in the earlier example came from. To do this we need to look at the XML file which JAS3 uses to define its own basic menus.

jas3.menus
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE MenuSystem SYSTEM "http://java.freehep.org/schemas/menus/1.0/menus.dtd">
<MenuSystem>
   <MenuBar id="mainMenu">
		
      <Menu name="File" mnemonic="F" location="100">
         <Component type="default" name="Exit" mnemonic="E" command="exit" location="990"/>
      </Menu>
		
      <Menu name="View" mnemonic="V" location="300">
         <Component type="checkbox" name="Tool Bar" mnemonic="T" 
	     description="Show the application tool bar" command="showToolBar" location="100"/>
         <Component type="checkbox" name="Status Bar" mnemonic="S" 
	     description="Show the application status bar" command="showStatusBar" location="110"/>
         <Menu name="Look and Feel" mnemonic="L" class="org.freehep.application.LookAndFeelMenu" location="700"/>
         <Menu name="Window Style" mnemonic="W" class="org.freehep.application.studio.PageManagerMenu" location="750"/>
      </Menu>
		
      <Menu name="Window" mnemonic="W" location="800" class="org.freehep.application.mdi.WindowMenu">
         <Component type="default" name="Close" mnemonic="C" command="closePage"/>
         <Component type="default" name="Close All" mnemonic="A" command="closeAllPages"/>
         <Separator/>
         <Component type="default" name="Cascade" mnemonic="s" command="cascade"/>
         <Component type="default" name="Tile Horizontally" mnemonic="h" command="tileHorizontally"/>
         <Component type="default" name="Tile Vertically" mnemonic="v" command="tileVertically"/>
      </Menu>
		
      <Menu name="Help" mnemonic="H" location="900">
         <Component type="default" name="Examples" mnemonic="E" command="examples" location="500"/>
         <Component type="default" name="About" mnemonic="A" icon="/toolbarButtonGraphics/general/About16.gif" 
	     command="about" location="900">
            <Accelerator shift="false" meta="false" key="VK_F1" ctrl="false" alt="false"/>
         </Component>
      </Menu>

   </MenuBar>
</MenuSystem>

Hopefully this file is largely self explanatory. We can see that the file defines a single menu bar (called mainMenu) then adds a set of menus (File, View, Window, Help), each of which has a few items (or components as they are called in the XML file). Each component has several attributes that can be associated with it, for example:

  • type One of "default", "checkbox" or "radio".
  • name The name of the menu or menu item as it appears in the GUI.
  • command The command that is sent to the application when the user selects this item.

Note also that each menu and component has a "location" associated with it. The location is used to order items in the menu or menu bar, and also as we will see later to allow menu files to be merged together.

In our earlier example we positioned our "Hello World" menu item at position 900100. This is just a shorthand way of specifying that it goes at location 100 in menu 900 (the Help menu). The addMenu item is not very flexible, so for more complex plugins it is better to specify the plugins menus using an .menus file similar to the one JAS3 itself uses. JAS3 includes support for merging menu files from different plugins, and allows plugins to define new menu items and any location, or to share menu items created by other plugins.

The code below shows how to re-implement our earlier HelloWorld plugin using a .menus file.

  • No labels