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

Last change on this file since 1433 was 1433, checked in by pharms, 10 years ago
  • added support to retrieve the GUI model to which a GUI element belongs from the GUI element itself
  • Property svn:executable set to *
File size: 8.9 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     * Constructor. Creates a new AbstractDefaultGUIElement.
75     * </p>
76     *
77     * @param specification
78     *            specification of the created GUI element
79     * @param parent
80     *            parent of the created GUI element; null means the element is a top-level window
81     */
82    public AbstractDefaultGUIElement(IGUIElementSpec specification, IGUIElement parent) {
83        this.specification = specification;
84        this.usageObserved = false;
85        setParent(parent);
86    }
87
88    /*
89     * (non-Javadoc)
90     *
91     * @see de.ugoe.cs.tasktree.guimodel.GUIElement#getSpecification()
92     */
93    @Override
94    public IGUIElementSpec getSpecification() {
95        return specification;
96    }
97
98    /*
99     * (non-Javadoc)
100     *
101     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement#getParent()
102     */
103    @Override
104    public IGUIElement getParent() {
105        return parent;
106    }
107
108    /* (non-Javadoc)
109     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement#getGUIModel()
110     */
111    @Override
112    public GUIModel getGUIModel() {
113       return guiModel;
114    }
115
116    /*
117     * (non-Javadoc)
118     *
119     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement#addEqualGUIElement(IGUIElement)
120     */
121    @Override
122    public void addEqualGUIElement(IGUIElement equalElement) {
123        if (!(equalElement instanceof AbstractDefaultGUIElement)) {
124            throw new IllegalArgumentException
125                ("this implementation can only handle other AbstractDefaultGUIElements");
126        }
127       
128        AbstractDefaultGUIElement other = (AbstractDefaultGUIElement) equalElement;
129       
130        synchronized (AbstractDefaultGUIElement.class) {
131            if (this.equalGUIElements == null) {
132                if (other.equalGUIElements == null) {
133                    this.equalGUIElements = new LinkedList<AbstractDefaultGUIElement>();
134                    this.equalGUIElements.add(this);
135                    this.equalGUIElements.add(other);
136                    other.equalGUIElements = this.equalGUIElements;
137                }
138                else {
139                    addIfNotContained(other.equalGUIElements, this);
140                    this.equalGUIElements = other.equalGUIElements;
141                }
142            }
143            else {
144                if (other.equalGUIElements == null) {
145                    addIfNotContained(this.equalGUIElements, other);
146                    other.equalGUIElements = this.equalGUIElements;
147                }
148                else if (this.equalGUIElements != other.equalGUIElements) {
149                    this.equalGUIElements.addAll(other.equalGUIElements);
150
151                    // we also have to set this new list for all other elements for which so
152                    // far list2 was registered
153                    for (AbstractDefaultGUIElement candidate : other.equalGUIElements) {
154                        candidate.equalGUIElements = this.equalGUIElements;
155                    }
156
157                    other.equalGUIElements = this.equalGUIElements;
158                }
159                // else
160                // in this case, both GUI elements should already be registered with the same
161                // lists.
162            }
163        }
164    }
165
166    /*
167     * (non-Javadoc)
168     *
169     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement#isUsed()
170     */
171    @Override
172    public boolean isUsed() {
173        return usageObserved;
174    }
175
176    /*
177     * (non-Javadoc)
178     *
179     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement#markUsed()
180     */
181    @Override
182    public void markUsed() {
183        this.usageObserved = true;
184    }
185
186    /*
187     * (non-Javadoc)
188     *
189     * @see GUIElement#equals(GUIElement)
190     */
191    public final boolean equals(Object other) {
192        // implement final, as GUI elements are all singletons and they equal only if they are the
193        // same object or if they are in the list of equal GUI elements
194        if (super.equals(other)) {
195            return true;
196        }
197        else if (other instanceof AbstractDefaultGUIElement) {
198            synchronized (AbstractDefaultGUIElement.class) {
199                if (equalGUIElements != null) {
200                    for (IGUIElement candidate : equalGUIElements) {
201                        if (candidate == other) {
202                            return true;
203                        }
204                    }
205                }
206            }
207        }
208
209        return false;
210    }
211
212    /*
213     * (non-Javadoc)
214     *
215     * @see java.lang.Object#hashCode()
216     */
217    @Override
218    public final int hashCode() {
219        // implement final, as GUI elements are all singletons and they equal only if they are the
220        // same object. If there are several GUI element objects that represent the same GUI element
221        // then they are stored in the list of equal elements. In this case, the hash code of the
222        // list is unique within the system
223        synchronized (AbstractDefaultGUIElement.class) {
224            if (this.equalGUIElements == null) {
225                this.equalGUIElements = new LinkedList<AbstractDefaultGUIElement>();
226                this.equalGUIElements.add(this);
227            }
228           
229            return System.identityHashCode(this.equalGUIElements);
230        }
231    }
232   
233    /**
234     * <p>
235     * updates the parent node of this node if required due to model restructuring
236     * </p>
237     */
238    void setParent(IGUIElement newParent) {
239        synchronized (AbstractDefaultGUIElement.class) {
240            // all equal GUI elements must have the same parent. Otherwise, they are not equal
241            // anymore and we would have discrepancies on the return value of getParent() on
242            // equal GUI elements.
243            this.parent = newParent;
244            if (equalGUIElements != null) {
245                for (AbstractDefaultGUIElement candidate : equalGUIElements) {
246                    candidate.parent = newParent;
247                }
248            }
249        }
250    }
251
252    /**
253     * <p>
254     * used to set the GUI model to which this GUI element belongs. Will be set automatically, if
255     * used in combination with {@link GUIModel};
256     * </p>
257     *
258     * @param guiModel
259     */
260    void setGUIModel(GUIModel guiModel) {
261        this.guiModel = guiModel;
262    }
263
264    /**
265     * <p>
266     * Adds an {@link AbstractDefaultGUIElement} as equal to a list of
267     * {@link AbstractDefaultGUIElement}s if and only if it is not already contained.
268     * </p>
269     *
270     * @param equalElementsList
271     *            list of {@link AbstractDefaultGUIElement} to which the GUI element is added
272     * @param guiElement
273     *            GUI element to be added
274     */
275    private void addIfNotContained(List<AbstractDefaultGUIElement> equalElementsList,
276                                   AbstractDefaultGUIElement       guiElement)
277    {
278        for (IGUIElement candidate : equalElementsList) {
279            if (candidate == guiElement) {
280                return;
281            }
282        }
283
284        equalElementsList.add(guiElement);
285    }
286
287}
Note: See TracBrowser for help on using the repository browser.