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

Last change on this file since 1691 was 1691, checked in by dmay, 10 years ago

start implementing key event, not working yet
also another id hack for testing purposes - this needs to be improved asap

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