001    /*
002     * Copyright 2002-2007 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 java.awt.Image;
019    import java.awt.Window;
020    import java.beans.PropertyChangeEvent;
021    import java.beans.PropertyChangeListener;
022    
023    import javax.swing.JComponent;
024    
025    import org.springframework.richclient.core.DefaultMessage;
026    import org.springframework.richclient.core.Message;
027    import org.springframework.richclient.form.Form;
028    import org.springframework.util.Assert;
029    import org.springframework.util.StringUtils;
030    
031    /**
032     * A TitledApplicationDialog that delegates to a single DialogPage for its
033     * title, content and messages.
034     * 
035     * @author oliverh
036     */
037    public abstract class TitledPageApplicationDialog extends TitledApplicationDialog {
038    
039            private DialogPage dialogPage;
040    
041            private PropertyChangeListener dialogPagePropertyChangeHandler = new PropertyChangeListener() {
042                    public void propertyChange(PropertyChangeEvent evt) {
043                            if (Messagable.MESSAGE_PROPERTY.equals(evt.getPropertyName())) {
044                                    update();
045                            }
046                            else if (DialogPage.PAGE_COMPLETE_PROPERTY.equals(evt.getPropertyName())) {
047                                    setEnabled(dialogPage.isPageComplete());
048                            }
049                            else {
050                                    update();
051                            }
052                    }
053            };
054    
055            private Image titlePaneImage;
056    
057            private String titlePaneTitle;
058    
059            /**
060             * Default constructor. Make sure to call {@link #setDialogPage(DialogPage)}
061             * prior to using this dialog.
062             */
063            public TitledPageApplicationDialog() {
064                    super();
065            }
066    
067            public TitledPageApplicationDialog(DialogPage dialogPage) {
068                    super();
069                    setDialogPage(dialogPage);
070            }
071    
072            public TitledPageApplicationDialog(Form form, Window parent) {
073                    this(new FormBackedDialogPage(form), parent);
074            }
075    
076            public TitledPageApplicationDialog(DialogPage dialogPage, Window parent) {
077                    super(dialogPage.getTitle(), parent);
078                    setDialogPage(dialogPage);
079            }
080    
081            public TitledPageApplicationDialog(DialogPage dialogPage, Window parent, CloseAction closeAction) {
082                    super(dialogPage.getTitle(), parent, closeAction);
083                    setDialogPage(dialogPage);
084            }
085    
086            protected void setDialogPage(DialogPage dialogPage) {
087                    Assert.notNull(dialogPage, "The single dialog page to display is required");
088                    this.dialogPage = dialogPage;
089            }
090    
091            protected DialogPage getDialogPage() {
092                    return dialogPage;
093            }
094    
095            protected JComponent createTitledDialogContentPane() {
096                    dialogPage.addPropertyChangeListener(dialogPagePropertyChangeHandler);
097                    update();
098                    return dialogPage.getControl();
099            }
100    
101            protected Message getDescription() {
102                    return new DefaultMessage(dialogPage.getDescription());
103            }
104    
105            protected void update() {
106                    if (!StringUtils.hasText(getTitle())) {
107                            setTitle(dialogPage.getTitle());
108                    }
109                    updateTitlePane();
110                    updateMessagePane();
111            }
112    
113            protected void updateTitlePane() {
114                    super.setTitlePaneTitle(titlePaneTitle != null ? titlePaneTitle : dialogPage.getTitle());
115                    super.setTitlePaneImage(titlePaneImage != null ? titlePaneImage : dialogPage.getImage());
116                    setDescription(dialogPage.getDescription());
117            }
118            
119            protected void updateMessagePane() {
120                    setMessage(dialogPage.getMessage());
121            }
122    
123            /**
124             * Sets the image to use in the title pane. Normally the image is provided
125             * by the current dialog page, but this method allows for overriding this.
126             * <p>
127             * If the image passed is null, the image of the dialog page will be used.
128             * @param image the image
129             * @see TitledApplicationDialog#setTitlePaneImage(Image)
130             */
131            public void setTitlePaneImage(Image image) {
132                    titlePaneImage = image;
133                    super.setTitlePaneImage(image);
134            }
135    
136            /**
137             * Sets the title to use in the title pane. Normally the title is provided
138             * by the current dialog page, but this method allows for overriding this.
139             * <p>
140             * If the title passed is null, the title of the dialog page will be used.
141             * @param title the title
142             * @see TitledApplicationDialog#setTitlePaneTitle(String)
143             */
144            public void setTitlePaneTitle(String title) {
145                    titlePaneTitle = title;
146                    super.setTitlePaneTitle(title);
147            }
148    }