KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > JXTitledPanel


1 /*
2  * $Id: JXTitledPanel.java,v 1.1 2005/02/24 20:35:32 rbair Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing;
9
10 import java.awt.BorderLayout JavaDoc;
11 import java.awt.Color JavaDoc;
12 import java.awt.Container JavaDoc;
13 import java.awt.GradientPaint JavaDoc;
14 import java.awt.Graphics JavaDoc;
15 import java.awt.Graphics2D JavaDoc;
16 import java.awt.GridBagConstraints JavaDoc;
17 import java.awt.GridBagLayout JavaDoc;
18 import java.awt.Image JavaDoc;
19 import java.awt.Insets JavaDoc;
20 import java.beans.PropertyChangeEvent JavaDoc;
21 import java.beans.PropertyChangeListener JavaDoc;
22
23 import javax.swing.BorderFactory JavaDoc;
24 import javax.swing.ImageIcon JavaDoc;
25 import javax.swing.JComponent JavaDoc;
26 import javax.swing.JLabel JavaDoc;
27 import javax.swing.JPanel JavaDoc;
28 import javax.swing.UIManager JavaDoc;
29 import javax.swing.plaf.ColorUIResource JavaDoc;
30 import org.jdesktop.swing.utils.UIManagerUtils;
31
32 /**
33  * A special type of Panel that has a Title section and a
34  * Content section.<br>
35  * The following 4 properties can be set with the UIManager to
36  * change the look and feel of the JTitledPanel:
37  * <ul>
38  * <li>JTitledPanel.title.foreground</li>
39  * <li>JTitledPanel.title.background</li>
40  * <li>JTitledPanel.title.font</li>
41  * </ul>
42  * @author Richard Bair
43  * @author Nicola Ken Barozzi
44  */

45 public class JXTitledPanel extends JPanel JavaDoc{
46     private static final ColorUIResource JavaDoc BLACK = new ColorUIResource JavaDoc(Color.BLACK);
47     private static final ColorUIResource JavaDoc WHITE = new ColorUIResource JavaDoc(Color.WHITE);
48     /**
49      * JLabel used for the title in the Title section of the JTitledPanel.
50      */

51     protected JLabel JavaDoc caption;
52     /**
53      * The text to use for the title
54      */

55     private String JavaDoc title = "";
56     /**
57      * The ContentPanel. Whatever this container is will be displayed in the
58      * Content section
59      */

60     private Container JavaDoc contentPanel;
61     /**
62      * The Title section panel.
63      */

64     protected JPanel JavaDoc topPanel;
65
66     /**
67      * Create a new JTitledPanel with an empty string for the title.
68      */

69     public JXTitledPanel() {
70         this("");
71     }
72
73     /**
74      * Create a new JTitledPanel with the given title as the title for the panel.
75      * @param title
76      */

77     public JXTitledPanel(String JavaDoc title) {
78         this.title = (title == null ? "" : title);
79         
80         //set the UIManager colors for this component to some defaults if they are not already set
81
UIManagerUtils.initDefault("JTitledPanel.title.foreground", "black", BLACK);
82         Color JavaDoc c = UIManager.getColor("ComboBox.selectionBackground");
83         //TOTAL HACK. TODO What should I do about default colors?
84
if (c == null) {
85             c = UIManager.getColor("ProgressBar.background");
86             if (c == null) {
87                 c = Color.decode("0x80b2ea");
88             }
89         }
90         UIManagerUtils.initDefault("JTitledPanel.title.darkBackground", "primary2", c);
91         UIManagerUtils.initDefault("JTitledPanel.title.lightBackground", "white", WHITE);
92         UIManagerUtils.initDefault("JTitledPanel.title.font", UIManager.getFont("Button.font"));
93         
94         initGui();
95     }
96
97     /**
98      * Create a new JTitledPanel with the given String as the title, and the
99      * given Container as the content panel.
100      * @param title
101      * @param content
102      */

103     public JXTitledPanel(String JavaDoc title, Container JavaDoc content) {
104         this(title);
105         setContentContainer(content);
106     }
107
108     /**
109      * @return
110      */

111     public String JavaDoc getTitle() {
112         return title;
113     }
114
115     /**
116      * @param string
117      */

118     public void setTitle(String JavaDoc title) {
119         String JavaDoc oldTitle = this.title;
120         this.title = (title == null ? "" : title);
121         caption.setText(title);
122         PropertyChangeEvent JavaDoc event = new PropertyChangeEvent JavaDoc(this, "title", oldTitle, title);
123         PropertyChangeListener JavaDoc[] listeners = this.getPropertyChangeListeners("title");
124         for (int i = 0; i < listeners.length; i++) {
125             listeners[i].propertyChange(event);
126         }
127     }
128     
129     /**
130      * Utility method for initializing the gui
131      */

132     private void initGui() {
133         //draw my beautiful self.
134
//the widget has a title bar, and a content area.
135
//The content area can in theory contain any component.
136

137         //in reality, I'd like to have a peer that does all the drawing
138
//(as in swing).
139
Color JavaDoc titleColor = UIManager.getColor("JTitledPanel.title.foreground");
140         
141         
142         this.setLayout(new BorderLayout JavaDoc());
143         
144         contentPanel = new JPanel JavaDoc();
145         ((JPanel JavaDoc)contentPanel).setBorder(BorderFactory.createEmptyBorder());
146         this.add(contentPanel, BorderLayout.CENTER);
147
148         caption = new JLabel JavaDoc(title);
149         caption.setFont(UIManager.getFont("JTitledPanel.title.font"));
150         topPanel = new JGradientPanel();
151         topPanel.setBorder(BorderFactory.createEmptyBorder());
152         topPanel.setLayout(new GridBagLayout JavaDoc());
153         caption.setForeground(titleColor);
154         topPanel.add(caption, new GridBagConstraints JavaDoc(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets JavaDoc(2, 12, 2, 4), 0, 0));
155         this.add(topPanel, BorderLayout.NORTH);
156         
157         this.setBorder(BorderFactory.createRaisedBevelBorder());
158         
159         setOpaque(false);
160     }
161
162     /**
163      * A special inner class who's background is painted as a gradient. This is used
164      * as the Title section of the JTitledPanel
165      * @author Richard Bair
166      * date: Jan 13, 2004
167      */

168     private static final class JGradientPanel extends JPanel JavaDoc {
169         private GradientPaint JavaDoc gp;
170         private double oldWidth = -1;
171         private double oldHeight = -1;
172         private ImageIcon JavaDoc helper = new ImageIcon JavaDoc();
173         public JGradientPanel() {
174         }
175         
176         //override the background color to provide for a gradient
177

178         /* (non-Javadoc)
179          * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
180          *
181          * There is some special optimization code in here that is kind of...er..shall we say, tricky.
182          * First off, the gradient panels are taking forever to draw. Therefore, I have resorted to caching the
183          * gradient'ed paint job so that I don't have to repaint it all the time.
184          */

185         protected void paintComponent(Graphics JavaDoc g) {
186             if (gp == null) {
187                 gp = new GradientPaint JavaDoc(0, 0, UIManager.getColor("JTitledPanel.title.darkBackground"), getWidth(), getHeight(), UIManager.getColor("JTitledPanel.title.lightBackground"));
188             }
189             //draw the gradient background
190
if (oldWidth != getWidth() || oldHeight != getHeight()) {
191                 Image JavaDoc savedImg = createImage(getWidth(), getHeight());
192                 Graphics2D JavaDoc imgg = (Graphics2D JavaDoc)savedImg.getGraphics();
193                 imgg.setPaint(gp);
194                 imgg.fillRect(0, 0, getWidth(), getHeight());
195                 oldWidth = getWidth();
196                 oldHeight = getHeight();
197                 helper.setImage(savedImg);
198             }
199             //draw the image
200
g.drawImage(helper.getImage(), 0, 0, getWidth(), getHeight(), helper.getImageObserver());
201         }
202
203     }
204     
205     /**
206      * @return
207      */

208     public Container JavaDoc getContentContainer() {
209         return contentPanel;
210     }
211     
212     public void setContentContainer(Container JavaDoc contentPanel) {
213         remove(this.contentPanel);
214         add(contentPanel, BorderLayout.CENTER);
215         this.contentPanel = contentPanel;
216     }
217     
218     /**
219      * Adds the given JComponent as a decoration on the right of the title
220      * @param decoration
221      */

222     public void addRightDecoration(JComponent JavaDoc decoration) {
223         topPanel.add(decoration, new GridBagConstraints JavaDoc(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets JavaDoc(0, 0, 0, 0), 0, 0));
224     }
225
226     /**
227      * @return Returns the topPanel.
228      */

229     protected JPanel JavaDoc getTopPanel() {
230         return topPanel;
231     }
232     
233     /**
234      * Adds the given JComponent as a decoration on the left of the title
235      * @param decoration
236      */

237     public void addLeftDecoration(JComponent JavaDoc decoration) {
238         topPanel.add(decoration, new GridBagConstraints JavaDoc(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets JavaDoc(0, 0, 0, 0), 0, 0));
239     }
240
241 }
Popular Tags