KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DefaultAxisEditor.java
29  * ----------------------
30  * (C) Copyright 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert;
33  * Contributor(s): Andrzej Porebski;
34  * Arnaud Lelievre;
35  *
36  * $Id: DefaultAxisEditor.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 AxisPropertyEditPanel.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.JTabbedPane JavaDoc;
62 import javax.swing.JTextField JavaDoc;
63
64 import org.jfree.chart.axis.Axis;
65 import org.jfree.chart.axis.NumberAxis;
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 import org.jfree.ui.RectangleInsets;
71
72 /**
73  * A panel for editing the properties of an axis.
74  */

75 class DefaultAxisEditor extends JPanel JavaDoc implements ActionListener JavaDoc {
76
77     /** The axis label. */
78     private JTextField JavaDoc label;
79
80     /** The label font. */
81     private Font JavaDoc labelFont;
82
83     /** The label paint. */
84     private PaintSample labelPaintSample;
85
86     /** A field showing a description of the label font. */
87     private JTextField JavaDoc labelFontField;
88
89     /** The font for displaying tick labels on the axis. */
90     private Font JavaDoc tickLabelFont;
91
92     /**
93      * A field containing a description of the font for displaying tick labels
94      * on the axis.
95      */

96     private JTextField JavaDoc tickLabelFontField;
97
98     /** The paint (color) for the tick labels. */
99     private PaintSample tickLabelPaintSample;
100
101     /**
102      * An empty sub-panel for extending the user interface to handle more
103      * complex axes.
104      */

105     private JPanel JavaDoc slot1;
106
107     /**
108      * An empty sub-panel for extending the user interface to handle more
109      * complex axes.
110      */

111     private JPanel JavaDoc slot2;
112
113     /** A flag that indicates whether or not the tick labels are visible. */
114     private JCheckBox JavaDoc showTickLabelsCheckBox;
115
116     /** A flag that indicates whether or not the tick marks are visible. */
117     private JCheckBox JavaDoc showTickMarksCheckBox;
118
119 // /** Insets text field. */
120
// private InsetsTextField tickLabelInsetsTextField;
121
//
122
// /** Label insets text field. */
123
// private InsetsTextField labelInsetsTextField;
124

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

147     public static DefaultAxisEditor getInstance(Axis axis) {
148
149         if (axis != null) {
150             // figure out what type of axis we have and instantiate the
151
// appropriate panel
152
if (axis instanceof NumberAxis) {
153                 return new DefaultNumberAxisEditor((NumberAxis) axis);
154             }
155             else {
156                 return new DefaultAxisEditor(axis);
157             }
158         }
159         else {
160             return null;
161         }
162
163     }
164
165     /**
166      * Standard constructor: builds a panel for displaying/editing the
167      * properties of the specified axis.
168      *
169      * @param axis the axis whose properties are to be displayed/edited in
170      * the panel.
171      */

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

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

296     public String JavaDoc getLabel() {
297         return this.label.getText();
298     }
299
300     /**
301      * Returns the current label font.
302      *
303      * @return The current label font.
304      */

305     public Font JavaDoc getLabelFont() {
306         return this.labelFont;
307     }
308
309     /**
310      * Returns the current label paint.
311      *
312      * @return The current label paint.
313      */

314     public Paint JavaDoc getLabelPaint() {
315         return this.labelPaintSample.getPaint();
316     }
317
318     /**
319      * Returns a flag that indicates whether or not the tick labels are visible.
320      *
321      * @return <code>true</code> if ick mark labels are visible.
322      */

323     public boolean isTickLabelsVisible() {
324         return this.showTickLabelsCheckBox.isSelected();
325     }
326
327     /**
328      * Returns the font used to draw the tick labels (if they are showing).
329      *
330      * @return The font used to draw the tick labels.
331      */

332     public Font JavaDoc getTickLabelFont() {
333         return this.tickLabelFont;
334     }
335
336     /**
337      * Returns the current tick label paint.
338      *
339      * @return The current tick label paint.
340      */

341     public Paint JavaDoc getTickLabelPaint() {
342         return this.tickLabelPaintSample.getPaint();
343     }
344
345     /**
346      * Returns the current value of the flag that determines whether or not
347      * tick marks are visible.
348      *
349      * @return <code>true</code> if tick marks are visible.
350      */

351     public boolean isTickMarksVisible() {
352         return this.showTickMarksCheckBox.isSelected();
353     }
354
355     /**
356      * Returns the current tick label insets value
357      *
358      * @return The current tick label insets value.
359      */

360     public RectangleInsets getTickLabelInsets() {
361         return (this.tickLabelInsets == null)
362             ? new RectangleInsets(0, 0, 0, 0)
363             : this.tickLabelInsets;
364     }
365
366     /**
367      * Returns the current label insets value
368      *
369      * @return The current label insets value.
370      */

371     public RectangleInsets getLabelInsets() {
372         return (this.labelInsets == null)
373             ? new RectangleInsets(0, 0, 0, 0) : this.labelInsets;
374     }
375
376     /**
377      * Returns a reference to the tabbed pane.
378      *
379      * @return A reference to the tabbed pane.
380      */

381     public JTabbedPane JavaDoc getOtherTabs() {
382         return this.otherTabs;
383     }
384
385     /**
386      * Handles user interaction with the property panel.
387      *
388      * @param event information about the event that triggered the call to
389      * this method.
390      */

391     public void actionPerformed(ActionEvent JavaDoc event) {
392         String JavaDoc command = event.getActionCommand();
393         if (command.equals("SelectLabelFont")) {
394             attemptLabelFontSelection();
395         }
396         else if (command.equals("SelectLabelPaint")) {
397             attemptModifyLabelPaint();
398         }
399         else if (command.equals("SelectTickLabelFont")) {
400             attemptTickLabelFontSelection();
401         }
402 // else if (command.equals("LabelInsets")) {
403
// editLabelInsets();
404
// }
405
// else if (command.equals("TickLabelInsets")) {
406
// editTickLabelInsets();
407
// }
408
}
409
410     /**
411      * Presents a font selection dialog to the user.
412      */

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

432     private void attemptModifyLabelPaint() {
433         Color JavaDoc c;
434         c = JColorChooser.showDialog(
435             this, localizationResources.getString("Label_Color"), Color.blue
436         );
437         if (c != null) {
438             this.labelPaintSample.setPaint(c);
439         }
440     }
441
442     /**
443      * Presents a tick label font selection dialog to the user.
444      */

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

498     /**
499      * Sets the properties of the specified axis to match the properties
500      * defined on this panel.
501      *
502      * @param axis the axis.
503      */

504     public void setAxisProperties(Axis axis) {
505         axis.setLabel(getLabel());
506         axis.setLabelFont(getLabelFont());
507         axis.setLabelPaint(getLabelPaint());
508         axis.setTickMarksVisible(isTickMarksVisible());
509         // axis.setTickMarkStroke(getTickMarkStroke());
510
axis.setTickLabelsVisible(isTickLabelsVisible());
511         axis.setTickLabelFont(getTickLabelFont());
512         axis.setTickLabelPaint(getTickLabelPaint());
513         axis.setTickLabelInsets(getTickLabelInsets());
514         axis.setLabelInsets(getLabelInsets());
515     }
516
517 }
518
Popular Tags