1 | package de.ugoe.cs.quest.eventcore.guimodel;
|
---|
2 |
|
---|
3 | import java.util.IdentityHashMap;
|
---|
4 | import java.util.LinkedList;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * TODO comment
|
---|
9 | *
|
---|
10 | * @version $Revision: $ $Date: $
|
---|
11 | * @author 2011, last modified by $Author: $
|
---|
12 | */
|
---|
13 | public abstract class AbstractDefaultGUIElement implements IGUIElement {
|
---|
14 |
|
---|
15 | /** */
|
---|
16 | public static final long serialVersionUID = 1L;
|
---|
17 |
|
---|
18 | /** the reference to equal GUI element manager (needed to preserve singleton behavior) */
|
---|
19 | private static final EqualGUIElementManager equalGUIElementManager =
|
---|
20 | new EqualGUIElementManager();
|
---|
21 |
|
---|
22 | /** the specification of the GUI element */
|
---|
23 | private IGUIElementSpec specification;
|
---|
24 |
|
---|
25 | /** the reference to the parent element */
|
---|
26 | private IGUIElement parent;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * <p>
|
---|
30 | * TODO: comment
|
---|
31 | * </p>
|
---|
32 | *
|
---|
33 | * @param specification
|
---|
34 | */
|
---|
35 | public AbstractDefaultGUIElement(IGUIElementSpec specification, IGUIElement parent) {
|
---|
36 | this.specification = specification;
|
---|
37 | this.parent = parent;
|
---|
38 | }
|
---|
39 |
|
---|
40 | /*
|
---|
41 | * (non-Javadoc)
|
---|
42 | *
|
---|
43 | * @see de.ugoe.cs.tasktree.guimodel.GUIElement#getSpecification()
|
---|
44 | */
|
---|
45 | @Override
|
---|
46 | public IGUIElementSpec getSpecification() {
|
---|
47 | return specification;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /* (non-Javadoc)
|
---|
51 | * @see de.ugoe.cs.quest.eventcore.guimodel.IGUIElement#getParent()
|
---|
52 | */
|
---|
53 | @Override
|
---|
54 | public IGUIElement getParent() {
|
---|
55 | return parent;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /* (non-Javadoc)
|
---|
59 | * @see de.ugoe.cs.quest.eventcore.guimodel.IGUIElement#addEqualGUIElement(IGUIElement)
|
---|
60 | */
|
---|
61 | @Override
|
---|
62 | public void addEqualGUIElement(IGUIElement equalElement)
|
---|
63 | {
|
---|
64 | equalGUIElementManager.addEqualGUIElements(this, equalElement);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * (non-Javadoc)
|
---|
69 | *
|
---|
70 | * @see GUIElement#equals(GUIElement)
|
---|
71 | */
|
---|
72 | public final boolean equals(Object other) {
|
---|
73 | // implement final, as GUI elements are all singletons and they equal only if they are the
|
---|
74 | // same object or if they are in the list of equal GUI elements
|
---|
75 | return super.equals(other) || equalGUIElementManager.equals(this, other);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /* (non-Javadoc)
|
---|
79 | * @see java.lang.Object#hashCode()
|
---|
80 | */
|
---|
81 | @Override
|
---|
82 | public final int hashCode() {
|
---|
83 | // implement final, as GUI elements are all singletons and they equal only if they are the
|
---|
84 | // same object. If there are several GUI element objects that represent the same GUI element
|
---|
85 | // then they are stored in the list of equal elements. In this case, the hash code of the
|
---|
86 | // list is unique within the system
|
---|
87 | return equalGUIElementManager.hashCode(this);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * <p>
|
---|
92 | * TODO comment
|
---|
93 | * </p>
|
---|
94 | *
|
---|
95 | * @version $Revision: $ $Date: 24.08.2012$
|
---|
96 | * @author 2012, last modified by $Author: pharms$
|
---|
97 | */
|
---|
98 | private static class EqualGUIElementManager {
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * <p>
|
---|
102 | * the internal map of GUI elements mapping each registered element to its equal variants.
|
---|
103 | * We use the {@link IdentityHashMap} as a normal hash map would not work because of the
|
---|
104 | * changing of the hash code of GUI elements at runtime.
|
---|
105 | * </p>
|
---|
106 | */
|
---|
107 | private IdentityHashMap<IGUIElement, List<IGUIElement>> identityHashMap =
|
---|
108 | new IdentityHashMap<IGUIElement, List<IGUIElement>>();
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * <p>
|
---|
112 | * TODO: comment
|
---|
113 | * </p>
|
---|
114 | *
|
---|
115 | * @param abstractDefaultGUIElement
|
---|
116 | * @param equalElement
|
---|
117 | */
|
---|
118 | private synchronized void addEqualGUIElements(IGUIElement guiElement1,
|
---|
119 | IGUIElement guiElement2)
|
---|
120 | {
|
---|
121 | List<IGUIElement> list1 = identityHashMap.get(guiElement1);
|
---|
122 | List<IGUIElement> list2 = identityHashMap.get(guiElement2);
|
---|
123 |
|
---|
124 | if (list1 == null) {
|
---|
125 | if (list2 == null) {
|
---|
126 | list2 = new LinkedList<IGUIElement>();
|
---|
127 | list2.add(guiElement1);
|
---|
128 | list2.add(guiElement2);
|
---|
129 | identityHashMap.put(guiElement1, list2);
|
---|
130 | identityHashMap.put(guiElement2, list2);
|
---|
131 | }
|
---|
132 | else {
|
---|
133 | addIfNotContained(list2, guiElement1);
|
---|
134 | identityHashMap.put(guiElement1, list2);
|
---|
135 | }
|
---|
136 | }
|
---|
137 | else {
|
---|
138 | if (list2 == null) {
|
---|
139 | addIfNotContained(list1, guiElement2);
|
---|
140 | identityHashMap.put(guiElement2, list1);
|
---|
141 | }
|
---|
142 | else if (list1 != list2) {
|
---|
143 | list1.addAll(list2);
|
---|
144 | identityHashMap.put(guiElement2, list1);
|
---|
145 | }
|
---|
146 | // else
|
---|
147 | // in this case, both GUI elements should already be registered with the same
|
---|
148 | // lists.
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * <p>
|
---|
154 | * TODO: comment
|
---|
155 | * </p>
|
---|
156 | *
|
---|
157 | * @param abstractDefaultGUIElement
|
---|
158 | * @return
|
---|
159 | */
|
---|
160 | private synchronized int hashCode(IGUIElement guiElement) {
|
---|
161 | return System.identityHashCode(getEqualElementsList(guiElement));
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * <p>
|
---|
166 | * TODO: comment
|
---|
167 | * </p>
|
---|
168 | *
|
---|
169 | * @param abstractDefaultGUIElement
|
---|
170 | * @param other
|
---|
171 | * @return
|
---|
172 | */
|
---|
173 | private synchronized boolean equals(IGUIElement guiElement, Object other) {
|
---|
174 | if (other instanceof IGUIElement) {
|
---|
175 | for (IGUIElement candidate : getEqualElementsList(guiElement)) {
|
---|
176 | if (candidate == other) {
|
---|
177 | return true;
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | return false;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * <p>
|
---|
187 | * TODO: comment
|
---|
188 | * </p>
|
---|
189 | *
|
---|
190 | * @param guiElement
|
---|
191 | * @return
|
---|
192 | */
|
---|
193 | private List<IGUIElement> getEqualElementsList(IGUIElement guiElement) {
|
---|
194 | List<IGUIElement> returnValue = identityHashMap.get(guiElement);
|
---|
195 |
|
---|
196 | if (returnValue == null) {
|
---|
197 | returnValue = new LinkedList<IGUIElement>();
|
---|
198 | returnValue.add(guiElement);
|
---|
199 | identityHashMap.put(guiElement, returnValue);
|
---|
200 | }
|
---|
201 |
|
---|
202 | return returnValue;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * <p>
|
---|
207 | * TODO: comment
|
---|
208 | * </p>
|
---|
209 | *
|
---|
210 | * @param resultingList
|
---|
211 | * @param guiElement1
|
---|
212 | */
|
---|
213 | private void addIfNotContained(List<IGUIElement> equalElementsList, IGUIElement guiElement)
|
---|
214 | {
|
---|
215 | for (IGUIElement candidate : equalElementsList) {
|
---|
216 | if (candidate == guiElement) {
|
---|
217 | return;
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | equalElementsList.add(guiElement);
|
---|
222 | }
|
---|
223 |
|
---|
224 | }
|
---|
225 |
|
---|
226 | }
|
---|