2 |
26 Feb 07 |
jari |
1 |
/* |
2 |
26 Feb 07 |
jari |
Copyright @ 1999-2004, The Institute for Genomic Research (TIGR). |
2 |
26 Feb 07 |
jari |
All rights reserved. |
2 |
26 Feb 07 |
jari |
4 |
*/ |
2 |
26 Feb 07 |
jari |
5 |
/* |
2 |
26 Feb 07 |
jari |
* GONode.java |
2 |
26 Feb 07 |
jari |
7 |
* |
2 |
26 Feb 07 |
jari |
* Created on August 11, 2004, 10:33 AM |
2 |
26 Feb 07 |
jari |
9 |
*/ |
2 |
26 Feb 07 |
jari |
10 |
|
2 |
26 Feb 07 |
jari |
11 |
package org.tigr.microarray.mev.cluster.gui.helpers.ktree; |
2 |
26 Feb 07 |
jari |
12 |
|
2 |
26 Feb 07 |
jari |
13 |
import java.awt.Dimension; |
2 |
26 Feb 07 |
jari |
14 |
import java.awt.Point; |
2 |
26 Feb 07 |
jari |
15 |
import java.awt.Rectangle; |
2 |
26 Feb 07 |
jari |
16 |
import java.io.Serializable; |
2 |
26 Feb 07 |
jari |
17 |
import java.util.Vector; |
2 |
26 Feb 07 |
jari |
//import awt.ktree.ITreeNode; |
2 |
26 Feb 07 |
jari |
//import awt.ktree.ITreeNodeRenderer; |
2 |
26 Feb 07 |
jari |
20 |
|
2 |
26 Feb 07 |
jari |
21 |
/** |
2 |
26 Feb 07 |
jari |
22 |
* |
2 |
26 Feb 07 |
jari |
* @author braisted |
2 |
26 Feb 07 |
jari |
24 |
*/ |
2 |
26 Feb 07 |
jari |
25 |
public class KNodeImpl implements ITreeNode, Serializable { |
2 |
26 Feb 07 |
jari |
26 |
|
2 |
26 Feb 07 |
jari |
27 |
protected int x = 0, y = 0; |
2 |
26 Feb 07 |
jari |
28 |
protected int w = 0, h = 0; |
2 |
26 Feb 07 |
jari |
29 |
|
2 |
26 Feb 07 |
jari |
30 |
protected Point location; |
2 |
26 Feb 07 |
jari |
31 |
|
2 |
26 Feb 07 |
jari |
32 |
protected boolean verboseRendering = true; |
2 |
26 Feb 07 |
jari |
33 |
|
2 |
26 Feb 07 |
jari |
34 |
protected ITreeNode [] children; |
2 |
26 Feb 07 |
jari |
35 |
protected ITreeNode [] parents; |
2 |
26 Feb 07 |
jari |
36 |
|
2 |
26 Feb 07 |
jari |
37 |
protected int level; |
2 |
26 Feb 07 |
jari |
38 |
|
2 |
26 Feb 07 |
jari |
/** Creates a new instance of GONode */ |
2 |
26 Feb 07 |
jari |
40 |
public KNodeImpl() { |
2 |
26 Feb 07 |
jari |
41 |
children = new ITreeNode[0]; |
2 |
26 Feb 07 |
jari |
42 |
parents = new ITreeNode[0]; |
2 |
26 Feb 07 |
jari |
43 |
} |
2 |
26 Feb 07 |
jari |
44 |
|
2 |
26 Feb 07 |
jari |
/** Creates a new instance of GONode */ |
2 |
26 Feb 07 |
jari |
46 |
public KNodeImpl(int xLoc, int yLoc) { |
2 |
26 Feb 07 |
jari |
47 |
location = new Point(xLoc, yLoc); |
2 |
26 Feb 07 |
jari |
48 |
x = xLoc; |
2 |
26 Feb 07 |
jari |
49 |
y = yLoc; |
2 |
26 Feb 07 |
jari |
50 |
children = new ITreeNode[0]; |
2 |
26 Feb 07 |
jari |
51 |
parents = new ITreeNode[0]; |
2 |
26 Feb 07 |
jari |
52 |
} |
2 |
26 Feb 07 |
jari |
53 |
|
2 |
26 Feb 07 |
jari |
54 |
/************************************** |
2 |
26 Feb 07 |
jari |
* ITreeNode Methods |
2 |
26 Feb 07 |
jari |
56 |
*/ |
2 |
26 Feb 07 |
jari |
57 |
|
2 |
26 Feb 07 |
jari |
58 |
public boolean contains(int xLoc, int yLoc) { |
2 |
26 Feb 07 |
jari |
59 |
return (xLoc >= x && xLoc <= x+w && yLoc > y && yLoc <= y+h); |
2 |
26 Feb 07 |
jari |
60 |
} |
2 |
26 Feb 07 |
jari |
61 |
|
2 |
26 Feb 07 |
jari |
62 |
public int getLevel() { |
2 |
26 Feb 07 |
jari |
63 |
return level; |
2 |
26 Feb 07 |
jari |
64 |
} |
2 |
26 Feb 07 |
jari |
65 |
|
2 |
26 Feb 07 |
jari |
66 |
public Point getTopAnchorPoint() { |
2 |
26 Feb 07 |
jari |
67 |
return new Point(x+w/2, y); |
2 |
26 Feb 07 |
jari |
68 |
} |
2 |
26 Feb 07 |
jari |
69 |
|
2 |
26 Feb 07 |
jari |
70 |
public Point getBottomAnchorPoint() { |
2 |
26 Feb 07 |
jari |
71 |
return new Point(x+w/2, y+h); |
2 |
26 Feb 07 |
jari |
72 |
} |
2 |
26 Feb 07 |
jari |
73 |
|
2 |
26 Feb 07 |
jari |
74 |
public ITreeNode[] getParents() { |
2 |
26 Feb 07 |
jari |
75 |
return parents; |
2 |
26 Feb 07 |
jari |
76 |
} |
2 |
26 Feb 07 |
jari |
77 |
|
2 |
26 Feb 07 |
jari |
78 |
public ITreeNode[] getChildren() { |
2 |
26 Feb 07 |
jari |
79 |
return children; |
2 |
26 Feb 07 |
jari |
80 |
} |
2 |
26 Feb 07 |
jari |
81 |
|
2 |
26 Feb 07 |
jari |
82 |
public Point getLocation() { |
2 |
26 Feb 07 |
jari |
83 |
return location; |
2 |
26 Feb 07 |
jari |
84 |
} |
2 |
26 Feb 07 |
jari |
85 |
|
2 |
26 Feb 07 |
jari |
86 |
public int getNodeHeight() { |
2 |
26 Feb 07 |
jari |
87 |
return level; |
2 |
26 Feb 07 |
jari |
88 |
} |
2 |
26 Feb 07 |
jari |
89 |
|
2 |
26 Feb 07 |
jari |
90 |
public boolean isVerboseRendering() { |
2 |
26 Feb 07 |
jari |
91 |
return verboseRendering; |
2 |
26 Feb 07 |
jari |
92 |
} |
2 |
26 Feb 07 |
jari |
93 |
|
2 |
26 Feb 07 |
jari |
94 |
public boolean addChild(ITreeNode child) { |
2 |
26 Feb 07 |
jari |
//check for conatains |
2 |
26 Feb 07 |
jari |
96 |
for(int i = 0; i < children.length; i++) { |
2 |
26 Feb 07 |
jari |
97 |
if(children[i] == child) |
2 |
26 Feb 07 |
jari |
98 |
return false; |
2 |
26 Feb 07 |
jari |
99 |
} |
2 |
26 Feb 07 |
jari |
100 |
ITreeNode [] newNodes = new ITreeNode [children.length+1]; |
2 |
26 Feb 07 |
jari |
101 |
System.arraycopy(children, 0, newNodes, 0, children.length); |
2 |
26 Feb 07 |
jari |
102 |
newNodes[newNodes.length -1] = child; |
2 |
26 Feb 07 |
jari |
103 |
children = newNodes; |
2 |
26 Feb 07 |
jari |
104 |
return true; |
2 |
26 Feb 07 |
jari |
105 |
} |
2 |
26 Feb 07 |
jari |
106 |
|
2 |
26 Feb 07 |
jari |
107 |
public boolean addParent(ITreeNode parent) { |
2 |
26 Feb 07 |
jari |
//check for conatains |
2 |
26 Feb 07 |
jari |
109 |
for(int i = 0; i < parents.length; i++) { |
2 |
26 Feb 07 |
jari |
110 |
if(parents[i] == parent) |
2 |
26 Feb 07 |
jari |
111 |
return false; |
2 |
26 Feb 07 |
jari |
112 |
} |
2 |
26 Feb 07 |
jari |
113 |
ITreeNode [] newNodes = new ITreeNode [parents.length+1]; |
2 |
26 Feb 07 |
jari |
114 |
System.arraycopy(parents, 0, newNodes, 0, parents.length); |
2 |
26 Feb 07 |
jari |
115 |
newNodes[newNodes.length -1] = parent; |
2 |
26 Feb 07 |
jari |
116 |
parents = newNodes; |
2 |
26 Feb 07 |
jari |
117 |
return true; |
2 |
26 Feb 07 |
jari |
118 |
} |
2 |
26 Feb 07 |
jari |
119 |
|
2 |
26 Feb 07 |
jari |
120 |
public void clearChildren() { |
2 |
26 Feb 07 |
jari |
121 |
this.children = new ITreeNode[0]; |
2 |
26 Feb 07 |
jari |
122 |
} |
2 |
26 Feb 07 |
jari |
123 |
|
2 |
26 Feb 07 |
jari |
124 |
public void clearParents() { |
2 |
26 Feb 07 |
jari |
125 |
this.parents = new ITreeNode[0]; |
2 |
26 Feb 07 |
jari |
126 |
} |
2 |
26 Feb 07 |
jari |
127 |
|
2 |
26 Feb 07 |
jari |
128 |
public void setParents(Vector parentVector) { |
2 |
26 Feb 07 |
jari |
129 |
this.parents = new ITreeNode[parentVector.size()]; |
2 |
26 Feb 07 |
jari |
130 |
for(int i = 0; i < parents.length; i++) { |
2 |
26 Feb 07 |
jari |
131 |
parents[i] = (ITreeNode)(parentVector.elementAt(i)); |
2 |
26 Feb 07 |
jari |
132 |
} |
2 |
26 Feb 07 |
jari |
133 |
} |
2 |
26 Feb 07 |
jari |
134 |
|
2 |
26 Feb 07 |
jari |
135 |
public void setChildren(Vector childVector) { |
2 |
26 Feb 07 |
jari |
136 |
this.children = new ITreeNode[childVector.size()]; |
2 |
26 Feb 07 |
jari |
137 |
for(int i = 0; i < children.length; i++) { |
2 |
26 Feb 07 |
jari |
138 |
children[i] = (ITreeNode)(childVector.elementAt(i)); |
2 |
26 Feb 07 |
jari |
139 |
} |
2 |
26 Feb 07 |
jari |
140 |
} |
2 |
26 Feb 07 |
jari |
141 |
|
2 |
26 Feb 07 |
jari |
142 |
public int getMaxPathLengthToRoot() { |
2 |
26 Feb 07 |
jari |
143 |
int maxPathLength = 0; |
2 |
26 Feb 07 |
jari |
144 |
if(parents == null) |
2 |
26 Feb 07 |
jari |
145 |
return 0; |
2 |
26 Feb 07 |
jari |
146 |
else { |
2 |
26 Feb 07 |
jari |
147 |
for(int i = 0; i < parents.length; i++) { |
2 |
26 Feb 07 |
jari |
148 |
maxPathLength = Math.max(maxPathLength, parents[i].getMaxPathLengthToRoot()); |
2 |
26 Feb 07 |
jari |
149 |
} |
2 |
26 Feb 07 |
jari |
150 |
} |
2 |
26 Feb 07 |
jari |
151 |
|
2 |
26 Feb 07 |
jari |
152 |
return maxPathLength+1; |
2 |
26 Feb 07 |
jari |
153 |
} |
2 |
26 Feb 07 |
jari |
154 |
|
2 |
26 Feb 07 |
jari |
155 |
public int getMinPathLengthToRoot() { |
2 |
26 Feb 07 |
jari |
156 |
int minPathLength = Integer.MAX_VALUE; |
2 |
26 Feb 07 |
jari |
157 |
if(parents == null) |
2 |
26 Feb 07 |
jari |
158 |
return 0; |
2 |
26 Feb 07 |
jari |
159 |
else { |
2 |
26 Feb 07 |
jari |
160 |
for(int i = 0; i < parents.length; i++) { |
2 |
26 Feb 07 |
jari |
161 |
minPathLength = Math.min(minPathLength, parents[i].getMinPathLengthToRoot()); |
2 |
26 Feb 07 |
jari |
162 |
} |
2 |
26 Feb 07 |
jari |
163 |
} |
2 |
26 Feb 07 |
jari |
164 |
return minPathLength; |
2 |
26 Feb 07 |
jari |
165 |
} |
2 |
26 Feb 07 |
jari |
166 |
|
2 |
26 Feb 07 |
jari |
167 |
|
2 |
26 Feb 07 |
jari |
168 |
public Dimension getSize() { |
2 |
26 Feb 07 |
jari |
169 |
return new Dimension(w,h); |
2 |
26 Feb 07 |
jari |
170 |
} |
2 |
26 Feb 07 |
jari |
171 |
|
2 |
26 Feb 07 |
jari |
172 |
public int getWidth() { |
2 |
26 Feb 07 |
jari |
173 |
return w; |
2 |
26 Feb 07 |
jari |
174 |
} |
2 |
26 Feb 07 |
jari |
175 |
|
2 |
26 Feb 07 |
jari |
176 |
public int getHeight() { |
2 |
26 Feb 07 |
jari |
177 |
return h; |
2 |
26 Feb 07 |
jari |
178 |
} |
2 |
26 Feb 07 |
jari |
179 |
|
2 |
26 Feb 07 |
jari |
180 |
public boolean hasChildren() { |
2 |
26 Feb 07 |
jari |
181 |
return children.length > 0; |
2 |
26 Feb 07 |
jari |
182 |
} |
2 |
26 Feb 07 |
jari |
183 |
|
2 |
26 Feb 07 |
jari |
184 |
public boolean hasParents() { |
2 |
26 Feb 07 |
jari |
185 |
return parents.length > 0; |
2 |
26 Feb 07 |
jari |
186 |
} |
2 |
26 Feb 07 |
jari |
187 |
|
2 |
26 Feb 07 |
jari |
188 |
public void setLevel(int level) { |
2 |
26 Feb 07 |
jari |
189 |
this.level = level; |
2 |
26 Feb 07 |
jari |
190 |
} |
2 |
26 Feb 07 |
jari |
191 |
|
2 |
26 Feb 07 |
jari |
192 |
public void getAncestors(Vector ancAccumulator) { |
2 |
26 Feb 07 |
jari |
193 |
if(this.parents.length == 0) |
2 |
26 Feb 07 |
jari |
194 |
return; |
2 |
26 Feb 07 |
jari |
195 |
else { |
2 |
26 Feb 07 |
jari |
//first add all parents |
2 |
26 Feb 07 |
jari |
197 |
for(int i = 0; i < parents.length; i++) { |
2 |
26 Feb 07 |
jari |
198 |
if(!ancAccumulator.contains(parents[i])) |
2 |
26 Feb 07 |
jari |
199 |
ancAccumulator.addElement(parents[i]); |
2 |
26 Feb 07 |
jari |
200 |
} |
2 |
26 Feb 07 |
jari |
//recurse on all parents |
2 |
26 Feb 07 |
jari |
202 |
for(int i = 0; i < parents.length; i++) { |
2 |
26 Feb 07 |
jari |
203 |
parents[i].getAncestors(ancAccumulator); |
2 |
26 Feb 07 |
jari |
204 |
} |
2 |
26 Feb 07 |
jari |
205 |
} |
2 |
26 Feb 07 |
jari |
206 |
} |
2 |
26 Feb 07 |
jari |
207 |
|
2 |
26 Feb 07 |
jari |
208 |
public void getSuccessors(Vector sucAccumulator) { |
2 |
26 Feb 07 |
jari |
209 |
if(this.children.length == 0) |
2 |
26 Feb 07 |
jari |
210 |
return; |
2 |
26 Feb 07 |
jari |
211 |
else { |
2 |
26 Feb 07 |
jari |
//first add all parents |
2 |
26 Feb 07 |
jari |
213 |
for(int i = 0; i < children.length; i++) { |
2 |
26 Feb 07 |
jari |
214 |
if(!sucAccumulator.contains(children[i])) |
2 |
26 Feb 07 |
jari |
215 |
sucAccumulator.addElement(children[i]); |
2 |
26 Feb 07 |
jari |
216 |
} |
2 |
26 Feb 07 |
jari |
//recurse on all children |
2 |
26 Feb 07 |
jari |
218 |
for(int i = 0; i < children.length; i++) { |
2 |
26 Feb 07 |
jari |
219 |
children[i].getSuccessors(sucAccumulator); |
2 |
26 Feb 07 |
jari |
220 |
} |
2 |
26 Feb 07 |
jari |
221 |
} |
2 |
26 Feb 07 |
jari |
222 |
} |
2 |
26 Feb 07 |
jari |
223 |
|
2 |
26 Feb 07 |
jari |
224 |
public boolean contains(Rectangle rect) { |
2 |
26 Feb 07 |
jari |
225 |
|
2 |
26 Feb 07 |
jari |
// return ( contains(rect.x, rect.y) || contains(rect.x, rect.y+rect.height) |
2 |
26 Feb 07 |
jari |
// || contains(rect.x+rect.width, rect.y) || contains(rect.x+rect.width, rect.y+rect.height)); |
2 |
26 Feb 07 |
jari |
228 |
return rect.contains(x,y,w,h); |
2 |
26 Feb 07 |
jari |
229 |
} |
2 |
26 Feb 07 |
jari |
230 |
|
2 |
26 Feb 07 |
jari |
231 |
} |