View Javadoc

1   /*
2    * ObjectLab, http://www.objectlab.co.uk/open is supporting JTreeMap.
3    * 
4    * Based in London, we are world leaders in the design and development 
5    * of bespoke applications for the securities financing markets.
6    * 
7    * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
8    *           ___  _     _           _   _          _
9    *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
10   *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
11   *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
12   *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
13   *                   |__/
14   *
15   *                     www.ObjectLab.co.uk
16   *
17   * $Id: ColorProvider.java 69 2006-10-24 16:20:20Z benoitx $
18   * 
19   * Copyright 2006 the original author or authors.
20   *
21   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
22   * use this file except in compliance with the License. You may obtain a copy of
23   * the License at
24   *
25   * http://www.apache.org/licenses/LICENSE-2.0
26   *
27   * Unless required by applicable law or agreed to in writing, software
28   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
30   * License for the specific language governing permissions and limitations under
31   * the License.
32   */
33  package net.sf.jtreemap.swing.provider;
34  
35  import java.awt.Color;
36  import java.awt.Graphics;
37  
38  import javax.swing.JPanel;
39  
40  import net.sf.jtreemap.swing.ColorProvider;
41  import net.sf.jtreemap.swing.JTreeMap;
42  import net.sf.jtreemap.swing.TreeMapNode;
43  import net.sf.jtreemap.swing.Value;
44  
45  /**
46   * ColorProvider who, with a max absolute value M, choose the color between
47   * values -M and M.
48   * 
49   * @author Laurent Dutheil
50   */
51  
52  public class RedGreenColorProvider extends ColorProvider {
53      private static final int COLOUR_MAX_VALUE = 255;
54  
55      /**
56       * 
57       */
58      private static final long serialVersionUID = 5030306338780462810L;
59  
60      private final JTreeMap jTreeMap;
61  
62      private JPanel legend;
63  
64      private Value maxAbsValue;
65  
66      private Value minVal;
67  
68      private final int[] tabColor = { 0, 60, 102, 153, 204, COLOUR_MAX_VALUE };
69  
70      private static final int[] TAB_LIMIT_VALUE = { 25, 76, 123, 179, 230, COLOUR_MAX_VALUE };
71  
72      /**
73       * Constructor
74       * 
75       * @param jTreeMap
76       *            the JTreeMap to color
77       */
78      public RedGreenColorProvider(final JTreeMap jTreeMap) {
79          this.jTreeMap = jTreeMap;
80      }
81  
82      @Override
83      public Color getColor(final Value value) {
84          // update the max absolute value
85          if (this.maxAbsValue == null) {
86              setMaxValue(this.jTreeMap.getRoot());
87          }
88  
89          final double dValeur = (value != null ? value.getValue() : 0.00);
90  
91          int colorIndex = (int) (COLOUR_MAX_VALUE * Math.abs(dValeur) / this.maxAbsValue.getValue());
92  
93          if (colorIndex > COLOUR_MAX_VALUE) {
94              colorIndex = COLOUR_MAX_VALUE;
95          }
96  
97          for (int i = 0; i < TAB_LIMIT_VALUE.length; i++) {
98              if (colorIndex <= TAB_LIMIT_VALUE[i]) {
99                  colorIndex = this.tabColor[i];
100                 break;
101             }
102         }
103 
104         if (dValeur >= 0) {
105             return new Color(0, colorIndex, 0);
106         }
107         return new Color(colorIndex, 0, 0);
108     }
109 
110     @Override
111     public JPanel getLegendPanel() {
112         if (this.legend == null) {
113             // update the max absolute value
114             if (this.maxAbsValue == null) {
115                 setMaxValue(this.jTreeMap.getRoot());
116             }
117             this.legend = new Legend();
118         }
119         return this.legend;
120     }
121 
122     /**
123      * Set the max and the min value
124      * 
125      * @param root
126      *            root of the JTreeMap
127      */
128     private void setMaxValue(final TreeMapNode root) {
129         if (root.isLeaf()) {
130             final Value value = root.getValue();
131             if (value != null && (this.maxAbsValue == null || Math.abs(value.getValue()) > this.maxAbsValue.getValue())) {
132                 try {
133                     final Class c = value.getClass();
134                     if (this.maxAbsValue == null || this.minVal == null) {
135                         this.maxAbsValue = (Value) (c.newInstance());
136                         this.minVal = (Value) (c.newInstance());
137                     }
138                     this.minVal.setValue(-Math.abs(value.getValue()));
139                     this.maxAbsValue.setValue(Math.abs(value.getValue()));
140                 } catch (final IllegalAccessException iae) {
141                     // ignore
142                 } catch (final InstantiationException ie) {
143                     // ignore
144                 }
145             }
146         } else {
147             for (final TreeMapNode node : root.getChildren()) {
148                 setMaxValue(node);
149             }
150         }
151     }
152 
153     /**
154      * Panel with the legend
155      * 
156      * @author Laurent Dutheil
157      */
158     private class Legend extends JPanel {
159         private static final int Y_INSET = 7;
160 
161         private static final int X_INSET = 15;
162 
163         private static final long serialVersionUID = -536198802533900214L;
164 
165         private static final int HEIGHT = 20;
166 
167         private static final int WIDTH = 10;
168 
169         private static final int X = 20;
170 
171         private static final int Y = 25;
172 
173         /**
174          * Constructor of Legend
175          */
176         public Legend() {
177             this.setPreferredSize(new java.awt.Dimension(
178                     2 * Legend.X + RedGreenColorProvider.this.tabColor.length * Legend.WIDTH, 2 * Legend.Y + Legend.HEIGHT));
179         }
180 
181         @Override
182         public void paintComponent(final Graphics g) {
183             super.paintComponent(g);
184             if (RedGreenColorProvider.this.minVal == null || RedGreenColorProvider.this.maxAbsValue == null) {
185                 return;
186             }
187 
188             int xCursor = 0;
189 
190             for (int i = RedGreenColorProvider.this.tabColor.length - 1; i > 0; i--) {
191                 g.setColor(new Color(RedGreenColorProvider.this.tabColor[i], 0, 0));
192                 g.fillRect(Legend.X + xCursor * Legend.WIDTH, Legend.Y, Legend.WIDTH, Legend.HEIGHT);
193                 xCursor++;
194             }
195 
196             g.setColor(Color.black);
197             g.drawString(RedGreenColorProvider.this.minVal.getLabel(), Legend.X - X_INSET, Legend.Y - Y_INSET);
198             g.drawString("0", Legend.X + xCursor * Legend.WIDTH, Legend.Y - Y_INSET);
199 
200             for (final int element : RedGreenColorProvider.this.tabColor) {
201                 g.setColor(new Color(0, element, 0));
202                 g.fillRect(Legend.X + xCursor * Legend.WIDTH, Legend.Y, Legend.WIDTH, Legend.HEIGHT);
203                 xCursor++;
204             }
205 
206             g.setColor(Color.black);
207             g.drawString(RedGreenColorProvider.this.maxAbsValue.getLabel(), Legend.X + (xCursor - 1) * Legend.WIDTH, Legend.Y
208                     - Y_INSET);
209         }
210     }
211 }
212 /*
213  *                 ObjectLab is supporing JTreeMap
214  * 
215  * Based in London, we are world leaders in the design and development 
216  * of bespoke applications for the securities financing markets.
217  * 
218  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
219  *           ___  _     _           _   _          _
220  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
221  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
222  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
223  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
224  *                   |__/
225  *
226  *                     www.ObjectLab.co.uk
227  */