Coverage Report - org.jfree.chart.editor.GradientColorPaletteEditor
 
Classes in this File Line Coverage Branch Coverage Complexity
GradientColorPaletteEditor
100%
57/57
100%
10/10
2.8
 
 1  
 /* ======================================================================= 
 2  
  * A visualisation library extension for JFreeChart. Please see JFreeChart
 3  
  * for further information.
 4  
  * =======================================================================
 5  
  * Copyright (C) 2006  University of Helsinki, Department of Computer Science
 6  
  *
 7  
  * This library is free software; you can redistribute it and/or
 8  
  * modify it under the terms of the GNU Lesser General Public
 9  
  * License as published by the Free Software Foundation; either
 10  
  * version 2.1 of the License, or (at your option) any later version.
 11  
  *
 12  
  * This library is distributed in the hope that it will be useful,
 13  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 15  
  * Lesser General Public License for more details.
 16  
  *
 17  
  * You should have received a copy of the GNU Lesser General Public
 18  
  * License along with this library; if not, write to the Free Software
 19  
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 20  
  * -----------------------------
 21  
  * Contact:  ohtu@cs.helsinki.fi
 22  
  * -----------------------------
 23  
  *
 24  
  */
 25  
 
 26  
 package org.jfree.chart.editor;
 27  
 
 28  
 import java.awt.Graphics2D;
 29  
 import java.awt.GridBagLayout;
 30  
 import java.awt.GridBagConstraints;
 31  
 import java.awt.Color;
 32  
 import java.awt.Insets;
 33  
 import java.awt.Point;
 34  
 import java.awt.Rectangle;
 35  
 import java.awt.geom.Rectangle2D;
 36  
 import java.awt.geom.Point2D;
 37  
 import java.awt.event.ActionListener;
 38  
 import java.awt.event.ActionEvent;
 39  
 
 40  
 import java.util.ResourceBundle;
 41  
 import java.util.Set;
 42  
 import java.util.Iterator;
 43  
 
 44  
 import java.lang.IllegalArgumentException;
 45  
 
 46  
 import javax.swing.BorderFactory;
 47  
 import javax.swing.JPanel;
 48  
 import javax.swing.JColorChooser;
 49  
 import javax.swing.JLabel;
 50  
 import javax.swing.JButton;
 51  
 import javax.swing.JCheckBox;
 52  
 import javax.swing.event.ChangeEvent;
 53  
 import javax.swing.event.ChangeListener;
 54  
 
 55  
 import org.jfree.ui.RectangleEdge;
 56  
 import org.jfree.ui.PaintSample;
 57  
 
 58  
 import org.jfree.chart.ChartMouseEvent;
 59  
 import org.jfree.chart.ChartMouseListener;
 60  
 import org.jfree.chart.axis.HeatMapAxis;
 61  
 import org.jfree.chart.axis.CategoryLabelPositions;
 62  
 import org.jfree.chart.entity.HCTreeNodeEntity;
 63  
 import org.jfree.chart.entity.HeatMapBlockEntity;
 64  
 import org.jfree.chart.entity.EntityCollection;
 65  
 import org.jfree.chart.labels.HCToolTipGenerator;
 66  
 import org.jfree.chart.plot.GradientColorPalette;
 67  
 import org.jfree.chart.plot.Plot;
 68  
 import org.jfree.chart.renderer.RendererState;
 69  
 import org.jfree.data.hc.HCDataset;
 70  
 import org.jfree.data.hc.HeatMap;
 71  
 import org.jfree.data.hc.DataRange;
 72  
 import org.jfree.data.hc.HCTreeNode;
 73  
 
 74  
 /**
 75  
  * xxx
 76  
  *
 77  
  * @author viski project.
 78  
  */
 79  
 public class GradientColorPaletteEditor
 80  
     extends JPanel
 81  
     implements ActionListener, ChangeListener {
 82  
 
 83  
     private JCheckBox logarithmicCB;
 84  
     private GradientColorPalette palette;
 85  
     private ColorInfo[] colors;
 86  
 
 87  
     public GradientColorPaletteEditor () {
 88  
 
 89  21
         super();
 90  21
         this.palette = null;
 91  21
         this.colors = null;
 92  
         
 93  21
     }
 94  
 
 95  
     public void setColoring (GradientColorPalette palette) {
 96  
 
 97  22
         this.palette = palette;
 98  
 
 99  22
     }
 100  
 
 101  
     /**
 102  
      * Listens for checkbox events.
 103  
      *
 104  
      * @param e  the event.
 105  
      */
 106  
     public void stateChanged(ChangeEvent e) {
 107  
 
 108  4
         if (e.getSource() == this.logarithmicCB) {
 109  
 
 110  4
             boolean value = this.logarithmicCB.isSelected();
 111  4
             this.palette.setLinear(!value);
 112  
 
 113  
         }
 114  
 
 115  4
     }
 116  
 
 117  
     /**
 118  
      * Listens button events
 119  
      *
 120  
      * @param e  the event.
 121  
      */
 122  
     public void actionPerformed(ActionEvent e) {
 123  
 
 124  
         int i;
 125  
         Color color;
 126  5
         ResourceBundle lr = ResourceBundle.getBundle(
 127  
                 "org.jfree.chart.editor.LocalizationBundle");
 128  
 
 129  5
         if (colors == null) return;
 130  
 
 131  30
         for (i = 0; i < this.colors.length; i++) {
 132  
 
 133  25
             if (e.getSource() == this.colors[i].button) {
 134  5
                 color = this.palette.getKeyColor(this.colors[i].value);
 135  
 
 136  5
                 color = JColorChooser.showDialog(
 137  
                     this, lr.getString("Change_color")+i, color
 138  
                 );
 139  5
                 if (color != null) {
 140  5
                     this.colors[i].sample.setPaint( color);
 141  5
                     this.palette.setKeyColor(this.colors[i].value, color);
 142  
                 }
 143  
             }
 144  
 
 145  
         }
 146  
 
 147  5
     }
 148  
     /**
 149  
      * Creates a new JPanel for adjusting heatmap colors.
 150  
      *
 151  
      * @return  The panel.
 152  
      */
 153  
     public JPanel getPanel() {
 154  2
         ResourceBundle lr = ResourceBundle.getBundle(
 155  
                 "org.jfree.chart.editor.LocalizationBundle");
 156  2
         JPanel panel = new JPanel();
 157  2
         GridBagConstraints c = new GridBagConstraints();
 158  
 
 159  2
         Set keyColors = this.palette.getKeyColors();
 160  2
         Iterator iterator = keyColors.iterator();
 161  
         int i;
 162  
 
 163  2
         panel.setLayout(new GridBagLayout());
 164  2
         panel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
 165  
 
 166  2
         c.fill = GridBagConstraints.VERTICAL;
 167  2
         c.insets = new Insets(5,2,5,2);
 168  
 
 169  2
         c.gridx = 0;
 170  2
         c.gridy = 0;
 171  
 
 172  2
         colors = new ColorInfo[keyColors.size()];
 173  
 
 174  2
         i = 0;
 175  10
         while (iterator.hasNext()) {
 176  
 
 177  8
             ColorInfo color = new ColorInfo();
 178  8
             color.value = ((Double)(iterator.next())).doubleValue();
 179  
             
 180  8
             c.gridx=0;
 181  
             String nameOfColor;
 182  8
             if (i==0) nameOfColor
 183  
                 = lr.getString("Color_for_values_below")+color.value;
 184  6
             else if (!iterator.hasNext()) nameOfColor
 185  
                 = lr.getString("Color_for_values_above")+color.value;
 186  4
             else nameOfColor
 187  
                 = lr.getString("Color_for_value")+color.value;
 188  8
             panel.add(new JLabel(nameOfColor),c);
 189  
 
 190  8
             c.gridx=1;
 191  8
             color.sample
 192  
                 = new PaintSample(this.palette.getColor(color.value));
 193  8
             panel.add(color.sample,c);
 194  
 
 195  8
             c.gridx=2;
 196  8
             color.button = new JButton(lr.getString("Change"));
 197  8
             panel.add(color.button,c);
 198  8
             color.button.addActionListener(this);
 199  
 
 200  8
             this.colors[i] = color;
 201  
 
 202  8
             c.gridy++;
 203  8
             i++;
 204  
 
 205  8
         }
 206  
 
 207  2
         c.gridx=0;
 208  2
         this.logarithmicCB = new JCheckBox(
 209  
             lr.getString("Use_logarithmic_scale"),
 210  
             !this.palette.isLinear()
 211  
         );
 212  2
         this.logarithmicCB.addChangeListener(this);
 213  2
         panel.add(this.logarithmicCB,c);
 214  2
         return panel;
 215  
     }
 216  
 
 217  
     private class ColorInfo {
 218  
 
 219  
         private PaintSample sample;
 220  
         private JButton button;
 221  
         private double value;
 222  
     }
 223  
 
 224  
 }
 225