source: trunk/autoquest-core-events/src/main/java/de/ugoe/cs/autoquest/eventcore/guimodel/AbstractDefaultGUIElement.java @ 1490

Last change on this file since 1490 was 1436, checked in by pharms, 10 years ago
  • corrected a bug in creating hashcodes for GUI elements (they changed after storing a GUI model to a file and then reloading it)
  • Property svn:executable set to *
File size: 9.2 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.eventcore.guimodel;
16
17import java.util.LinkedList;
18import java.util.List;
19
20/**
21 * <p>
22 * Skeletal implementation for GUI elements.
23 * </p>
24 *
25 * @version 1.0
26 * @author Patrick Harms
27 */
28public abstract class AbstractDefaultGUIElement implements IGUIElement {
29
30    /**
31     * <p>
32     * Id for object serialization.
33     * </p>
34     */
35    public static final long serialVersionUID = 1L;
36
37    /**
38     * <p>
39     * Specification of the GUI element
40     * </p>
41     */
42    private final IGUIElementSpec specification;
43
44    /**
45     * <p>
46     * Reference to the parent element
47     * </p>
48     */
49    private IGUIElement parent;
50   
51    /**
52     * <p>
53     * List of other GUI elements being equal to this
54     * </p>
55     */
56    private List<AbstractDefaultGUIElement> equalGUIElements = null;
57   
58    /**
59     * <p>
60     * Boolean that indicates if a GUIElement was used during a session.
61     * </p>
62     */
63    private boolean usageObserved;
64
65    /**
66     * <p>
67     * the reference to the GUI model to which this GUI element belongs.
68     * </p>
69     */
70    private GUIModel guiModel;
71   
72    /**
73     * <p>
74     * the hash code of this object
75     * </p>
76     */
77    private int hashCode;
78
79    /**
80     * <p>
81     * Constructor. Creates a new AbstractDefaultGUIElement.
82     * </p>
83     *
84     * @param specification
85     *            specification of the created GUI element
86     * @param parent
87     *            parent of the created GUI element; null means the element is a top-level window
88     */
89    public AbstractDefaultGUIElement(IGUIElementSpec specification, IGUIElement parent) {
90        this.specification = specification;
91        this.usageObserved = false;
92        setParent(parent);
93       
94        if (specification != null) {
95            this.hashCode = specification.hashCode();
96        }
97        else {
98            this.hashCode = 0;
99        }
100    }
101
102    /*
103     * (non-Javadoc)
104     *
105     * @see de.ugoe.cs.tasktree.guimodel.GUIElement#getSpecification()
106     */
107    @Override
108    public IGUIElementSpec getSpecification() {
109        return specification;
110    }
111
112    /*
113     * (non-Javadoc)
114     *
115     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement#getParent()
116     */
117    @Override
118    public IGUIElement getParent() {
119        return parent;
120    }
121
122    /* (non-Javadoc)
123     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement#getGUIModel()
124     */
125    @Override
126    public GUIModel getGUIModel() {
127       return guiModel;
128    }
129
130    /*
131     * (non-Javadoc)
132     *
133     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement#addEqualGUIElement(IGUIElement)
134     */
135    @Override
136    public void addEqualGUIElement(IGUIElement equalElement) {
137        if (!(equalElement instanceof AbstractDefaultGUIElement)) {
138            throw new IllegalArgumentException
139                ("this implementation can only handle other AbstractDefaultGUIElements");
140        }
141       
142        AbstractDefaultGUIElement other = (AbstractDefaultGUIElement) equalElement;
143       
144        synchronized (AbstractDefaultGUIElement.class) {
145            if (this.equalGUIElements == null) {
146                if (other.equalGUIElements == null) {
147                    this.equalGUIElements = new LinkedList<AbstractDefaultGUIElement>();
148                    this.equalGUIElements.add(this);
149                    this.equalGUIElements.add(other);
150                    other.equalGUIElements = this.equalGUIElements;
151                    other.hashCode = this.hashCode;
152                }
153                else {
154                    addIfNotContained(other.equalGUIElements, this);
155                    this.equalGUIElements = other.equalGUIElements;
156                    this.hashCode = other.hashCode;
157                }
158            }
159            else {
160                if (other.equalGUIElements == null) {
161                    addIfNotContained(this.equalGUIElements, other);
162                    other.equalGUIElements = this.equalGUIElements;
163                    other.hashCode = this.hashCode;
164                }
165                else if (this.equalGUIElements != other.equalGUIElements) {
166                    this.equalGUIElements.addAll(other.equalGUIElements);
167
168                    // we also have to set this new list for all other elements for which so
169                    // far list2 was registered
170                    for (AbstractDefaultGUIElement candidate : other.equalGUIElements) {
171                        candidate.equalGUIElements = this.equalGUIElements;
172                        candidate.hashCode = this.hashCode;
173                    }
174
175                    other.equalGUIElements = this.equalGUIElements;
176                    other.hashCode = this.hashCode;
177                }
178                // else
179                // in this case, both GUI elements should already be registered with the same
180                // lists.
181            }
182        }
183    }
184
185    /*
186     * (non-Javadoc)
187     *
188     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement#isUsed()
189     */
190    @Override
191    public boolean isUsed() {
192        return usageObserved;
193    }
194
195    /*
196     * (non-Javadoc)
197     *
198     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement#markUsed()
199     */
200    @Override
201    public void markUsed() {
202        this.usageObserved = true;
203    }
204
205    /*
206     * (non-Javadoc)
207     *
208     * @see GUIElement#equals(GUIElement)
209     */
210    public final boolean equals(Object other) {
211        // implement final, as GUI elements are all singletons and they equal only if they are the
212        // same object or if they are in the list of equal GUI elements
213        if (super.equals(other)) {
214            return true;
215        }
216        else if (other instanceof AbstractDefaultGUIElement) {
217            synchronized (AbstractDefaultGUIElement.class) {
218                if (equalGUIElements != null) {
219                    for (IGUIElement candidate : equalGUIElements) {
220                        if (candidate == other) {
221                            return true;
222                        }
223                    }
224                }
225            }
226        }
227
228        return false;
229    }
230
231    /*
232     * (non-Javadoc)
233     *
234     * @see java.lang.Object#hashCode()
235     */
236    @Override
237    public final int hashCode() {
238        // implement final, as GUI elements are all singletons and they equal only if they are the
239        // same object. If there are several GUI element objects that represent the same GUI element
240        // then they are stored in the list of equal elements. But at least their type is expected
241        // to be equal, so return the hash code of the type.
242        return hashCode;
243    }
244   
245    /**
246     * <p>
247     * updates the parent node of this node if required due to model restructuring
248     * </p>
249     */
250    void setParent(IGUIElement newParent) {
251        synchronized (AbstractDefaultGUIElement.class) {
252            // all equal GUI elements must have the same parent. Otherwise, they are not equal
253            // anymore and we would have discrepancies on the return value of getParent() on
254            // equal GUI elements.
255            this.parent = newParent;
256            if (equalGUIElements != null) {
257                for (AbstractDefaultGUIElement candidate : equalGUIElements) {
258                    candidate.parent = newParent;
259                }
260            }
261        }
262    }
263
264    /**
265     * <p>
266     * used to set the GUI model to which this GUI element belongs. Will be set automatically, if
267     * used in combination with {@link GUIModel};
268     * </p>
269     *
270     * @param guiModel
271     */
272    void setGUIModel(GUIModel guiModel) {
273        this.guiModel = guiModel;
274    }
275
276    /**
277     * <p>
278     * Adds an {@link AbstractDefaultGUIElement} as equal to a list of
279     * {@link AbstractDefaultGUIElement}s if and only if it is not already contained.
280     * </p>
281     *
282     * @param equalElementsList
283     *            list of {@link AbstractDefaultGUIElement} to which the GUI element is added
284     * @param guiElement
285     *            GUI element to be added
286     */
287    private void addIfNotContained(List<AbstractDefaultGUIElement> equalElementsList,
288                                   AbstractDefaultGUIElement       guiElement)
289    {
290        for (IGUIElement candidate : equalElementsList) {
291            if (candidate == guiElement) {
292                return;
293            }
294        }
295
296        equalElementsList.add(guiElement);
297    }
298
299}
Note: See TracBrowser for help on using the repository browser.