1 | /* |
---|
2 | * |
---|
3 | */ |
---|
4 | package de.ugoe.cs.autoquest.tasktrees.alignment.algorithms; |
---|
5 | |
---|
6 | import java.util.ArrayList; |
---|
7 | import java.util.List; |
---|
8 | |
---|
9 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel; |
---|
10 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
11 | |
---|
12 | // TODO: Auto-generated Javadoc |
---|
13 | /** |
---|
14 | * The Class AlignmentHelpers. |
---|
15 | */ |
---|
16 | public class AlignmentHelpers extends GUIModel { |
---|
17 | |
---|
18 | /** |
---|
19 | * Distance between. |
---|
20 | * |
---|
21 | * @param first the first |
---|
22 | * @param second the second |
---|
23 | * @return the int |
---|
24 | */ |
---|
25 | public static int distanceBetween(IGUIElement first, IGUIElement second) { |
---|
26 | |
---|
27 | int hopcount1 = 0; |
---|
28 | int hopcount2 = 0; |
---|
29 | final List<IGUIElement> tmp = new ArrayList<IGUIElement>(); |
---|
30 | tmp.add(first); |
---|
31 | tmp.add(second); |
---|
32 | final IGUIElement commonDenominator = getCommonDenominator(tmp); |
---|
33 | |
---|
34 | while (!(first.equals(commonDenominator))) { |
---|
35 | first = first.getParent(); |
---|
36 | hopcount1++; |
---|
37 | } |
---|
38 | |
---|
39 | while (!(second.equals(commonDenominator))) { |
---|
40 | second = second.getParent(); |
---|
41 | hopcount2++; |
---|
42 | } |
---|
43 | |
---|
44 | return hopcount1 + hopcount2; |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * <p> |
---|
49 | * return a common denominator for the provided list of GUI elements, i.e. a |
---|
50 | * GUI element, that is part of the parent GUI hiearchy of all GUI elements |
---|
51 | * in the list. If there is no common denominator, the method returns null. |
---|
52 | * </p> |
---|
53 | * |
---|
54 | * @param guiElements the gui elements |
---|
55 | * @return the common denominator |
---|
56 | */ |
---|
57 | private static IGUIElement getCommonDenominator( |
---|
58 | List<IGUIElement> guiElements) { |
---|
59 | IGUIElement commonDenominator = null; |
---|
60 | |
---|
61 | if (guiElements.size() > 0) { |
---|
62 | final List<IGUIElement> commonDenominatorPath = new ArrayList<IGUIElement>(); |
---|
63 | |
---|
64 | // create a reference list using the first GUI element |
---|
65 | IGUIElement guiElement = guiElements.get(0); |
---|
66 | while (guiElement != null) { |
---|
67 | // We can use every type |
---|
68 | // if (guiElementMatchesConsideredTypes(guiElement)) { |
---|
69 | commonDenominatorPath.add(0, guiElement); |
---|
70 | // } |
---|
71 | guiElement = guiElement.getParent(); |
---|
72 | } |
---|
73 | |
---|
74 | if (commonDenominatorPath.size() == 0) { |
---|
75 | return null; |
---|
76 | } |
---|
77 | |
---|
78 | // for each other GUI element, check the reference list for the |
---|
79 | // first element in the |
---|
80 | // path, that is not common to the current one, and delete it as |
---|
81 | // well as it subsequent |
---|
82 | // siblings |
---|
83 | final List<IGUIElement> currentPath = new ArrayList<IGUIElement>(); |
---|
84 | for (int i = 1; i < guiElements.size(); i++) { |
---|
85 | currentPath.clear(); |
---|
86 | guiElement = guiElements.get(i); |
---|
87 | while (guiElement != null) { |
---|
88 | // if (guiElementMatchesConsideredTypes(guiElement)) { |
---|
89 | currentPath.add(0, guiElement); |
---|
90 | // } |
---|
91 | guiElement = guiElement.getParent(); |
---|
92 | } |
---|
93 | |
---|
94 | // determine the index of the first unequal path element |
---|
95 | int index = 0; |
---|
96 | while ((index < commonDenominatorPath.size()) |
---|
97 | && (index < currentPath.size()) |
---|
98 | && commonDenominatorPath.get(index).equals( |
---|
99 | currentPath.get(index))) { |
---|
100 | index++; |
---|
101 | } |
---|
102 | |
---|
103 | // remove all elements from the common denonimator path, that do |
---|
104 | // not match |
---|
105 | while (index < commonDenominatorPath.size()) { |
---|
106 | commonDenominatorPath.remove(index); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | if (commonDenominatorPath.size() > 0) { |
---|
111 | commonDenominator = commonDenominatorPath |
---|
112 | .get(commonDenominatorPath.size() - 1); |
---|
113 | } |
---|
114 | } |
---|
115 | return commonDenominator; |
---|
116 | } |
---|
117 | |
---|
118 | /** The Constant serialVersionUID. */ |
---|
119 | private static final long serialVersionUID = -2593092958275693133L; |
---|
120 | |
---|
121 | } |
---|