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.BorderLayout;
019    import java.awt.Image;
020    import java.awt.Window;
021    import java.beans.PropertyChangeListener;
022    
023    import javax.swing.JComponent;
024    import javax.swing.JPanel;
025    import javax.swing.JSeparator;
026    
027    import org.springframework.richclient.core.DefaultMessage;
028    import org.springframework.richclient.core.DescriptionConfigurable;
029    import org.springframework.richclient.core.Message;
030    import org.springframework.richclient.image.config.ImageConfigurable;
031    import org.springframework.richclient.util.GuiStandardUtils;
032    
033    public abstract class TitledApplicationDialog extends ApplicationDialog implements Messagable, ImageConfigurable,
034                    DescriptionConfigurable {
035            private TitlePane titlePane = new TitlePane();
036    
037            private Message description = new DefaultMessage("Title pane description");
038    
039            private JComponent pageControl;
040    
041            private JComponent contentPane;
042    
043            public TitledApplicationDialog() {
044                    super();
045            }
046    
047            public TitledApplicationDialog(String title, Window parent) {
048                    super(title, parent);
049            }
050    
051            public TitledApplicationDialog(String title, Window parent, CloseAction closeAction) {
052                    super(title, parent, closeAction);
053            }
054    
055            public void setCaption(String shortDescription) {
056                    throw new UnsupportedOperationException("What can I do with a caption?");
057            }
058    
059            public void setDescription(String description) {
060                    this.description = new DefaultMessage(description);
061                    setMessage(this.description);
062            }
063    
064            public void setTitlePaneTitle(String titleAreaText) {
065                    titlePane.setTitle(titleAreaText);
066            }
067    
068            protected String getTitlePaneTitle() {
069                    return titlePane.getTitle();
070            }
071    
072            public void setTitlePaneImage(Image image) {
073                    titlePane.setImage(image);
074            }
075    
076            protected Image getTitlePaneImage() {
077                    return titlePane.getImage();
078            }
079    
080            public void setImage(Image image) {
081                    setTitlePaneImage(image);
082            }
083    
084            public Message getMessage() {
085                    return titlePane.getMessage();
086            }
087    
088            public void setMessage(Message message) {
089                    if (message == null || DefaultMessage.EMPTY_MESSAGE.equals(message)) {
090                            titlePane.setMessage(getDescription());
091                    }
092                    else {
093                            titlePane.setMessage(message);
094                    }
095            }
096    
097            public boolean isMessageShowing() {
098                    return titlePane.isMessageShowing();
099            }
100    
101            protected Message getDescription() {
102                    return description;
103            }
104    
105            protected void setContentPane(JComponent c) {
106                    if (isControlCreated()) {
107                            pageControl.remove(contentPane);
108                            contentPane = c;
109                            pageControl.add(contentPane);
110                            pageControl.revalidate();
111                            pageControl.repaint();
112                    }
113                    else {
114                            throw new IllegalStateException("Cannot set content pane until control is created");
115                    }
116            }
117    
118            protected void addDialogComponents() {
119                    JComponent dialogContentPane = createDialogContentPane();
120                    getDialog().getContentPane().add(dialogContentPane, BorderLayout.CENTER);
121                    getDialog().getContentPane().add(createButtonBar(), BorderLayout.SOUTH);
122            }
123    
124            /**
125             * {@inheritDoc}
126             *
127             * Creates an additional panel at the top containing a title/message area.
128             * This can be used in conjunction with validation reporters to show the
129             * most recent error or to simply show a title and a description of the
130             * current Dialog.
131             *
132             * Use {@link #createTitledDialogContentPane()} to add your custom components.
133             */
134            protected JComponent createDialogContentPane() {
135                    pageControl = new JPanel(new BorderLayout());
136                    JPanel titlePaneContainer = new JPanel(new BorderLayout());
137                    setMessage(getDescription());
138                    titlePaneContainer.add(titlePane.getControl());
139                    titlePaneContainer.add(new JSeparator(), BorderLayout.SOUTH);
140                    pageControl.add(titlePaneContainer, BorderLayout.NORTH);
141                    contentPane = createTitledDialogContentPane();
142                    if (getPreferredSize() != null) {
143                            contentPane.setPreferredSize(getPreferredSize());
144                    }
145                    GuiStandardUtils.attachDialogBorder(contentPane);
146                    pageControl.add(contentPane);
147                    return pageControl;
148            }
149    
150            /**
151             *
152             * @return a component that will be added as the content of the Titled Dialog.
153             */
154            protected abstract JComponent createTitledDialogContentPane();
155    
156            /**
157             * Dispose of the dialog content.
158             */
159            protected void disposeDialogContentPane() {
160                    contentPane = null;
161                    pageControl = null;
162            }
163    
164            public void addPropertyChangeListener(PropertyChangeListener listener) {
165                    titlePane.addPropertyChangeListener(listener);
166            }
167    
168            public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
169                    titlePane.addPropertyChangeListener(propertyName, listener);
170            }
171    
172            public void removePropertyChangeListener(PropertyChangeListener listener) {
173                    titlePane.removePropertyChangeListener(listener);
174            }
175    
176            public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
177                    titlePane.removePropertyChangeListener(propertyName, listener);
178            }
179    }