source: trunk/autoquest-jfcmonitor/src/main/java/de/ugoe/cs/autoquest/jfcmonitor/JFCComponent.java @ 928

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