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.wizard;
017    
018    import java.awt.Image;
019    
020    import javax.swing.JComponent;
021    
022    import org.springframework.core.style.ToStringCreator;
023    import org.springframework.richclient.dialog.AbstractDialogPage;
024    
025    public abstract class AbstractWizardPage extends AbstractDialogPage implements WizardPage {
026        private Wizard wizard;
027    
028        private WizardPage previousPage;
029    
030        /**
031         * Creates a wizard page. This titles of this dialog page will be configured
032         * using the default ObjectConfigurer.
033         * 
034         * @param pageId
035         *            the id of this wizard page. This will be used to configure the
036         *            page.
037         */
038        protected AbstractWizardPage(String pageId) {
039            this(pageId, false);
040        }
041    
042        /**
043         * Creates a new wizard page.
044         * 
045         * @param pageId
046         *            the id of this wizard page
047         * @param autoConfigure
048         *            whether or not to use an ObjectConfigurer to configure the
049         *            titles of this dialog page using the given pageId
050         */
051        protected AbstractWizardPage(String pageId, boolean autoConfigure) {
052            super(pageId, autoConfigure);
053        }
054    
055        /**
056         * Creates a new wizard page with the given title.
057         * 
058         * @param pageId
059         *            the id of this wizard page
060         * @param autoConfigure
061         *            whether or not to use an ObjectConfigurer to configure the
062         *            titles of this dialog page using the given pageId
063         * @param title
064         *            the title of this wizard page, or <code>null</code> if none
065         */
066        protected AbstractWizardPage(String pageId, boolean autoConfigure, String title) {
067            super(pageId, autoConfigure, title);
068        }
069    
070        /**
071         * Creates a new wizard page with the given title and image.
072         * 
073         * @param pageId
074         *            the id of this wizard page
075         * @param autoConfigure
076         *            whether or not to use an ObjectConfigurer to configure the
077         *            titles of this wizard page using the given pageId
078         * @param title
079         *            the title of this wizard page, or <code>null</code> if none
080         * @param image
081         *            the image for this wizard page, or <code>null</code> if none
082         */
083        protected AbstractWizardPage(String pageId, boolean autoConfigure, String title, Image icon) {
084            super(pageId, autoConfigure, title, icon);
085        }
086    
087        public String getKey() {
088            return getWizard().getId() + "." + getId();
089        }
090    
091        public Image getImage() {
092            Image image = super.getImage();
093            if (image != null) {
094                return image;
095            }
096            return wizard.getDefaultPageImage();
097        }
098    
099        public WizardPage getNextPage() {
100            if (wizard == null) {
101                return null;
102            }
103            return wizard.getNextPage(this);
104        }
105    
106        public WizardPage getPreviousPage() {
107            if (previousPage != null) {
108                return previousPage;
109            }
110            if (wizard == null) {
111                return null;
112            }
113            return wizard.getPreviousPage(this);
114        }
115    
116        public boolean canFlipToNextPage() {
117            return isPageComplete() && getNextPage() != null;
118        }
119    
120        protected boolean isCurrentPage() {
121            return (getContainer() != null && this == getContainer().getCurrentPage());
122        }
123    
124        protected WizardContainer getContainer() {
125            if (wizard == null) {
126                return null;
127            }
128            return wizard.getContainer();
129        }
130    
131        public Wizard getWizard() {
132            return wizard;
133        }
134    
135        public void setPreviousPage(WizardPage page) {
136            previousPage = page;
137        }
138    
139        public void setVisible(boolean visible) {
140            JComponent control = getControl();
141            if (control != null) {
142                super.setVisible(visible);
143                control.requestFocusInWindow();
144            }
145        }
146    
147        public void setEnabled(boolean enabled) {
148            setPageComplete(enabled);
149        }
150    
151        public void setWizard(Wizard newWizard) {
152            Wizard oldValue = this.wizard;
153            this.wizard = newWizard;
154            firePropertyChange("wizard", oldValue, newWizard);
155        }
156    
157        public void onAboutToShow() {
158        }
159    
160        public String toString() {
161            return new ToStringCreator(this).append("id", getId()).toString();
162        }
163    }