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