001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.struct;
025:
026: import jacareto.parse.RecordTokenizer;
027: import jacareto.parse.RecordTokenizerState;
028: import jacareto.record.MouseEventRecordable;
029: import jacareto.system.Environment;
030:
031: import java.awt.event.MouseEvent;
032:
033: import java.util.Vector;
034:
035: /**
036: * This structure element stands for a mouse drag.
037: *
038: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
039: * @version 1.0
040: */
041: public class MouseDrag extends StructureElement {
042: /** The mouse pressed event recordable. */
043: private MouseEventRecordable mousePressed;
044:
045: /** The mouse released event recordable. */
046: private MouseEventRecordable mouseReleased;
047:
048: /** The x coordinate of the motion start position. */
049: private int startX;
050:
051: /** The x coordinate of the motion stop position. */
052: private int stopX;
053:
054: /** The y coordinate of the motion start position. */
055: private int startY;
056:
057: /** The y coordinate of the motion stop position. */
058: private int stopY;
059:
060: /** The name of the source component. */
061: private String sourceName;
062:
063: /** The name of the component. */
064: private String componentName;
065:
066: /**
067: * Creates a new "mouse drag" structure element.
068: *
069: * @param env the environment
070: * @param children the child structure elements
071: */
072: public MouseDrag(Environment env, StructureElement[] children) {
073: super (env, children);
074:
075: MouseEventRecordable firstMouseEvent = (MouseEventRecordable) children[0];
076:
077: StructureElement tmp;
078: int index = children.length - 1;
079:
080: do {
081: tmp = children[index--];
082: } while (!(tmp instanceof MouseEventRecordable));
083:
084: MouseEventRecordable lastMouseEvent = (MouseEventRecordable) tmp;
085:
086: startX = firstMouseEvent.getX();
087: startY = firstMouseEvent.getY();
088: stopX = lastMouseEvent.getX();
089: stopY = lastMouseEvent.getY();
090: sourceName = firstMouseEvent.getSourceName();
091: componentName = firstMouseEvent.getComponentName();
092:
093: if (firstMouseEvent.getID() == MouseEvent.MOUSE_PRESSED) {
094: mousePressed = firstMouseEvent;
095: }
096:
097: if (lastMouseEvent.getID() == MouseEvent.MOUSE_RELEASED) {
098: mouseReleased = lastMouseEvent;
099: }
100: }
101:
102: /**
103: * Parses a record which is tokenized by the given record tokenizer.
104: *
105: * @param env DOCUMENT ME!
106: * @param recordTokenizer the record tokenizer
107: *
108: * @return a structure element, or <code>null</code> if this class cannot parse the record at
109: * the current position
110: */
111: public static StructureElement parse(Environment env,
112: RecordTokenizer recordTokenizer) {
113: StructureElement result = null;
114: Vector addedChildren = null;
115: StructureElement element = null;
116:
117: RecordTokenizerState rtState = recordTokenizer.saveState();
118:
119: try {
120: MouseEventRecordable firstMouseEvent = (MouseEventRecordable) recordTokenizer
121: .get();
122:
123: if ((firstMouseEvent == null)
124: || ((firstMouseEvent.getID() != MouseEvent.MOUSE_PRESSED) && (firstMouseEvent
125: .getID() != MouseEvent.MOUSE_DRAGGED))) {
126: return null;
127: }
128:
129: addedChildren = new Vector(20, 20);
130: addedChildren.add(firstMouseEvent);
131: recordTokenizer.forward();
132:
133: boolean proceed = true;
134:
135: while (proceed) {
136: proceed = false;
137:
138: element = recordTokenizer.get();
139:
140: if ((element != null)
141: && element instanceof MouseEventRecordable
142: && (((MouseEventRecordable) element).getID() == MouseEvent.MOUSE_DRAGGED)) {
143: addedChildren.add(element);
144: recordTokenizer.forward();
145: proceed = true;
146: } else {
147: element = MouseChangedComponent.parse(env,
148: recordTokenizer);
149:
150: if (element == null) {
151: element = MouseEntered.parse(env,
152: recordTokenizer);
153: }
154:
155: if (element == null) {
156: element = MouseExited.parse(env,
157: recordTokenizer);
158: }
159:
160: if (element != null) {
161: addedChildren.add(element);
162: proceed = true;
163: }
164: }
165: }
166:
167: if (firstMouseEvent.getID() == MouseEvent.MOUSE_PRESSED) {
168: MouseEventRecordable lastMouseEvent = (MouseEventRecordable) recordTokenizer
169: .next();
170:
171: if (lastMouseEvent != null) {
172: addedChildren.add(lastMouseEvent);
173:
174: // mouse pressed and released at different positions
175: // if no mouse drag event has occured
176: if ((firstMouseEvent.getID() == MouseEvent.MOUSE_PRESSED)
177: && (lastMouseEvent.getID() == MouseEvent.MOUSE_RELEASED)) {
178: if ((lastMouseEvent.getID() != MouseEvent.MOUSE_RELEASED)
179: || ((addedChildren.size() == 2)
180: && (firstMouseEvent.getX() == lastMouseEvent
181: .getX()) && (firstMouseEvent
182: .getY() == lastMouseEvent
183: .getY()))) {
184: throw new Exception();
185: }
186: }
187: }
188: }
189:
190: StructureElement[] children = vectorToArray(addedChildren);
191: result = new MouseDrag(env, children);
192: } catch (Exception e) {
193: recordTokenizer.restoreState(rtState);
194: }
195:
196: return result;
197: }
198:
199: /**
200: * Returns the x-coordinate of the motion start position.
201: *
202: * @return DOCUMENT ME!
203: */
204: public int getStartX() {
205: return startX;
206: }
207:
208: /**
209: * Returns the y-coordinate of the motion start position.
210: *
211: * @return DOCUMENT ME!
212: */
213: public int getStartY() {
214: return startY;
215: }
216:
217: /**
218: * Returns the x-coordinate of the motion stop position.
219: *
220: * @return DOCUMENT ME!
221: */
222: public int getStopX() {
223: return stopX;
224: }
225:
226: /**
227: * Returns the y-coordinate of the motion stop position.
228: *
229: * @return DOCUMENT ME!
230: */
231: public int getStopY() {
232: return stopY;
233: }
234:
235: /**
236: * Returns the source name.
237: *
238: * @return DOCUMENT ME!
239: */
240: public String getSourceName() {
241: return sourceName;
242: }
243:
244: /**
245: * Returns the component name.
246: *
247: * @return DOCUMENT ME!
248: */
249: public String getComponentName() {
250: return componentName;
251: }
252:
253: /**
254: * Returns the mouse pressed event recordable. May return <code>null</code> if there is not
255: * mouse pressed event.
256: *
257: * @return DOCUMENT ME!
258: */
259: public MouseEventRecordable getMousePressed() {
260: return mousePressed;
261: }
262:
263: /**
264: * Returns the mouse released event recordable. May return <code>null</code> if there is not
265: * mouse released event.
266: *
267: * @return DOCUMENT ME!
268: */
269: public MouseEventRecordable getMouseReleased() {
270: return mouseReleased;
271: }
272:
273: /**
274: * Returns the name of the element.
275: *
276: * @return the name
277: */
278: public String getElementName() {
279: return language.getString("Structures.MouseDrag.Name");
280: }
281:
282: /**
283: * Returns a description of the element.
284: *
285: * @return the description
286: */
287: public String getElementDescription() {
288: return language.getString("Structures.MouseDrag.Description");
289: }
290:
291: /**
292: * Returns a String which describes the content of the element shortly.
293: *
294: * @return a string with a short description of the element
295: */
296: public String toShortString() {
297: return getElementName() + " (" + startX + "," + startY
298: + ") -> (" + stopX + "," + stopY + ")";
299: }
300:
301: /**
302: * Clones the element.
303: *
304: * @return DOCUMENT ME!
305: */
306: public Object clone() {
307: StructureElement[] clonedChildren = getClonedChildren();
308:
309: return new MouseDrag(env, clonedChildren);
310: }
311: }
|