KickJava   Java API By Example, From Geeks To Geeks.

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


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  * LegendPropertyEditPanel.java
28  * ----------------------------
29  * (C) Copyright 2000-2004, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert;
32  * Contributor(s): Arnaud Lelievre;
33  * Daniel Gredler;
34  *
35  * $Id: LegendPropertyEditPanel.java,v 1.5 2005/03/29 12:56:52 mungady Exp $
36  *
37  * Changes (from 24-Aug-2001)
38  * --------------------------
39  * 24-Aug-2001 : Added standard source header. Fixed DOS encoding problem (DG);
40  * 07-Nov-2001 : Separated the JCommon Class Library classes, JFreeChart now
41  * requires jcommon.jar (DG);
42  * 25-Jun-2002 : Revised header, removed redundant code (DG);
43  * 08-Sep-2003 : Added internationalization via use of properties
44  * resourceBundle (RFE 690236) (AL);
45  * 24-Aug-2004 : Applied patch 1014378 (DG);
46  *
47  */

48
49 package org.jfree.chart.ui;
50
51 import java.awt.BasicStroke JavaDoc;
52 import java.awt.BorderLayout JavaDoc;
53 import java.awt.Color JavaDoc;
54 import java.awt.Font JavaDoc;
55 import java.awt.Paint JavaDoc;
56 import java.awt.Stroke JavaDoc;
57 import java.awt.event.ActionEvent JavaDoc;
58 import java.awt.event.ActionListener JavaDoc;
59 import java.util.ResourceBundle JavaDoc;
60
61 import javax.swing.BorderFactory JavaDoc;
62 import javax.swing.JButton JavaDoc;
63 import javax.swing.JCheckBox JavaDoc;
64 import javax.swing.JColorChooser JavaDoc;
65 import javax.swing.JLabel JavaDoc;
66 import javax.swing.JOptionPane JavaDoc;
67 import javax.swing.JPanel JavaDoc;
68
69 import org.jfree.chart.JFreeChart;
70 import org.jfree.chart.OldLegend;
71 import org.jfree.chart.DefaultOldLegend;
72 import org.jfree.layout.LCBLayout;
73 import org.jfree.ui.FontChooserPanel;
74 import org.jfree.ui.FontDisplayField;
75 import org.jfree.ui.PaintSample;
76 import org.jfree.ui.StrokeChooserPanel;
77 import org.jfree.ui.StrokeSample;
78
79 /**
80  * A panel for editing the properties of a {@link OldLegend}.
81  */

82 class LegendPropertyEditPanel extends JPanel JavaDoc implements ActionListener JavaDoc {
83
84     /** Whether or not to display the legend on the chart. */
85     private boolean showLegend;
86
87     /** The checkbox to indicate whether or not to display the legend. */
88     private JCheckBox JavaDoc showLegendCheckBox;
89
90     /** The stroke (pen) used to draw the legend outline. */
91     private StrokeSample outlineStroke;
92
93     /** The button used to select the legend outline stroke. */
94     private JButton JavaDoc selectOutlineStrokeButton;
95
96     /** The paint (color) used to draw the legend outline. */
97     private PaintSample outlinePaint;
98
99     /** The button used to select the legend outline color. */
100     private JButton JavaDoc selectOutlinePaintButton;
101
102     /** The paint (color) used to fill the legend background. */
103     private PaintSample backgroundPaint;
104
105     /** The button used to select the legend background color. */
106     private JButton JavaDoc selectBackgroundPaintButton;
107
108     /** The font used to draw the series names. */
109     private Font JavaDoc seriesFont;
110
111     /** The button used to select the series name font. */
112     private JButton JavaDoc selectSeriesFontButton;
113
114     /** The paint (color) used to draw the series names. */
115     private PaintSample seriesPaint;
116
117     /** The button used to select the series name paint. */
118     private JButton JavaDoc selectSeriesPaintButton;
119
120     /** An array of strokes (pens) to select from. */
121     private StrokeSample[] availableStrokeSamples;
122
123     /** A field for displaying the series label font. */
124     private FontDisplayField fontDisplayField;
125
126     /** The resourceBundle for the localization. */
127     protected static ResourceBundle JavaDoc localizationResources
128         = ResourceBundle.getBundle("org.jfree.chart.ui.LocalizationBundle");
129
130     /**
131      * Standard constructor: builds a panel based on the specified legend.
132      * If the specified legend is <tt>null</tt>, the panel will reflect the
133      * fact that no legend is to be displayed.
134      *
135      * @param legend the legend, which should be changed.
136      */

137     public LegendPropertyEditPanel(OldLegend legend) {
138
139         DefaultOldLegend l = (legend != null
140             ? (DefaultOldLegend) legend : new DefaultOldLegend());
141         this.showLegend = (legend != null);
142         this.outlineStroke = new StrokeSample(l.getOutlineStroke());
143         this.outlinePaint = new PaintSample(l.getOutlinePaint());
144         this.backgroundPaint = new PaintSample(l.getBackgroundPaint());
145         this.seriesFont = l.getItemFont();
146         this.seriesPaint = new PaintSample(l.getItemPaint());
147
148         this.availableStrokeSamples = new StrokeSample[4];
149         this.availableStrokeSamples[0]
150             = new StrokeSample(new BasicStroke JavaDoc(1.0f));
151         this.availableStrokeSamples[1]
152             = new StrokeSample(new BasicStroke JavaDoc(2.0f));
153         this.availableStrokeSamples[2]
154             = new StrokeSample(new BasicStroke JavaDoc(3.0f));
155         this.availableStrokeSamples[3]
156             = new StrokeSample(new BasicStroke JavaDoc(4.0f));
157
158         setLayout(new BorderLayout JavaDoc());
159
160         JPanel JavaDoc general = new JPanel JavaDoc(new BorderLayout JavaDoc());
161         general.setBorder(
162             BorderFactory.createTitledBorder(
163                 BorderFactory.createEtchedBorder(),
164                 localizationResources.getString("General")
165             )
166         );
167
168         JPanel JavaDoc interior = new JPanel JavaDoc(new LCBLayout(6));
169         interior.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
170
171         interior.add(
172             new JLabel JavaDoc(localizationResources.getString("Show_Legend"))
173         );
174         this.showLegendCheckBox = new JCheckBox JavaDoc();
175         this.showLegendCheckBox.setSelected(this.showLegend);
176         this.showLegendCheckBox.setActionCommand("ShowLegend");
177         this.showLegendCheckBox.addActionListener(this);
178         interior.add(new JPanel JavaDoc());
179         interior.add(this.showLegendCheckBox);
180
181         interior.add(new JLabel JavaDoc(localizationResources.getString("Outline")));
182         interior.add(this.outlineStroke);
183         this.selectOutlineStrokeButton
184             = new JButton JavaDoc(localizationResources.getString("Select..."));
185         this.selectOutlineStrokeButton.setActionCommand("OutlineStroke");
186         this.selectOutlineStrokeButton.addActionListener(this);
187         interior.add(this.selectOutlineStrokeButton);
188
189         interior.add(
190             new JLabel JavaDoc(localizationResources.getString("Outline_Paint"))
191         );
192         this.selectOutlinePaintButton
193             = new JButton JavaDoc(localizationResources.getString("Select..."));
194         this.selectOutlinePaintButton.setActionCommand("OutlinePaint");
195         this.selectOutlinePaintButton.addActionListener(this);
196         interior.add(this.outlinePaint);
197         interior.add(this.selectOutlinePaintButton);
198
199         interior.add(new JLabel JavaDoc(localizationResources.getString("Background")));
200         this.selectBackgroundPaintButton = new JButton JavaDoc(
201             localizationResources.getString("Select...")
202         );
203         this.selectBackgroundPaintButton.setActionCommand("BackgroundPaint");
204         this.selectBackgroundPaintButton.addActionListener(this);
205         interior.add(this.backgroundPaint);
206         interior.add(this.selectBackgroundPaintButton);
207
208         interior.add(
209             new JLabel JavaDoc(localizationResources.getString("Series_label_font"))
210         );
211         this.selectSeriesFontButton
212             = new JButton JavaDoc(localizationResources.getString("Select..."));
213         this.selectSeriesFontButton.setActionCommand("SeriesFont");
214         this.selectSeriesFontButton.addActionListener(this);
215         this.fontDisplayField = new FontDisplayField(this.seriesFont);
216         interior.add(this.fontDisplayField);
217         interior.add(this.selectSeriesFontButton);
218
219         interior.add(
220             new JLabel JavaDoc(localizationResources.getString("Series_label_paint"))
221         );
222         this.selectSeriesPaintButton
223             = new JButton JavaDoc(localizationResources.getString("Select..."));
224         this.selectSeriesPaintButton.setActionCommand("SeriesPaint");
225         this.selectSeriesPaintButton.addActionListener(this);
226         interior.add(this.seriesPaint);
227         interior.add(this.selectSeriesPaintButton);
228
229         this.enableOrDisableControls();
230
231         general.add(interior);
232         add(general, BorderLayout.NORTH);
233     }
234
235     /**
236      * Returns the current outline stroke.
237      *
238      * @return The current outline stroke.
239      */

240     public Stroke JavaDoc getOutlineStroke() {
241         return this.outlineStroke.getStroke();
242     }
243
244     /**
245      * Returns the current outline paint.
246      *
247      * @return The current outline paint.
248      */

249     public Paint JavaDoc getOutlinePaint() {
250         return this.outlinePaint.getPaint();
251     }
252
253     /**
254      * Returns the current background paint.
255      *
256      * @return The current background paint.
257      */

258     public Paint JavaDoc getBackgroundPaint() {
259         return this.backgroundPaint.getPaint();
260     }
261
262     /**
263      * Returns the current series label font.
264      *
265      * @return The current series label font.
266      */

267     public Font JavaDoc getSeriesFont() {
268         return this.seriesFont;
269     }
270
271     /**
272      * Returns the current series label paint.
273      *
274      * @return The current series label paint.
275      */

276     public Paint JavaDoc getSeriesPaint() {
277         return this.seriesPaint.getPaint();
278     }
279
280     /**
281      * Handles user interactions with the panel.
282      *
283      * @param event the event.
284      */

285     public void actionPerformed(ActionEvent JavaDoc event) {
286         String JavaDoc command = event.getActionCommand();
287         if (command.equals("OutlineStroke")) {
288             attemptModifyOutlineStroke();
289         }
290         else if (command.equals("OutlinePaint")) {
291             attemptModifyOutlinePaint();
292         }
293         else if (command.equals("BackgroundPaint")) {
294             attemptModifyBackgroundPaint();
295         }
296         else if (command.equals("SeriesFont")) {
297             attemptModifySeriesFont();
298         }
299         else if (command.equals("SeriesPaint")) {
300             attemptModifySeriesPaint();
301         }
302         else if (command.equals("ShowLegend")) {
303             attemptModifyShowLegend();
304         }
305     }
306
307     /**
308      * Allows the user the opportunity to change the outline stroke.
309      */

310     private void attemptModifyOutlineStroke() {
311
312         StrokeChooserPanel panel = new StrokeChooserPanel(
313             this.outlineStroke, this.availableStrokeSamples
314         );
315         int result = JOptionPane.showConfirmDialog(
316             this, panel,
317             localizationResources.getString("Pen_Stroke_Selection"),
318             JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE
319         );
320
321         if (result == JOptionPane.OK_OPTION) {
322             this.outlineStroke.setStroke(panel.getSelectedStroke());
323         }
324
325     }
326
327     /**
328      * Allows the user the opportunity to change the outline paint.
329      */

330     private void attemptModifyOutlinePaint() {
331         Color JavaDoc c;
332         c = JColorChooser.showDialog(
333             this, localizationResources.getString("Outline_Color"),
334             Color.blue
335         );
336         if (c != null) {
337             this.outlinePaint.setPaint(c);
338         }
339     }
340
341     /**
342      * Allows the user the opportunity to change the background paint.
343      */

344     private void attemptModifyBackgroundPaint() {
345         Color JavaDoc c;
346         c = JColorChooser.showDialog(
347             this, localizationResources.getString("Background_Color"),
348             Color.blue
349         );
350         if (c != null) {
351             this.backgroundPaint.setPaint(c);
352         }
353     }
354
355     /**
356      * Allows the user the opportunity to change the series label font.
357      */

358     public void attemptModifySeriesFont() {
359
360         FontChooserPanel panel = new FontChooserPanel(this.seriesFont);
361         int result = JOptionPane.showConfirmDialog(
362             this, panel, localizationResources.getString("Font_Selection"),
363             JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE
364         );
365
366         if (result == JOptionPane.OK_OPTION) {
367             this.seriesFont = panel.getSelectedFont();
368             this.fontDisplayField.setText(
369                 this.seriesFont.getFontName() + ", " + this.seriesFont.getSize()
370             );
371         }
372
373     }
374
375     /**
376      * Allows the user the opportunity to change the series label paint.
377      */

378     private void attemptModifySeriesPaint() {
379         Color JavaDoc c;
380         c = JColorChooser.showDialog(
381             this, localizationResources.getString("Series_Label_Color"),
382             Color.blue
383         );
384         if (c != null) {
385             this.seriesPaint.setPaint(c);
386         }
387     }
388
389     /**
390      * Allow the user the opportunity to change whether the legend is
391      * displayed on the chart or not.
392      */

393     private void attemptModifyShowLegend() {
394         this.showLegend = this.showLegendCheckBox.isSelected();
395         this.enableOrDisableControls();
396     }
397
398     /**
399      * If we are supposed to show the legend, the controls are enabled.
400      * If we are not supposed to show the legend, the controls are disabled.
401      */

402     private void enableOrDisableControls() {
403         boolean enabled = (this.showLegend == true);
404         this.selectOutlineStrokeButton.setEnabled(enabled);
405         this.selectOutlinePaintButton.setEnabled(enabled);
406         this.selectBackgroundPaintButton.setEnabled(enabled);
407         this.selectSeriesFontButton.setEnabled(enabled);
408         this.selectSeriesPaintButton.setEnabled(enabled);
409     }
410
411     /**
412      * Sets the properties of the specified legend to match the properties
413      * defined on this panel.
414      *
415      * @param chart the chart whose legend is to be modified.
416      */

417     public void setLegendProperties(JFreeChart chart) {
418         if (this.showLegend) {
419             OldLegend legend = chart.getOldLegend();
420             if (legend == null) {
421                 legend = new DefaultOldLegend();
422                 chart.setOldLegend(legend);
423             }
424             if (legend instanceof DefaultOldLegend) {
425                 // only supports StandardLegend at present
426
DefaultOldLegend standard = (DefaultOldLegend) legend;
427                 standard.setOutlineStroke(getOutlineStroke());
428                 standard.setOutlinePaint(getOutlinePaint());
429                 standard.setBackgroundPaint(getBackgroundPaint());
430                 standard.setItemFont(getSeriesFont());
431                 standard.setItemPaint(getSeriesPaint());
432             }
433             else {
434                 // raise exception - unrecognised legend
435
}
436         }
437         else {
438             chart.setOldLegend(null);
439         }
440     }
441
442 }
443
Popular Tags