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$
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;
34  
35  import java.util.Enumeration;
36  import java.util.List;
37  
38  import javax.swing.tree.DefaultMutableTreeNode;
39  
40  /**
41   * Node of a JTreeMap.<BR>
42   *
43   * If the node is a branch, only the label is set.<BR>
44   * If the node is a leaf, we need a label, a weight and a value.
45   * <p>
46   * You can also use a TreeMapNode in a JTree.
47   *
48   * @author Laurent Dutheil
49   */
50  
51  public class TreeMapNode extends DefaultMutableTreeNode {
52      private static final int DEFAULT_BORDER_SIZE = 3;
53  
54      private static final long serialVersionUID = 742372833853976103L;
55  
56      // max border between two nodes of the same level
57      private static int border = DEFAULT_BORDER_SIZE;
58  
59      private int height;
60      private Value value;
61      private double weight = 0.0;
62      private int width;
63      private int x;
64      private int y;
65  
66      /**
67       * Get the max border between two nodes of the same level.
68       *
69       * @return Returns the border.
70       */
71      public static int getBorder() {
72          return TreeMapNode.border;
73      }
74  
75      /**
76       * Set the max border between two nodes of the same level.
77       *
78       * @param border
79       *            The border to set.
80       */
81      public static void setBorder(final int border) {
82          TreeMapNode.border = border;
83      }
84  
85      /**
86       * Constructor for a branch.
87       *
88       * @param label
89       *            label of the branch.
90       */
91      public TreeMapNode(final String label) {
92          super(label);
93          super.allowsChildren = true;
94      }
95  
96      /**
97       * Constructor for a leaf.
98       *
99       * @param label
100      *            label of the leaf.
101      * @param weight
102      *            weight of the leaf (if negative, we take the absolute value).
103      * @param value
104      *            Value associated a la feuille
105      */
106     public TreeMapNode(final String label, final double weight, final Value value) {
107         super(label);
108         // the weight must be positive
109         this.weight = Math.abs(weight);
110         this.value = value;
111         super.allowsChildren = false;
112     }
113 
114     /**
115      * add a new child to the node.
116      *
117      * @param newChild
118      *            new child
119      */
120     public void add(final TreeMapNode newChild) {
121         super.add(newChild);
122         this.setWeight(this.weight + newChild.getWeight());
123     }
124 
125     /**
126      * get the active leaf.<BR>
127      * null if the passed position is not in this tree.
128      *
129      * @param xParam
130      *            x-coordinate
131      * @param yParam
132      *            y-coordinate
133      * @return active leaf
134      */
135     public TreeMapNode getActiveLeaf(final int xParam, final int yParam) {
136 
137         if (this.isLeaf()) {
138             if (xParam >= this.getX() && xParam <= this.getX() + this.getWidth() && yParam >= this.getY()
139                     && yParam <= this.getY() + this.getHeight()) {
140                 return this;
141             }
142         } else {
143             for (final Enumeration e = this.children(); e.hasMoreElements();) {
144                 final TreeMapNode node = (TreeMapNode) e.nextElement();
145                 if (xParam >= node.getX() && xParam <= node.getX() + node.getWidth() && yParam >= node.getY()
146                         && yParam <= node.getY() + node.getHeight()) {
147                     return node.getActiveLeaf(xParam, yParam);
148                 }
149             }
150         }
151         return null;
152     }
153 
154     /**
155      * get the first child which fits the position.<BR>
156      * null if the passed position is not in this tree.
157      *
158      * @param xParam
159      *            x-coordinate
160      * @param yParam
161      *            y-coordinate
162      * @return the first child which fits the position.
163      */
164     public TreeMapNode getChild(final int xParam, final int yParam) {
165         if (!this.isLeaf()) {
166             for (final Enumeration e = this.children(); e.hasMoreElements();) {
167                 final TreeMapNode node = (TreeMapNode) e.nextElement();
168                 if (xParam >= node.getX() && xParam <= node.getX() + node.getWidth() && yParam >= node.getY()
169                         && yParam <= node.getY() + node.getHeight()) {
170                     return node;
171                 }
172             }
173 
174         }
175         return null;
176     }
177 
178     /**
179      * get a List with the children.
180      *
181      * @return List with the children
182      */
183     @SuppressWarnings("unchecked")
184     public List<TreeMapNode> getChildren() {
185         return this.children;
186     }
187 
188     /**
189      * get the height.
190      *
191      * @return the height
192      */
193     public int getHeight() {
194         return this.height;
195     }
196 
197     /**
198      * get the label.
199      *
200      * @return the label
201      */
202     public String getLabel() {
203         return getUserObject() != null ? getUserObject().toString() : "";
204     }
205 
206     /**
207      * get the label of the Value.
208      *
209      * @return the label of the Value
210      */
211     public String getLabelValue() {
212         return value != null ? this.value.getLabel() : "";
213     }
214 
215     /**
216      * get the Value.
217      *
218      * @return the value
219      */
220     public Value getValue() {
221         return this.value;
222     }
223 
224     /**
225      * get the double Value.
226      *
227      * @return the double value
228      */
229     public double getDoubleValue() {
230         return this.value.getValue();
231     }
232 
233     /**
234      * get the weight.
235      *
236      * @return the weight
237      */
238     public double getWeight() {
239         return this.weight;
240     }
241 
242     /**
243      * get the width.
244      *
245      * @return the width
246      */
247     public int getWidth() {
248         return this.width;
249     }
250 
251     /**
252      * get the x-coordinate.
253      *
254      * @return the x-coordinate
255      */
256     public int getX() {
257         return this.x;
258     }
259 
260     /**
261      * get the y-coordinate.
262      *
263      * @return the y-coordinate
264      */
265     public int getY() {
266         return this.y;
267     }
268 
269     /**
270      * set the position and the size.
271      *
272      * @param xParam
273      *            x-coordinate
274      * @param yParam
275      *            y-coordinate
276      * @param widthParam
277      *            the new width
278      * @param heightParam
279      *            the new height
280      */
281     public void setDimension(final int xParam, final int yParam, final int widthParam, final int heightParam) {
282         this.x = xParam;
283         this.y = yParam;
284         this.width = widthParam;
285         this.height = heightParam;
286     }
287 
288     /**
289      * set the height.
290      *
291      * @param height
292      *            la nouvelle valeur de height
293      */
294     public void setHeight(final int height) {
295         this.height = height;
296     }
297 
298     /**
299      * set the label.
300      *
301      * @param label
302      *            the new label
303      */
304     public void setLabel(final String label) {
305         this.userObject = label;
306     }
307 
308     /**
309      * set the position.
310      *
311      * @param xParam
312      *            x-coordinate
313      * @param yParam
314      *            y-coordinate
315      */
316     public void setPosition(final int xParam, final int yParam) {
317         this.x = xParam;
318         this.y = yParam;
319     }
320 
321     /**
322      * set size.
323      *
324      * @param widthParam
325      *            the new width
326      * @param heightParam
327      *            the new height
328      */
329     public void setSize(final int widthParam, final int heightParam) {
330         this.width = widthParam;
331         this.height = heightParam;
332     }
333 
334     /**
335      * set the Value.
336      *
337      * @param value
338      *            the new Value
339      */
340     public void setValue(final Value value) {
341         this.value = value;
342     }
343 
344     /**
345      * set the weight of the node and update the parents.
346      *
347      * @param weight
348      *            the new weight
349      */
350     public void setWeight(final double weight) {
351         final double newWeight = Math.abs(weight);
352         if (this.parent != null) {
353             ((TreeMapNode) this.parent).setWeight(((TreeMapNode) this.parent).weight - this.weight + newWeight);
354         }
355         this.weight = newWeight;
356     }
357 
358     /**
359      * set the width.
360      *
361      * @param width
362      *            la nouvelle valeur de width
363      */
364     public void setWidth(final int width) {
365         this.width = width;
366     }
367 
368     /**
369      * set the x-coordinate.
370      *
371      * @param x
372      *            the new x-coordinate
373      */
374     public void setX(final int x) {
375         this.x = x;
376     }
377 
378     /**
379      * set the y-coordinate.
380      *
381      * @param y
382      *            the new y-coordinate
383      */
384     public void setY(final int y) {
385         this.y = y;
386     }
387 }
388 /*
389  *                 ObjectLab is supporing JTreeMap
390  *
391  * Based in London, we are world leaders in the design and development
392  * of bespoke applications for the securities financing markets.
393  *
394  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
395  *           ___  _     _           _   _          _
396  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
397  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
398  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
399  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
400  *                   |__/
401  *
402  *                     www.ObjectLab.co.uk
403  */