KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ColorBarPropertyEditPanel.java
29  * ------------------------------
30  * (C) Copyright 2002-2004, by David M. O'Donnell and Contributors.
31  *
32  * Original Author: David M. O'Donnell;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  * Arnaud Lelievre;
35  *
36  * $Id: DefaultColorBarEditor.java,v 1.1.2.1 2005/11/24 16:11:48 mungady Exp $
37  *
38  * Changes
39  * -------
40  * 26-Nov-2002 : Version 1 contributed by David M. O'Donnell (DG);
41  * 08-Sep-2003 : Added internationalization via use of properties
42  * resourceBundle (RFE 690236) (AL);
43  * 24-Nov-2005 : Moved and renamed: org.jfree.chart.ui.ColorBarPropertyEditPanel
44  * --> DefaultColorBarEditor (DG);
45  *
46  */

47
48 package org.jfree.chart.editor;
49
50 import java.awt.event.ActionEvent JavaDoc;
51 import java.util.ResourceBundle JavaDoc;
52
53 import javax.swing.BorderFactory JavaDoc;
54 import javax.swing.JButton JavaDoc;
55 import javax.swing.JCheckBox JavaDoc;
56 import javax.swing.JLabel JavaDoc;
57 import javax.swing.JOptionPane JavaDoc;
58 import javax.swing.JPanel JavaDoc;
59 import javax.swing.JTabbedPane JavaDoc;
60
61 import org.jfree.chart.axis.ColorBar;
62 import org.jfree.chart.axis.NumberAxis;
63 import org.jfree.chart.plot.GreyPalette;
64 import org.jfree.chart.plot.RainbowPalette;
65 import org.jfree.layout.LCBLayout;
66
67
68 /**
69  * A ColorBarPropertyEditPanel. Extends NumberAxisPropertyEditPanel to allow
70  * change general axis type parameters.
71  *
72  * @author David M. O'Donnell
73  */

74 class DefaultColorBarEditor extends DefaultNumberAxisEditor {
75
76     /**
77      * A checkbox that indicates whether or not the color indices should run
78      * high to low.
79      */

80     private JCheckBox JavaDoc invertPaletteCheckBox;
81
82     /** Flag set by invertPaletteCheckBox. */
83     private boolean invertPalette = false;
84
85     /** A checkbox that indicates whether the palette is stepped. */
86     private JCheckBox JavaDoc stepPaletteCheckBox;
87
88     /** Flag set by stepPaletteCheckBox. */
89     private boolean stepPalette = false;
90
91     /** The Palette Sample displaying the current Palette. */
92     private PaletteSample currentPalette;
93
94     /** An array of availiable sample palettes. */
95     private PaletteSample[] availablePaletteSamples;
96
97     /** The resourceBundle for the localization. */
98    protected static ResourceBundle JavaDoc localizationResources =
99        ResourceBundle.getBundle("org.jfree.chart.editor.LocalizationBundle");
100
101     /**
102      * Creates a new edit panel for a color bar.
103      *
104      * @param colorBar the color bar.
105      */

106     public DefaultColorBarEditor(ColorBar colorBar) {
107         super((NumberAxis) colorBar.getAxis());
108         this.invertPalette = colorBar.getColorPalette().isInverse();
109         this.stepPalette = colorBar.getColorPalette().isStepped();
110         this.currentPalette = new PaletteSample(colorBar.getColorPalette());
111         this.availablePaletteSamples = new PaletteSample[2];
112         this.availablePaletteSamples[0]
113             = new PaletteSample(new RainbowPalette());
114         this.availablePaletteSamples[1]
115             = new PaletteSample(new GreyPalette());
116
117         JTabbedPane JavaDoc other = getOtherTabs();
118
119         JPanel JavaDoc palettePanel = new JPanel JavaDoc(new LCBLayout(4));
120         palettePanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
121
122         palettePanel.add(new JPanel JavaDoc());
123         this.invertPaletteCheckBox = new JCheckBox JavaDoc(
124             localizationResources.getString("Invert_Palette"),
125             this.invertPalette
126         );
127         this.invertPaletteCheckBox.setActionCommand("invertPalette");
128         this.invertPaletteCheckBox.addActionListener(this);
129         palettePanel.add(this.invertPaletteCheckBox);
130         palettePanel.add(new JPanel JavaDoc());
131
132         palettePanel.add(new JPanel JavaDoc());
133         this.stepPaletteCheckBox = new JCheckBox JavaDoc(
134             localizationResources.getString("Step_Palette"),
135             this.stepPalette
136         );
137         this.stepPaletteCheckBox.setActionCommand("stepPalette");
138         this.stepPaletteCheckBox.addActionListener(this);
139         palettePanel.add(this.stepPaletteCheckBox);
140         palettePanel.add(new JPanel JavaDoc());
141
142         palettePanel.add(
143             new JLabel JavaDoc(localizationResources.getString("Palette"))
144         );
145         JButton JavaDoc button
146             = new JButton JavaDoc(localizationResources.getString("Set_palette..."));
147         button.setActionCommand("PaletteChoice");
148         button.addActionListener(this);
149         palettePanel.add(this.currentPalette);
150         palettePanel.add(button);
151
152         other.add(localizationResources.getString("Palette"), palettePanel);
153
154     }
155
156     /**
157      * Handles actions from within the property panel.
158      *
159      * @param event the event.
160      */

161     public void actionPerformed(ActionEvent JavaDoc event) {
162         String JavaDoc command = event.getActionCommand();
163         if (command.equals("PaletteChoice")) {
164             attemptPaletteSelection();
165         }
166         else if (command.equals("invertPalette")) {
167             this.invertPalette = this.invertPaletteCheckBox.isSelected();
168         }
169         else if (command.equals("stepPalette")) {
170             this.stepPalette = this.stepPaletteCheckBox.isSelected();
171         }
172         else {
173             super.actionPerformed(event); // pass to super-class for handling
174
}
175     }
176
177     /**
178      * Handle a palette selection.
179      */

180     private void attemptPaletteSelection() {
181         PaletteChooserPanel panel
182             = new PaletteChooserPanel(null, this.availablePaletteSamples);
183         int result = JOptionPane.showConfirmDialog(
184             this, panel, localizationResources.getString("Palette_Selection"),
185             JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE
186         );
187
188         if (result == JOptionPane.OK_OPTION) {
189             double zmin = this.currentPalette.getPalette().getMinZ();
190             double zmax = this.currentPalette.getPalette().getMaxZ();
191             this.currentPalette.setPalette(panel.getSelectedPalette());
192             this.currentPalette.getPalette().setMinZ(zmin);
193             this.currentPalette.getPalette().setMaxZ(zmax);
194         }
195     }
196
197     /**
198      * Sets the properties of the specified axis to match the properties
199      * defined on this panel.
200      *
201      * @param colorBar the color bar.
202      */

203     public void setAxisProperties(ColorBar colorBar) {
204         super.setAxisProperties(colorBar.getAxis());
205         colorBar.setColorPalette(this.currentPalette.getPalette());
206         colorBar.getColorPalette().setInverse(this.invertPalette); //dmo added
207
colorBar.getColorPalette().setStepped(this.stepPalette); //dmo added
208
}
209
210     /**
211      * A static method that returns a panel that is appropriate for the axis
212      * type.
213      *
214      * @param colorBar the color bar.
215      *
216      * @return A panel or <code>null</code< if axis is <code>null</code>.
217      */

218     public static DefaultColorBarEditor getInstance(ColorBar colorBar) {
219
220         if (colorBar != null) {
221             return new DefaultColorBarEditor(colorBar);
222         }
223         else {
224             return null;
225         }
226
227     }
228
229 }
230
Popular Tags