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.awt.Image;
019    import java.awt.Window;
020    import java.beans.PropertyChangeListener;
021    
022    import javax.swing.Icon;
023    import javax.swing.JComponent;
024    
025    import org.springframework.richclient.application.PageComponentContext;
026    import org.springframework.richclient.application.PageComponentDescriptor;
027    import org.springframework.richclient.application.View;
028    import org.springframework.richclient.application.statusbar.StatusBar;
029    import org.springframework.richclient.command.CommandManager;
030    import org.springframework.richclient.factory.AbstractControlFactory;
031    import org.springframework.util.Assert;
032    
033    public abstract class AbstractView extends AbstractControlFactory implements View {
034        private PageComponentDescriptor descriptor;
035    
036        private PageComponentContext context;
037    
038        public void setDescriptor(PageComponentDescriptor descriptor) {
039            Assert.notNull(descriptor, "The view descriptor is required");
040            Assert.state(this.descriptor == null, "A view's descriptor may only be set once");
041            this.descriptor = descriptor;
042        }
043    
044        public final void setContext(PageComponentContext context) {
045            Assert.notNull(context, "This view's page component context is required");
046            Assert.state(this.context == null, "A view's context may only be set once");
047            this.context = context;
048            registerLocalCommandExecutors(context);
049        }
050    
051        public String getId() {
052            return getDescriptor().getId();
053        }
054    
055        public PageComponentDescriptor getDescriptor() {
056            Assert.state(descriptor != null, "View descriptor property is not set; it is required");
057            return descriptor;
058        }
059    
060        public PageComponentContext getContext() {
061            return context;
062        }
063    
064        public void componentOpened() {
065        }
066    
067        public void componentClosed() {
068        }
069    
070        public void componentFocusGained() {
071        }
072    
073        public void componentFocusLost() {
074        }
075    
076        public String getCaption() {
077            return getDescriptor().getCaption();
078        }
079    
080        public String getDescription() {
081            return getDescriptor().getDescription();
082        }
083    
084        public String getDisplayName() {
085            return getDescriptor().getDisplayName();
086        }
087    
088        public Icon getIcon() {
089            return getDescriptor().getIcon();
090        }
091    
092        public Image getImage() {
093            return getDescriptor().getImage();
094        }
095    
096        protected final Window getWindowControl() {
097            return getContext().getWindow().getControl();
098        }
099    
100        protected final CommandManager getWindowCommandManager() {
101            return context.getWindow().getCommandManager();
102        }
103    
104        protected final StatusBar getStatusBar() {
105            return context.getWindow().getStatusBar();
106        }
107    
108        protected abstract JComponent createControl();
109    
110        /**
111         * Template method called once when this view is initialized; allows subclasses to register local executors for
112         * shared commands with the view context.
113         * 
114         * @param context
115         *            the view context
116         */
117        protected void registerLocalCommandExecutors(PageComponentContext context) {
118    
119        }
120    
121        public void addPropertyChangeListener(PropertyChangeListener listener) {
122            getDescriptor().addPropertyChangeListener(listener);
123        }
124    
125        public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
126            getDescriptor().addPropertyChangeListener(propertyName, listener);
127        }
128    
129        public void removePropertyChangeListener(PropertyChangeListener listener) {
130            getDescriptor().removePropertyChangeListener(listener);
131        }
132    
133        public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
134            getDescriptor().removePropertyChangeListener(propertyName, listener);
135        }
136    
137        public void dispose() {
138    
139        }
140    
141        public boolean canClose() {
142            return true;
143        }
144    
145        public void close() {
146            context.getPage().close(this);
147        }
148    
149        /**
150         * {@inheritDoc}
151         * 
152         * This implementation does nothing.
153         */
154        public void setInput(Object input) {
155    
156        }
157    }