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.ActionEventRecordable;
029: import jacareto.record.FocusEventRecordable;
030: import jacareto.record.MouseEventRecordable;
031: import jacareto.record.Recordable;
032: import jacareto.system.Environment;
033:
034: import java.awt.event.FocusEvent;
035: import java.awt.event.MouseEvent;
036:
037: /**
038: * A button has been activated
039: *
040: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
041: * @version 1.0
042: */
043: public class ButtonActivated extends StructureElement {
044: /** The action command. */
045: private String actionCommand;
046:
047: /** The source name. */
048: private String sourceName;
049:
050: /**
051: * Creates a new "button activated" structure element.
052: *
053: * @param env the environment
054: * @param children the child structure elements
055: */
056: public ButtonActivated(Environment env, StructureElement[] children) {
057: super (env, children);
058:
059: ActionEventRecordable actionRecordable = (ActionEventRecordable) children[children.length - 1];
060: actionCommand = actionRecordable.getActionCommand();
061: sourceName = actionRecordable.getSourceName();
062: }
063:
064: /**
065: * Parses a record which is tokenized by the given record tokenizer.
066: *
067: * @param env DOCUMENT ME!
068: * @param recordTokenizer the record tokenizer
069: *
070: * @return a structure element, or <code>null</code> if this class cannot parse the record at
071: * the current position
072: */
073: public static StructureElement parse(Environment env,
074: RecordTokenizer recordTokenizer) {
075: StructureElement result = null;
076: StructureElement[] children = null;
077:
078: RecordTokenizerState rtState = recordTokenizer.saveState();
079:
080: // (Focus Change)
081: // Action event on a button
082: try {
083: StructureElement focusChange = FocusChange.parse(env,
084: recordTokenizer);
085: ActionEventRecordable action = (ActionEventRecordable) recordTokenizer
086: .next();
087:
088: if (action.getSourceClass().equals("javax.swing.JButton")
089: || action.getSourceClass()
090: .equals("java.awt.Button")) {
091: if (focusChange != null) {
092: children = new StructureElement[2];
093: children[0] = focusChange;
094: children[1] = action;
095: } else {
096: children = new StructureElement[1];
097: children[0] = action;
098: }
099: } else {
100: throw new Exception();
101: }
102: } catch (Throwable t) {
103: recordTokenizer.restoreState(rtState);
104: }
105:
106: // mouse pressed, ((focus lost), focus gained), mouse released, action event, mouse clicked
107: if (children == null) {
108: try {
109: MouseEventRecordable mousePressed = (MouseEventRecordable) recordTokenizer
110: .next();
111:
112: Recordable focusFirst = null;
113: Recordable focusSecond = null;
114:
115: if (recordTokenizer.hasMore()) {
116: focusFirst = recordTokenizer.get();
117: }
118:
119: if (recordTokenizer.hasMore(1)) {
120: focusSecond = recordTokenizer.get(1);
121: }
122:
123: if ((focusFirst != null)
124: && focusFirst instanceof FocusEventRecordable) {
125: if ((focusSecond != null)
126: && focusSecond instanceof FocusEventRecordable
127: && (((FocusEventRecordable) focusFirst)
128: .getID() == FocusEvent.FOCUS_LOST)
129: && (((FocusEventRecordable) focusSecond)
130: .getID() == FocusEvent.FOCUS_GAINED)) {
131: recordTokenizer.forward(2);
132: } else {
133: recordTokenizer.forward(1);
134: focusSecond = null;
135: }
136: } else {
137: focusFirst = null;
138: focusSecond = null;
139: }
140:
141: MouseEventRecordable mouseReleased = (MouseEventRecordable) recordTokenizer
142: .next();
143: ActionEventRecordable action = (ActionEventRecordable) recordTokenizer
144: .next();
145: MouseEventRecordable mouseClicked = (MouseEventRecordable) recordTokenizer
146: .next();
147:
148: if ((mousePressed.getID() == MouseEvent.MOUSE_PRESSED)
149: && (mouseReleased.getID() == MouseEvent.MOUSE_RELEASED)
150: && (mouseClicked.getID() == MouseEvent.MOUSE_CLICKED)
151: && (action.getSourceClass().equals(
152: "javax.swing.JButton") || action
153: .getSourceClass().equals(
154: "java.awt.Button"))) {
155: StructureElement[] clickChildren = new StructureElement[3];
156: clickChildren[0] = mousePressed;
157: clickChildren[1] = mouseReleased;
158: clickChildren[2] = mouseClicked;
159:
160: MouseClick mouseClick = new MouseClick(env,
161: clickChildren);
162:
163: if (focusSecond != null) {
164: StructureElement[] focusChangeChildren = new StructureElement[3];
165: focusChangeChildren[0] = mouseClick;
166: focusChangeChildren[1] = focusFirst;
167: focusChangeChildren[2] = focusSecond;
168:
169: FocusChange focusChange = new FocusChange(env,
170: focusChangeChildren,
171: ((FocusEventRecordable) focusFirst)
172: .getSourceName(),
173: ((FocusEventRecordable) focusSecond)
174: .getSourceName());
175: children = new StructureElement[2];
176: children[0] = focusChange;
177: children[1] = action;
178: } else if (focusFirst != null) {
179: children = new StructureElement[3];
180: children[0] = mouseClick;
181: children[1] = focusFirst;
182: children[2] = action;
183: } else {
184: children = new StructureElement[2];
185: children[0] = mouseClick;
186: children[1] = action;
187: }
188: } else {
189: throw new Exception();
190: }
191: } catch (Throwable t) {
192: recordTokenizer.restoreState(rtState);
193: }
194: }
195:
196: // mouse click, action event
197: if (children == null) {
198: try {
199: StructureElement mouseClick = MouseClick.parse(env,
200: recordTokenizer);
201:
202: if (mouseClick != null) {
203: ActionEventRecordable action = (ActionEventRecordable) recordTokenizer
204: .next();
205:
206: if (action.getSourceClass().equals(
207: "javax.swing.JButton")
208: || action.getSourceClass().equals(
209: "java.awt.Button")) {
210: children = new StructureElement[2];
211: children[0] = mouseClick;
212: children[1] = action;
213: } else {
214: throw new Exception();
215: }
216: }
217: } catch (Throwable t) {
218: recordTokenizer.restoreState(rtState);
219: }
220: }
221:
222: // mouse drag, action event
223: if (children == null) {
224: try {
225: MouseDrag mouseDrag = (MouseDrag) MouseDrag.parse(env,
226: recordTokenizer);
227:
228: if (mouseDrag != null) {
229: ActionEventRecordable action = (ActionEventRecordable) recordTokenizer
230: .next();
231:
232: if ((action.getSourceClass().equals(
233: "javax.swing.JButton") || action
234: .getSourceClass().equals("java.awt.Button"))
235: && mouseDrag.getMousePressed()
236: .getSourceName().equals(
237: action.getSourceName())) {
238: children = new StructureElement[2];
239: children[0] = mouseDrag;
240: children[1] = action;
241: } else {
242: throw new Exception();
243: }
244: }
245: } catch (Throwable t) {
246: recordTokenizer.restoreState(rtState);
247: }
248: }
249:
250: // Create the result;
251: if (children != null) {
252: result = new ButtonActivated(env, children);
253: } else {
254: recordTokenizer.restoreState(rtState);
255: }
256:
257: return result;
258: }
259:
260: /**
261: * Returns the action command.
262: *
263: * @return DOCUMENT ME!
264: */
265: public String getActionCommand() {
266: return actionCommand;
267: }
268:
269: /**
270: * Returns the name of the source component.
271: *
272: * @return DOCUMENT ME!
273: */
274: public String getSourceName() {
275: return sourceName;
276: }
277:
278: /**
279: * Returns the name of the element.
280: *
281: * @return the name
282: */
283: public String getElementName() {
284: return language.getString("Structures.ButtonActivated.Name")
285: + " (" + actionCommand + ")";
286: }
287:
288: /**
289: * Returns a description of the element.
290: *
291: * @return the description
292: */
293: public String getElementDescription() {
294: return language
295: .getString("Structures.ButtonActivated.Description");
296: }
297:
298: /**
299: * Returns a String which describes the content of the element shortly.
300: *
301: * @return a string with a short description of the element
302: */
303: public String toShortString() {
304: return getElementName();
305: }
306:
307: /**
308: * Clones the element.
309: *
310: * @return DOCUMENT ME!
311: */
312: public Object clone() {
313: StructureElement[] clonedChildren = getClonedChildren();
314:
315: return new ButtonActivated(env, clonedChildren);
316: }
317:
318: /**
319: * {@inheritDoc}
320: */
321: public boolean hasProcTime() {
322: return true;
323: }
324: }
|