001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * 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, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.command.config;
017    
018    import org.apache.commons.logging.Log;
019    import org.apache.commons.logging.LogFactory;
020    import org.springframework.richclient.application.ApplicationServicesLocator;
021    import org.springframework.richclient.application.config.ApplicationObjectConfigurer;
022    import org.springframework.richclient.command.AbstractCommand;
023    import org.springframework.richclient.command.CommandServices;
024    import org.springframework.richclient.util.Assert;
025    import org.springframework.util.ClassUtils;
026    import org.springframework.util.StringUtils;
027    
028    /**
029     * @author Keith Donald
030     */
031    public class DefaultCommandConfigurer implements CommandConfigurer {
032        private final Log logger = LogFactory.getLog(getClass());
033    
034        private CommandServices commandServices;
035    
036        private ApplicationObjectConfigurer objectConfigurer;
037    
038        public DefaultCommandConfigurer() {
039        }
040    
041        public DefaultCommandConfigurer(CommandServices commandServices) {
042            setCommandServices(commandServices);
043        }
044    
045        public void setApplicationObjectConfigurer(ApplicationObjectConfigurer configurer) {
046            this.objectConfigurer = configurer;
047        }
048    
049        public void setCommandServices(CommandServices services) {
050            this.commandServices = services;
051        }
052    
053        public AbstractCommand configure(AbstractCommand command) {
054            Assert.required(command, "command");
055            return configure(command, getObjectConfigurer());
056        }
057    
058        protected ApplicationObjectConfigurer getObjectConfigurer() {
059            if (objectConfigurer == null) {
060                objectConfigurer = (ApplicationObjectConfigurer)ApplicationServicesLocator.services().getService(ApplicationObjectConfigurer.class);
061            }
062            return objectConfigurer;
063        }
064    
065        public AbstractCommand configure(AbstractCommand command, ApplicationObjectConfigurer configurer) {
066            command.setCommandServices(getCommandServices());
067            String objectName = command.getId();
068            if (command.isAnonymous()) {
069                objectName = ClassUtils.getShortNameAsProperty(command.getClass());
070                int lastDot = objectName.lastIndexOf('.');
071                if (lastDot != -1) {
072                    objectName = StringUtils.uncapitalize(objectName.substring(lastDot + 1));
073                }
074            }
075    
076            // Configure the command itself
077            configurer.configure( command, objectName );
078    
079            // Configure the command face
080            if (logger.isDebugEnabled()) {
081                logger.debug("Configuring faces (aka visual appearance descriptors) for " + command);
082            }
083            CommandFaceDescriptor face = new CommandFaceDescriptor();
084            configurer.configure(face, objectName);
085            command.setFaceDescriptor(face);
086            return command;
087        }
088    
089        protected CommandServices getCommandServices() {
090            if (commandServices == null) {
091                commandServices = (CommandServices) ApplicationServicesLocator.services().getService(CommandServices.class);
092            }
093            return commandServices;
094        }
095    
096    }