1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, 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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * ------------------------------
  25. * ColorBarPropertyEditPanel.java
  26. * ------------------------------
  27. * (C) Copyright 2002-2004, by David M. O'Donnell and Contributors.
  28. *
  29. * Original Author: David M. O'Donnell;
  30. * Contributor(s): David Gilbert (for Object Refinery Limited);
  31. * Arnaud Lelievre;
  32. *
  33. * $Id: ColorBarPropertyEditPanel.java,v 1.1 2004/08/31 14:54:21 mungady Exp $
  34. *
  35. * Changes
  36. * -------
  37. * 26-Nov-2002 : Version 1 contributed by David M. O'Donnell (DG);
  38. * 08-Sep-2003 : Added internationalization via use of properties resourceBundle (RFE 690236) (AL);
  39. *
  40. */
  41. package org.jfree.chart.ui;
  42. import java.awt.event.ActionEvent;
  43. import java.util.ResourceBundle;
  44. import javax.swing.BorderFactory;
  45. import javax.swing.JButton;
  46. import javax.swing.JCheckBox;
  47. import javax.swing.JLabel;
  48. import javax.swing.JOptionPane;
  49. import javax.swing.JPanel;
  50. import javax.swing.JTabbedPane;
  51. import org.jfree.chart.axis.ColorBar;
  52. import org.jfree.chart.axis.NumberAxis;
  53. import org.jfree.layout.LCBLayout;
  54. /**
  55. * A ColorBarPropertyEditPanel. Extends NumberAxisPropertyEditPanel to allow change general
  56. * axis type parameters.
  57. *
  58. * @author David M. O'Donnell
  59. */
  60. public class ColorBarPropertyEditPanel extends NumberAxisPropertyEditPanel {
  61. /** A checkbox that indicates whether or not the color indices should run high to low. */
  62. private JCheckBox invertPaletteCheckBox;
  63. /** Flag set by invertPaletteCheckBox. */
  64. private boolean invertPalette = false;
  65. /** A checkbox that indicates whether the palette is stepped. */
  66. private JCheckBox stepPaletteCheckBox;
  67. /** Flag set by stepPaletteCheckBox. */
  68. private boolean stepPalette = false;
  69. /** The Palette Sample displaying the current Palette. */
  70. private PaletteSample currentPalette;
  71. /** An array of availiable sample palettes. */
  72. private PaletteSample[] availablePaletteSamples;
  73. /** The resourceBundle for the localization. */
  74. protected static ResourceBundle localizationResources =
  75. ResourceBundle.getBundle("org.jfree.chart.ui.LocalizationBundle");
  76. /**
  77. * Creates a new edit panel for a color bar.
  78. *
  79. * @param colorBar the color bar.
  80. */
  81. public ColorBarPropertyEditPanel(ColorBar colorBar) {
  82. super((NumberAxis) colorBar.getAxis());
  83. this.invertPalette = colorBar.getColorPalette().isInverse(); //dmo added
  84. this.stepPalette = colorBar.getColorPalette().isStepped(); //dmo added
  85. this.currentPalette = new PaletteSample(colorBar.getColorPalette());
  86. this.availablePaletteSamples = new PaletteSample[2];
  87. this.availablePaletteSamples[0] = new PaletteSample(new RainbowPalette());
  88. this.availablePaletteSamples[1] = new PaletteSample(new GreyPalette());
  89. JTabbedPane other = getOtherTabs();
  90. JPanel palettePanel = new JPanel(new LCBLayout(4));
  91. palettePanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
  92. palettePanel.add(new JPanel());
  93. this.invertPaletteCheckBox = new JCheckBox(
  94. localizationResources.getString("Invert_Palette"), this.invertPalette
  95. );
  96. this.invertPaletteCheckBox.setActionCommand("invertPalette");
  97. this.invertPaletteCheckBox.addActionListener(this);
  98. palettePanel.add(this.invertPaletteCheckBox);
  99. palettePanel.add(new JPanel());
  100. palettePanel.add(new JPanel());
  101. this.stepPaletteCheckBox = new JCheckBox(localizationResources.getString("Step_Palette"),
  102. this.stepPalette);
  103. this.stepPaletteCheckBox.setActionCommand("stepPalette");
  104. this.stepPaletteCheckBox.addActionListener(this);
  105. palettePanel.add(this.stepPaletteCheckBox);
  106. palettePanel.add(new JPanel());
  107. palettePanel.add(new JLabel(localizationResources.getString("Palette")));
  108. JButton button = new JButton(localizationResources.getString("Set_palette..."));
  109. button.setActionCommand("PaletteChoice");
  110. button.addActionListener(this);
  111. palettePanel.add(this.currentPalette);
  112. palettePanel.add(button);
  113. other.add(localizationResources.getString("Palette"), palettePanel);
  114. }
  115. /**
  116. * Handles actions from within the property panel.
  117. *
  118. * @param event the event.
  119. */
  120. public void actionPerformed(ActionEvent event) {
  121. String command = event.getActionCommand();
  122. if (command.equals("PaletteChoice")) {
  123. attemptPaletteSelection();
  124. }
  125. else if (command.equals("invertPalette")) {
  126. this.invertPalette = this.invertPaletteCheckBox.isSelected();
  127. }
  128. else if (command.equals("stepPalette")) {
  129. this.stepPalette = this.stepPaletteCheckBox.isSelected();
  130. }
  131. else {
  132. super.actionPerformed(event); // pass to the super-class for handling
  133. }
  134. }
  135. /**
  136. * Handle a palette selection.
  137. */
  138. private void attemptPaletteSelection() {
  139. PaletteChooserPanel panel = new PaletteChooserPanel(null, this.availablePaletteSamples);
  140. int result =
  141. JOptionPane.showConfirmDialog(this, panel,
  142. localizationResources.getString("Palette_Selection"),
  143. JOptionPane.OK_CANCEL_OPTION,
  144. JOptionPane.PLAIN_MESSAGE);
  145. if (result == JOptionPane.OK_OPTION) {
  146. double zmin = this.currentPalette.getPalette().getMinZ();
  147. double zmax = this.currentPalette.getPalette().getMaxZ();
  148. this.currentPalette.setPalette(panel.getSelectedPalette());
  149. this.currentPalette.getPalette().setMinZ(zmin);
  150. this.currentPalette.getPalette().setMaxZ(zmax);
  151. }
  152. }
  153. /**
  154. * Sets the properties of the specified axis to match the properties defined on this panel.
  155. *
  156. * @param colorBar the color bar.
  157. */
  158. public void setAxisProperties(ColorBar colorBar) {
  159. super.setAxisProperties(colorBar.getAxis());
  160. colorBar.setColorPalette(this.currentPalette.getPalette());
  161. colorBar.getColorPalette().setInverse(this.invertPalette); //dmo added
  162. colorBar.getColorPalette().setStepped(this.stepPalette); //dmo added
  163. }
  164. /**
  165. * A static method that returns a panel that is appropriate for the axis
  166. * type.
  167. *
  168. * @param colorBar the color bar.
  169. *
  170. * @return a panel or <code>null</code< if axis is <code>null</code>.
  171. */
  172. public static ColorBarPropertyEditPanel getInstance(ColorBar colorBar) {
  173. if (colorBar != null) {
  174. return new ColorBarPropertyEditPanel(colorBar);
  175. }
  176. else {
  177. return null;
  178. }
  179. }
  180. }