Package ucar.ui.prefs

Class Debug


  • public class Debug
    extends Object
    Provides static methods for managing persistent lists of debugging flags that can be set by the user at runtime. A new debug flag is added to the Store whenever isSet() is called and the flag name does not already exist. The flags can be set dynamically at runtime through a JMenu, with the name of the flag used for the menu label, and the / separator indicating menu levels. Note that this class does not depend on using PreferencesExt, and so could also be used with the default Preferences implementations.

    To use, make sure that you call setStore() before anything else; it is a good idea to use a separate node to avoid name collisions:

     Debug.setStore(prefs.node("/debugFlags"));
     

    To allow user control at runtime, add a JMenu instance variable to your main menu, which constructs itself whenever it is called using the current set of flags, e.g.:

     JRootPane rootPane = getRootPaneContainer().getRootPane();
     JMenuBar mb = new JMenuBar();
     rootPane.setJMenuBar(mb);
     JMenu sysMenu = new JMenu("System");
     mb.add(sysMenu);
     
     JMenu debugMenu = (JMenu) sysMenu.add(new JMenu("Debug"));
     debugMenu.addMenuListener(new MenuListener() {
       public void menuSelected(MenuEvent e) {
         Debug.constructMenu(debugMenu);
       }
     
       public void menuDeselected(MenuEvent e) {}
     
       public void menuCanceled(MenuEvent e) {}
     });
     sysMenu.add(debugMenu);
     
    Then in your application code, check to see if a debug flag is turned on, for example:
     if (Debug.isSet("util/configure/stuff"))
       do_good_stuff();
     

    The first time this code is called, this flag will be added to the Store and to the menu (with a value of false). The user can then turn it on using the menu system. This allows you to add fine-grained debugging control without having to manage a separate list of flags. Since these are all static methods, you do not have to pass around a global Debug object. In this example, the top menu item to be added will be c alled "util" the second item called "configure", and the flag itself will be called "stuff". The menu may nest to arbitrary depth. Similarly, a substore of the main debug Store (the one passed into setStore()) is created called "util", which has a substore called "configure", which has a boolean value whose key is "stuff". Thus name collisions should be easy to avoid.

    See Also:
    Preferences
    • Constructor Detail

      • Debug

        public Debug()
    • Method Detail

      • setStore

        public static void setStore​(Preferences debugStore)
        Set the persistent data. You must call this before any other call.
      • isSet

        public static boolean isSet​(String flagName)
        Return the value of the named flag. If it doesnt exist, it will be added to the store and the menu with a value of "false".
      • set

        public static void set​(String flagName,
                               boolean value)
        Set the value of the named flag. If it doesnt exist, it will be added to the store and the menu.
      • removeAll

        public static void removeAll()
        Clear all flags (set to false).
      • constructMenu

        public static void constructMenu​(JMenu topMenu)
        Construct cascading pull-aside menus using the values of the debug flags in the Preferences object.
        Parameters:
        topMenu - attach the menus as children of this one.