|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ComponentNode.java | 0% | 0% | 0% | 0% |
|
1 |
package junit.extensions.jfcunit.tools;
|
|
2 |
|
|
3 |
import java.awt.Component;
|
|
4 |
import java.awt.Container;
|
|
5 |
import java.awt.Frame;
|
|
6 |
import java.awt.Window;
|
|
7 |
|
|
8 |
import java.util.Enumeration;
|
|
9 |
import java.util.Vector;
|
|
10 |
|
|
11 |
import javax.swing.tree.TreeNode;
|
|
12 |
|
|
13 |
|
|
14 |
/**
|
|
15 |
* Tree node for the ComponentBrowser application.
|
|
16 |
*
|
|
17 |
* @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
|
|
18 |
*/
|
|
19 |
public class ComponentNode implements TreeNode { |
|
20 |
/**
|
|
21 |
* Component.
|
|
22 |
*/
|
|
23 |
private Component m_component;
|
|
24 |
|
|
25 |
/**
|
|
26 |
* Parent node.
|
|
27 |
*/
|
|
28 |
private ComponentNode m_parent;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* Constructor.
|
|
32 |
*
|
|
33 |
* @param parent node of parent.
|
|
34 |
* @param comp Component to be held by the node.
|
|
35 |
*/
|
|
36 | 0 |
public ComponentNode(final ComponentNode parent, final Component comp) {
|
37 | 0 |
m_parent = parent; |
38 | 0 |
m_component = comp; |
39 |
} |
|
40 |
|
|
41 |
/**
|
|
42 |
* Can the node support children.
|
|
43 |
*
|
|
44 |
* @return true if the node Component is a Container.
|
|
45 |
*/
|
|
46 | 0 |
public boolean getAllowsChildren() { |
47 | 0 |
return (m_component == null) || m_component instanceof Container; |
48 |
} |
|
49 |
|
|
50 |
/**
|
|
51 |
* Get the specified child.
|
|
52 |
*
|
|
53 |
* @param childIndex The index of the child node to be returned.
|
|
54 |
* @return TreeNode The child node in the tree.
|
|
55 |
*/
|
|
56 | 0 |
public TreeNode getChildAt(final int childIndex) { |
57 | 0 |
Component child; |
58 |
|
|
59 | 0 |
if (m_component == null) { |
60 | 0 |
child = (Window) getAllWindows().elementAt(childIndex); |
61 | 0 |
} else if (m_component instanceof Container) { |
62 | 0 |
Container container = (Container) m_component; |
63 | 0 |
int cnt = container.getComponentCount();
|
64 |
|
|
65 | 0 |
if ((childIndex >= cnt) && container instanceof Window) { |
66 | 0 |
Window[] windows = ((Window) container).getOwnedWindows(); |
67 | 0 |
child = windows[childIndex - cnt]; |
68 |
} else {
|
|
69 | 0 |
child = container.getComponent(childIndex); |
70 |
} |
|
71 |
} else {
|
|
72 | 0 |
throw new RuntimeException("no child with index " + childIndex); |
73 |
} |
|
74 |
|
|
75 | 0 |
return new ComponentNode(this, child); |
76 |
} |
|
77 |
|
|
78 |
/**
|
|
79 |
* Get the number of children.
|
|
80 |
*
|
|
81 |
* @return int Number of children.
|
|
82 |
*/
|
|
83 | 0 |
public int getChildCount() { |
84 | 0 |
int num = 0;
|
85 |
|
|
86 | 0 |
if (m_component == null) { |
87 | 0 |
num = getAllWindows().size(); |
88 | 0 |
} else if (m_component instanceof Container) { |
89 | 0 |
Container container = (Container) m_component; |
90 | 0 |
num = container.getComponentCount(); |
91 |
|
|
92 | 0 |
if (container instanceof Window) { |
93 | 0 |
num += ((Window) container).getOwnedWindows().length; |
94 |
} |
|
95 |
} |
|
96 |
|
|
97 | 0 |
return num;
|
98 |
} |
|
99 |
|
|
100 |
/**
|
|
101 |
* Get the component held by this node of the tree.
|
|
102 |
*
|
|
103 |
* @return Component
|
|
104 |
*/
|
|
105 | 0 |
public Component getComponent() {
|
106 | 0 |
return m_component;
|
107 |
} |
|
108 |
|
|
109 |
/**
|
|
110 |
* Get the index of the node given.
|
|
111 |
*
|
|
112 |
* @param node TreeNode for which the index should be given.
|
|
113 |
* @return int Returns the index of the node or -1 if the
|
|
114 |
* node is not found.
|
|
115 |
*/
|
|
116 | 0 |
public int getIndex(final TreeNode node) { |
117 | 0 |
int index = -1;
|
118 | 0 |
int num = getChildCount();
|
119 |
|
|
120 | 0 |
for (int i = 0; i < num; i++) { |
121 | 0 |
if (node.equals(getChildAt(i))) {
|
122 | 0 |
index = i; |
123 |
|
|
124 | 0 |
break;
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 | 0 |
return index;
|
129 |
} |
|
130 |
|
|
131 |
/**
|
|
132 |
* Is the node a leaf.
|
|
133 |
*
|
|
134 |
* @return boolean True if the node has no children.
|
|
135 |
*/
|
|
136 | 0 |
public boolean isLeaf() { |
137 | 0 |
return !getAllowsChildren() || (getChildCount() == 0);
|
138 |
} |
|
139 |
|
|
140 |
/**
|
|
141 |
* Get the parent node.
|
|
142 |
*
|
|
143 |
* @return TreeNode Reference to parent.
|
|
144 |
*/
|
|
145 | 0 |
public TreeNode getParent() {
|
146 | 0 |
return m_parent;
|
147 |
} |
|
148 |
|
|
149 |
/**
|
|
150 |
* Get the children of this node.
|
|
151 |
*
|
|
152 |
* @return Enumeration of the children of type ComponentNode.
|
|
153 |
*/
|
|
154 | 0 |
public Enumeration children() {
|
155 | 0 |
Vector kids = new Vector();
|
156 | 0 |
int num = getChildCount();
|
157 |
|
|
158 | 0 |
for (int i = 0; i < num; i++) { |
159 | 0 |
kids.add(getChildAt(i)); |
160 |
} |
|
161 |
|
|
162 | 0 |
return kids.elements();
|
163 |
} |
|
164 |
|
|
165 |
/**
|
|
166 |
* Check equality.
|
|
167 |
*
|
|
168 |
* @param other Object to be compared.
|
|
169 |
* @return boolean true if the objects are equal.
|
|
170 |
*/
|
|
171 | 0 |
public boolean equals(final Object other) { |
172 | 0 |
return ((other instanceof ComponentNode) |
173 |
&& (m_component == ((ComponentNode) other).m_component)); |
|
174 |
} |
|
175 |
|
|
176 |
/**
|
|
177 |
* The hashCode of this node.
|
|
178 |
*
|
|
179 |
* @return int hashCode of the referenced Component or 1
|
|
180 |
* if no component is referenced (ex. Top of Tree).
|
|
181 |
*/
|
|
182 | 0 |
public int hashCode() { |
183 | 0 |
if (m_component == null) { |
184 | 0 |
return 1;
|
185 |
} |
|
186 |
|
|
187 | 0 |
return m_component.hashCode();
|
188 |
} |
|
189 |
|
|
190 |
/**
|
|
191 |
* Generate a description of the object.
|
|
192 |
*
|
|
193 |
* @return String description of this object.
|
|
194 |
*/
|
|
195 | 0 |
public String toString() {
|
196 | 0 |
if (m_component == null) { |
197 | 0 |
return "All Frames"; |
198 |
} |
|
199 |
|
|
200 |
// mComponent is not null
|
|
201 | 0 |
if (m_component.getName() == null) { |
202 | 0 |
return (m_component.getClass().getName() + " : " |
203 |
+ m_component.hashCode()); |
|
204 |
} |
|
205 |
|
|
206 | 0 |
return m_component.getName();
|
207 |
} |
|
208 |
|
|
209 |
/**
|
|
210 |
* This method is used to build a Vector of all open windows (Frames)
|
|
211 |
* after filtering the ComponentBrowser window.
|
|
212 |
*
|
|
213 |
* @return Vector A list of all windows excluding the ComponentBrowser window
|
|
214 |
*/
|
|
215 | 0 |
private Vector getAllWindows() {
|
216 | 0 |
Window[] allWindows = Frame.getFrames(); |
217 | 0 |
Vector filteredWindows = new Vector(allWindows.length);
|
218 |
|
|
219 | 0 |
if ((allWindows == null) || (allWindows.length == 0)) { |
220 | 0 |
filteredWindows.trimToSize(); |
221 |
|
|
222 | 0 |
return filteredWindows;
|
223 |
} |
|
224 |
|
|
225 | 0 |
for (int i = 0; i < allWindows.length; i++) { |
226 | 0 |
if ((allWindows[i] != null) |
227 |
&& allWindows[i] instanceof Frame
|
|
228 |
&& allWindows[i].isShowing() |
|
229 |
&& (!ComponentBrowser.TITLE.equals( |
|
230 |
((Frame) allWindows[i]).getTitle()))) { |
|
231 | 0 |
filteredWindows.addElement(allWindows[i]); |
232 |
} |
|
233 |
} |
|
234 |
|
|
235 | 0 |
filteredWindows.trimToSize(); |
236 |
|
|
237 | 0 |
return filteredWindows;
|
238 |
} |
|
239 |
} |
|
240 |
|
|