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.ItemEventRecordable;
030: import jacareto.record.MouseEventRecordable;
031: import jacareto.system.Environment;
032:
033: import java.awt.event.ItemEvent;
034: import java.awt.event.MouseEvent;
035:
036: import java.util.Vector;
037:
038: /**
039: * An item has been selected or deselected.
040: *
041: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
042: * @version 1.01
043: */
044: public class ItemStateChange extends StructureElement {
045: /** Whether or not the item has been selected. */
046: private boolean selected;
047:
048: /** The action command (if there is one). */
049: private String actionName;
050:
051: /** The item event recordable contained in this item state change. */
052: private ItemEventRecordable itemEventRecordable;
053:
054: /** The source name. */
055: private String sourceName;
056:
057: /** The source class. */
058: private String sourceClass;
059:
060: /**
061: * Creates a new "item state change" structure element. One of the children must be an item
062: * event recordable, one should be an action event recordable (but has not to).
063: *
064: * @param env the environment
065: * @param children the child structure elements
066: */
067: public ItemStateChange(Environment env, StructureElement[] children) {
068: super (env, children);
069:
070: ActionEventRecordable action = null;
071:
072: for (int i = 0; i < children.length; i++) {
073: if (children[i] instanceof ItemEventRecordable) {
074: itemEventRecordable = (ItemEventRecordable) children[i];
075: } else if (children[i] instanceof ActionEventRecordable) {
076: action = (ActionEventRecordable) children[i];
077: }
078: }
079:
080: this .sourceName = itemEventRecordable.getSourceName();
081: this .sourceClass = itemEventRecordable.getSourceClass();
082: selected = itemEventRecordable.getStateChange() == ItemEvent.SELECTED;
083: actionName = "";
084:
085: if (action != null) {
086: actionName = action.getActionCommand();
087: }
088: }
089:
090: /**
091: * Creates a new "item state change" structure element.
092: *
093: * @param env the environment
094: * @param children the child structure elements
095: * @param actionName the action command (if there is one)
096: * @param selected whether or not the item has been selected
097: */
098: public ItemStateChange(Environment env,
099: StructureElement[] children, String actionName,
100: boolean selected) {
101: super (env, children);
102: this .selected = selected;
103: this .actionName = actionName;
104:
105: for (int i = 0; i < children.length; i++) {
106: if (children[i] instanceof ItemEventRecordable) {
107: itemEventRecordable = (ItemEventRecordable) children[i];
108:
109: break;
110: }
111: }
112:
113: this .sourceName = itemEventRecordable.getSourceName();
114: this .sourceClass = itemEventRecordable.getSourceClass();
115: }
116:
117: /**
118: * Parses a record which is tokenized by the given record tokenizer.
119: *
120: * @param env DOCUMENT ME!
121: * @param recordTokenizer the record tokenizer
122: *
123: * @return a structure element, or <code>null</code> if this class cannot parse the record at
124: * the current position
125: */
126: public static StructureElement parse(Environment env,
127: RecordTokenizer recordTokenizer) {
128: StructureElement result = null;
129: StructureElement[] children = null;
130:
131: ItemEventRecordable itemEvent = null;
132: ActionEventRecordable action = null;
133:
134: RecordTokenizerState rtState = recordTokenizer.saveState();
135:
136: // mouse pressed, item event, focus change, mouse released, mouse clicked
137: try {
138: MouseEventRecordable mousePressed = (MouseEventRecordable) recordTokenizer
139: .next();
140: itemEvent = (ItemEventRecordable) recordTokenizer.next();
141:
142: FocusChange focusChange = (FocusChange) FocusChange.parse(
143: env, recordTokenizer);
144: MouseEventRecordable mouseReleased = (MouseEventRecordable) recordTokenizer
145: .next();
146: MouseEventRecordable mouseClicked = (MouseEventRecordable) recordTokenizer
147: .next();
148:
149: boolean cond = itemEvent != null;
150: cond = cond
151: && ((action == null) || itemEvent.getSourceName()
152: .equals(action.getSourceName()));
153: cond = cond
154: && (mousePressed.getID() == MouseEvent.MOUSE_PRESSED)
155: && (mouseReleased.getID() == MouseEvent.MOUSE_RELEASED)
156: && (mouseClicked.getID() == MouseEvent.MOUSE_CLICKED);
157:
158: if (cond) {
159: StructureElement[] mouseChildren = new StructureElement[3];
160: mouseChildren[0] = mousePressed;
161: mouseChildren[1] = mouseReleased;
162: mouseChildren[2] = mouseClicked;
163:
164: MouseClick mouseClick = new MouseClick(env,
165: mouseChildren);
166: Vector addedChildren = new Vector(3);
167: addedChildren.add(mouseClick);
168:
169: if (focusChange != null) {
170: addedChildren.add(focusChange);
171: }
172:
173: addedChildren.add(itemEvent);
174: children = vectorToArray(addedChildren);
175: } else {
176: throw new Exception();
177: }
178: } catch (Throwable t) {
179: recordTokenizer.restoreState(rtState);
180: }
181:
182: // (focus change), item event, (action event)
183: if (children == null) {
184: try {
185: FocusChange focusChange = (FocusChange) FocusChange
186: .parse(env, recordTokenizer);
187: itemEvent = (ItemEventRecordable) recordTokenizer
188: .next();
189: action = null;
190:
191: StructureElement tmp = recordTokenizer.get();
192:
193: if ((tmp != null)
194: && tmp instanceof ActionEventRecordable) {
195: action = (ActionEventRecordable) tmp;
196: recordTokenizer.forward();
197: }
198:
199: boolean cond = itemEvent != null;
200: cond = cond
201: && ((action == null) || itemEvent
202: .getSourceName().equals(
203: action.getSourceName()));
204:
205: if (cond) {
206: Vector addedChildren = new Vector(5);
207:
208: if (focusChange != null) {
209: addedChildren.add(focusChange);
210: }
211:
212: addedChildren.add(itemEvent);
213:
214: if (action != null) {
215: addedChildren.add(action);
216: }
217:
218: children = vectorToArray(addedChildren);
219: } else {
220: throw new Exception();
221: }
222: } catch (Throwable t) {
223: recordTokenizer.restoreState(rtState);
224: }
225: }
226:
227: // Create the result;
228: if (children != null) {
229: boolean selected = itemEvent.getStateChange() == ItemEvent.SELECTED;
230: String actionName = "";
231:
232: if (action != null) {
233: actionName = action.getActionCommand();
234: }
235:
236: //result = new ItemStateChange (env, children);
237: result = new ItemStateChange(env, children, actionName,
238: selected);
239: } else {
240: recordTokenizer.restoreState(rtState);
241: }
242:
243: return result;
244: }
245:
246: /**
247: * Returns the name of the element.
248: *
249: * @return the name
250: */
251: public String getElementName() {
252: return language.getString("Structures.ItemStateChange.Name");
253: }
254:
255: /**
256: * Returns a description of the element.
257: *
258: * @return the description
259: */
260: public String getElementDescription() {
261: return language
262: .getString("Structures.ItemStateChange.Description");
263: }
264:
265: /**
266: * Returns a String which describes the content of the element shortly.
267: *
268: * @return a string with a short description of the element
269: */
270: public String toShortString() {
271: String result = "";
272:
273: if ((actionName != null) && !actionName.equals("")) {
274: result += ("\"" + actionName + "\" ");
275:
276: if (selected) {
277: result += language
278: .getString("Structures.ItemStateChange.Selected");
279: } else {
280: result += language
281: .getString("Structures.ItemStateChange.Deselected");
282: }
283: } else {
284: if (selected) {
285: result = language
286: .getString("Structures.ItemStateChange.ItemSelected");
287: } else {
288: result = language
289: .getString("Structures.ItemStateChange.ItemDeselected");
290: }
291: }
292:
293: return result;
294: }
295:
296: /**
297: * Returns whether or not the item has been selected
298: *
299: * @return DOCUMENT ME!
300: */
301: public boolean isSelected() {
302: return selected;
303: }
304:
305: /**
306: * Returns the action command (if there is one).
307: *
308: * @return DOCUMENT ME!
309: */
310: public String getActionName() {
311: return actionName;
312: }
313:
314: /**
315: * Returns the source name.
316: *
317: * @return DOCUMENT ME!
318: */
319: public String getSourceName() {
320: return sourceName;
321: }
322:
323: /**
324: * Returns the source class.
325: *
326: * @return DOCUMENT ME!
327: */
328: public String getSourceClass() {
329: return sourceClass;
330: }
331:
332: /**
333: * Returns the item event recordable contained in this structure element.
334: *
335: * @return DOCUMENT ME!
336: */
337: public ItemEventRecordable getItemEventRecordable() {
338: return itemEventRecordable;
339: }
340:
341: /**
342: * Clones the element.
343: *
344: * @return DOCUMENT ME!
345: */
346: public Object clone() {
347: StructureElement[] clonedChildren = getClonedChildren();
348:
349: return new ItemStateChange(env, clonedChildren, actionName,
350: selected);
351: }
352:
353: public boolean hasProcTime() {
354: return true;
355: }
356: }
|