1 |
|
---|
2 | package de.ugoe.cs.quest.jfcmonitor;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.Container;
|
---|
6 | import java.awt.event.ContainerListener;
|
---|
7 | import java.beans.PropertyChangeListener;
|
---|
8 | import java.io.File;
|
---|
9 | import java.lang.reflect.InvocationTargetException;
|
---|
10 | import java.lang.reflect.Method;
|
---|
11 | import java.security.InvalidParameterException;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.LinkedList;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Map;
|
---|
17 |
|
---|
18 | import javax.accessibility.AccessibleContext;
|
---|
19 |
|
---|
20 | import de.ugoe.cs.util.StringTools;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * <p>
|
---|
24 | * This class manages information about the current GUI. It always contains the current GUI
|
---|
25 | * hierarchy.
|
---|
26 | * </p>
|
---|
27 | *
|
---|
28 | * @author Steffen Herbold
|
---|
29 | * @author Fabian Glaser
|
---|
30 | * @version 1.0
|
---|
31 | */
|
---|
32 | public class JFCComponent {
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * <p>
|
---|
36 | * Map of all known GUI components.
|
---|
37 | * </p>
|
---|
38 | */
|
---|
39 | private static Map<Component, JFCComponent> knownComponents =
|
---|
40 | new HashMap<Component, JFCComponent>();
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * <p>
|
---|
44 | * List of PropertyChangeListeners that are registered on the components.
|
---|
45 | * </p>
|
---|
46 | */
|
---|
47 | private static List<PropertyChangeListener> propertyChangeListeners =
|
---|
48 | new ArrayList<PropertyChangeListener>();
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * <p>
|
---|
52 | * List of ContainerListeners that are registered on the components.
|
---|
53 | * </p>
|
---|
54 | */
|
---|
55 | private static List<ContainerListener> containerListeners =
|
---|
56 | new ArrayList<ContainerListener>();
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * <p>
|
---|
60 | * Adds a AWT component to the GUI hierarchy. If the component already exists in the hierarchy,
|
---|
61 | * it is not added a second time.
|
---|
62 | * </p>
|
---|
63 | *
|
---|
64 | * @param component
|
---|
65 | * component that is added
|
---|
66 | */
|
---|
67 | public static void add(Component component) {
|
---|
68 | add(component, find(component.getParent()));
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * <p>
|
---|
73 | * Adds a new PropertyChangeListener to the components.
|
---|
74 | * </p>
|
---|
75 | * @param list
|
---|
76 | * the PropertyChangeListener
|
---|
77 | */
|
---|
78 | public static void addPropertyChangeListener(PropertyChangeListener list){
|
---|
79 | propertyChangeListeners.add(list);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * <p>
|
---|
84 | * Adds a new ContainerListener to the components.
|
---|
85 | * </p>
|
---|
86 | * @param list
|
---|
87 | * the ContainerListener
|
---|
88 | */
|
---|
89 | public static void addContainerListener(ContainerListener list){
|
---|
90 | containerListeners.add(list);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * <p>
|
---|
95 | * Tests if a component is already known.
|
---|
96 | * </p>
|
---|
97 | * @param component to be tested
|
---|
98 | * @return true, if component is already known, false otherwise.
|
---|
99 | */
|
---|
100 | public static boolean isKnown(Component component){
|
---|
101 | if (knownComponents.containsKey(component))
|
---|
102 | return true;
|
---|
103 | return false;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * <p>
|
---|
108 | * Adds a AWT component to the GUI hierarchy. If the component already exists in the hierarchy,
|
---|
109 | * it is not added a second time.
|
---|
110 | * </p>
|
---|
111 | *
|
---|
112 | * @param component
|
---|
113 | * component that is added
|
---|
114 | * @param parent
|
---|
115 | * parent of the component
|
---|
116 | */
|
---|
117 | public static void add(Component component, JFCComponent parent) {
|
---|
118 | if (!knownComponents.containsKey(component)) {
|
---|
119 | knownComponents.put(component, new JFCComponent(component, parent));
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * <p>
|
---|
125 | * Finds a component in the GUI hierarchy and returns the corresponding JFComponent instance.
|
---|
126 | * Returns null if the component is not found.
|
---|
127 | * </p>
|
---|
128 | *
|
---|
129 | * @param component
|
---|
130 | * component that is searched for
|
---|
131 | * @return corresponding JFComponent instance; null if the compenent is not found
|
---|
132 | */
|
---|
133 | public static JFCComponent find(Component component) {
|
---|
134 | return knownComponents.get(component);
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * <p>
|
---|
139 | * Removes a component from the GUI hierarchy. In case the component is not part of the known
|
---|
140 | * hierachy, nothing happens.
|
---|
141 | * </p>
|
---|
142 | *
|
---|
143 | * @param component
|
---|
144 | * component to be removed
|
---|
145 | */
|
---|
146 | public static void remove(Component component) {
|
---|
147 | JFCComponent jfcComponent = knownComponents.remove(component);
|
---|
148 | if (jfcComponent != null) {
|
---|
149 | jfcComponent.removeFromParent();
|
---|
150 | jfcComponent.removeChildren();
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * <p>
|
---|
156 | * Parent of the GUI component. null means, that the component has no parent.
|
---|
157 | * </p>
|
---|
158 | */
|
---|
159 | private JFCComponent parent = null;
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * <p>
|
---|
163 | * Child components of the component.
|
---|
164 | * </p>
|
---|
165 | */
|
---|
166 | private List<JFCComponent> children = new LinkedList<JFCComponent>();
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * <p>
|
---|
170 | * Reference to the actual GUI component.
|
---|
171 | * </p>
|
---|
172 | */
|
---|
173 | private Component component;
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * <p>
|
---|
177 | * Helper attribute that contains the title of the component. Set by {@link #setTitle()}.
|
---|
178 | * </p>
|
---|
179 | */
|
---|
180 | private String title = null;
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * <p>
|
---|
184 | * Helper attribute that contains the class of the component. Set by {@link #setClass()}.
|
---|
185 | * </p>
|
---|
186 | */
|
---|
187 | private String componentClass = null;
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * <p>
|
---|
191 | * Helper attribute that contains the icon of the component. Set by {@link #setIcon()}.
|
---|
192 | * </p>
|
---|
193 | */
|
---|
194 | private String icon = null;
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * <p>
|
---|
198 | * Helper attribute that contains the icon of the component. Set by {@link #setIndex()}.
|
---|
199 | * </p>
|
---|
200 | */
|
---|
201 | private int index = -1;
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * <p>
|
---|
205 | * Constructor. Creates a new JFCComponent. Only used internally by
|
---|
206 | * {@link #add(Component, JFCComponent)}.
|
---|
207 | * </p>
|
---|
208 | *
|
---|
209 | * @param component
|
---|
210 | * component associated with the JFCComponent
|
---|
211 | * @param parent
|
---|
212 | * parent of the component; null if there is no parent
|
---|
213 | */
|
---|
214 | private JFCComponent(Component component, JFCComponent parent) {
|
---|
215 | if (component == null) {
|
---|
216 | throw new InvalidParameterException("parameter component must not be null");
|
---|
217 | }
|
---|
218 | this.component = component;
|
---|
219 |
|
---|
220 | AccessibleContext context = component.getAccessibleContext();
|
---|
221 | if (context != null){
|
---|
222 | for (PropertyChangeListener listener: propertyChangeListeners)
|
---|
223 | context.addPropertyChangeListener(listener);
|
---|
224 | }
|
---|
225 |
|
---|
226 | this.parent = parent;
|
---|
227 | if (parent != null) {
|
---|
228 | parent.addChild(this);
|
---|
229 | }
|
---|
230 |
|
---|
231 | if (component instanceof Container) {
|
---|
232 | for (ContainerListener listener: containerListeners)
|
---|
233 | ((Container) component).addContainerListener(listener);
|
---|
234 |
|
---|
235 | for (Component childComponent : ((Container) component).getComponents()) {
|
---|
236 | add(childComponent, this);
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * <p>
|
---|
243 | * Adds a child component to the current component.
|
---|
244 | * </p>
|
---|
245 | *
|
---|
246 | * @param child
|
---|
247 | * child component to be added
|
---|
248 | */
|
---|
249 | private void addChild(JFCComponent child) {
|
---|
250 | children.add(child);
|
---|
251 | }
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * <p>
|
---|
255 | * Returns an XML representation of the component.
|
---|
256 | * </p>
|
---|
257 | *
|
---|
258 | * @return XLM snippet
|
---|
259 | */
|
---|
260 | public String getXML() {
|
---|
261 | setClass();
|
---|
262 | setIcon();
|
---|
263 | setIndex();
|
---|
264 | setTitle();
|
---|
265 | StringBuilder builder = new StringBuilder();
|
---|
266 | builder.append("<component");
|
---|
267 | if (parent != null){
|
---|
268 | if (!JFCComponent.isKnown(parent.component))
|
---|
269 | throw new AssertionError("Referenced parent is not known.");
|
---|
270 | builder.append(" parent=\"" + Integer.toHexString(parent.component.hashCode()) + "\"");
|
---|
271 | }
|
---|
272 | builder.append(">"+ StringTools.ENDLINE);
|
---|
273 | builder.append(" <param name=\"title\" value=\"" + title + "\" />" + StringTools.ENDLINE);
|
---|
274 | builder.append(" <param name=\"class\" value=\"" + componentClass + "\" />" +
|
---|
275 | StringTools.ENDLINE);
|
---|
276 | builder.append(" <param name=\"icon\" value=\"" + icon + "\" />" + StringTools.ENDLINE);
|
---|
277 | builder.append(" <param name=\"index\" value=\"" + index + "\" />" + StringTools.ENDLINE);
|
---|
278 | builder.append(" <param name=\"hash\" value=\"" +
|
---|
279 | Integer.toHexString(component.hashCode()) + "\" />" + StringTools.ENDLINE);
|
---|
280 | builder.append(getInheritanceTree());
|
---|
281 | builder.append("</component>" + StringTools.ENDLINE);
|
---|
282 | return builder.toString();
|
---|
283 | }
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * <p>
|
---|
287 | * Returns an XML representation of the components children.
|
---|
288 | * </p>
|
---|
289 | * @return XML representation of children
|
---|
290 | */
|
---|
291 | public String printChildren(){
|
---|
292 | StringBuilder builder = new StringBuilder();
|
---|
293 | for (JFCComponent child: children){
|
---|
294 | builder.append(child.getXML());
|
---|
295 | builder.append(child.printChildren());
|
---|
296 | }
|
---|
297 | return builder.toString();
|
---|
298 | }
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * <p>
|
---|
302 | * Removes a child component from the current component.
|
---|
303 | * </p>
|
---|
304 | *
|
---|
305 | * @param child
|
---|
306 | * child component to be removed
|
---|
307 | */
|
---|
308 | private void removeChild(JFCComponent child) {
|
---|
309 | children.remove(child);
|
---|
310 | }
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * <p>
|
---|
314 | * Removes the component from the list of children of its parent.
|
---|
315 | * </p>
|
---|
316 | */
|
---|
317 | private void removeFromParent() {
|
---|
318 | if (parent != null) {
|
---|
319 | parent.removeChild(this);
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | /**
|
---|
324 | * <p>
|
---|
325 | * Triggers the removals of all child components from the GUI hierarchy, i.e., calls
|
---|
326 | * {@link #remove(Component)} for all child components.
|
---|
327 | * </p>
|
---|
328 | */
|
---|
329 | private void removeChildren() {
|
---|
330 | for (JFCComponent child : children) {
|
---|
331 | remove(child.component);
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * <p>
|
---|
337 | * Sets the {@link #title} of the component. The title is defined as follows (first in the list,
|
---|
338 | * that is not null):
|
---|
339 | * <ul>
|
---|
340 | * <li>accessible name of the component if available</li>
|
---|
341 | * <li>{@link #icon} of the component</li>
|
---|
342 | * <li>name of the component</li>
|
---|
343 | * <li>coordinates of the component</li>
|
---|
344 | * </ul>
|
---|
345 | * </p>
|
---|
346 | */
|
---|
347 | private void setTitle() {
|
---|
348 | title = null; // reset title
|
---|
349 |
|
---|
350 | AccessibleContext accessibleContext = component.getAccessibleContext();
|
---|
351 | if (accessibleContext != null) {
|
---|
352 | title = accessibleContext.getAccessibleName();
|
---|
353 | }
|
---|
354 | if (title == null) {
|
---|
355 | title = icon;
|
---|
356 | }
|
---|
357 | if (title == null) {
|
---|
358 | title = component.getName();
|
---|
359 | }
|
---|
360 | if (title == null) {
|
---|
361 | // use coordinates as last resort
|
---|
362 | title = "Pos(" + component.getX() + "," + component.getY() + ")";
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * <p>
|
---|
368 | * Sets the {@link #componentClass} of the component.
|
---|
369 | * </p>
|
---|
370 | */
|
---|
371 | private void setClass() {
|
---|
372 | componentClass = component.getClass().getName();
|
---|
373 | }
|
---|
374 |
|
---|
375 | /**
|
---|
376 | * <p>
|
---|
377 | * Sets the {@link #icon} of the component.
|
---|
378 | * </p>
|
---|
379 | */
|
---|
380 | private void setIcon() {
|
---|
381 | icon = null; // reset icon
|
---|
382 |
|
---|
383 | Method getIconMethod;
|
---|
384 | try {
|
---|
385 | getIconMethod = component.getClass().getMethod("getIcon", new Class[0]);
|
---|
386 | if (getIconMethod != null) {
|
---|
387 | Object iconObject = getIconMethod.invoke(component, new Object[] { });
|
---|
388 | if (iconObject != null) {
|
---|
389 | String iconPath = iconObject.toString();
|
---|
390 | if (!iconPath.contains("@")) {
|
---|
391 | System.out.println("iconPath");
|
---|
392 | String[] splitResult =
|
---|
393 | iconPath.split(File.separatorChar == '\\' ? "\\\\" : File.separator);
|
---|
394 | icon = splitResult[splitResult.length - 1];
|
---|
395 | }
|
---|
396 | }
|
---|
397 | }
|
---|
398 | }
|
---|
399 | catch (SecurityException e) {}
|
---|
400 | catch (NoSuchMethodException e) {}
|
---|
401 | catch (IllegalArgumentException e) {}
|
---|
402 | catch (IllegalAccessException e) {}
|
---|
403 | catch (InvocationTargetException e) {
|
---|
404 | System.err.println("Found method with name " + "getIcon" + " but could not access it.");
|
---|
405 | }
|
---|
406 | }
|
---|
407 |
|
---|
408 | /**
|
---|
409 | * <p>
|
---|
410 | * Sets the {@link #index} of the component as the index in the parent, if it is accessible.
|
---|
411 | * </p>
|
---|
412 | */
|
---|
413 | private void setIndex() {
|
---|
414 | index = -1; // reset index
|
---|
415 |
|
---|
416 | AccessibleContext accessibleContext = component.getAccessibleContext();
|
---|
417 | if (accessibleContext != null) {
|
---|
418 | index = accessibleContext.getAccessibleIndexInParent();
|
---|
419 | }
|
---|
420 | }
|
---|
421 |
|
---|
422 | /**
|
---|
423 | * <p>
|
---|
424 | * Constructs a string that represents inheritance of component.
|
---|
425 | * </p>
|
---|
426 | * @return
|
---|
427 | */
|
---|
428 | private String getInheritanceTree(){
|
---|
429 | StringBuilder builder = new StringBuilder();
|
---|
430 | Class<? extends Object> classobject = component.getClass();
|
---|
431 | while(classobject.getSuperclass() != null){
|
---|
432 | classobject = classobject.getSuperclass();
|
---|
433 | builder.append(" <ancestor name=\"");
|
---|
434 | builder.append(classobject.getName());
|
---|
435 | builder.append("\" />" + StringTools.ENDLINE);
|
---|
436 | }
|
---|
437 | return builder.toString();
|
---|
438 | }
|
---|
439 | }
|
---|