001    /*
002     * Copyright 2002-2008 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.application.support;
017    
018    import java.util.Collections;
019    import java.util.Map;
020    
021    import org.springframework.beans.BeanUtils;
022    import org.springframework.beans.BeanWrapper;
023    import org.springframework.beans.BeanWrapperImpl;
024    import org.springframework.beans.factory.BeanInitializationException;
025    import org.springframework.beans.factory.BeanNameAware;
026    import org.springframework.beans.factory.InitializingBean;
027    import org.springframework.richclient.application.ApplicationWindow;
028    import org.springframework.richclient.application.PageComponent;
029    import org.springframework.richclient.application.View;
030    import org.springframework.richclient.application.ViewDescriptor;
031    import org.springframework.richclient.command.ActionCommand;
032    import org.springframework.richclient.command.config.CommandButtonLabelInfo;
033    import org.springframework.richclient.command.support.ShowViewCommand;
034    import org.springframework.richclient.core.LabeledObjectSupport;
035    import org.springframework.util.Assert;
036    
037    /**
038     * Provides a standard implementation of {@link ViewDescriptor}.
039     * 
040     * @author Keith Donald
041     */
042    public class DefaultViewDescriptor extends LabeledObjectSupport implements ViewDescriptor, BeanNameAware,
043            InitializingBean {
044        private String id;
045    
046        private Class<? extends View> viewClass;
047    
048        private Map<String, Object> viewProperties;
049    
050        public DefaultViewDescriptor() {
051            // default constructor for spring creation
052        }
053    
054        public DefaultViewDescriptor(String id, Class<? extends View> viewClass) {
055            this(id, viewClass, Collections.<String, Object> emptyMap());
056        }
057    
058        public DefaultViewDescriptor(String id, Class<? extends View> viewClass, Map<String, Object> viewProperties) {
059            setId(id);
060            setViewClass(viewClass);
061            setViewProperties(viewProperties);
062        }
063    
064        public void setBeanName(String beanName) {
065            setId(beanName);
066        }
067    
068        public void setId(String id) {
069            Assert.notNull("id is required");
070            this.id = id;
071        }
072    
073        public String getId() {
074            return id;
075        }
076    
077        public Class<? extends View> getViewClass() {
078            return viewClass;
079        }
080    
081        public void setViewClass(Class<? extends View> viewClass) {
082            Assert.notNull(viewClass, "viewClass cannot be null");
083            Assert.isTrue(View.class.isAssignableFrom(viewClass), "viewClass doesn't derive from View");
084    
085            this.viewClass = viewClass;
086        }
087    
088        public void setViewProperties(Map<String, Object> viewProperties) {
089            this.viewProperties = viewProperties;
090        }
091    
092        public PageComponent createPageComponent() {
093            return createView();
094        }
095    
096        protected View createView() {
097            Assert.state(viewClass != null, "View class to instantiate is not set");
098            Object o = BeanUtils.instantiateClass(viewClass);
099            Assert.isTrue((o instanceof View), "View class '" + viewClass
100                    + "' was instantiated, but instance is not a View!");
101            View view = (View) o;
102            view.setDescriptor(this);
103            if (viewProperties != null) {
104                BeanWrapper wrapper = new BeanWrapperImpl(view);
105                wrapper.setPropertyValues(viewProperties);
106            }
107    
108            if (view instanceof InitializingBean) {
109                try {
110                    ((InitializingBean) view).afterPropertiesSet();
111                } catch (Exception e) {
112                    throw new BeanInitializationException("Problem running on " + view, e);
113                }
114            }
115            return view;
116        }
117    
118        public CommandButtonLabelInfo getShowViewCommandLabel() {
119            return getLabel();
120        }
121    
122        public ActionCommand createShowViewCommand(ApplicationWindow window) {
123            return new ShowViewCommand(this, window);
124        }
125    
126        public void afterPropertiesSet() throws Exception {
127            Assert.notNull(id, "id is mandatory");
128            Assert.notNull(viewClass, "viewClass is mandatory");
129        }
130    
131    }