1 | // Module : $RCSfile: GUIModel.java,v $ |
---|
2 | // Version : $Revision: 0.0 $ $Author: pharms $ $Date: 14.08.2012 $ |
---|
3 | // Project : quest-core-events |
---|
4 | // Creation : 2012 by pharms |
---|
5 | // Copyright : Patrick Harms, 2012 |
---|
6 | |
---|
7 | package de.ugoe.cs.quest.eventcore.guimodel; |
---|
8 | |
---|
9 | import java.util.ArrayList; |
---|
10 | import java.util.List; |
---|
11 | |
---|
12 | /** |
---|
13 | * <p> |
---|
14 | * The goal of a GUI model is to correctly l |
---|
15 | * </p> |
---|
16 | * |
---|
17 | * @version $Revision: $ $Date: 14.08.2012$ |
---|
18 | * @author 2012, last modified by $Author: pharms$ |
---|
19 | */ |
---|
20 | public class GUIModel { |
---|
21 | |
---|
22 | /** |
---|
23 | * |
---|
24 | */ |
---|
25 | private TreeNode root = new TreeNode(); |
---|
26 | |
---|
27 | /** |
---|
28 | * |
---|
29 | */ |
---|
30 | private List<TreeNode> allNodes = new ArrayList<TreeNode>(); |
---|
31 | |
---|
32 | /** |
---|
33 | * TODO: comment |
---|
34 | * |
---|
35 | * @param currentGUIElementPath |
---|
36 | * @return |
---|
37 | * @throws GUIModelException |
---|
38 | */ |
---|
39 | public IGUIElement integratePath(List<? extends IGUIElementSpec> guiElementPath, |
---|
40 | IGUIElementFactory guiElementFactory) |
---|
41 | throws GUIModelException |
---|
42 | { |
---|
43 | List<IGUIElementSpec> remainingPath = new ArrayList<IGUIElementSpec>(); |
---|
44 | |
---|
45 | for (IGUIElementSpec spec : guiElementPath) |
---|
46 | { |
---|
47 | remainingPath.add(spec); |
---|
48 | } |
---|
49 | |
---|
50 | return integratePath(root, remainingPath, guiElementFactory); |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * TODO: comment |
---|
55 | * |
---|
56 | * @param root |
---|
57 | * @return |
---|
58 | */ |
---|
59 | public List<IGUIElement> getChildren(IGUIElement guiElement) { |
---|
60 | for (TreeNode node : allNodes) { |
---|
61 | if (node.guiElement.equals(guiElement)) { |
---|
62 | List<IGUIElement> result = new ArrayList<IGUIElement>(); |
---|
63 | |
---|
64 | if (node.children != null) { |
---|
65 | for (TreeNode child : node.children) { |
---|
66 | result.add(child.guiElement); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | return result; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | return null; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * TODO: comment |
---|
79 | * |
---|
80 | * @return |
---|
81 | */ |
---|
82 | public List<IGUIElement> getRootElements() { |
---|
83 | List<IGUIElement> roots = new ArrayList<IGUIElement>(); |
---|
84 | for (TreeNode rootChild : root.children) { |
---|
85 | roots.add(rootChild.guiElement); |
---|
86 | } |
---|
87 | return roots; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * <p> |
---|
92 | * TODO: comment |
---|
93 | * </p> |
---|
94 | * |
---|
95 | * @param root2 |
---|
96 | * @param guiElementPath |
---|
97 | * @param guiElementFactory |
---|
98 | * @return |
---|
99 | * @throws GUIModelException |
---|
100 | */ |
---|
101 | private IGUIElement integratePath(TreeNode parentNode, |
---|
102 | List<? extends IGUIElementSpec> remainingPath, |
---|
103 | IGUIElementFactory guiElementFactory) |
---|
104 | throws GUIModelException |
---|
105 | { |
---|
106 | IGUIElementSpec specToIntegrateElementFor = remainingPath.remove(0); |
---|
107 | |
---|
108 | List<TreeNode> matchingChildren = new ArrayList<TreeNode>(); |
---|
109 | int maximumSimilarity = 0; |
---|
110 | |
---|
111 | if (parentNode.children != null) { |
---|
112 | for (TreeNode child : parentNode.children) { |
---|
113 | int similarityLevel = getSimilarityLevel |
---|
114 | (specToIntegrateElementFor, child.guiElement.getSpecification()); |
---|
115 | |
---|
116 | if (similarityLevel >= maximumSimilarity) { |
---|
117 | if (maximumSimilarity == 100) { |
---|
118 | throw new GUIModelException |
---|
119 | ("several children of gui element " + parentNode.guiElement + |
---|
120 | " pretend to fully match specification " + specToIntegrateElementFor); |
---|
121 | } |
---|
122 | |
---|
123 | if (maximumSimilarity != similarityLevel) { |
---|
124 | matchingChildren.clear(); |
---|
125 | maximumSimilarity = similarityLevel; |
---|
126 | } |
---|
127 | |
---|
128 | matchingChildren.add(child); |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | // we do not see something as matching if the similarity level is below 80% |
---|
134 | if (maximumSimilarity < 80) { |
---|
135 | matchingChildren.clear(); |
---|
136 | } |
---|
137 | |
---|
138 | // if we get here, the corresponding path does not exist yet. So create it |
---|
139 | if (matchingChildren.size() == 0) { |
---|
140 | matchingChildren.add |
---|
141 | (parentNode.addChild |
---|
142 | (guiElementFactory.instantiateGUIElement(specToIntegrateElementFor))); |
---|
143 | } |
---|
144 | else if (matchingChildren.size() > 1) { |
---|
145 | throw new GUIModelException |
---|
146 | ("several children of gui element " + parentNode.guiElement + |
---|
147 | " match the specification " + specToIntegrateElementFor + " at the same level. " + |
---|
148 | "Can not decide which is the right one."); |
---|
149 | } |
---|
150 | |
---|
151 | if (remainingPath.size() > 0) { |
---|
152 | return integratePath(matchingChildren.get(0), remainingPath, guiElementFactory); |
---|
153 | } |
---|
154 | else { |
---|
155 | return matchingChildren.get(0).guiElement; |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * <p> |
---|
161 | * TODO: comment |
---|
162 | * </p> |
---|
163 | * |
---|
164 | * @param spec1 |
---|
165 | * @param spec2 |
---|
166 | * @return |
---|
167 | * @throws GUIModelException |
---|
168 | */ |
---|
169 | private int getSimilarityLevel(IGUIElementSpec spec1, IGUIElementSpec spec2) |
---|
170 | throws GUIModelException |
---|
171 | { |
---|
172 | if (spec1 == spec2) { |
---|
173 | return 100; |
---|
174 | } |
---|
175 | else if (spec1 != null) { |
---|
176 | int level = spec1.getSimilarity(spec2); |
---|
177 | |
---|
178 | if ((level < 0) || (100 < level)) { |
---|
179 | throw new GUIModelException("invalid tree node similarity provided (" + level + |
---|
180 | "). Must be between 0 and 100."); |
---|
181 | } |
---|
182 | |
---|
183 | return level; |
---|
184 | } |
---|
185 | else { |
---|
186 | return 0; |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | * <p> |
---|
192 | * TODO comment |
---|
193 | * </p> |
---|
194 | * |
---|
195 | * @version $Revision: $ $Date: 17.08.2012$ |
---|
196 | * @author 2012, last modified by $Author: pharms$ |
---|
197 | */ |
---|
198 | private class TreeNode |
---|
199 | { |
---|
200 | /** */ |
---|
201 | private IGUIElement guiElement; |
---|
202 | |
---|
203 | /** */ |
---|
204 | private List<TreeNode> children; |
---|
205 | |
---|
206 | /** */ |
---|
207 | //private TreeNode parent; |
---|
208 | |
---|
209 | /** |
---|
210 | * <p> |
---|
211 | * TODO: comment |
---|
212 | * </p> |
---|
213 | * |
---|
214 | * @param guiElement |
---|
215 | * @return |
---|
216 | */ |
---|
217 | private TreeNode addChild(IGUIElement guiElement) |
---|
218 | { |
---|
219 | if (children == null) |
---|
220 | { |
---|
221 | children = new ArrayList<TreeNode>(); |
---|
222 | } |
---|
223 | |
---|
224 | TreeNode child = new TreeNode(); |
---|
225 | child.guiElement = guiElement; |
---|
226 | //child.parent = this; |
---|
227 | children.add(child); |
---|
228 | |
---|
229 | allNodes.add(child); |
---|
230 | |
---|
231 | return child; |
---|
232 | } |
---|
233 | } |
---|
234 | } |
---|