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.FocusEventRecordable;
029: import jacareto.record.KeyEventRecordable;
030: import jacareto.record.MouseEventRecordable;
031: import jacareto.system.Environment;
032:
033: import java.awt.event.FocusEvent;
034: import java.awt.event.KeyEvent;
035: import java.awt.event.MouseEvent;
036:
037: /**
038: * This structure element stands for a focus change.
039: *
040: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
041: * @version 1.01
042: */
043: public class FocusChange extends StructureElement {
044: /** The name of the component which has lost the focus. */
045: private String oldComponent;
046:
047: /** The name of the component which has gained the focus. */
048: private String newComponent;
049:
050: /**
051: * Creates a new "focus change" structure element. Extactly one child of this element must be a
052: * "focus gained", one a "focus lost"-event.
053: *
054: * @param env the environment
055: * @param children the child structure elements
056: */
057: public FocusChange(Environment env, StructureElement[] children) {
058: super (env, children);
059:
060: StructureElement[] filteredChildren = getChildren("jacareto.record.FocusEventRecordable");
061: FocusEventRecordable focus1 = (FocusEventRecordable) filteredChildren[0];
062: FocusEventRecordable focus2 = (FocusEventRecordable) filteredChildren[1];
063:
064: if (focus1.getID() == FocusEvent.FOCUS_GAINED) {
065: this .oldComponent = focus1.getComponentName();
066: this .newComponent = focus2.getComponentName();
067: } else {
068: this .oldComponent = focus2.getComponentName();
069: this .newComponent = focus1.getComponentName();
070: }
071: }
072:
073: /**
074: * Creates a new "focus change" structure element.
075: *
076: * @param env the environment
077: * @param children the child structure elements
078: * @param oldComponent the name of the component which has lost the focus
079: * @param newComponent the name of the component which has gained the focus
080: */
081: public FocusChange(Environment env, StructureElement[] children,
082: String oldComponent, String newComponent) {
083: super (env, children);
084: this .oldComponent = oldComponent;
085: this .newComponent = newComponent;
086: }
087:
088: /**
089: * Parses a record which is tokenized by the given record tokenizer.
090: *
091: * @param env DOCUMENT ME!
092: * @param recordTokenizer the record tokenizer
093: *
094: * @return a structure element, or <code>null</code> if this class cannot parse the record at
095: * the current position
096: */
097: public static StructureElement parse(Environment env,
098: RecordTokenizer recordTokenizer) {
099: // focus lost, focus gained
100: try {
101: FocusEventRecordable foLost = (FocusEventRecordable) recordTokenizer
102: .get();
103: FocusEventRecordable foGained = (FocusEventRecordable) recordTokenizer
104: .get(1);
105: boolean cond = true;
106: cond = cond && (foLost.getID() == FocusEvent.FOCUS_LOST);
107: cond = cond
108: && (foGained.getID() == FocusEvent.FOCUS_GAINED);
109:
110: if (cond) {
111: StructureElement[] children = new StructureElement[2];
112: children[0] = foLost;
113: children[1] = foGained;
114: recordTokenizer.forward(2);
115:
116: //return new FocusChange (env, children);
117: return new FocusChange(env, children, foLost
118: .getComponentName(), foGained
119: .getComponentName());
120: } else {
121: throw new Exception();
122: }
123: } catch (Throwable t) {
124: ;
125: }
126:
127: // mouse pressed, focus lost, focus gained, (mouse released, mouse clicked | mouse dragged, mouse released)
128: try {
129: MouseEventRecordable meFirst = (MouseEventRecordable) recordTokenizer
130: .get();
131: FocusEventRecordable foLost = (FocusEventRecordable) recordTokenizer
132: .get(1);
133: FocusEventRecordable foGained = (FocusEventRecordable) recordTokenizer
134: .get(2);
135: MouseEventRecordable meSecond = (MouseEventRecordable) recordTokenizer
136: .get(3);
137: MouseEventRecordable meThird = (MouseEventRecordable) recordTokenizer
138: .get(4);
139:
140: boolean cond = true;
141: cond = cond && (foLost.getID() == FocusEvent.FOCUS_LOST);
142: cond = cond
143: && (foGained.getID() == FocusEvent.FOCUS_GAINED);
144: cond = cond
145: && (meFirst.getID() == MouseEvent.MOUSE_PRESSED);
146: cond = cond
147: && (((meSecond.getID() == MouseEvent.MOUSE_RELEASED) && (meThird
148: .getID() == MouseEvent.MOUSE_CLICKED)) || ((meSecond
149: .getID() == MouseEvent.MOUSE_DRAGGED) && (meThird
150: .getID() == MouseEvent.MOUSE_RELEASED)));
151:
152: if (meThird.getID() == MouseEvent.MOUSE_CLICKED) {
153: cond = cond && (meFirst.getX() == meSecond.getX())
154: && (meFirst.getX() == meThird.getX());
155: cond = cond && (meFirst.getY() == meSecond.getY())
156: && (meFirst.getY() == meThird.getY());
157: }
158:
159: if (cond) {
160: StructureElement[] mouseChildren = new StructureElement[3];
161: mouseChildren[0] = meFirst;
162: mouseChildren[1] = meSecond;
163: mouseChildren[2] = meThird;
164:
165: StructureElement[] children = new StructureElement[3];
166: children[0] = new MouseClick(env, mouseChildren);
167: children[1] = foLost;
168: children[2] = foGained;
169: recordTokenizer.forward(5);
170:
171: //return new FocusChange (env, children);
172: return new FocusChange(env, children, foLost
173: .getComponentName(), foGained
174: .getComponentName());
175: } else {
176: throw new Exception();
177: }
178: } catch (Throwable t) {
179: ;
180: }
181:
182: // focus lost, mouse pressed, focus gained, (mouse drag), mouse released
183: RecordTokenizerState rtState = recordTokenizer.saveState();
184:
185: try {
186: FocusEventRecordable foLost = (FocusEventRecordable) recordTokenizer
187: .next();
188: MouseEventRecordable meFirst = (MouseEventRecordable) recordTokenizer
189: .next();
190: FocusEventRecordable foGained = (FocusEventRecordable) recordTokenizer
191: .next();
192: StructureElement drag = MouseDrag.parse(env,
193: recordTokenizer);
194: MouseEventRecordable meSecond = (MouseEventRecordable) recordTokenizer
195: .next();
196:
197: boolean cond = true;
198: cond = cond && (foLost.getID() == FocusEvent.FOCUS_LOST);
199: cond = cond
200: && (foGained.getID() == FocusEvent.FOCUS_GAINED);
201: cond = cond
202: && (meFirst.getID() == MouseEvent.MOUSE_PRESSED);
203: cond = cond
204: && (meSecond.getID() == MouseEvent.MOUSE_RELEASED);
205:
206: if (cond) {
207: StructureElement[] children = null;
208:
209: if (drag != null) {
210: children = new StructureElement[5];
211: children[3] = drag;
212: } else {
213: children = new StructureElement[4];
214: }
215:
216: children[0] = foLost;
217: children[1] = meFirst;
218: children[2] = foGained;
219: children[children.length - 1] = meSecond;
220:
221: //return new FocusChange (env, children);
222: return new FocusChange(env, children, foLost
223: .getComponentName(), foGained
224: .getComponentName());
225: } else {
226: throw new Exception();
227: }
228: } catch (Throwable t) {
229: recordTokenizer.restoreState(rtState);
230: }
231:
232: // mouse pressed, focus lost, focus gained, (mouse drag), mouse released
233: rtState = recordTokenizer.saveState();
234:
235: try {
236: MouseEventRecordable meFirst = (MouseEventRecordable) recordTokenizer
237: .next();
238: FocusEventRecordable foLost = (FocusEventRecordable) recordTokenizer
239: .next();
240: FocusEventRecordable foGained = (FocusEventRecordable) recordTokenizer
241: .next();
242: StructureElement drag = MouseDrag.parse(env,
243: recordTokenizer);
244: MouseEventRecordable meSecond = (MouseEventRecordable) recordTokenizer
245: .next();
246:
247: boolean cond = true;
248: cond = cond && (foLost.getID() == FocusEvent.FOCUS_LOST);
249: cond = cond
250: && (foGained.getID() == FocusEvent.FOCUS_GAINED);
251: cond = cond
252: && (meFirst.getID() == MouseEvent.MOUSE_PRESSED);
253: cond = cond
254: && (meSecond.getID() == MouseEvent.MOUSE_RELEASED);
255:
256: if (cond) {
257: StructureElement[] children = null;
258:
259: if (drag != null) {
260: children = new StructureElement[5];
261: children[3] = drag;
262: } else {
263: children = new StructureElement[4];
264: }
265:
266: children[0] = meFirst;
267: children[1] = foLost;
268: children[2] = foGained;
269: children[children.length - 1] = meSecond;
270:
271: //return new FocusChange (env, children);
272: return new FocusChange(env, children, foLost
273: .getComponentName(), foGained
274: .getComponentName());
275: } else {
276: throw new Exception();
277: }
278: } catch (Throwable t) {
279: recordTokenizer.restoreState(rtState);
280: }
281:
282: // tab pressed, focus lost, focus gained, tab released
283: rtState = recordTokenizer.saveState();
284:
285: try {
286: KeyEventRecordable tabPressed = (KeyEventRecordable) recordTokenizer
287: .next();
288: FocusEventRecordable foLost = (FocusEventRecordable) recordTokenizer
289: .next();
290: FocusEventRecordable foGained = (FocusEventRecordable) recordTokenizer
291: .next();
292: KeyEventRecordable tabReleased = (KeyEventRecordable) recordTokenizer
293: .next();
294:
295: boolean cond = true;
296: cond = cond && (foLost.getID() == FocusEvent.FOCUS_LOST);
297: cond = cond
298: && (foGained.getID() == FocusEvent.FOCUS_GAINED);
299: cond = cond && (tabPressed.getKeyCode() == KeyEvent.VK_TAB);
300: cond = cond
301: && (tabReleased.getKeyCode() == KeyEvent.VK_TAB);
302:
303: if (cond) {
304: StructureElement[] keyTypedChildren = new StructureElement[2];
305: keyTypedChildren[0] = tabPressed;
306: keyTypedChildren[1] = tabReleased;
307:
308: KeyTyped keyTyped = new KeyTyped(env, keyTypedChildren,
309: 0);
310:
311: StructureElement[] children = new StructureElement[3];
312:
313: children[0] = keyTyped;
314: children[1] = foLost;
315: children[2] = foGained;
316:
317: //return new FocusChange (env, children);
318: return new FocusChange(env, children, foLost
319: .getComponentName(), foGained
320: .getComponentName());
321: } else {
322: throw new Exception();
323: }
324: } catch (Throwable t) {
325: recordTokenizer.restoreState(rtState);
326: }
327:
328: // nothing found
329: return null;
330: }
331:
332: /**
333: * Returns the name of the element.
334: *
335: * @return the name
336: */
337: public String getElementName() {
338: return language.getString("Structures.FocusChange.Name");
339: }
340:
341: /**
342: * Returns a description of the element.
343: *
344: * @return the description
345: */
346: public String getElementDescription() {
347: return language.getString("Structures.FocusChange.Description");
348: }
349:
350: /**
351: * Returns the mouse click of the focus change.
352: *
353: * @return the mouse click or <code>null</code> if the focus change does not have such a child
354: */
355: public MouseClick getMouseClick() {
356: StructureElement first = getChild(0);
357:
358: if (first instanceof MouseClick) {
359: return (MouseClick) first;
360: } else {
361: return null;
362: }
363: }
364:
365: /**
366: * Returns a String which describes the content of the element shortly.
367: *
368: * @return a string with a short description of the element
369: */
370: public String toShortString() {
371: return getElementName();
372: }
373:
374: /**
375: * Returns the name of the component which has lost the focus.
376: *
377: * @return DOCUMENT ME!
378: */
379: public String getOldComponent() {
380: return oldComponent;
381: }
382:
383: /**
384: * Returns the name of the component which has gained the focus.
385: *
386: * @return DOCUMENT ME!
387: */
388: public String getNewComponent() {
389: return newComponent;
390: }
391:
392: /**
393: * Clones the element.
394: *
395: * @return DOCUMENT ME!
396: */
397: public Object clone() {
398: StructureElement[] clonedChildren = getClonedChildren();
399:
400: return new FocusChange(env, clonedChildren, oldComponent,
401: newComponent);
402: }
403:
404: /**
405: * {@inheritDoc}
406: */
407: public boolean hasProcTime() {
408: return false;
409: }
410: }
|