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.command.support;
017    
018    import org.springframework.beans.factory.InitializingBean;
019    import org.springframework.richclient.application.ApplicationWindow;
020    import org.springframework.richclient.application.PropertyNotSetException;
021    import org.springframework.richclient.application.View;
022    import org.springframework.richclient.application.ViewDescriptor;
023    import org.springframework.richclient.util.Assert;
024    
025    /**
026     * An action command for displaying a {@link View} based on a provided {@link ViewDescriptor}.
027     */
028    public class ShowViewCommand extends ApplicationWindowAwareCommand implements InitializingBean {
029        
030        private ViewDescriptor viewDescriptor;
031        
032        /**
033         * Creates a new uninitialized {@code ShowViewCommand}. The {@code applicationWindow} and 
034         * {@code viewDescriptor} properties must be set before using the new instance.
035         */
036        public ShowViewCommand() {
037            //do nothing
038        }
039    
040        /**
041         * Creates a new {@code ShowViewCommand} with the given view descriptor and associated 
042         * application window. The new instance will have a command identifier equal to the id from
043         * the view descriptor, the command will be enabled by default.
044         *
045         * @param viewDescriptor The object describing the view that this command will be 
046         * responsible for showing.
047         * @param applicationWindow The application window that the command belongs to.
048         * 
049         * @throw IllegalArgumentException if {@code viewDescriptor} or {@code applicationWindow} are null.
050         */
051        public ShowViewCommand(ViewDescriptor viewDescriptor, ApplicationWindow applicationWindow) {
052            Assert.required(applicationWindow, "applicationWindow");
053            setViewDescriptor(viewDescriptor);
054            setApplicationWindow(applicationWindow);
055            setEnabled(true);
056        }
057        
058        /**
059         * {@inheritDoc}
060         */
061        public void afterPropertiesSet() {
062            PropertyNotSetException.throwIfNull(getApplicationWindow(), "applicationWindow", getClass());
063            PropertyNotSetException.throwIfNull(this.viewDescriptor, "viewDescriptor", getClass());
064        }
065    
066        /**
067         * Sets the descriptor for the view that is to be opened by this command object. This
068         * command object will be assigned the id, label, icon, and caption from the given view
069         * descriptor.
070         *
071         * @param viewDescriptor The view descriptor, cannot be null.
072         * 
073         * @throws IllegalArgumentException if {@code viewDescriptor} is null.
074         */
075        public final void setViewDescriptor(ViewDescriptor viewDescriptor) {
076            Assert.required(viewDescriptor, "viewDescriptor");
077            setId(viewDescriptor.getId()); 
078            setLabel(viewDescriptor.getShowViewCommandLabel());
079            setIcon(viewDescriptor.getIcon());
080            setCaption(viewDescriptor.getCaption());
081            this.viewDescriptor = viewDescriptor;
082        }
083    
084        /**
085         * Causes the view described by this instance's view descriptor to be shown.
086         */
087        protected void doExecuteCommand() {
088            //FIXME getApplicationWindow can potentially return null. This should probably be 
089            //made an invariant on the ApplicationWindowAwareCommand, that it never returns null.
090            //Same applies to ApplicationWindow.getPage(), can also return null
091            getApplicationWindow().getPage().showView(this.viewDescriptor.getId());
092        }
093    
094    }