1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2013, 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  * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
25  * Other names may be trademarks of their respective owners.]
26  *
27  * -----------------------
28  * DefaultTitleEditor.java
29  * -----------------------
30  * (C) Copyright 2005-2008, by Object Refinery Limited.
31  *
32  * Original Author:  David Gilbert (for Object Refinery Limited);
33  * Contributor(s):   Arnaud Lelievre;
34  *                   Daniel Gredler;
35  *
36  * Changes
37  * -------
38  * 24-Nov-2005 : Version 1, based on TitlePropertyEditPanel.java (DG);
39  * 18-Dec-2008 : Use ResourceBundleWrapper - see patch 1607918 by
40  *               Jess Thrysoee (DG);
41  *
42  */
43 
44 package org.jfree.chart.editor;
45 
46 import java.awt.BorderLayout;
47 import java.awt.Color;
48 import java.awt.Font;
49 import java.awt.Paint;
50 import java.awt.event.ActionEvent;
51 import java.awt.event.ActionListener;
52 import java.util.ResourceBundle;
53 
54 import javax.swing.BorderFactory;
55 import javax.swing.JButton;
56 import javax.swing.JCheckBox;
57 import javax.swing.JColorChooser;
58 import javax.swing.JLabel;
59 import javax.swing.JOptionPane;
60 import javax.swing.JPanel;
61 import javax.swing.JTextField;
62 
63 import org.jfree.chart.JFreeChart;
64 import org.jfree.chart.title.TextTitle;
65 import org.jfree.chart.title.Title;
66 import org.jfree.chart.util.ResourceBundleWrapper;
67 import org.jfree.layout.LCBLayout;
68 import org.jfree.ui.FontChooserPanel;
69 import org.jfree.ui.FontDisplayField;
70 import org.jfree.ui.PaintSample;
71 
72 /**
73  * A panel for editing the properties of a chart title.
74  */
75 class DefaultTitleEditor extends JPanel implements ActionListener {
76 
77     /** Whether or not to display the title on the chart. */
78     private boolean showTitle;
79 
80     /** The checkbox to indicate whether or not to display the title. */
81     private JCheckBox showTitleCheckBox;
82 
83     /** A field for displaying/editing the title text. */
84     private JTextField titleField;
85 
86     /** The font used to draw the title. */
87     private Font titleFont;
88 
89     /** A field for displaying a description of the title font. */
90     private JTextField fontfield;
91 
92     /** The button to use to select a new title font. */
93     private JButton selectFontButton;
94 
95     /** The paint (color) used to draw the title. */
96     private PaintSample titlePaint;
97 
98     /** The button to use to select a new paint (color) to draw the title. */
99     private JButton selectPaintButton;
100 
101     /** The resourceBundle for the localization. */
102     protected static ResourceBundle localizationResources
103             = ResourceBundleWrapper.getBundle(
104                     "org.jfree.chart.editor.LocalizationBundle");
105 
106     /**
107      * Standard constructor: builds a panel for displaying/editing the
108      * properties of the specified title.
109      *
110      * @param title  the title, which should be changed.
111      */
DefaultTitleEditor(Title title)112     public DefaultTitleEditor(Title title) {
113 
114         TextTitle t = (title != null ? (TextTitle) title
115                 : new TextTitle(localizationResources.getString("Title")));
116         this.showTitle = (title != null);
117         this.titleFont = t.getFont();
118         this.titleField = new JTextField(t.getText());
119         this.titlePaint = new PaintSample(t.getPaint());
120 
121         setLayout(new BorderLayout());
122 
123         JPanel general = new JPanel(new BorderLayout());
124         general.setBorder(
125             BorderFactory.createTitledBorder(
126                 BorderFactory.createEtchedBorder(),
127                 localizationResources.getString("General")
128             )
129         );
130 
131         JPanel interior = new JPanel(new LCBLayout(4));
132         interior.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
133 
134         interior.add(new JLabel(localizationResources.getString("Show_Title")));
135         this.showTitleCheckBox = new JCheckBox();
136         this.showTitleCheckBox.setSelected(this.showTitle);
137         this.showTitleCheckBox.setActionCommand("ShowTitle");
138         this.showTitleCheckBox.addActionListener(this);
139         interior.add(new JPanel());
140         interior.add(this.showTitleCheckBox);
141 
142         JLabel titleLabel = new JLabel(localizationResources.getString("Text"));
143         interior.add(titleLabel);
144         interior.add(this.titleField);
145         interior.add(new JPanel());
146 
147         JLabel fontLabel = new JLabel(localizationResources.getString("Font"));
148         this.fontfield = new FontDisplayField(this.titleFont);
149         this.selectFontButton = new JButton(
150             localizationResources.getString("Select...")
151         );
152         this.selectFontButton.setActionCommand("SelectFont");
153         this.selectFontButton.addActionListener(this);
154         interior.add(fontLabel);
155         interior.add(this.fontfield);
156         interior.add(this.selectFontButton);
157 
158         JLabel colorLabel = new JLabel(
159             localizationResources.getString("Color")
160         );
161         this.selectPaintButton = new JButton(
162             localizationResources.getString("Select...")
163         );
164         this.selectPaintButton.setActionCommand("SelectPaint");
165         this.selectPaintButton.addActionListener(this);
166         interior.add(colorLabel);
167         interior.add(this.titlePaint);
168         interior.add(this.selectPaintButton);
169 
170         this.enableOrDisableControls();
171 
172         general.add(interior);
173         add(general, BorderLayout.NORTH);
174     }
175 
176     /**
177      * Returns the title text entered in the panel.
178      *
179      * @return The title text entered in the panel.
180      */
getTitleText()181     public String getTitleText() {
182         return this.titleField.getText();
183     }
184 
185     /**
186      * Returns the font selected in the panel.
187      *
188      * @return The font selected in the panel.
189      */
getTitleFont()190     public Font getTitleFont() {
191         return this.titleFont;
192     }
193 
194     /**
195      * Returns the paint selected in the panel.
196      *
197      * @return The paint selected in the panel.
198      */
getTitlePaint()199     public Paint getTitlePaint() {
200         return this.titlePaint.getPaint();
201     }
202 
203     /**
204      * Handles button clicks by passing control to an appropriate handler
205      * method.
206      *
207      * @param event  the event
208      */
209     @Override
actionPerformed(ActionEvent event)210     public void actionPerformed(ActionEvent event) {
211 
212         String command = event.getActionCommand();
213 
214         if (command.equals("SelectFont")) {
215             attemptFontSelection();
216         }
217         else if (command.equals("SelectPaint")) {
218             attemptPaintSelection();
219         }
220         else if (command.equals("ShowTitle")) {
221             attemptModifyShowTitle();
222         }
223     }
224 
225     /**
226      * Presents a font selection dialog to the user.
227      */
attemptFontSelection()228     public void attemptFontSelection() {
229 
230         FontChooserPanel panel = new FontChooserPanel(this.titleFont);
231         int result =
232             JOptionPane.showConfirmDialog(
233                 this, panel, localizationResources.getString("Font_Selection"),
234                 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE
235             );
236 
237         if (result == JOptionPane.OK_OPTION) {
238             this.titleFont = panel.getSelectedFont();
239             this.fontfield.setText(
240                 this.titleFont.getFontName() + " " + this.titleFont.getSize()
241             );
242         }
243     }
244 
245     /**
246      * Allow the user the opportunity to select a Paint object.  For now, we
247      * just use the standard color chooser - all colors are Paint objects, but
248      * not all Paint objects are colors (later we can implement a more general
249      * Paint chooser).
250      */
attemptPaintSelection()251     public void attemptPaintSelection() {
252         Paint p = this.titlePaint.getPaint();
253         Color defaultColor = (p instanceof Color ? (Color) p : Color.blue);
254         Color c = JColorChooser.showDialog(
255             this, localizationResources.getString("Title_Color"), defaultColor
256         );
257         if (c != null) {
258             this.titlePaint.setPaint(c);
259         }
260     }
261 
262     /**
263      * Allow the user the opportunity to change whether the title is
264      * displayed on the chart or not.
265      */
attemptModifyShowTitle()266     private void attemptModifyShowTitle() {
267         this.showTitle = this.showTitleCheckBox.isSelected();
268         this.enableOrDisableControls();
269     }
270 
271     /**
272      * If we are supposed to show the title, the controls are enabled.
273      * If we are not supposed to show the title, the controls are disabled.
274      */
enableOrDisableControls()275     private void enableOrDisableControls() {
276         boolean enabled = (this.showTitle == true);
277         this.titleField.setEnabled(enabled);
278         this.selectFontButton.setEnabled(enabled);
279         this.selectPaintButton.setEnabled(enabled);
280     }
281 
282     /**
283      * Sets the properties of the specified title to match the properties
284      * defined on this panel.
285      *
286      * @param chart  the chart whose title is to be modified.
287      */
setTitleProperties(JFreeChart chart)288     public void setTitleProperties(JFreeChart chart) {
289         if (this.showTitle) {
290             TextTitle title = chart.getTitle();
291             if (title == null) {
292                 title = new TextTitle();
293                 chart.setTitle(title);
294             }
295             title.setText(getTitleText());
296             title.setFont(getTitleFont());
297             title.setPaint(getTitlePaint());
298         }
299         else {
300             chart.setTitle((TextTitle) null);
301         }
302     }
303 
304 }
305