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