KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > ui > TitlePropertyEditPanel


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ---------------------------
27  * TitlePropertyEditPanel.java
28  * ---------------------------
29  * (C) Copyright 2000-2004, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): Arnaud Lelievre;
33  * Daniel Gredler;
34  *
35  * $Id: TitlePropertyEditPanel.java,v 1.6 2005/05/03 10:11:23 mungady Exp $
36  *
37  * Changes (from 24-Aug-2001)
38  * --------------------------
39  * 24-Aug-2001 : Added standard source headaer. Fixed DOS encoding problem (DG);
40  * 07-Nov-2001 : Separated the JCommon Class Library classes, JFreeChart now
41  * requires jcommon.jar (DG);
42  * 31-Jan-2002 : Removed Title.java and StandardTitle.java. Disabled some
43  * methods in this class until support for AbstractTitle is
44  * added (DG);
45  * 20-May-2003 : Restored initialisation of titleField and titlePaint to prevent
46  * NullPointer when using this class. (TM)
47  * 08-Sep-2003 : Added internationalization via use of properties
48  * resourceBundle (RFE 690236) (AL);
49  * 08-Jan-2004 : Renamed AbstractTitle --> Title and moved to new package (DG);
50  * 24-Aug-2004 : Applied patch 1014378 (DG);
51  *
52  */

53
54 package org.jfree.chart.ui;
55
56 import java.awt.BorderLayout JavaDoc;
57 import java.awt.Color JavaDoc;
58 import java.awt.Font JavaDoc;
59 import java.awt.Paint JavaDoc;
60 import java.awt.event.ActionEvent JavaDoc;
61 import java.awt.event.ActionListener JavaDoc;
62 import java.util.ResourceBundle JavaDoc;
63
64 import javax.swing.BorderFactory JavaDoc;
65 import javax.swing.JButton JavaDoc;
66 import javax.swing.JCheckBox JavaDoc;
67 import javax.swing.JColorChooser JavaDoc;
68 import javax.swing.JLabel JavaDoc;
69 import javax.swing.JOptionPane JavaDoc;
70 import javax.swing.JPanel JavaDoc;
71 import javax.swing.JTextField JavaDoc;
72
73 import org.jfree.chart.JFreeChart;
74 import org.jfree.chart.title.Title;
75 import org.jfree.chart.title.TextTitle;
76 import org.jfree.layout.LCBLayout;
77 import org.jfree.ui.FontChooserPanel;
78 import org.jfree.ui.FontDisplayField;
79 import org.jfree.ui.PaintSample;
80
81 /**
82  * A panel for editing the properties of a chart title.
83  */

84 public class TitlePropertyEditPanel extends JPanel JavaDoc implements ActionListener JavaDoc {
85
86     /** Whether or not to display the title on the chart. */
87     private boolean showTitle;
88
89     /** The checkbox to indicate whether or not to display the title. */
90     private JCheckBox JavaDoc showTitleCheckBox;
91
92     /** A field for displaying/editing the title text. */
93     private JTextField JavaDoc titleField;
94
95     /** The font used to draw the title. */
96     private Font JavaDoc titleFont;
97
98     /** A field for displaying a description of the title font. */
99     private JTextField JavaDoc fontfield;
100
101     /** The button to use to select a new title font. */
102     private JButton JavaDoc selectFontButton;
103
104     /** The paint (color) used to draw the title. */
105     private PaintSample titlePaint;
106
107     /** The button to use to select a new paint (color) to draw the title. */
108     private JButton JavaDoc selectPaintButton;
109
110     /** The resourceBundle for the localization. */
111     protected static ResourceBundle JavaDoc localizationResources
112         = ResourceBundle.getBundle("org.jfree.chart.ui.LocalizationBundle");
113
114     /**
115      * Standard constructor: builds a panel for displaying/editing the
116      * properties of the specified title.
117      *
118      * @param title the title, which should be changed.
119      */

120     public TitlePropertyEditPanel(Title title) {
121
122         TextTitle t = (title != null ? (TextTitle) title
123                 : new TextTitle(localizationResources.getString("Title")));
124         this.showTitle = (title != null);
125         this.titleFont = t.getFont();
126         this.titleField = new JTextField JavaDoc(t.getText());
127         this.titlePaint = new PaintSample(t.getPaint());
128
129         setLayout(new BorderLayout JavaDoc());
130
131         JPanel JavaDoc general = new JPanel JavaDoc(new BorderLayout JavaDoc());
132         general.setBorder(
133             BorderFactory.createTitledBorder(
134                 BorderFactory.createEtchedBorder(),
135                 localizationResources.getString("General")
136             )
137         );
138
139         JPanel JavaDoc interior = new JPanel JavaDoc(new LCBLayout(4));
140         interior.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
141
142         interior.add(new JLabel JavaDoc(localizationResources.getString("Show_Title")));
143         this.showTitleCheckBox = new JCheckBox JavaDoc();
144         this.showTitleCheckBox.setSelected(this.showTitle);
145         this.showTitleCheckBox.setActionCommand("ShowTitle");
146         this.showTitleCheckBox.addActionListener(this);
147         interior.add(new JPanel JavaDoc());
148         interior.add(this.showTitleCheckBox);
149
150         JLabel JavaDoc titleLabel = new JLabel JavaDoc(localizationResources.getString("Text"));
151         interior.add(titleLabel);
152         interior.add(this.titleField);
153         interior.add(new JPanel JavaDoc());
154
155         JLabel JavaDoc fontLabel = new JLabel JavaDoc(localizationResources.getString("Font"));
156         this.fontfield = new FontDisplayField(this.titleFont);
157         this.selectFontButton = new JButton JavaDoc(
158             localizationResources.getString("Select...")
159         );
160         this.selectFontButton.setActionCommand("SelectFont");
161         this.selectFontButton.addActionListener(this);
162         interior.add(fontLabel);
163         interior.add(this.fontfield);
164         interior.add(this.selectFontButton);
165
166         JLabel JavaDoc colorLabel = new JLabel JavaDoc(
167             localizationResources.getString("Color")
168         );
169         this.selectPaintButton = new JButton JavaDoc(
170             localizationResources.getString("Select...")
171         );
172         this.selectPaintButton.setActionCommand("SelectPaint");
173         this.selectPaintButton.addActionListener(this);
174         interior.add(colorLabel);
175         interior.add(this.titlePaint);
176         interior.add(this.selectPaintButton);
177
178         this.enableOrDisableControls();
179
180         general.add(interior);
181         add(general, BorderLayout.NORTH);
182     }
183
184     /**
185      * Returns the title text entered in the panel.
186      *
187      * @return The title text entered in the panel.
188      */

189     public String JavaDoc getTitleText() {
190         return this.titleField.getText();
191     }
192
193     /**
194      * Returns the font selected in the panel.
195      *
196      * @return The font selected in the panel.
197      */

198     public Font JavaDoc getTitleFont() {
199         return this.titleFont;
200     }
201
202     /**
203      * Returns the paint selected in the panel.
204      *
205      * @return The paint selected in the panel.
206      */

207     public Paint JavaDoc getTitlePaint() {
208         return this.titlePaint.getPaint();
209     }
210
211     /**
212      * Handles button clicks by passing control to an appropriate handler
213      * method.
214      *
215      * @param event the event
216      */

217     public void actionPerformed(ActionEvent JavaDoc event) {
218
219         String JavaDoc command = event.getActionCommand();
220
221         if (command.equals("SelectFont")) {
222             attemptFontSelection();
223         }
224         else if (command.equals("SelectPaint")) {
225             attemptPaintSelection();
226         }
227         else if (command.equals("ShowTitle")) {
228             attemptModifyShowTitle();
229         }
230     }
231
232     /**
233      * Presents a font selection dialog to the user.
234      */

235     public void attemptFontSelection() {
236
237         FontChooserPanel panel = new FontChooserPanel(this.titleFont);
238         int result =
239             JOptionPane.showConfirmDialog(
240                 this, panel, localizationResources.getString("Font_Selection"),
241                 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE
242             );
243
244         if (result == JOptionPane.OK_OPTION) {
245             this.titleFont = panel.getSelectedFont();
246             this.fontfield.setText(
247                 this.titleFont.getFontName() + " " + this.titleFont.getSize()
248             );
249         }
250     }
251
252     /**
253      * Allow the user the opportunity to select a Paint object. For now, we
254      * just use the standard color chooser - all colors are Paint objects, but
255      * not all Paint objects are colors (later we can implement a more general
256      * Paint chooser).
257      */

258     public void attemptPaintSelection() {
259         Paint JavaDoc p = this.titlePaint.getPaint();
260         Color JavaDoc defaultColor = (p instanceof Color JavaDoc ? (Color JavaDoc) p : Color.blue);
261         Color JavaDoc c = JColorChooser.showDialog(
262             this, localizationResources.getString("Title_Color"), defaultColor
263         );
264         if (c != null) {
265             this.titlePaint.setPaint(c);
266         }
267     }
268
269     /**
270      * Allow the user the opportunity to change whether the title is
271      * displayed on the chart or not.
272      */

273     private void attemptModifyShowTitle() {
274         this.showTitle = this.showTitleCheckBox.isSelected();
275         this.enableOrDisableControls();
276     }
277
278     /**
279      * If we are supposed to show the title, the controls are enabled.
280      * If we are not supposed to show the title, the controls are disabled.
281      */

282     private void enableOrDisableControls() {
283         boolean enabled = (this.showTitle == true);
284         this.titleField.setEnabled(enabled);
285         this.selectFontButton.setEnabled(enabled);
286         this.selectPaintButton.setEnabled(enabled);
287     }
288
289     /**
290      * Sets the properties of the specified title to match the properties
291      * defined on this panel.
292      *
293      * @param chart the chart whose title is to be modified.
294      */

295     public void setTitleProperties(JFreeChart chart) {
296         if (this.showTitle) {
297             TextTitle title = chart.getTitle();
298             if (title == null) {
299                 title = new TextTitle();
300                 chart.setTitle(title);
301             }
302             title.setText(getTitleText());
303             title.setFont(getTitleFont());
304             title.setPaint(getTitlePaint());
305         }
306         else {
307             chart.setTitle((TextTitle) null);
308         }
309     }
310
311 }
312
Popular Tags