001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.springframework.richclient.command.support; 017 018 import org.apache.commons.logging.Log; 019 import org.apache.commons.logging.LogFactory; 020 import org.springframework.beans.BeansException; 021 import org.springframework.beans.factory.BeanFactory; 022 import org.springframework.beans.factory.BeanFactoryAware; 023 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 024 import org.springframework.beans.factory.config.BeanPostProcessor; 025 import org.springframework.richclient.application.ApplicationServicesLocator; 026 import org.springframework.richclient.command.AbstractCommand; 027 import org.springframework.richclient.command.ActionCommand; 028 import org.springframework.richclient.command.ActionCommandExecutor; 029 import org.springframework.richclient.command.ActionCommandInterceptor; 030 import org.springframework.richclient.command.CommandGroup; 031 import org.springframework.richclient.command.CommandGroupFactoryBean; 032 import org.springframework.richclient.command.CommandManager; 033 import org.springframework.richclient.command.CommandNotOfRequiredTypeException; 034 import org.springframework.richclient.command.CommandRegistry; 035 import org.springframework.richclient.command.CommandRegistryListener; 036 import org.springframework.richclient.command.CommandServices; 037 import org.springframework.richclient.command.ExclusiveCommandGroup; 038 import org.springframework.richclient.command.TargetableActionCommand; 039 import org.springframework.richclient.command.config.CommandButtonConfigurer; 040 import org.springframework.richclient.command.config.CommandConfigurer; 041 import org.springframework.richclient.command.config.CommandFaceDescriptor; 042 import org.springframework.richclient.factory.ButtonFactory; 043 import org.springframework.richclient.factory.ComponentFactory; 044 import org.springframework.richclient.factory.MenuFactory; 045 import org.springframework.util.Assert; 046 047 /** 048 * @author Keith Donald 049 */ 050 public class DefaultCommandManager implements CommandManager, BeanPostProcessor, BeanFactoryAware { 051 private final Log logger = LogFactory.getLog(getClass()); 052 053 private BeanFactory beanFactory; 054 055 private final DefaultCommandRegistry commandRegistry = new DefaultCommandRegistry(); 056 057 private CommandServices commandServices; 058 059 private CommandConfigurer commandConfigurer; 060 061 public DefaultCommandManager() { 062 063 } 064 065 public DefaultCommandManager(CommandRegistry parent) { 066 setParent(parent); 067 } 068 069 public DefaultCommandManager(CommandServices commandServices) { 070 setCommandServices(commandServices); 071 } 072 073 public void setCommandServices(CommandServices commandServices) { 074 Assert.notNull(commandServices, "A command services implementation is required"); 075 this.commandServices = commandServices; 076 } 077 078 public CommandServices getCommandServices() { 079 if(commandServices == null) { 080 commandServices = (CommandServices) ApplicationServicesLocator.services().getService(CommandServices.class); 081 } 082 return commandServices; 083 } 084 085 public void setParent(CommandRegistry parent) { 086 commandRegistry.setParent(parent); 087 } 088 089 public CommandConfigurer getCommandConfigurer() { 090 if(commandConfigurer == null) { 091 commandConfigurer = (CommandConfigurer) ApplicationServicesLocator.services().getService(CommandConfigurer.class); 092 } 093 return commandConfigurer; 094 } 095 096 public void setCommandConfigurer(CommandConfigurer commandConfigurer) { 097 Assert.notNull(commandConfigurer, "command configurer must not be null"); 098 this.commandConfigurer = commandConfigurer; 099 } 100 101 public void setBeanFactory(BeanFactory beanFactory) { 102 this.beanFactory = beanFactory; 103 } 104 105 public ComponentFactory getComponentFactory(){ 106 return getCommandServices().getComponentFactory(); 107 } 108 109 public ButtonFactory getToolBarButtonFactory() { 110 return getCommandServices().getButtonFactory(); 111 } 112 113 public ButtonFactory getButtonFactory() { 114 return getCommandServices().getButtonFactory(); 115 } 116 117 public MenuFactory getMenuFactory() { 118 return getCommandServices().getMenuFactory(); 119 } 120 121 public CommandButtonConfigurer getDefaultButtonConfigurer() { 122 return getCommandServices().getDefaultButtonConfigurer(); 123 } 124 125 public CommandButtonConfigurer getToolBarButtonConfigurer() { 126 return getCommandServices().getToolBarButtonConfigurer(); 127 } 128 129 public CommandButtonConfigurer getMenuItemButtonConfigurer() { 130 return getCommandServices().getMenuItemButtonConfigurer(); 131 } 132 133 public CommandButtonConfigurer getPullDownMenuButtonConfigurer() { 134 return getCommandServices().getPullDownMenuButtonConfigurer(); 135 } 136 137 public CommandFaceDescriptor getFaceDescriptor(AbstractCommand command, String faceDescriptorId) { 138 if (beanFactory == null) { 139 return null; 140 } 141 try { 142 return (CommandFaceDescriptor)beanFactory.getBean(command.getId() + "." + faceDescriptorId, 143 CommandFaceDescriptor.class); 144 } 145 catch (NoSuchBeanDefinitionException e) { 146 try { 147 return (CommandFaceDescriptor)beanFactory.getBean(faceDescriptorId, CommandFaceDescriptor.class); 148 } 149 catch (NoSuchBeanDefinitionException ex) { 150 return null; 151 } 152 } 153 } 154 155 public ActionCommand getActionCommand(String commandId) { 156 return (ActionCommand) commandRegistry.getCommand(commandId, ActionCommand.class); 157 } 158 159 public CommandGroup getCommandGroup(String groupId) { 160 return (CommandGroup)commandRegistry.getCommand(groupId, CommandGroup.class); 161 } 162 163 public boolean containsCommandGroup(String groupId) { 164 return commandRegistry.containsCommandGroup(groupId); 165 } 166 167 public boolean containsActionCommand(String commandId) { 168 return commandRegistry.containsActionCommand(commandId); 169 } 170 171 public void addCommandInterceptor(String commandId, ActionCommandInterceptor interceptor) { 172 getActionCommand(commandId).addCommandInterceptor(interceptor); 173 } 174 175 public void removeCommandInterceptor(String commandId, ActionCommandInterceptor interceptor) { 176 getActionCommand(commandId).removeCommandInterceptor(interceptor); 177 } 178 179 public void registerCommand(AbstractCommand command) { 180 if (logger.isDebugEnabled()) { 181 logger.debug("Configuring and registering new command '" + command.getId() + "'"); 182 } 183 configure(command); 184 commandRegistry.registerCommand(command); 185 } 186 187 public void setTargetableActionCommandExecutor(String commandId, ActionCommandExecutor executor) { 188 commandRegistry.setTargetableActionCommandExecutor(commandId, executor); 189 } 190 191 public void addCommandRegistryListener(CommandRegistryListener l) { 192 this.commandRegistry.addCommandRegistryListener(l); 193 } 194 195 public void removeCommandRegistryListener(CommandRegistryListener l) { 196 this.commandRegistry.removeCommandRegistryListener(l); 197 } 198 199 public TargetableActionCommand createTargetableActionCommand(String commandId, ActionCommandExecutor delegate) { 200 Assert.notNull(commandId, "Registered targetable action commands must have an id."); 201 TargetableActionCommand newCommand = new TargetableActionCommand(commandId, delegate); 202 registerCommand(newCommand); 203 return newCommand; 204 } 205 206 public CommandGroup createCommandGroup(String groupId, Object[] members) { 207 Assert.notNull(groupId, "Registered command groups must have an id."); 208 CommandGroup newGroup = new CommandGroupFactoryBean(groupId, this.commandRegistry, this, members) 209 .getCommandGroup(); 210 registerCommand(newGroup); 211 return newGroup; 212 } 213 214 public ExclusiveCommandGroup createExclusiveCommandGroup(String groupId, Object[] members) { 215 Assert.notNull(groupId, "Registered exclusive command groups must have an id."); 216 CommandGroupFactoryBean newGroupFactory = new CommandGroupFactoryBean(groupId, this.commandRegistry, this, 217 members); 218 newGroupFactory.setExclusive(true); 219 registerCommand(newGroupFactory.getCommandGroup()); 220 return (ExclusiveCommandGroup)newGroupFactory.getCommandGroup(); 221 } 222 223 public AbstractCommand configure(AbstractCommand command) { 224 return getCommandConfigurer().configure(command); 225 } 226 227 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 228 if (bean instanceof AbstractCommand) { 229 registerCommand((AbstractCommand)bean); 230 } 231 return bean; 232 } 233 234 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 235 if (bean instanceof CommandGroupFactoryBean) { 236 CommandGroupFactoryBean factory = (CommandGroupFactoryBean)bean; 237 factory.setCommandRegistry(commandRegistry); 238 } 239 else if (bean instanceof AbstractCommand) { 240 configure((AbstractCommand)bean); 241 } 242 return bean; 243 } 244 245 /** 246 * {@inheritDoc} 247 */ 248 public boolean containsCommand(String commandId) { 249 return this.commandRegistry.containsCommand(commandId); 250 } 251 252 /** 253 * {@inheritDoc} 254 */ 255 public Object getCommand(String commandId, Class requiredType) throws CommandNotOfRequiredTypeException { 256 return this.commandRegistry.getCommand(commandId, requiredType); 257 } 258 259 /** 260 * {@inheritDoc} 261 */ 262 public Object getCommand(String commandId) { 263 return this.commandRegistry.getCommand(commandId); 264 } 265 266 /** 267 * {@inheritDoc} 268 */ 269 public Class getType(String commandId) { 270 return this.commandRegistry.getType(commandId); 271 } 272 273 /** 274 * {@inheritDoc} 275 */ 276 public boolean isTypeMatch(String commandId, Class targetType) { 277 return this.commandRegistry.isTypeMatch(commandId, targetType); 278 } 279 280 }