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.io.Serializable;
36
37 /**
38 * Tree builder for a JTreeMap.
39 *
40 * @author Laurent Dutheil
41 */
42
43 public class TreeMapNodeBuilder implements Serializable {
44 /**
45 *
46 */
47 private static final long serialVersionUID = -1340499387405510692L;
48
49 private TreeMapNode root;
50
51 /**
52 * Add a branch to the tree. <BR>
53 * If the parent is null, the build node become the root if and only if the
54 * tree have no root yet. If the parent is null and if the root is already
55 * build, the node will NOT be added to the tree.
56 *
57 * @param label
58 * label of the node
59 * @param parent
60 * father of the node
61 * @return the created node
62 */
63 public TreeMapNode buildBranch(final String label, final TreeMapNode parent) {
64 final TreeMapNode node = new TreeMapNode(label);
65 if (parent != null) {
66 parent.add(node);
67 } else if (this.root == null) {
68 this.root = node;
69 }
70 return node;
71 }
72
73 /**
74 * add a leaf to the tree. <BR>
75 * If the parent is null, the build node become the root if and only if the
76 * tree have no root yet. If the parent is null and if the root is already
77 * build, the node will NOT be added to the tree.
78 *
79 * @param label
80 * label of the leaf
81 * @param weight
82 * weight of the leaf
83 * @param value
84 * Value of the leaf
85 * @param parent
86 * father of the leaf
87 * @return the created node
88 */
89 public TreeMapNode buildLeaf(final String label, final double weight, final Value value, final TreeMapNode parent) {
90 final TreeMapNode node = new TreeMapNode(label, weight, value);
91 if (parent != null) {
92 parent.add(node);
93 } else if (this.root == null) {
94 this.root = node;
95 }
96 return node;
97 }
98
99 /**
100 * get the build tree.
101 *
102 * @return the root of the tree
103 */
104 public TreeMapNode getRoot() {
105 return this.root;
106 }
107 }
108 /*
109 * ObjectLab is supporing JTreeMap
110 *
111 * Based in London, we are world leaders in the design and development
112 * of bespoke applications for the securities financing markets.
113 *
114 * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
115 * ___ _ _ _ _ _
116 * / _ \| |__ (_) ___ ___| |_| | __ _| |__
117 * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \
118 * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) |
119 * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
120 * |__/
121 *
122 * www.ObjectLab.co.uk
123 */