SWIRLCAAGT

Component names

Sometimes it is useful to retrieve a certain menu element from a menu bar after it has been built by the menu builder, for example to further customize it in a way which is not supported by SWIRL. For this reason each component built by de menu builder is given a name that is the same as its id attribute. (Submenus have a name attribute instead.) This name can be retrieved from the component using JComponent.getName(). The SwirlUtilities class also provides methods getChildWithName and getDescendantsWithName which search for a component with a given name.

Predicates

The MenuBuilder has some support for conditional inclusion of menus using predicates. For example, the following menu is configured in such a way that the last menu item and the separator will not be present when it is built on a Mac OS X operating system.

           
<menu i18n="file.caption">
   <action id="new">
      <caption i18n="new.text"/>
   </action>
   ...
   <separator unless="Mac OS X">
   <action id="exit" unless="Mac OS X">
      <caption i18n="new.text"/>
   </action>
</menu>
            
Note the unless attribute in both the <separator> and the <action>. This attribute, and its opposite when, is supported by almost every element in the configuration file.

There is no magic happening here: predicates are simply strings that can be registered with the menu builder at run time. It is the application that needs to attach meaning to them. For example:

if (System.getProperty("os.name").toLowerCase().startsWith("mac os x"))
    menuBuilder.setPredicate ("Mac OS X");
...
JMenuBar menuBar = menuBuilder.createJMenuBar("main.menu");