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