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.dialog;
017    
018    import javax.swing.JComponent;
019    
020    import org.springframework.richclient.form.Form;
021    
022    /**
023     * An implementation of DialogPage that delegates to a FormPage for its control,
024     * pageComplete status and messages.
025     *
026     * @author Oliver Hutchison
027     */
028    public class FormBackedDialogPage extends AbstractDialogPage {
029        private Form backingFormPage;
030    
031        /**
032         * Creates a new FormBackedDialogPage
033         *
034         * @param backingFormPage
035         *            a named form page that will provide the control for this
036         *            dialog page
037         */
038        public FormBackedDialogPage(Form backingFormPage) {
039            this(backingFormPage, true);
040        }
041    
042        public FormBackedDialogPage(Form backingFormPage, boolean autoConfigure) {
043            super(backingFormPage.getId(), autoConfigure);
044            this.backingFormPage = backingFormPage;
045        }
046    
047        /**
048         * Creates a new FormPageBackedDialogPage.
049         *
050         * @param parentPageId
051         *            the id of a containing parent page. This will be used to
052         *            configure page titles/description
053         * @param backingFormPage
054         *            the FormPage which will provide the control for this page.
055         */
056        public FormBackedDialogPage(String parentPageId, Form backingFormPage) {
057            super(parentPageId + (backingFormPage.getId() != null ? "." + backingFormPage.getId() : ""));
058            this.backingFormPage = backingFormPage;
059        }
060    
061        /**
062         * Get the Form backing this dialog page.
063         * @return form
064         */
065        public Form getBackingFormPage() {
066            return backingFormPage;
067        }
068    
069        public void onAboutToShow() {
070            setEnabled(!backingFormPage.hasErrors());
071        }
072    
073        protected JComponent createControl() {
074            JComponent formControl = backingFormPage.getControl();
075            initPageValidationReporter();
076            return formControl;
077        }
078    
079        protected void initPageValidationReporter() {
080            backingFormPage.newSingleLineResultsReporter(this);
081            backingFormPage.addGuarded(this);
082        }
083    
084        public void setEnabled(boolean enabled) {
085            setPageComplete(enabled);
086        }
087    }