KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > editor > DefaultTitleEditor


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
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -----------------------
28  * DefaultTitleEditor.java
29  * -----------------------
30  * (C) Copyright 2005, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): Arnaud Lelievre;
34  * Daniel Gredler;
35  *
36  * $Id: DefaultTitleEditor.java,v 1.1.2.1 2005/11/24 16:11:48 mungady Exp $
37  *
38  * Changes
39  * -------
40  * 24-Nov-2005 : Version 1, based on TitlePropertyEditPanel.java (DG);
41  *
42  */

43
44 package org.jfree.chart.editor;
45
46 import java.awt.BorderLayout JavaDoc;
47 import java.awt.Color JavaDoc;
48 import java.awt.Font JavaDoc;
49 import java.awt.Paint JavaDoc;
50 import java.awt.event.ActionEvent JavaDoc;
51 import java.awt.event.ActionListener JavaDoc;
52 import java.util.ResourceBundle JavaDoc;
53
54 import javax.swing.BorderFactory JavaDoc;
55 import javax.swing.JButton JavaDoc;
56 import javax.swing.JCheckBox JavaDoc;
57 import javax.swing.JColorChooser JavaDoc;
58 import javax.swing.JLabel JavaDoc;
59 import javax.swing.JOptionPane JavaDoc;
60 import javax.swing.JPanel JavaDoc;
61 import javax.swing.JTextField JavaDoc;
62
63 import org.jfree.chart.JFreeChart;
64 import org.jfree.chart.title.Title;
65 import org.jfree.chart.title.TextTitle;
66 import org.jfree.layout.LCBLayout;
67 import org.jfree.ui.FontChooserPanel;
68 import org.jfree.ui.FontDisplayField;
69 import org.jfree.ui.PaintSample;
70
71 /**
72  * A panel for editing the properties of a chart title.
73  */

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

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

179     public String JavaDoc getTitleText() {
180         return this.titleField.getText();
181     }
182
183     /**
184      * Returns the font selected in the panel.
185      *
186      * @return The font selected in the panel.
187      */

188     public Font JavaDoc getTitleFont() {
189         return this.titleFont;
190     }
191
192     /**
193      * Returns the paint selected in the panel.
194      *
195      * @return The paint selected in the panel.
196      */

197     public Paint JavaDoc getTitlePaint() {
198         return this.titlePaint.getPaint();
199     }
200
201     /**
202      * Handles button clicks by passing control to an appropriate handler
203      * method.
204      *
205      * @param event the event
206      */

207     public void actionPerformed(ActionEvent JavaDoc event) {
208
209         String JavaDoc command = event.getActionCommand();
210
211         if (command.equals("SelectFont")) {
212             attemptFontSelection();
213         }
214         else if (command.equals("SelectPaint")) {
215             attemptPaintSelection();
216         }
217         else if (command.equals("ShowTitle")) {
218             attemptModifyShowTitle();
219         }
220     }
221
222     /**
223      * Presents a font selection dialog to the user.
224      */

225     public void attemptFontSelection() {
226
227         FontChooserPanel panel = new FontChooserPanel(this.titleFont);
228         int result =
229             JOptionPane.showConfirmDialog(
230                 this, panel, localizationResources.getString("Font_Selection"),
231                 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE
232             );
233
234         if (result == JOptionPane.OK_OPTION) {
235             this.titleFont = panel.getSelectedFont();
236             this.fontfield.setText(
237                 this.titleFont.getFontName() + " " + this.titleFont.getSize()
238             );
239         }
240     }
241
242     /**
243      * Allow the user the opportunity to select a Paint object. For now, we
244      * just use the standard color chooser - all colors are Paint objects, but
245      * not all Paint objects are colors (later we can implement a more general
246      * Paint chooser).
247      */

248     public void attemptPaintSelection() {
249         Paint JavaDoc p = this.titlePaint.getPaint();
250         Color JavaDoc defaultColor = (p instanceof Color JavaDoc ? (Color JavaDoc) p : Color.blue);
251         Color JavaDoc c = JColorChooser.showDialog(
252             this, localizationResources.getString("Title_Color"), defaultColor
253         );
254         if (c != null) {
255             this.titlePaint.setPaint(c);
256         }
257     }
258
259     /**
260      * Allow the user the opportunity to change whether the title is
261      * displayed on the chart or not.
262      */

263     private void attemptModifyShowTitle() {
264         this.showTitle = this.showTitleCheckBox.isSelected();
265         this.enableOrDisableControls();
266     }
267
268     /**
269      * If we are supposed to show the title, the controls are enabled.
270      * If we are not supposed to show the title, the controls are disabled.
271      */

272     private void enableOrDisableControls() {
273         boolean enabled = (this.showTitle == true);
274         this.titleField.setEnabled(enabled);
275         this.selectFontButton.setEnabled(enabled);
276         this.selectPaintButton.setEnabled(enabled);
277     }
278
279     /**
280      * Sets the properties of the specified title to match the properties
281      * defined on this panel.
282      *
283      * @param chart the chart whose title is to be modified.
284      */

285     public void setTitleProperties(JFreeChart chart) {
286         if (this.showTitle) {
287             TextTitle title = chart.getTitle();
288             if (title == null) {
289                 title = new TextTitle();
290                 chart.setTitle(title);
291             }
292             title.setText(getTitleText());
293             title.setFont(getTitleFont());
294             title.setPaint(getTitlePaint());
295         }
296         else {
297             chart.setTitle((TextTitle) null);
298         }
299     }
300
301 }
302
Popular Tags