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.Collection; |
---|
11 | import java.util.HashMap; |
---|
12 | import java.util.List; |
---|
13 | import java.util.Map; |
---|
14 | |
---|
15 | /** |
---|
16 | * TODO comment |
---|
17 | * |
---|
18 | * @version $Revision: $ $Date: 14.08.2012$ |
---|
19 | * @author 2012, last modified by $Author: pharms$ |
---|
20 | */ |
---|
21 | public class GUIModel<T extends IGUIElement> { |
---|
22 | |
---|
23 | /** |
---|
24 | * |
---|
25 | */ |
---|
26 | private Collection<T> guiElements = new ArrayList<T>(); |
---|
27 | |
---|
28 | /** |
---|
29 | * |
---|
30 | */ |
---|
31 | private Map<T, List<T>> childRelations = new HashMap<T, List<T>>(); |
---|
32 | |
---|
33 | /** |
---|
34 | * TODO: comment |
---|
35 | * |
---|
36 | * @param currentGUIElementPath |
---|
37 | * @return |
---|
38 | * @throws GUIModelException |
---|
39 | */ |
---|
40 | public T checkAndIntegratePath(List<T> guiElementPath) throws GUIModelException { |
---|
41 | T existingGuiElement = check(guiElementPath); |
---|
42 | |
---|
43 | if (existingGuiElement == null) { |
---|
44 | T terminalGuiElement = null; |
---|
45 | T child = null; |
---|
46 | boolean doBreak = false; |
---|
47 | for (int i = guiElementPath.size() - 1; ((i >= 0) && (!doBreak)); i--) { |
---|
48 | T newElement = guiElementPath.get(i); |
---|
49 | existingGuiElement = null; |
---|
50 | |
---|
51 | for (T candidate : guiElements) { |
---|
52 | if (candidate.equals(newElement)) { |
---|
53 | existingGuiElement = candidate; |
---|
54 | break; |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | // element not yet contained in model. Add it. |
---|
59 | if (existingGuiElement == null) |
---|
60 | { |
---|
61 | guiElements.add(newElement); |
---|
62 | existingGuiElement = newElement; |
---|
63 | } |
---|
64 | else |
---|
65 | { |
---|
66 | doBreak = true; |
---|
67 | } |
---|
68 | |
---|
69 | if (terminalGuiElement == null) { |
---|
70 | terminalGuiElement = existingGuiElement; |
---|
71 | } |
---|
72 | |
---|
73 | if (child != null) { |
---|
74 | List<T> children = childRelations.get(existingGuiElement); |
---|
75 | if (children == null) { |
---|
76 | children = new ArrayList<T>(); |
---|
77 | childRelations.put(existingGuiElement, children); |
---|
78 | } |
---|
79 | children.add(child); |
---|
80 | } |
---|
81 | child = existingGuiElement; |
---|
82 | } |
---|
83 | |
---|
84 | existingGuiElement = terminalGuiElement; |
---|
85 | } |
---|
86 | |
---|
87 | return existingGuiElement; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * TODO: comment |
---|
92 | * |
---|
93 | * @param root |
---|
94 | * @return |
---|
95 | */ |
---|
96 | public List<T> getChildren(T guiElement) { |
---|
97 | return childRelations.get(guiElement); |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * TODO: comment |
---|
102 | * |
---|
103 | * @return |
---|
104 | */ |
---|
105 | public List<T> getRootElements() { |
---|
106 | List<T> roots = new ArrayList<T>(); |
---|
107 | for (T guiElement : guiElements) { |
---|
108 | if (guiElement.getParent() == null) { |
---|
109 | roots.add(guiElement); |
---|
110 | } |
---|
111 | } |
---|
112 | return roots; |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * TODO: comment |
---|
117 | * |
---|
118 | * @param guiElementPath |
---|
119 | * @throws GUIModelException |
---|
120 | */ |
---|
121 | private T check(List<T> guiElementPath) throws GUIModelException { |
---|
122 | T guiElementInModel = null; |
---|
123 | |
---|
124 | for (int i = 0; i < guiElementPath.size(); i++) { |
---|
125 | guiElementInModel = null; |
---|
126 | |
---|
127 | T newElement = guiElementPath.get(i); |
---|
128 | T newParent = (i > 0 ? guiElementPath.get(i - 1) : null); |
---|
129 | |
---|
130 | if ((newElement.getParent() != null) && |
---|
131 | (!newElement.getParent().equals(newParent))) |
---|
132 | { |
---|
133 | throw new GUIModelException |
---|
134 | ("new GUI element " + newElement + " denotes a parent element (" + |
---|
135 | newElement.getParent() + ") which is not identical to the parent element " + |
---|
136 | "denoted by the element path (" + newParent + ")"); |
---|
137 | } |
---|
138 | |
---|
139 | for (T existingElement : guiElements) { |
---|
140 | if (existingElement.equals(newElement)) { |
---|
141 | if (guiElementInModel != null) { |
---|
142 | throw new GUIModelException |
---|
143 | ("several GUI elements already existing in the model pretend to " + |
---|
144 | "match the new element " + newElement); |
---|
145 | } |
---|
146 | |
---|
147 | if ((existingElement.getParent() != newParent) && |
---|
148 | ((existingElement.getParent() != null) && |
---|
149 | (!existingElement.getParent().equals(newParent)))) |
---|
150 | { |
---|
151 | throw new GUIModelException |
---|
152 | ("the new path denotes the GUI element " + newElement + |
---|
153 | " with parent " + newElement.getParent() + " that already exists in " + |
---|
154 | "the model with a distinct parent element (" + |
---|
155 | existingElement.getParent() + ")"); |
---|
156 | } |
---|
157 | |
---|
158 | guiElementInModel = existingElement; |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | return guiElementInModel; |
---|
164 | } |
---|
165 | |
---|
166 | } |
---|