source: branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/AlignmentHelpers.java @ 1733

Last change on this file since 1733 was 1733, checked in by rkrimmel, 10 years ago

Used Eclipse code cleanup

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