KickJava   Java API By Example, From Geeks To Geeks.

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


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  * AxisPropertyEditPanel.java
28  * --------------------------
29  * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert;
32  * Contributor(s): Andrzej Porebski;
33  * Arnaud Lelievre;
34  *
35  * $Id: AxisPropertyEditPanel.java,v 1.3 2005/04/21 09:43:33 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  * 21-Nov-2001 : Allowed for null axes (DG);
43  * 09-Apr-2002 : Minor change to import statement to fix Javadoc error (DG);
44  * 15-Oct-2002 : Fixed errors reported by Checkstyle (DG);
45  * 08-Sep-2003 : Added internationalization via use of properties
46  * resourceBundle (RFE 690236) (AL);
47  *
48  */

49
50 package org.jfree.chart.ui;
51
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.event.ActionEvent JavaDoc;
57 import java.awt.event.ActionListener JavaDoc;
58 import java.util.ResourceBundle JavaDoc;
59
60 import javax.swing.BorderFactory JavaDoc;
61 import javax.swing.JButton JavaDoc;
62 import javax.swing.JCheckBox JavaDoc;
63 import javax.swing.JColorChooser JavaDoc;
64 import javax.swing.JLabel JavaDoc;
65 import javax.swing.JOptionPane JavaDoc;
66 import javax.swing.JPanel JavaDoc;
67 import javax.swing.JTabbedPane JavaDoc;
68 import javax.swing.JTextField JavaDoc;
69
70 import org.jfree.chart.axis.Axis;
71 import org.jfree.chart.axis.NumberAxis;
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.RectangleInsets;
77
78 /**
79  * A panel for editing the properties of an axis.
80  *
81  */

82 public class AxisPropertyEditPanel extends JPanel JavaDoc implements ActionListener JavaDoc {
83
84     /** The axis label. */
85     private JTextField JavaDoc label;
86
87     /** The label font. */
88     private Font JavaDoc labelFont;
89
90     /** The label paint. */
91     private PaintSample labelPaintSample;
92
93     /** A field showing a description of the label font. */
94     private JTextField JavaDoc labelFontField;
95
96     /** The font for displaying tick labels on the axis. */
97     private Font JavaDoc tickLabelFont;
98
99     /**
100      * A field containing a description of the font for displaying tick labels
101      * on the axis.
102      */

103     private JTextField JavaDoc tickLabelFontField;
104
105     /** The paint (color) for the tick labels. */
106     private PaintSample tickLabelPaintSample;
107
108     /**
109      * An empty sub-panel for extending the user interface to handle more
110      * complex axes.
111      */

112     private JPanel JavaDoc slot1;
113
114     /**
115      * An empty sub-panel for extending the user interface to handle more
116      * complex axes.
117      */

118     private JPanel JavaDoc slot2;
119
120     /** A flag that indicates whether or not the tick labels are visible. */
121     private JCheckBox JavaDoc showTickLabelsCheckBox;
122
123     /** A flag that indicates whether or not the tick marks are visible. */
124     private JCheckBox JavaDoc showTickMarksCheckBox;
125
126 // /** Insets text field. */
127
// private InsetsTextField tickLabelInsetsTextField;
128
//
129
// /** Label insets text field. */
130
// private InsetsTextField labelInsetsTextField;
131

132     /** The tick label insets. */
133     private RectangleInsets tickLabelInsets;
134
135     /** The label insets. */
136     private RectangleInsets labelInsets;
137
138     /** A tabbed pane for... */
139     private JTabbedPane JavaDoc otherTabs;
140
141     /** The resourceBundle for the localization. */
142     protected static ResourceBundle JavaDoc localizationResources =
143         ResourceBundle.getBundle("org.jfree.chart.ui.LocalizationBundle");
144
145     /**
146      * A static method that returns a panel that is appropriate for the axis
147      * type.
148      *
149      * @param axis the axis whose properties are to be displayed/edited in
150      * the panel.
151      *
152      * @return A panel or <code>null</code< if axis is <code>null</code>.
153      */

154     public static AxisPropertyEditPanel getInstance(Axis axis) {
155
156         if (axis != null) {
157             // figure out what type of axis we have and instantiate the
158
// appropriate panel
159
if (axis instanceof NumberAxis) {
160                 return new NumberAxisPropertyEditPanel((NumberAxis) axis);
161             }
162             else {
163                 return new AxisPropertyEditPanel(axis);
164             }
165         }
166         else {
167             return null;
168         }
169
170     }
171
172     /**
173      * Standard constructor: builds a panel for displaying/editing the
174      * properties of the specified axis.
175      *
176      * @param axis the axis whose properties are to be displayed/edited in
177      * the panel.
178      */

179     public AxisPropertyEditPanel(Axis axis) {
180
181         this.labelFont = axis.getLabelFont();
182         this.labelPaintSample = new PaintSample(axis.getLabelPaint());
183         this.tickLabelFont = axis.getTickLabelFont();
184         this.tickLabelPaintSample = new PaintSample(axis.getTickLabelPaint());
185
186         // Insets values
187
this.tickLabelInsets = axis.getTickLabelInsets();
188         this.labelInsets = axis.getLabelInsets();
189
190         setLayout(new BorderLayout JavaDoc());
191
192         JPanel JavaDoc general = new JPanel JavaDoc(new BorderLayout JavaDoc());
193         general.setBorder(
194             BorderFactory.createTitledBorder(
195                 BorderFactory.createEtchedBorder(),
196                 localizationResources.getString("General")
197             )
198         );
199
200         JPanel JavaDoc interior = new JPanel JavaDoc(new LCBLayout(5));
201         interior.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
202         interior.add(new JLabel JavaDoc(localizationResources.getString("Label")));
203         this.label = new JTextField JavaDoc(axis.getLabel());
204         interior.add(this.label);
205         interior.add(new JPanel JavaDoc());
206
207         interior.add(new JLabel JavaDoc(localizationResources.getString("Font")));
208         this.labelFontField = new FontDisplayField(this.labelFont);
209         interior.add(this.labelFontField);
210         JButton JavaDoc b = new JButton JavaDoc(localizationResources.getString("Select..."));
211         b.setActionCommand("SelectLabelFont");
212         b.addActionListener(this);
213         interior.add(b);
214
215         interior.add(new JLabel JavaDoc(localizationResources.getString("Paint")));
216         interior.add(this.labelPaintSample);
217         b = new JButton JavaDoc(localizationResources.getString("Select..."));
218         b.setActionCommand("SelectLabelPaint");
219         b.addActionListener(this);
220         interior.add(b);
221
222 // interior.add(
223
// new JLabel(localizationResources.getString("Label_Insets"))
224
// );
225
// b = new JButton(localizationResources.getString("Edit..."));
226
// b.setActionCommand("LabelInsets");
227
// b.addActionListener(this);
228
// this.labelInsetsTextField = new InsetsTextField(this.labelInsets);
229
// interior.add(this.labelInsetsTextField);
230
// interior.add(b);
231
//
232
// interior.add(
233
// new JLabel(localizationResources.getString("Tick_Label_Insets"))
234
// );
235
// b = new JButton(localizationResources.getString("Edit..."));
236
// b.setActionCommand("TickLabelInsets");
237
// b.addActionListener(this);
238
// this.tickLabelInsetsTextField
239
// = new InsetsTextField(this.tickLabelInsets);
240
// interior.add(this.tickLabelInsetsTextField);
241
// interior.add(b);
242

243         general.add(interior);
244
245         add(general, BorderLayout.NORTH);
246
247         this.slot1 = new JPanel JavaDoc(new BorderLayout JavaDoc());
248
249         JPanel JavaDoc other = new JPanel JavaDoc(new BorderLayout JavaDoc());
250         other.setBorder(BorderFactory.createTitledBorder(
251                              BorderFactory.createEtchedBorder(),
252                              localizationResources.getString("Other")));
253
254         this.otherTabs = new JTabbedPane JavaDoc();
255         this.otherTabs.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
256
257         JPanel JavaDoc ticks = new JPanel JavaDoc(new LCBLayout(3));
258         ticks.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
259
260         this.showTickLabelsCheckBox = new JCheckBox JavaDoc(
261             localizationResources.getString("Show_tick_labels"),
262             axis.isTickLabelsVisible()
263         );
264         ticks.add(this.showTickLabelsCheckBox);
265         ticks.add(new JPanel JavaDoc());
266         ticks.add(new JPanel JavaDoc());
267
268         ticks.add(
269             new JLabel JavaDoc(localizationResources.getString("Tick_label_font"))
270         );
271         this.tickLabelFontField = new FontDisplayField(this.tickLabelFont);
272         ticks.add(this.tickLabelFontField);
273         b = new JButton JavaDoc(localizationResources.getString("Select..."));
274         b.setActionCommand("SelectTickLabelFont");
275         b.addActionListener(this);
276         ticks.add(b);
277
278         this.showTickMarksCheckBox = new JCheckBox JavaDoc(
279             localizationResources.getString("Show_tick_marks"),
280             axis.isTickMarksVisible()
281         );
282         ticks.add(this.showTickMarksCheckBox);
283         ticks.add(new JPanel JavaDoc());
284         ticks.add(new JPanel JavaDoc());
285
286         this.otherTabs.add(localizationResources.getString("Ticks"), ticks);
287
288         other.add(this.otherTabs);
289
290         this.slot1.add(other);
291
292         this.slot2 = new JPanel JavaDoc(new BorderLayout JavaDoc());
293         this.slot2.add(this.slot1, BorderLayout.NORTH);
294         add(this.slot2);
295
296     }
297
298     /**
299      * Returns the current axis label.
300      *
301      * @return The current axis label.
302      */

303     public String JavaDoc getLabel() {
304         return this.label.getText();
305     }
306
307     /**
308      * Returns the current label font.
309      *
310      * @return The current label font.
311      */

312     public Font JavaDoc getLabelFont() {
313         return this.labelFont;
314     }
315
316     /**
317      * Returns the current label paint.
318      *
319      * @return The current label paint.
320      */

321     public Paint JavaDoc getLabelPaint() {
322         return this.labelPaintSample.getPaint();
323     }
324
325     /**
326      * Returns a flag that indicates whether or not the tick labels are visible.
327      *
328      * @return <code>true</code> if ick mark labels are visible.
329      */

330     public boolean isTickLabelsVisible() {
331         return this.showTickLabelsCheckBox.isSelected();
332     }
333
334     /**
335      * Returns the font used to draw the tick labels (if they are showing).
336      *
337      * @return The font used to draw the tick labels.
338      */

339     public Font JavaDoc getTickLabelFont() {
340         return this.tickLabelFont;
341     }
342
343     /**
344      * Returns the current tick label paint.
345      *
346      * @return The current tick label paint.
347      */

348     public Paint JavaDoc getTickLabelPaint() {
349         return this.tickLabelPaintSample.getPaint();
350     }
351
352     /**
353      * Returns the current value of the flag that determines whether or not
354      * tick marks are visible.
355      *
356      * @return <code>true</code> if tick marks are visible.
357      */

358     public boolean isTickMarksVisible() {
359         return this.showTickMarksCheckBox.isSelected();
360     }
361
362     /**
363      * Returns the current tick label insets value
364      *
365      * @return The current tick label insets value.
366      */

367     public RectangleInsets getTickLabelInsets() {
368         return (this.tickLabelInsets == null)
369             ? new RectangleInsets(0, 0, 0, 0)
370             : this.tickLabelInsets;
371     }
372
373     /**
374      * Returns the current label insets value
375      *
376      * @return The current label insets value.
377      */

378     public RectangleInsets getLabelInsets() {
379         return (this.labelInsets == null)
380             ? new RectangleInsets(0, 0, 0, 0) : this.labelInsets;
381     }
382
383     /**
384      * Returns a reference to the tabbed pane.
385      *
386      * @return A reference to the tabbed pane.
387      */

388     public JTabbedPane JavaDoc getOtherTabs() {
389         return this.otherTabs;
390     }
391
392     /**
393      * Handles user interaction with the property panel.
394      *
395      * @param event information about the event that triggered the call to
396      * this method.
397      */

398     public void actionPerformed(ActionEvent JavaDoc event) {
399         String JavaDoc command = event.getActionCommand();
400         if (command.equals("SelectLabelFont")) {
401             attemptLabelFontSelection();
402         }
403         else if (command.equals("SelectLabelPaint")) {
404             attemptModifyLabelPaint();
405         }
406         else if (command.equals("SelectTickLabelFont")) {
407             attemptTickLabelFontSelection();
408         }
409 // else if (command.equals("LabelInsets")) {
410
// editLabelInsets();
411
// }
412
// else if (command.equals("TickLabelInsets")) {
413
// editTickLabelInsets();
414
// }
415
}
416
417     /**
418      * Presents a font selection dialog to the user.
419      */

420     private void attemptLabelFontSelection() {
421
422         FontChooserPanel panel = new FontChooserPanel(this.labelFont);
423         int result = JOptionPane.showConfirmDialog(this, panel,
424             localizationResources.getString("Font_Selection"),
425             JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
426
427         if (result == JOptionPane.OK_OPTION) {
428             this.labelFont = panel.getSelectedFont();
429             this.labelFontField.setText(
430                 this.labelFont.getFontName() + " " + this.labelFont.getSize()
431             );
432         }
433
434     }
435
436     /**
437      * Allows the user the opportunity to change the outline paint.
438      */

439     private void attemptModifyLabelPaint() {
440         Color JavaDoc c;
441         c = JColorChooser.showDialog(
442             this, localizationResources.getString("Label_Color"), Color.blue
443         );
444         if (c != null) {
445             this.labelPaintSample.setPaint(c);
446         }
447     }
448
449     /**
450      * Presents a tick label font selection dialog to the user.
451      */

452     public void attemptTickLabelFontSelection() {
453
454         FontChooserPanel panel = new FontChooserPanel(this.tickLabelFont);
455         int result = JOptionPane.showConfirmDialog(this, panel,
456             localizationResources.getString("Font_Selection"),
457             JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
458
459         if (result == JOptionPane.OK_OPTION) {
460             this.tickLabelFont = panel.getSelectedFont();
461             this.tickLabelFontField.setText(
462                 this.tickLabelFont.getFontName() + " "
463                 + this.tickLabelFont.getSize()
464             );
465         }
466
467     }
468
469 // /**
470
// * Presents insets chooser panel allowing user to modify tick label's
471
// * individual insets values. Updates the current insets text field if edit
472
// * is accepted.
473
// */
474
// private void editTickLabelInsets() {
475
// InsetsChooserPanel panel = new InsetsChooserPanel(this.tickLabelInsets);
476
// int result = JOptionPane.showConfirmDialog(
477
// this, panel, localizationResources.getString("Edit_Insets"),
478
// JOptionPane.PLAIN_MESSAGE
479
// );
480
//
481
// if (result == JOptionPane.OK_OPTION) {
482
// this.tickLabelInsets = panel.getInsets();
483
// this.tickLabelInsetsTextField.setInsets(this.tickLabelInsets);
484
// }
485
// }
486
//
487
// /**
488
// * Presents insets chooser panel allowing user to modify label's
489
// * individual insets values. Updates the current insets text field if edit
490
// * is accepted.
491
// */
492
// private void editLabelInsets() {
493
// InsetsChooserPanel panel = new InsetsChooserPanel(this.labelInsets);
494
// int result = JOptionPane.showConfirmDialog(
495
// this, panel, localizationResources.getString("Edit_Insets"),
496
// JOptionPane.PLAIN_MESSAGE
497
// );
498
//
499
// if (result == JOptionPane.OK_OPTION) {
500
// this.labelInsets = panel.getInsets();
501
// this.labelInsetsTextField.setInsets(this.labelInsets);
502
// }
503
// }
504

505     /**
506      * Sets the properties of the specified axis to match the properties
507      * defined on this panel.
508      *
509      * @param axis the axis.
510      */

511     public void setAxisProperties(Axis axis) {
512         axis.setLabel(getLabel());
513         axis.setLabelFont(getLabelFont());
514         axis.setLabelPaint(getLabelPaint());
515         axis.setTickMarksVisible(isTickMarksVisible());
516         // axis.setTickMarkStroke(getTickMarkStroke());
517
axis.setTickLabelsVisible(isTickLabelsVisible());
518         axis.setTickLabelFont(getTickLabelFont());
519         axis.setTickLabelPaint(getTickLabelPaint());
520         axis.setTickLabelInsets(getTickLabelInsets());
521         axis.setLabelInsets(getLabelInsets());
522     }
523
524 }
525
Popular Tags