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.Color;
019    import java.awt.Font;
020    import java.awt.Image;
021    import java.beans.PropertyChangeListener;
022    
023    import javax.swing.Icon;
024    import javax.swing.ImageIcon;
025    import javax.swing.JComponent;
026    import javax.swing.JLabel;
027    import javax.swing.JPanel;
028    import javax.swing.UIManager;
029    
030    import org.springframework.richclient.core.Message;
031    import org.springframework.richclient.core.TitleConfigurable;
032    import org.springframework.richclient.factory.AbstractControlFactory;
033    import org.springframework.richclient.image.config.ImageConfigurable;
034    import org.springframework.richclient.layout.TableLayoutBuilder;
035    
036    import com.jgoodies.forms.factories.FormFactory;
037    
038    /**
039     * A container class that that has a title area for displaying a title and an
040     * image as well as a common area for displaying a description, a message, or an
041     * error message.
042     */
043    public class TitlePane extends AbstractControlFactory implements MessagePane, TitleConfigurable, ImageConfigurable {
044    
045        /**
046         * Image source key for banner image (value <code>dialog_title_banner</code>).
047         */
048        public static final String DEFAULT_TITLE_IMAGE = "titledDialog.image";
049    
050        private String title = "Title Pane Title";
051    
052        private Image image;
053    
054        private JLabel titleLabel;
055    
056        private JLabel iconLabel;
057    
058        private MessagePane messagePane;
059    
060        public TitlePane() {
061            this(DefaultMessageAreaPane.DEFAULT_LINES_TO_DISPLAY);
062        }
063    
064        public TitlePane(int linesToDisplay) {
065            this.messagePane = new DefaultMessageAreaPane(linesToDisplay, this);
066        }
067    
068        public void setTitle(String newTitle) {
069            if (newTitle == null) {
070                newTitle = "";
071            }
072            this.title = newTitle;
073            if (isControlCreated()) {
074                titleLabel.setText(newTitle);
075            }
076        }
077    
078        public void setImage(Image image) {
079            this.image = image;
080            if (isControlCreated()) {
081                iconLabel.setIcon(getIcon());
082            }
083        }
084    
085        protected JComponent createControl() {
086            titleLabel = new JLabel();
087            titleLabel.setName("title");
088            titleLabel.setOpaque(false);
089            titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD));
090            titleLabel.setText(title);
091    
092            iconLabel = new JLabel();
093            iconLabel.setName("icon");
094            iconLabel.setBackground(getBackgroundColor());
095            iconLabel.setIcon(getIcon());
096    
097            JPanel panel = new JPanel();
098            panel.setName("panel");
099            panel.setBackground(getBackgroundColor());
100            TableLayoutBuilder table = new TableLayoutBuilder(panel);
101            table.row(FormFactory.LINE_GAP_ROWSPEC);
102            table.gapCol();
103            table.cell(titleLabel);
104            table.gapCol();
105            table.cell(iconLabel, "rowspan=2 colspec=pref");
106            table.row(FormFactory.NARROW_LINE_GAP_ROWSPEC);
107            table.cell(messagePane.getControl());
108            table.row(FormFactory.NARROW_LINE_GAP_ROWSPEC);
109            return table.getPanel();
110        }
111    
112        private Icon getIcon() {
113            if (image != null)
114                return new ImageIcon(image);
115    
116            return new ImageIcon(getImageSource().getImage(DEFAULT_TITLE_IMAGE));
117        }
118    
119        private Color getBackgroundColor() {
120            Color c = UIManager.getLookAndFeel().getDefaults().getColor("primaryControlHighlight");
121            if (c == null) {
122                c = UIManager.getColor("controlLtHighlight");
123            }
124            return c;
125        }
126    
127        public boolean isMessageShowing() {
128            return messagePane.isMessageShowing();
129        }
130    
131        public Message getMessage() {
132            return messagePane.getMessage();
133        }
134    
135        public void setMessage(Message newMessage) {
136            messagePane.setMessage(newMessage);
137        }
138    
139        public void addPropertyChangeListener(PropertyChangeListener listener) {
140            messagePane.addPropertyChangeListener(listener);
141        }
142    
143        public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
144            messagePane.addPropertyChangeListener(propertyName, listener);
145        }
146    
147        public void removePropertyChangeListener(PropertyChangeListener listener) {
148            messagePane.removePropertyChangeListener(listener);
149        }
150    
151        public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
152            messagePane.removePropertyChangeListener(propertyName, listener);
153        }
154    
155            public String getTitle() {
156                    return title;
157            }
158    
159            public Image getImage() {
160                    return image;
161            }
162    }