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.Dimension;
37  import java.awt.FontMetrics;
38  import java.awt.Graphics;
39  import java.util.Enumeration;
40  import java.util.TreeMap;
41  
42  import javax.swing.JPanel;
43  
44  import net.sf.jtreemap.swing.JTreeMap;
45  import net.sf.jtreemap.swing.TreeMapNode;
46  import net.sf.jtreemap.swing.Value;
47  
48  /**
49   * <p>
50   * ColorProvider who choose the color between 13 predefined COLOURS.
51   * </p>
52   * <p>
53   * Each value is associated to a color. If all the COLOURS are already
54   * associated the new value is associated to the first color (and so on...)
55   * </p>
56   *
57   * @author Laurent DUTHEIL
58   */
59  public class RandomColorProvider extends ColorProvider {
60      private static final long serialVersionUID = -8184356270950978553L;
61  
62      private static final Color[] COLOURS = new Color[] { new Color(255, 0, 0), new Color(0, 255, 0), new Color(0, 0, 255), new Color(255, 255, 0),
63              new Color(255, 0, 255), new Color(0, 255, 255), new Color(102, 102, 51), new Color(255, 51, 153), new Color(255, 153, 51),
64              new Color(204, 204, 51), new Color(205, 102, 204), new Color(51, 153, 255), new Color(153, 102, 0) };
65  
66      private int cursor = 0;
67      private final TreeMap<Value, Color> mapping = new TreeMap<>();
68      private JPanel legend;
69      private final JTreeMap jTreeMap;
70  
71      /**
72       * Constructor
73       *
74       * @param jTreeMap
75       *            jTreeMap to color
76       */
77      public RandomColorProvider(final JTreeMap jTreeMap) {
78          this.jTreeMap = jTreeMap;
79      }
80  
81      /*
82       * (non-Javadoc)
83       *
84       * @see net.sf.jtreemap.swing.ColorProvider#getColor(double)
85       */
86      @Override
87      public Color getColor(final Value value) {
88          if (!this.mapping.containsKey(value)) {
89              mapping.put(value, COLOURS[this.cursor]);
90              cursor++;
91              if (this.cursor == COLOURS.length) {
92                  cursor = 0;
93              }
94          }
95          return mapping.get(value);
96      }
97  
98      void setValues(final TreeMapNode root) {
99          if (root.isLeaf()) {
100             final Value value = root.getValue();
101             getColor(value);
102         } else {
103             for (final Enumeration e = root.children(); e.hasMoreElements();) {
104                 final TreeMapNode node = (TreeMapNode) e.nextElement();
105                 setValues(node);
106             }
107         }
108     }
109 
110     /*
111      * (non-Javadoc)
112      *
113      * @see net.sf.jtreemap.swing.ColorProvider#getLegendPanel()
114      */
115     @Override
116     public JPanel getLegendPanel() {
117         if (this.legend == null) {
118             legend = new Legend();
119         }
120         return legend;
121     }
122 
123     /**
124      * Panel with the legend
125      *
126      * @author Laurent Dutheil
127      */
128     protected class Legend extends JPanel {
129         private static final int OFFSET = 3;
130         private static final int X_OFFSET = 15;
131         private static final int INITIAL_X_POS = 20;
132         private static final long serialVersionUID = 4652239358357480113L;
133         private static final int Y = 25;
134         private static final int WIDTH = 10;
135         private static final int HEIGHT = 20;
136 
137         @Override
138         public void paintComponent(final Graphics g) {
139             super.paintComponent(g);
140             if (RandomColorProvider.this.mapping.isEmpty()) {
141                 RandomColorProvider.this.setValues(jTreeMap.getRoot());
142             }
143             final FontMetrics fm = g.getFontMetrics();
144             final int yString = Legend.Y + (Legend.HEIGHT + fm.getAscent() - fm.getDescent()) / 2;
145 
146             int xPosition = INITIAL_X_POS;
147             for (final Value value : RandomColorProvider.this.mapping.keySet()) {
148                 final Color color = RandomColorProvider.this.mapping.get(value);
149                 g.setColor(color);
150                 g.fillRect(xPosition, Legend.Y, Legend.WIDTH, Legend.HEIGHT);
151                 g.setColor(Color.black);
152                 xPosition = xPosition + Legend.WIDTH + OFFSET;
153                 g.drawString(value.getLabel(), xPosition, yString);
154                 xPosition = xPosition + fm.stringWidth(value.getLabel()) + X_OFFSET;
155             }
156 
157             setPreferredSize(new Dimension(xPosition, 2 * Legend.Y + Legend.HEIGHT));
158             setSize(this.getPreferredSize());
159         }
160     }
161 }
162 /*
163  *                 ObjectLab is supporing JTreeMap
164  *
165  * Based in London, we are world leaders in the design and development
166  * of bespoke applications for the securities financing markets.
167  *
168  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
169  *           ___  _     _           _   _          _
170  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
171  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
172  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
173  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
174  *                   |__/
175  *
176  *                     www.ObjectLab.co.uk
177  */