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