source: trunk/autoquest-plugin-jfc/src/main/java/de/ugoe/cs/autoquest/plugin/jfc/guimodel/JFCGUIElement.java @ 1876

Last change on this file since 1876 was 1876, checked in by pharms, 9 years ago
  • added support for views in GUIs
  • Property svn:executable set to *
File size: 6.1 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.plugin.jfc.guimodel;
16
17import de.ugoe.cs.autoquest.eventcore.guimodel.AbstractDefaultGUIElement;
18import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
19import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec;
20import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIView;
21
22/**
23 * <p>
24 * Base class for all JFC GUI elements.
25 * </p>
26 *
27 * @version 1.0
28 * @author Patrick Harms
29 */
30public class JFCGUIElement extends AbstractDefaultGUIElement {
31
32    /**
33     * <p>
34     * Id for object serialization.
35     * </p>
36     */
37    private static final long serialVersionUID = 1L;
38
39    /**
40     * <p>
41     * Specification of the GUI Element
42     * </p>
43     */
44    private JFCGUIElementSpec specification;
45
46    /**
47     * <p>
48     * Constructor. Creates a new JFCGUIElement.
49     * </p>
50     *
51     * @param specification
52     *            specification of created GUI element
53     * @param parent
54     *            parent of the created GUI element; null means that the element is a top-level
55     *            window
56     */
57    public JFCGUIElement(JFCGUIElementSpec specification, JFCGUIElement parent) {
58        super(specification, parent);
59        this.specification = specification;
60    }
61
62    /*
63     * (non-Javadoc)
64     *
65     * @see de.ugoe.cs.autoquest.eventcore.IEventTarget#getPlatform()
66     */
67    @Override
68    public String getPlatform() {
69        return "JFC";
70    }
71
72    /**
73     * <p>
74     * Returns the type of the GUI element, i.e., the name of its Java class.
75     * </p>
76     *
77     * @return the Java class name
78     */
79    public String getJavaType() {
80        return specification.getType();
81    }
82
83    /**
84     * <p>
85     * Returns the name of the GUI element.
86     * </p>
87     *
88     * @return the name
89     */
90    public String getName() {
91        return specification.getName();
92    }
93
94    /**
95     * <p>
96     * Returns the icon of the GUI element.
97     * </p>
98     *
99     * @return the icon
100     */
101    String getIcon() {
102        return specification.getIcon();
103    }
104
105    /**
106     * <p>
107     * Returns the index of the GUI element.
108     * </p>
109     *
110     * @return the index
111     */
112    int getIndex() {
113        return specification.getIndex();
114    }
115
116    /**
117     * <p>
118     * Returns the object hash of the GUI element.
119     * </p>
120     *
121     * @return the object hash
122     */
123    int getElementHash() {
124        return specification.getElementHash();
125    }
126
127    /*
128     * (non-Javadoc)
129     *
130     * @see
131     * de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement#updateSpecification(de.ugoe.cs.autoquest
132     * .eventcore .guimodel.IGUIElementSpec)
133     */
134    @Override
135    public void updateSpecification(IGUIElementSpec updateSpecification) {
136        if (updateSpecification instanceof JFCGUIElementSpec) {
137            specification.update(((JFCGUIElementSpec) updateSpecification));
138        }
139    }
140
141    /*
142     * (non-Javadoc)
143     *
144     * @see java.lang.Object#toString()
145     */
146    @Override
147    public String getStringIdentifier() {
148        String str = this.toString();
149        if (getParent() != null) {
150            return str + "<-" + getParent().getStringIdentifier();
151        }
152        return str;
153    }
154
155    public String getJacaretoHierarchy() {
156        String str;
157
158        // get the Java classname, ignore the package hierarchy if present
159        String[] parts = getSpecification().getType().split("\\.");
160
161        // find the correct Jacareto index
162        // jacareto indices start at 1
163        int jacIndex = ((JFCGUIElementSpec) getSpecification()).getAltIndex() + 1;
164        str = parts[parts.length - 1] + "_(" + jacIndex + ")";
165
166        if (getParent() != null) {
167            return ((JFCGUIElement) getParent()).getJacaretoHierarchy() + "." + str;
168        }
169        return str;
170    }
171
172    public String getJacaretoRoot() {
173        return getJacaretoHierarchy().split("\\.")[0];
174    }
175
176    /*
177     * (non-Javadoc)
178     *
179     * @see java.lang.Object#toString()
180     */
181    @Override
182    public String toString() {
183        String str =
184            getElementDescriptor() + "(" + getName() + "," + getElementHash() + "," + getIcon() +
185                "," + getIndex() + ")";
186        return str;
187    }
188
189    /* (non-Javadoc)
190     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement#getView()
191     */
192    @Override
193    public IGUIView getView() {
194        IGUIElement element = this;
195       
196        while ((element != null) && (!(element instanceof IGUIView))) {
197            if (!(element.getParent() instanceof JFCTabbedPane)) {
198                element = element.getParent();
199            }
200            else {
201                // break, as all children of a tabbed pane are always views
202                break;
203            }
204        }
205       
206        return (IGUIView) element;
207    }
208
209    /*
210     * (non-Javadoc)
211     *
212     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement#getDistanceTo(IGUIElement)
213     */
214    @Override
215    public double getDistanceTo(IGUIElement otherElement) {
216        throw new UnsupportedOperationException("not implemented yet");
217    }
218
219    /**
220     * <p>
221     * A short string describing the GUI element, e.g., Button, Canvas, or ScrollBar.
222     * </p>
223     *
224     * @return short element descriptor
225     */
226    protected String getElementDescriptor() {
227        return "Default";
228    }
229
230}
Note: See TracBrowser for help on using the repository browser.