Author: mes
Date: 2010-11-14 15:47:52 -0800 (Sun, 14 Nov 2010)
New Revision: 22838

Modified:
   
core3/swing-application-api/trunk/src/main/java/org/cytoscape/application/swing/AcceleratorParser.java
   
core3/swing-application-api/trunk/src/main/java/org/cytoscape/application/swing/CyAction.java
   
core3/swing-application-api/trunk/src/main/java/org/cytoscape/application/swing/CytoscapeAction.java
Log:
updated interface a bit

Modified: 
core3/swing-application-api/trunk/src/main/java/org/cytoscape/application/swing/AcceleratorParser.java
===================================================================
--- 
core3/swing-application-api/trunk/src/main/java/org/cytoscape/application/swing/AcceleratorParser.java
      2010-11-14 22:28:19 UTC (rev 22837)
+++ 
core3/swing-application-api/trunk/src/main/java/org/cytoscape/application/swing/AcceleratorParser.java
      2010-11-14 23:47:52 UTC (rev 22838)
@@ -7,6 +7,8 @@
 import java.awt.Toolkit;
 import java.util.Map;
 import java.util.HashMap;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Parses accelerator combinations to be used with menu items.
@@ -42,9 +44,9 @@
  */
 class AcceleratorParser
 {
-       static final Map<String,Integer> modifiers = new 
HashMap<String,Integer>(8, 1.0f);
-       static
-       {
+       private static final Map<String,Integer> modifiers = new 
HashMap<String,Integer>();
+       private static final Logger logger = 
LoggerFactory.getLogger(AcceleratorParser.class);
+       static {
                modifiers.put("command",        
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
                modifiers.put("cmd",            
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
                modifiers.put("meta",           
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
@@ -85,53 +87,48 @@
         * <li><code>shift circumflex</code></li>
         * </ul></p>
         * @param string A well formatted accelerator combination described 
above.
-        * @throws IllegalArgumentException if <code>string</code> is not well 
formed
         */
-       static KeyStroke parse(String string)
-       {
+       static KeyStroke parse(String string) {
                int keyCode = 0;
                int modifierCode = 0;
                final StringTokenizer tokenizer = new StringTokenizer(string);
-               while (tokenizer.hasMoreTokens())
-               {
+               while (tokenizer.hasMoreTokens()) {
                        String token = tokenizer.nextToken();
-                       if (tokenizer.hasMoreTokens())
-                       {
-                               Integer modifier = modifiers.get(token);
-                               if (modifier == null)
-                                       throw new 
IllegalArgumentException(String.format("The modifier \'%s\' in \'%s\' is 
invalid; valid modifiers are: %s", token, string, 
modifiers.keySet().toString()));
-                               modifierCode |= modifier.intValue();
-                       }
-                       else
-                       {
+                       if (tokenizer.hasMoreTokens()) {
+                               modifierCode |= lookupModifier(token); 
+                       } else {
                                keyCode = lookupVKCode(token);
                        }
                }
 
                if (keyCode == 0)
-                       throw new IllegalArgumentException("No virtual key was 
specified");
+                       return null;    
 
                return KeyStroke.getKeyStroke(keyCode, modifierCode);
        }
 
-       static int lookupVKCode(String name)
-       {
-               String error = String.format("The virtual key \'%s\' does not 
exist", name);
-               name = "VK_" + name.toUpperCase();
+       private static int lookupModifier(String name) {
+               Integer modifier = modifiers.get(name);
+               if (modifier == null) {
+                       logger.warn("The modifier '" + name + "' is invalid; 
valid modifiers are: " + modifiers.keySet().toString());
+                       return 0;
+               }
+               return modifier.intValue();
+       }
 
+       private static int lookupVKCode(String name) {
+               String error = "The virtual key 'VK_" + name +"' does not 
exist";
+
                int code = 0;
-               try
-               {
+               try {
                        code = 
KeyEvent.class.getField(name).getInt(KeyEvent.class);
+               } catch (NoSuchFieldException ex) {
+                       code = 0;
+                       logger.warn(error);     
+               } catch (IllegalAccessException ex) {
+                       code = 0;
+                       logger.warn(error);     
                }
-               catch (NoSuchFieldException ex)
-               {
-                       throw new IllegalArgumentException(error, ex);
-               }
-               catch (IllegalAccessException ex)
-               {
-                       throw new IllegalArgumentException(error, ex);
-               }
 
                return code;
        }

Modified: 
core3/swing-application-api/trunk/src/main/java/org/cytoscape/application/swing/CyAction.java
===================================================================
--- 
core3/swing-application-api/trunk/src/main/java/org/cytoscape/application/swing/CyAction.java
       2010-11-14 22:28:19 UTC (rev 22837)
+++ 
core3/swing-application-api/trunk/src/main/java/org/cytoscape/application/swing/CyAction.java
       2010-11-14 23:47:52 UTC (rev 22838)
@@ -38,6 +38,7 @@
 package org.cytoscape.application.swing;
 
 import javax.swing.Action;
+import javax.swing.KeyStroke;
 import javax.swing.event.MenuEvent;
 import javax.swing.event.MenuListener;
 import javax.swing.event.PopupMenuListener;
@@ -81,25 +82,18 @@
        float getToolbarGravity();
 
        /**
-        * Returns whether or not this action is accelerated.
-        * @return Whether or not this action is accelerated.
+        * Returns the accelerator KeyStroke defined for this action. 
+        * Will return null if no accelerator is set.
+        * @return the accelerator KeyStroke defined for this action. 
+        * Will return null if no accelerator is set.
         */
-       boolean isAccelerated();
+       KeyStroke getAcceleratorKeyStroke();
 
-       /**
-        * Returns the key code used to identify this action.
-        * @return the key code used to identify this action.
-        */
-       int getKeyCode();
-
-       /**
-        * Returns the key modifiers used to identify this action.
-        * @return the key modifiers used to identify this action.
-        */
-       int getKeyModifiers();
-
        /** 
-        * Returns the string identifying the preferred menu.
+        * This method returns a Menu specification string. Submenus are 
preceeded
+        * by dots in this string, so the result "File.Import" specifies the 
submenu
+        * "Import" of the menu "File". If the result is null, the menu will be
+        * placed in a default location.
         * @return the string identifying the preferred menu.
         */
        String getPreferredMenu();

Modified: 
core3/swing-application-api/trunk/src/main/java/org/cytoscape/application/swing/CytoscapeAction.java
===================================================================
--- 
core3/swing-application-api/trunk/src/main/java/org/cytoscape/application/swing/CytoscapeAction.java
        2010-11-14 22:28:19 UTC (rev 22837)
+++ 
core3/swing-application-api/trunk/src/main/java/org/cytoscape/application/swing/CytoscapeAction.java
        2010-11-14 23:47:52 UTC (rev 22838)
@@ -60,9 +60,8 @@
        protected float menuGravity = 1.0f; 
        protected float toolbarGravity = 1.0f; 
        protected boolean acceleratorSet = false;
-       protected int keyModifiers;
-       protected int keyCode;
-       protected String consoleName;
+       protected KeyStroke acceleratorKeyStroke = null;
+       protected String name;
        protected boolean useCheckBoxMenuItem = false;
        protected boolean inToolBar = false;
        protected boolean inMenuBar = true;
@@ -77,17 +76,27 @@
         */
        public CytoscapeAction(final String name, final CyApplicationManager 
applicationManager) {
                super(name);
-               this.consoleName = name;
+               this.name = name;
                this.applicationManager = applicationManager;
-
-               consoleName = consoleName.replaceAll(":. \'", "");
        }
 
        /**
         * Creates a new CytoscapeAction object.
         *
         * @param configProps A String-String Map of configuration metadata. 
This
-        * will usually be the Map provided by the Spring service configuration.
+        * will usually be the Map provided by the Spring service 
configuration. 
+        * Available configuration keys include:
+        * <ul>
+        * <li>title</li>
+        * <li>preferredMenu</li>
+        * <li>preferredButtonGroup</li>
+        * <li>iconName</li>
+        * <li>tooltip</li>
+        * <li>inToolBar</li>
+        * <li>inMenuBar</li>
+        * <li>enableFor</li>
+        * <li>accelerator</li>
+        * </ul>
         * @param applicationManager The application manager providing context 
for this action.
         */
        public CytoscapeAction(final Map configProps, final 
CyApplicationManager applicationManager) {
@@ -113,63 +122,53 @@
                if ( foundInToolBar != null )
                        inToolBar = true;
 
+               String foundInMenuBar = (String)(configProps.get("inMenuBar"));
+               if ( foundInMenuBar != null )
+                       inMenuBar = true;
+
                enableFor = (String)(configProps.get("enableFor"));
 
                String keyComboString = (String) configProps.get("accelerator");
-               if (keyComboString != null) {
-                       try
-                       {
-                               KeyStroke keyStroke = 
AcceleratorParser.parse(keyComboString);
-                               super.putValue(Action.ACCELERATOR_KEY, 
keyStroke);
-                       }
-                       catch (IllegalArgumentException ex)
-                       {
-                               System.out.println(String.format("WARNING: The 
action \'%s\' has specified the following invalid key combination: %s", 
consoleName, keyComboString));
-                               System.out.println(" => " + ex.getMessage());
-                       }
-               }
+               if (keyComboString != null) 
+                       setAcceleratorKeyStroke( 
AcceleratorParser.parse(keyComboString) );
        }
 
        /**
-        * @inheritdoc 
+        * Sets the name of the action.
+        * @param name The name of the action. 
         */
        public void setName(String name) {
-               this.consoleName = name;
+               this.name = name;
        }
 
        /**
         * @inheritdoc 
         */
        public String getName() {
-               return consoleName;
+               return name;
        }
 
        /**
-        * By default all CytoscapeActions wish to be included in 
CommunityMenuBars,
-        * but you may override if you wish.
-        *
-        * @return true If this Action should be included in a CommunityMenuBar.
-        * @see #getPrefferedMenu();
-        * @beaninfo (ri)
+        * By default all CytoscapeActions wish to be included in the menu bar
+        * at the 'preferredMenuName' location is specified and the 'Tools' menu
+        * not.
+        * @return true if this CyAction should be included in menu bar. 
         */
        public boolean isInMenuBar() {
                return inMenuBar;
        }
 
        /**
-        * By default no CytoscapeActions wish to be included in 
CommunityToolBars,
-        * but you may override if you wish.
-        *
-        * @return true If this Action should be included in a CommunityMenuBar.
-        * @see #getPrefferedButtonGroup();
-        * @beaninfo (ri)
+        * By default no CytoscapeActions will be included in the toolbar. 
+        * @return true if this Action should be included in the toolbar. 
         */
        public boolean isInToolBar() {
                return inToolBar;
        }
 
        /**
-        * @inheritdoc 
+        * Sets the gravity used to order this action in the menu bar.
+        * @param gravity The gravity for ordering menu bar actions.
         */
        public void setMenuGravity(float gravity) {
                menuGravity = gravity;
@@ -183,7 +182,8 @@
        }
 
        /**
-        * @inheritdoc 
+        * Sets the gravity used to order this action in the toolbar.
+        * @param gravity The gravity for ordering toolbar actions.
         */
        public void setToolbarGravity(float gravity) {
                toolbarGravity = gravity;
@@ -197,94 +197,67 @@
        }
 
        /**
-        * @inheritdoc 
+     * Sets the accelerator KeyStroke for this action. 
+     * @param ks The KeyStroke to be used as an accelerator for this action. 
+     * This parameter may be null, in which case no accelerator is defined.
         */
-       public void setAcceleratorCombo(int key_code, int key_mods) {
-               acceleratorSet = true;
-               keyCode = key_code;
-               keyModifiers = key_mods;
+       public void setAcceleratorKeyStroke(KeyStroke ks) {
+               acceleratorKeyStroke = ks;      
        }
 
        /**
-        * @inheritdoc 
+        * @inheritdoc
         */
-       public boolean isAccelerated() {
-               return acceleratorSet;
+       public KeyStroke getAcceleratorKeyStroke() {
+               return acceleratorKeyStroke;
        }
 
        /**
-        * @inheritdoc 
+        * @inheritdoc
         */
-       public int getKeyCode() {
-               return keyCode;
-       }
-
-       /**
-        * @inheritdoc 
-        */
-       public int getKeyModifiers() {
-               return keyModifiers;
-       }
-
-       /**
-        * This method returns a Menu specification string. Submenus are 
preceeded
-        * by dots in this string, so the result "File.Import" specifies the 
submenu
-        * "Import" of the menu "File". If the result is null, the menu will be
-        * placed in a default location.
-        *
-        * @return a Menu specification string, or null if this Action should be
-        *         placed in a default Menu.
-        * @see #inMenuBar()
-        */
        public String getPreferredMenu() {
                return preferredMenu;
        }
 
        /**
-        * @inheritdoc 
+        * Sets the preferredMenuString.  See the {...@link #getPreferredMenu} 
+        * description for formatting description.
+        * @param new_preferred The string describing the preferred menu name.
         */
        public void setPreferredMenu(String new_preferred) {
                preferredMenu = new_preferred;
        } 
 
-       /**
-        * This method returns a ButtonGroup specification string. Subgroups are
-        * preceeded by dots in this string, so the result "Edit.Selection 
Modes"
-        * specifies the subgroup "Selection Modes" of the group "Edit". If the
-        * result is null, the button will be placed in a default location.
-        *
-        * @return a ButtonGroup specification string, or null if the button for
-        *         this Action should be placed in a default ButtonGroup.
-        * @see #inToolBar()
+       /** 
+        * @inheritdoc
         */
        public String getPreferredButtonGroup() {
                return preferredButtonGroup;
        }
 
        /**
-        * @inheritdoc 
+        * Sets the preferred button group.
+        * @param new_preferred The preferred button group for this action. 
         */
        public void setPreferredButtonGroup(String new_preferred) {
                preferredButtonGroup = new_preferred;
        } 
 
        /**
-        * Indicates whether a check box menu item should be used instead of a 
normal one.
+        * @inheritdoc
         */
        public boolean useCheckBoxMenuItem() {
                return useCheckBoxMenuItem;
        }
 
        /**
-        * This method can be used at your discretion, but otherwise does 
nothing.  It exists
-        * primarily to have access to menuSelected().
+        * This method can be used at your discretion, but otherwise does 
nothing.  
         * @param e The triggering event.
         */
     public void menuCanceled(MenuEvent e) {}
 
        /**
-        * This method can be used at your discretion, but otherwise does 
nothing.  It exists
-        * primarily to have access to menuSelected().
+        * This method can be used at your discretion, but otherwise does 
nothing.  
         * @param e The triggering event.
         */
     public void menuDeselected(MenuEvent e) {}
@@ -310,19 +283,26 @@
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 
enableMenus(); }
 
        /**
-        * This method can be used at your discretion, but otherwise does 
nothing.  It exists
-        * primarily to have access to menuSelected().
+        * This method can be used at your discretion, but otherwise does 
nothing.  
         * @param e The triggering event.
         */
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
 
        /**
-        * This method can be used at your discretion, but otherwise does 
nothing.  It exists
-        * primarily to have access to menuSelected().
+        * This method can be used at your discretion, but otherwise does 
nothing.
         * @param e The triggering event.
         */
        public void popupMenuCanceled(PopupMenuEvent e) {}
 
+       //
+       // The following methods are utility methods that that enable or 
disable 
+       // the action based on the state of Cytoscape.  These methods are meant 
to
+       // reduce duplicate code since many actions demand the same state to be
+       // functional (e.g. a network and network view must exist). These 
methods
+       // are generally called from within implementations of {...@link 
#menuSelected}, 
+       // but can be called from anywhere.
+       //
+
        private void enableMenus() {
                if ( enableFor == null || enableFor.equals("") )
                        setEnabled(true);
@@ -334,15 +314,6 @@
                        enableForSelectedNetworkObjs();
        }
 
-       //
-       // The following methods are utility methods that that enable or 
disable 
-       // the action based on the state of Cytoscape.  These methods are meant 
to
-       // reduce duplicate code since many actions demand the same state to be
-       // functional (e.g. a network and network view must exist). These 
methods
-       // are generally called from within implementations of {...@link 
#menuSelected}, 
-       // but can be called from anywhere.
-       //
-
        /**
         * Enable the action if the current network exists and is not null.
         */

-- 
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en.

Reply via email to