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 lombok.extern.slf4j.Slf4j;
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  @Slf4j
52  public class RedGreenColorProvider extends ColorProvider {
53      private static final long serialVersionUID = 5030306338780462810L;
54      private static final int COLOUR_MAX_VALUE = 255;
55      private final JTreeMap jTreeMap;
56      private JPanel legend;
57      private Value maxAbsValue;
58      private Value minVal;
59      private static final int[] TAB_COLOR = { 0, 60, 102, 153, 204, COLOUR_MAX_VALUE };
60      private static final int[] TAB_LIMIT_VALUE = { 25, 76, 123, 179, 230, COLOUR_MAX_VALUE };
61  
62      /**
63       * Constructor
64       *
65       * @param jTreeMap
66       *            the JTreeMap to color
67       */
68      public RedGreenColorProvider(final JTreeMap jTreeMap) {
69          this.jTreeMap = jTreeMap;
70      }
71  
72      @Override
73      public Color getColor(final Value value) {
74          // update the max absolute value
75          if (this.maxAbsValue == null) {
76              setMaxValue(this.jTreeMap.getRoot());
77          }
78  
79          final double dValeur = value != null ? value.getValue() : 0.00;
80  
81          int colorIndex = (int) (COLOUR_MAX_VALUE * Math.abs(dValeur) / this.maxAbsValue.getValue());
82  
83          if (colorIndex > COLOUR_MAX_VALUE) {
84              colorIndex = COLOUR_MAX_VALUE;
85          }
86  
87          for (int i = 0; i < TAB_LIMIT_VALUE.length; i++) {
88              if (colorIndex <= TAB_LIMIT_VALUE[i]) {
89                  colorIndex = TAB_COLOR[i];
90                  break;
91              }
92          }
93  
94          if (dValeur >= 0) {
95              return new Color(0, colorIndex, 0);
96          }
97          return new Color(colorIndex, 0, 0);
98      }
99  
100     @Override
101     public JPanel getLegendPanel() {
102         if (this.legend == null) {
103             // update the max absolute value
104             if (this.maxAbsValue == null) {
105                 setMaxValue(this.jTreeMap.getRoot());
106             }
107             this.legend = new Legend();
108         }
109         return this.legend;
110     }
111 
112     /**
113      * Set the max and the min value
114      *
115      * @param root
116      *            root of the JTreeMap
117      */
118     private void setMaxValue(final TreeMapNode root) {
119         if (root.isLeaf()) {
120             final Value value = root.getValue();
121             if (value != null && (this.maxAbsValue == null || Math.abs(value.getValue()) > this.maxAbsValue.getValue())) {
122                 try {
123                     final Class<?> c = value.getClass();
124                     if (this.maxAbsValue == null || this.minVal == null) {
125                         this.maxAbsValue = (Value) c.newInstance();
126                         this.minVal = (Value) c.newInstance();
127                     }
128                     this.minVal.setValue(-Math.abs(value.getValue()));
129                     this.maxAbsValue.setValue(Math.abs(value.getValue()));
130                 } catch (final IllegalAccessException | InstantiationException ie) {
131                     // ignore
132                     log.error("Issue with max value", ie);
133                 }
134             }
135         } else {
136             for (final TreeMapNode node : root.getChildren()) {
137                 setMaxValue(node);
138             }
139         }
140     }
141 
142     /**
143      * Panel with the legend
144      *
145      * @author Laurent Dutheil
146      */
147     private class Legend extends JPanel {
148         private static final int Y_INSET = 7;
149         private static final int X_INSET = 15;
150         private static final long serialVersionUID = -536198802533900214L;
151         private static final int HEIGHT = 20;
152         private static final int WIDTH = 10;
153         private static final int X = 20;
154         private static final int Y = 25;
155 
156         /**
157          * Constructor of Legend
158          */
159         public Legend() {
160             this.setPreferredSize(
161                     new java.awt.Dimension(2 * (Legend.X + RedGreenColorProvider.TAB_COLOR.length * Legend.WIDTH), 2 * (Legend.Y + Legend.HEIGHT)));
162         }
163 
164         @Override
165         public void paintComponent(final Graphics g) {
166             super.paintComponent(g);
167             if (RedGreenColorProvider.this.minVal == null || RedGreenColorProvider.this.maxAbsValue == null) {
168                 return;
169             }
170 
171             int xCursor = 0;
172 
173             for (int i = RedGreenColorProvider.TAB_COLOR.length - 1; i > 0; i--) {
174                 g.setColor(new Color(RedGreenColorProvider.TAB_COLOR[i], 0, 0));
175                 g.fillRect(Legend.X + xCursor * Legend.WIDTH, Legend.Y, Legend.WIDTH, Legend.HEIGHT);
176                 xCursor++;
177             }
178 
179             g.setColor(Color.black);
180             g.drawString(RedGreenColorProvider.this.minVal.getLabel(), Legend.X - X_INSET, Legend.Y - Y_INSET);
181             g.drawString("0", Legend.X + xCursor * Legend.WIDTH, Legend.Y - Y_INSET);
182 
183             for (final int element : RedGreenColorProvider.TAB_COLOR) {
184                 g.setColor(new Color(0, element, 0));
185                 g.fillRect(Legend.X + xCursor * Legend.WIDTH, Legend.Y, Legend.WIDTH, Legend.HEIGHT);
186                 xCursor++;
187             }
188 
189             g.setColor(Color.black);
190             g.drawString(RedGreenColorProvider.this.maxAbsValue.getLabel(), Legend.X + (xCursor - 2) * Legend.WIDTH, Legend.Y - Y_INSET);
191         }
192     }
193 }
194 /*
195  *                 ObjectLab is supporing JTreeMap
196  *
197  * Based in London, we are world leaders in the design and development
198  * of bespoke applications for the securities financing markets.
199  *
200  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
201  *           ___  _     _           _   _          _
202  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
203  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
204  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
205  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
206  *                   |__/
207  *
208  *                     www.ObjectLab.co.uk
209  */