source: trunk/autoquest-core-events/src/main/java/de/ugoe/cs/autoquest/eventcore/guimodel/GUIModelHelpers.java @ 2252

Last change on this file since 2252 was 1399, checked in by pharms, 10 years ago
  • centralized determination of a common parent GUI element of several GUI elements as well as the calculation of the distance of two GUI elements in a GUI element tree
File size: 4.3 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.eventcore.guimodel;
16
17import java.util.ArrayList;
18import java.util.List;
19
20/**
21 * <p>
22 * convenience methods for working with GUI elements. E.g. to determine, if two GUI elements have a
23 * common parent on their parent hierarchies.
24 * </p>
25 *
26 * @author Patrick Harms, Ralph Krimmel
27 */
28public class GUIModelHelpers {
29
30    /**
31     * <p>
32     * return a common denominator for the provided list of GUI elements, i.e. a GUI element, that
33     * is part of the parent GUI hiearchy of all GUI elements in the list. If there is no common
34     * denominator, the method returns null.
35     * </p>
36     *
37     * @param guiElements the GUI elements to get the common denominator of
38     */
39    public static IGUIElement getCommonDenominator(IGUIElement... guiElements) {
40        IGUIElement commonDenominator = null;
41
42        if (guiElements.length > 0) {
43            List<IGUIElement> commonDenominatorPath = new ArrayList<IGUIElement>();
44
45            // create a reference list using the first GUI element
46            IGUIElement guiElement = guiElements[0];
47            while (guiElement != null) {
48                commonDenominatorPath.add(0, guiElement);
49                guiElement = guiElement.getParent();
50            }
51
52            if (commonDenominatorPath.size() == 0) {
53                return null;
54            }
55
56            // for each other GUI element, check the reference list for the first element in the
57            // path, that is not common to the current one, and delete it as well as it subsequent
58            // siblings
59            List<IGUIElement> currentPath = new ArrayList<IGUIElement>();
60            for (int i = 1; i < guiElements.length; i++) {
61                currentPath.clear();
62                guiElement = guiElements[i];
63                while (guiElement != null) {
64                    currentPath.add(0, guiElement);
65                    guiElement = guiElement.getParent();
66                }
67
68                // determine the index of the first unequal path element
69                int index = 0;
70                while ((index < commonDenominatorPath.size()) && (index < currentPath.size()) &&
71                        commonDenominatorPath.get(index).equals(currentPath.get(index)))
72                {
73                    index++;
74                }
75
76                // remove all elements from the common denonimator path, that do
77                // not match
78                while (index < commonDenominatorPath.size()) {
79                    commonDenominatorPath.remove(index);
80                }
81            }
82
83            if (commonDenominatorPath.size() > 0) {
84                commonDenominator = commonDenominatorPath.get(commonDenominatorPath.size() - 1);
85            }
86        }
87       
88        return commonDenominator;
89    }
90
91    /**
92     * <p>
93     * determines the number of hops through the GUI element hierarchy to get from one GUI element
94     * to another.
95     * </p>
96     *
97     * @param first  the first GUI element, i.e., the start in the hierarchy
98     * @param second the second GUI element, i.e., the end in the hierarchy
99     *
100     * @return the number of required hops to get from the first GUI element to the second,
101     *         INTEGER.MAX_INT if the GUI elements do not share a common parent
102     */
103    public static int getDistance(IGUIElement first, IGUIElement second) {
104        int hopcount = 0;
105       
106        IGUIElement commonDenominator = getCommonDenominator(first, second);
107
108        while (!(first.equals(commonDenominator))) {
109            first = first.getParent();
110            hopcount++;
111        }
112
113        while (!(second.equals(commonDenominator))) {
114            second = second.getParent();
115            hopcount++;
116        }
117
118        return hopcount;
119    }
120
121}
Note: See TracBrowser for help on using the repository browser.