Revision: 1656
          http://svn.sourceforge.net/spring-rich-c/?rev=1656&view=rev
Author:   kevinstembridge
Date:     2007-01-12 12:25:00 -0800 (Fri, 12 Jan 2007)

Log Message:
-----------
Added logging, improved javadoc, improved error messages

Modified Paths:
--------------
    
trunk/spring-richclient/support/src/main/java/org/springframework/richclient/command/CommandGroupFactoryBean.java

Modified: 
trunk/spring-richclient/support/src/main/java/org/springframework/richclient/command/CommandGroupFactoryBean.java
===================================================================
--- 
trunk/spring-richclient/support/src/main/java/org/springframework/richclient/command/CommandGroupFactoryBean.java
   2007-01-12 20:21:56 UTC (rev 1655)
+++ 
trunk/spring-richclient/support/src/main/java/org/springframework/richclient/command/CommandGroupFactoryBean.java
   2007-01-12 20:25:00 UTC (rev 1656)
@@ -15,8 +15,12 @@
  */
 package org.springframework.richclient.command;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.springframework.beans.factory.BeanNameAware;
 import org.springframework.beans.factory.FactoryBean;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.richclient.application.PropertyNotSetException;
 import org.springframework.richclient.command.config.CommandConfigurer;
 import org.springframework.richclient.core.SecurityControllable;
 import org.springframework.richclient.util.Assert;
@@ -25,6 +29,7 @@
 /**
  * A [EMAIL PROTECTED] FactoryBean} that produces a [EMAIL PROTECTED] 
CommandGroup}.
  * 
+ * <p> 
  * Use of this bean simplifies the process of building up complex nested 
command groups such as the
  * main menu of an application window, a toolbar or popup menus. The main 
property of interest 
  * when creating a bean definition for this class is the [EMAIL PROTECTED] 
members} list. This list defines the 
@@ -36,11 +41,12 @@
  * <li>[EMAIL PROTECTED] #GLUE_MEMBER_CODE}: Represents a 'glue' component 
between command group members.</li>
  * <li>[EMAIL PROTECTED] #SEPARATOR_MEMBER_CODE}: Represents a separator 
between command group members.</li>
  * </ul>
+ *  </p>
  *  
  * @author Keith Donald
  */
 //TODO confirm if the use of the command: and group: prefixes is still 
supported/required
-public class CommandGroupFactoryBean implements BeanNameAware, FactoryBean, 
SecurityControllable {
+public class CommandGroupFactoryBean implements BeanNameAware, FactoryBean, 
SecurityControllable, InitializingBean {
     
     /** The string that represents a glue component, to be used between other 
members of the command group. */
     public static final String GLUE_MEMBER_CODE = "glue";
@@ -53,6 +59,9 @@
 
     /** The string prefix that indicates a command group member that is 
another command group. */
     public static final String GROUP_MEMBER_PREFIX = "group:";
+    
+    /** Class logger, available to subclasses. */
+    protected Log logger = LogFactory.getLog(getClass());
 
     private String groupId;
 
@@ -60,7 +69,7 @@
 
     private CommandConfigurer commandConfigurer;
 
-    private Object[] encodedMembers;
+    private Object[] members;
 
     private boolean exclusive;
 
@@ -73,7 +82,9 @@
     /**
      * Creates a new uninitialized [EMAIL PROTECTED] CommandGroupFactoryBean}. 
If created by the Spring 
      * IoC container, the [EMAIL PROTECTED] groupId} assigned to this instance 
will be the bean name of the
-     * bean as declared in the bean definition file.
+     * bean as declared in the bean definition file. If using this 
constructor, a non-null list
+     * of command group members must be provided by calling the [EMAIL 
PROTECTED] #setMembers(Object[])} 
+     * method before this instance is used.
      */
     public CommandGroupFactoryBean() {
         //do nothing
@@ -86,11 +97,16 @@
      * @param groupId The identifier that will be assigned to the command 
group produced by this
      * factory. Note that if this instance is created by a Spring IoC 
container, the group ID 
      * provided here will be overwritten by the bean name of this instance's 
bean definition.
-     * @param encodedMembers The (possibly) encoded representation of the 
members of the command 
-     * group to be produced by this factory.
+     * 
+     * @param members The collection of objects that specify the members of 
the command 
+     * group. These objects are expected to be either instances of [EMAIL 
PROTECTED] AbstractCommand} or 
+     * strings. See the class documentation for details on how these strings 
will be interpreted.
+     * Must not be null.
+     * 
+     * @throws IllegalArgumentException if [EMAIL PROTECTED] members} is null.
      */
-    public CommandGroupFactoryBean(String groupId, Object[] encodedMembers) {
-        this(groupId, null, null, encodedMembers);
+    public CommandGroupFactoryBean(String groupId, Object[] members) {
+        this(groupId, null, null, members);
     }
 
     /**
@@ -99,13 +115,16 @@
      * @param groupId The value to be used as the command identifier of the 
command group 
      * produced by this factory.
      * @param commandRegistry The registry that will be used to retrieve the 
actual instances of 
-     * the command group members as specified in [EMAIL PROTECTED] 
encodedMembers}.
-     * @param encodedMembers The collection of objects that specify the 
members of the command 
+     * the command group members as specified in [EMAIL PROTECTED] members}.
+     * @param members The collection of objects that specify the members of 
the command 
      * group. These objects are expected to be either instances of [EMAIL 
PROTECTED] AbstractCommand} or 
-     * strings. See the class documentation for details on how these strings 
will be interpreted. 
+     * strings. See the class documentation for details on how these strings 
will be interpreted.
+     * Must not be null.
+     * 
+     * @throws IllegalArgumentException if [EMAIL PROTECTED] members} is null.
      */
-    public CommandGroupFactoryBean(String groupId, CommandRegistry 
commandRegistry, Object[] encodedMembers) {
-        this(groupId, commandRegistry, null, encodedMembers);
+    public CommandGroupFactoryBean(String groupId, CommandRegistry 
commandRegistry, Object[] members) {
+        this(groupId, commandRegistry, null, members);
     }
 
     /**
@@ -114,24 +133,40 @@
      * @param groupId The value to be used as the command identifier of the 
command group 
      * produced by this factory.
      * @param commandRegistry The registry that will be used to retrieve the 
actual instances of 
-     * the command group members as specified in [EMAIL PROTECTED] 
encodedMembers}.
-     * @param encodedMembers The collection of objects that specify the 
members of the command 
+     * the command group members as specified in [EMAIL PROTECTED] members}.
+     * @param members The collection of objects that specify the members of 
the command 
      * group. These objects are expected to be either instances of [EMAIL 
PROTECTED] AbstractCommand} or 
-     * strings. See the class documentation for details on how these strings 
will be interpreted.
-     * @param configurer The object that will be used to configure the command 
objects contained
+     * strings. See the class documentation for details on how these strings 
will be interpreted. 
+     * Must not be null.
+     * @param commandConfigurer The object that will be used to configure the 
command objects contained
      * in this factory's command group.
+     * 
+     * @throws IllegalArgumentException if [EMAIL PROTECTED] members} is null.
      */
-    public CommandGroupFactoryBean(String groupId, CommandRegistry 
commandRegistry, CommandConfigurer configurer,
-            Object[] encodedMembers) {
-        setBeanName(groupId);
-        setCommandRegistry(commandRegistry);
-        setMembers(encodedMembers);
-        setCommandConfigurer(configurer);
+    public CommandGroupFactoryBean(String groupId, 
+                                   CommandRegistry commandRegistry, 
+                                   CommandConfigurer commandConfigurer,
+                                   Object[] members) {
+        
+        Assert.required(members, "members");
+        
+        this.groupId = groupId;
+        this.commandRegistry = commandRegistry;
+        this.members = members;
+        this.commandConfigurer = commandConfigurer;
+        
     }
+    
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public void afterPropertiesSet() {
+        PropertyNotSetException.throwIfNull(this.members, "members", 
getClass());
+    }
 
     /**
      * Sets the registry that will be used to retrieve the actual instances of 
the command
-     * group members as specified in the encoded members list provided to 
[EMAIL PROTECTED] #setMembers(Object[])}.
+     * group members as specified in the encoded members collection.
      *
      * @param commandRegistry The registry containing commands for the command 
group produced
      * by this factory. May be null.
@@ -152,14 +187,18 @@
 
     /**
      * Sets the collection of objects that specify the members of the command 
group produced 
-     * by this factory. The objects in [EMAIL PROTECTED] encodedMembers} are 
expected to be either instances
+     * by this factory. The objects in [EMAIL PROTECTED] members} are expected 
to be either instances
      * of [EMAIL PROTECTED] AbstractCommand} or strings. See the class 
documentation for details on how 
      * these strings will be interpreted. 
      *
-     * @param encodedMembers The (possibly) encoded representation of the 
command group members.
+     * @param members The (possibly) encoded representation of the command 
group members.
+     * Must not be null.
+     * 
+     * @throws IllegalArgumentException if [EMAIL PROTECTED] members} is null.
      */
-    public void setMembers(Object[] encodedMembers) {
-        this.encodedMembers = encodedMembers;
+    public final void setMembers(Object[] members) {
+        Assert.required(members, "members");
+        this.members = members;
     }
 
     /**
@@ -171,9 +210,14 @@
         this.groupId = beanName;
     }
 
-    // Do we need a public getter for this property?
+    /**
+     * Returns the value of the flag that indicates whether or not this 
factory produces an 
+     * exclusive command group.
+     * @return The exclusive flag.
+     * @see ExclusiveCommandGroup
+     */
     public boolean isExclusive() {
-        return exclusive;
+        return this.exclusive;
     }
 
     /**
@@ -188,8 +232,8 @@
 
     /**
      * Sets the flag that indicates whether or not the command group produced 
by this factory
-     * allows no items in the group to be selected. This is only relevant for 
exclusive command
-     * groups.
+     * allows no items in the group to be selected, default is false. This is 
only relevant for 
+     * exclusive command groups.
      *
      * @param allowsEmptySelection Set [EMAIL PROTECTED] true} for the command 
group to allow none of its
      * members to be selected.
@@ -252,10 +296,14 @@
      *
      * @param group The group that is to contain the commands from the encoded 
members list. Must 
      * not be null.
+     * 
+     * @throws InvalidGroupMemberEncodingException if a member prefix is 
provided without 
+     * a command id.
      */
     private void initCommandGroupMembers(CommandGroup group) {
-        for (int i = 0; i < encodedMembers.length; i++) {
-            Object o = encodedMembers[i];
+        for (int i = 0; i < members.length; i++) {
+            Object o = members[i];
+            
             if (o instanceof AbstractCommand) {
                 group.addInternal((AbstractCommand)o);
                 configureIfNecessary((AbstractCommand)o);
@@ -269,10 +317,28 @@
                     group.addGlueInternal();
                 }
                 else if (str.startsWith(COMMAND_MEMBER_PREFIX)) {
-                    addCommandMember(str.substring(8), false, group);
+                    
+                    String commandId = 
str.substring(COMMAND_MEMBER_PREFIX.length());
+                    
+                    if (!StringUtils.hasText(commandId)) {
+                        throw new InvalidGroupMemberEncodingException(
+                                "The group member encoding does not specify a 
command id",
+                                str);
+                    }
+                    
+                    
addCommandMember(str.substring(COMMAND_MEMBER_PREFIX.length()), false, group);
                 }
                 else if (str.startsWith(GROUP_MEMBER_PREFIX)) {
-                    addCommandMember(str.substring(6), true, group);
+                    
+                    String commandId = 
str.substring(GROUP_MEMBER_PREFIX.length());
+                    
+                    if (!StringUtils.hasText(commandId)) {
+                        throw new InvalidGroupMemberEncodingException(
+                                "The group member encoding does not specify a 
command id",
+                                str);
+                    }
+                    
+                    addCommandMember(commandId, true, group);
                 }
                 else {
                     addCommandMember(str, false, group);
@@ -280,7 +346,7 @@
             }
         }
     }
-
+    
     /**
      * Adds the command object with the given id to the given command group. 
If a command 
      * registry has not yet been provided to this factory, the command id will 
be passed as 
@@ -293,41 +359,41 @@
      * 
      */
     private void addCommandMember(String commandId, boolean isGroup, 
CommandGroup group) {
-        EncodedGroupMemberInfo info = parseEncodedCommandInfo(commandId);
+        
+        Assert.required(commandId, "commandId");
+        Assert.required(group, "group");
+        
+        if (logger.isDebugEnabled()) {
+            logger.debug("adding command group member with id [" 
+                         + commandId 
+                         + "] to group [" 
+                         + group.getId() 
+                         + "]");
+        }
+        
         AbstractCommand command = null;
+        
         if (commandRegistry != null) {
             if (isGroup) {
-                command = commandRegistry.getCommandGroup(info.commandId);
+                command = commandRegistry.getCommandGroup(commandId);
                 if (command != null) {
                     group.addInternal(command);
                 }
             }
             else {
-                command = commandRegistry.getActionCommand(info.commandId);
+                command = commandRegistry.getActionCommand(commandId);
                 if (command != null) {
                     group.addInternal(command);
                 }
             }
         }
+        
         if (command == null) {
-            group.addLazyInternal(info.commandId);
+            group.addLazyInternal(commandId);
         }
+        
     }
 
-    private EncodedGroupMemberInfo parseEncodedCommandInfo(String str) {
-        String[] info = StringUtils.commaDelimitedListToStringArray(str);
-        if (info.length == 1) {
-            return new EncodedGroupMemberInfo(info[0]);
-        }
-        else if (info.length == 2) {
-            return new EncodedGroupMemberInfo(info[0]);
-        }
-        else {
-            throw new IllegalArgumentException("Invalid encoded command group 
info '" + str
-                    + "'; code should be of format <groupId>,[inlined]");
-        }
-    }
-
     /**
      * Configures the given command if this instance has been provided with a 
[EMAIL PROTECTED] CommandConfigurer}.
      * 
@@ -346,14 +412,6 @@
         
     }
 
-    private static final class EncodedGroupMemberInfo {
-        private String commandId;
-
-        public EncodedGroupMemberInfo(String commandId) {
-            this.commandId = commandId;
-        }
-    }
-
     /**
      * Returns the Class object for [EMAIL PROTECTED] CommandGroup}.
      * @return CommandGroup.class


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
spring-rich-c-cvs mailing list
spring-rich-c-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/spring-rich-c-cvs

Reply via email to