Author: mes
Date: 2009-12-01 12:14:54 -0800 (Tue, 01 Dec 2009)
New Revision: 18624

Added:
   cytoscape/trunk/src/cytoscape/command/CyCommandNamespace.java
Modified:
   cytoscape/trunk/src/cytoscape/command/CyCommandManager.java
Log:
updated api

Modified: cytoscape/trunk/src/cytoscape/command/CyCommandManager.java
===================================================================
--- cytoscape/trunk/src/cytoscape/command/CyCommandManager.java 2009-12-01 
20:01:47 UTC (rev 18623)
+++ cytoscape/trunk/src/cytoscape/command/CyCommandManager.java 2009-12-01 
20:14:54 UTC (rev 18624)
@@ -54,32 +54,57 @@
  * method. Commands may be accessed with a combination of command name and 
namespace.
  */
 public class CyCommandManager {
-       private static Map<String, Map<String,CyCommand>> comMap;
-       private final static String HELP = "help";
+       private static Map<CyCommandNamespace, Map<String,CyCommand>> comMap;
+       private static Map<String, CyCommandNamespace> nsMap;
+       private final static String HELP = "help"; 
+       private final static CyCommandNamespace HELP_NS = new 
CyCommandNamespaceImpl(HELP);
 
        static {
-               comMap = new HashMap<String, Map<String,CyCommand>>();
+               comMap = new HashMap<CyCommandNamespace, 
Map<String,CyCommand>>();
+               nsMap = new HashMap<String, CyCommandNamespace>();
 
                // special case for help
-               comMap.put( HELP, new HashMap<String,CyCommand>());
-               comMap.get(HELP).put(HELP, new HelpCommand());
+               comMap.put(HELP_NS, new HashMap<String,CyCommand>());
+               comMap.get(HELP_NS).put(HELP, new HelpCommand());
+               nsMap.put(HELP,HELP_NS);
        }
 
+    public static CyCommandNamespace reserveNamespace(final String namespace) 
+               throws RuntimeException {
+
+               if (nsMap.containsKey(namespace))
+                       throw new RuntimeException("Command namespace: 
"+namespace+" is already reserved");
+               
+               CyCommandNamespace ns = new CyCommandNamespaceImpl(namespace);
+               nsMap.put(namespace, ns);
+               comMap.put(ns, new HashMap<String,CyCommand>());
+               
+               comMap.get(HELP_NS).put(namespace, new 
HelpSpecificCommand(namespace));
+
+               return ns;
+       }
+
     /**
-     * register a new CyCommandHandler.
+     * register a new CyCommand.
      *
      * @param com the command we want to register
      * @throws RuntimeException if the command is already registered
      */
-       public static void register(CyCommand com) throws RuntimeException {
+       public static void register(CyCommandNamespace ns, CyCommand com) 
throws RuntimeException {
                if (com == null) 
                        return;
+               if (ns == null) 
+                       return;
 
-               if (!comMap.containsKey( com.getNamespace())) 
-                       comMap.put( com.getNamespace(), new 
HashMap<String,CyCommand>() );
+               if ( !ns.getNamespaceName().equals(com.getNamespace()) ) 
+                       throw new RuntimeException("Command: " + com.toString() 
+ 
+                                                  " is not part of namespace: 
" + ns.getNamespaceName());
 
-               Map<String,CyCommand> subComMap = 
comMap.get(com.getNamespace());
+               Map<String,CyCommand> subComMap = comMap.get(ns);
 
+               if ( subComMap == null ) 
+                       throw new RuntimeException("Namespace is not 
registered!");
+
                if ( subComMap.containsKey( com.getCommandName() ) )
                        throw new RuntimeException("Command: " + 
com.getNamespace() + " " + 
                                                   com.getCommandName()+ " 
already exists!");
@@ -88,7 +113,7 @@
 
                // add help for this namespace if necessary
                if ( subComMap.size() == 1 )
-                       comMap.get(HELP).put(com.getNamespace(), 
+                       comMap.get(HELP_NS).put(com.getNamespace(), 
                                               new 
HelpSpecificCommand(com.getNamespace()));
        }
 
@@ -105,10 +130,13 @@
                if ((name == null) || (name.length() == 0)) 
                        throw new RuntimeException("null or zero length command 
name");
 
-               Map<String,CyCommand> subComMap = comMap.get(namespace);
-               if ( subComMap == null )
-                       throw new RuntimeException("namespace " + namespace + " 
does not exist!");
+               CyCommandNamespace ns = nsMap.get(namespace);
 
+               if ( ns == null )
+                       throw new RuntimeException("namespace has not been 
registered");
+
+               Map<String,CyCommand> subComMap = comMap.get(ns);
+
                return subComMap.get(name);
        }
 
@@ -134,10 +162,17 @@
      * @return the list of commands that are currently registered
      */
        public static List<CyCommand> getCommandList(String namespace) {
+               if ((namespace == null) || (namespace.length() == 0)) 
+                       throw new RuntimeException("null or zero length 
namespace");
+
+               CyCommandNamespace ns = nsMap.get(namespace);
+
+               if ( ns == null )
+                       throw new RuntimeException("namespace has not been 
registered");
+
                List<CyCommand> list = new ArrayList<CyCommand>();
 
-               if ( comMap.containsKey( namespace ) )
-                       list.addAll( comMap.get(namespace).values() ); 
+               list.addAll( comMap.get(ns).values() ); 
 
                return list;
        }
@@ -148,14 +183,32 @@
      * @param com the command to unregister
      */
        public static void unRegister(CyCommand com) {
+               CyCommandNamespace ns = nsMap.get(com.getNamespace());
+               if ( ns == null )
+                       throw new RuntimeException("namespace has not been 
registered");
 
-               Map<String,CyCommand> subComMap = 
comMap.get(com.getNamespace());
+               Map<String,CyCommand> subComMap = comMap.get(ns);
 
                subComMap.remove( com.getCommandName() );
 
                // if this is the last command for a namespace, 
                // remove the help for the namespace
-               if ( subComMap.size() == 0 )
-                       comMap.get(HELP).remove(com.getNamespace());
+               if ( subComMap.size() == 0 ) {
+                       comMap.get(HELP_NS).remove(com.getNamespace());
+                       // TODO also make the namespace available again?
+               }
        }
+
+    /**
+     * Internal implementation for the CyCommandNamespace class.
+     */
+    private static class CyCommandNamespaceImpl implements CyCommandNamespace {
+        private String namespace;
+
+        CyCommandNamespaceImpl(String namespace) {
+            this.namespace = namespace;
+        }
+
+        public String getNamespaceName() { return namespace; }
+    }
 }

Added: cytoscape/trunk/src/cytoscape/command/CyCommandNamespace.java
===================================================================
--- cytoscape/trunk/src/cytoscape/command/CyCommandNamespace.java               
                (rev 0)
+++ cytoscape/trunk/src/cytoscape/command/CyCommandNamespace.java       
2009-12-01 20:14:54 UTC (rev 18624)
@@ -0,0 +1,52 @@
+/*
+File: CyCommandNamespace.java
+
+Copyright (c) 2009, The Cytoscape Consortium (www.cytoscape.org)
+
+The Cytoscape Consortium is:
+- Institute for Systems Biology
+- University of California San Diego
+- Memorial Sloan-Kettering Cancer Center
+- Institut Pasteur
+- Agilent Technologies
+- University of California San Francisco
+
+This library is free software; you can redistribute it and/or modify it
+under the terms of the GNU Lesser General Public License as published
+by the Free Software Foundation; either version 2.1 of the License, or
+any later version.
+
+This library is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
+MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
+documentation provided hereunder is on an "as is" basis, and the
+Institute for Systems Biology and the Whitehead Institute
+have no obligations to provide maintenance, support,
+updates, enhancements or modifications.  In no event shall the
+Institute for Systems Biology and the Whitehead Institute
+be liable to any party for direct, indirect, special,
+incidental or consequential damages, including lost profits, arising
+out of the use of this software and its documentation, even if the
+Institute for Systems Biology and the Whitehead Institute
+have been advised of the possibility of such damage.  See
+the GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License
+along with this library; if not, write to the Free Software Foundation,
+Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+*/
+package cytoscape.command;
+
+/**
+ * A CyCommandNamespace represents a reservation for a group of {...@link 
CyCommand}s.  
+ * A namespace reservation must be secured before a client can register 
commands
+ * to be executed.
+ */
+public interface CyCommandNamespace {
+       /**
+        * Return the string name that is held by this namespace.
+        *
+        * @return the name reserved by this namespace.
+        */
+       String getNamespaceName();
+}

--

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