Class BAMutil


  • public class BAMutil
    extends Object
    Button, Action and Menu utilities: static stationHelper methods for building ucar.unidata.UI's.

    Example for Toggle Action

     AbstractAction dsAction =  new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
         Boolean state = (Boolean) getValue( BAMutil.STATE);
         addCoords = state.booleanValue();
         String tooltip = addCoords ? "add Coordinates in ON" : "add Coordinates is OFF";
         dsButt.setToolTipText(tooltip);
        }
       };
       BAMutil.setActionProperties( dsAction, "Dataset", "add Coordinates is OFF", true, 'D', -1);
       addCoords = prefs.getBoolean( "dsState", false);
       dsAction.putValue(BAMutil.STATE, new Boolean(addCoords));
       AbstractButton dsButt = BAMutil.addActionToContainer(buttPanel, dsAction);
    
       ...
       prefs.putBoolean("dsState", dsButt.getModel().isSelected());
     
     
     
    • Constructor Detail

      • BAMutil

        public BAMutil()
    • Method Detail

      • setResourcePath

        public static void setResourcePath​(String path)
        Set the resource path for icons, images, cursors.
        Parameters:
        path - relative to the classpath
      • getResourcePath

        public static String getResourcePath()
      • getIcon

        public static ImageIcon getIcon​(String name,
                                        boolean errMsg)
        Get the named Icon from the default resource (jar file).
        Parameters:
        name - name of the Icon ( will look for .gif)
        errMsg - true= print error message if not found
        Returns:
        the Icon or null if not found
      • getImage

        public static Image getImage​(String name)
        Get the named Image from the default resource (jar file).
        Parameters:
        name - name of the Image ( will look for .gif)
        Returns:
        the Image or null if not found
      • makeCursor

        public static Cursor makeCursor​(String name)
        Make a cursor from the named Image in the default resource (jar file)
        Parameters:
        name - name of the Image ( will look for .gif)
        Returns:
        the Cursor or null if failure
      • makeButtcon

        public static AbstractButton makeButtcon​(Icon icon,
                                                 Icon selected,
                                                 String tooltip,
                                                 boolean is_toggle)
        Make a "buttcon" = button with an Icon
        Parameters:
        icon - the normal Icon
        selected - the selected Icon
        tooltip - the tooltip
        is_toggle - if true, make JToggleButton, else JButton
        Returns:
        the buttcon (JButton or JToggleButton)
      • makeButtcon

        public static AbstractButton makeButtcon​(String iconName,
                                                 String tooltip,
                                                 boolean is_toggle)
        Make a "buttcon" = button with an Icon
        Parameters:
        iconName - name of the Icon ( will look for .gif)
        tooltip - the tooltip
        is_toggle - if true, make JToggleButton, else JButton
        Returns:
        the buttcon (JButton or JToggleButton)
      • addActionToMenu

        public static JMenuItem addActionToMenu​(JMenu menu,
                                                Action act,
                                                int menuPos)
        creates a MenuItem using the given Action and adds it to the given Menu. Uses Properties that have been set on the Action (see setActionProperties()). All are optional except for Action.SHORT_DESCRIPTION:
               Action.SHORT_DESCRIPTION   String     MenuItem text (required)
               Action.SMALL_ICON          Icon       the Icon to Use
               BAMutil.SELECTED_ICON      Icon       the Icon when selected (optional)
               BAMutil.TOGGLE             Boolean    true if its a toggle
               BAMutil.MNEMONIC           Integer    menu item shortcut
               BAMutil.ACCEL              Integer    menu item global keyboard accelerator
         

        The Action is triggered when the MenuItem is selected. Enabling and disabling the Action does the same for the MenuItem. For toggles, state is maintained in the Action, and MenuItem state changes when the Action state changes.

        The point of all this is that once you set it up, you work exclusively with the action object, and all changes are automatically reflected in the UI.
        Parameters:
        menu - add to this menu
        act - the Action to make it out of
        menuPos - if >= 0, add at this position, otherwise append
        Returns:
        the MenuItem created
      • addActionToContainerPos

        public static AbstractButton addActionToContainerPos​(Container c,
                                                             Action act,
                                                             int pos)
        creates an AbstractButton using the given Action and adds it to the given Container at the position.. Uses Properties that have been set on the Action (see setActionProperties()). All are optional except for Action.SMALL_ICON:
               Action.SMALL_ICON          Icon       the Icon to Use (required)
               BAMutil.SELECTED_ICON      Icon       the Icon when selected (optional)
               Action.SHORT_DESCRIPTION   String     tooltip
               BAMutil.TOGGLE             Boolean    true if its a toggle
         

        The Action is triggered when the Button is selected. Enabling and disabling the Action does the same for the Button. For toggles, state is maintained in the Action, and the Button state changes when the Action state changes.

        The point of all this is that once you set it up, you work exclusively with the action object, and all changes are automatically reflected in the UI.
        Parameters:
        c - add to this Container
        act - the Action to make it out of
        pos - add to the container at this position (if pos < 0, add at the end)
        Returns:
        the AbstractButton created (JButton or JToggleButton)
      • addActionToContainer

        public static AbstractButton addActionToContainer​(Container c,
                                                          Action act)
        Same as addActionToContainerPos, but add to end of Container
        Parameters:
        c - add to this container
        act - add this action
        Returns:
        the Button that triggers this action, in case you want to decorate it
      • setActionProperties

        public static void setActionProperties​(AbstractAction act,
                                               String icon_name,
                                               String action_name,
                                               boolean is_toggle,
                                               int mnemonic,
                                               int accel)
        Standard way to set Properties for Actions. This also looks for an Icon "Sel" and if it exists: 1) sets SelectedIcon if its a toggle, or 2) sets the Icon when selected (optional) if its not a toggle If is_toggle, a toggle button is created (in addActionToContainer()), default state false To get or set the state of the toggle button: Boolean state = (Boolean) action.getValue(BAMutil.STATE); action.putValue(BAMutil.STATE, new Boolean(true/false));
        Parameters:
        act - add properties to this action
        icon_name - name of icon (or null).
        action_name - menu name / tooltip
        is_toggle - true if its a toggle
        mnemonic - menu item shortcut
        accel - menu item global keyboard accelerator
      • setActionPropertiesToggle

        public static void setActionPropertiesToggle​(AbstractAction act,
                                                     String icon_name,
                                                     String action_name,
                                                     boolean toggleValue,
                                                     int mnemonic,
                                                     int accel)
        Standard way to set Properties and state for "Toggle" Actions. *
        Parameters:
        act - add properties to this action
        icon_name - name of icon (or null).
        action_name - menu name / tooltip
        toggleValue - default value of toggle
        mnemonic - menu item shortcut
        accel - menu item global keyboard accelerator