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.KeyEventRecordable;
031: import jacareto.system.Environment;
032:
033: import java.awt.event.KeyEvent;
034:
035: import java.util.Vector;
036:
037: /**
038: * This structure element stands for a "menu item chosen" structure element.
039: *
040: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
041: * @version 1.01
042: */
043: public class MenuItemChosen extends StructureElement {
044: /** The item name of the chosen menu item. */
045: private String itemName;
046:
047: /**
048: * Creates a new "menu opened" structure element. One child must be an action event recordable.
049: *
050: * @param env the environment
051: * @param children the child structure elements
052: */
053: public MenuItemChosen(Environment env, StructureElement[] children) {
054: super (env, children);
055: itemName = "";
056:
057: for (int i = 0; i < children.length; i++) {
058: if (children[i] instanceof ActionEventRecordable) {
059: itemName = ((ActionEventRecordable) children[i])
060: .getActionCommand();
061:
062: break;
063: }
064: }
065: }
066:
067: /**
068: * Creates a new "menu opened" structure element.
069: *
070: * @param env the environment
071: * @param children the child structure elements
072: * @param itemName the name of the chosen menu item
073: */
074: public MenuItemChosen(Environment env, StructureElement[] children,
075: String itemName) {
076: super (env, children);
077: this .itemName = itemName;
078: }
079:
080: /**
081: * Parses a record which is tokenized by the given record tokenizer.
082: *
083: * @param env DOCUMENT ME!
084: * @param recordTokenizer the record tokenizer
085: *
086: * @return a structure element, or <code>null</code> if this class cannot parse the record at
087: * the current position
088: */
089: public static StructureElement parse(Environment env,
090: RecordTokenizer recordTokenizer) {
091: StructureElement result = null;
092: Vector addedChildren = null;
093:
094: RecordTokenizerState rtState = recordTokenizer.saveState();
095:
096: // Menu Opened
097: // (Mouse Motion over Components | KeySequence)*
098: // (Focus Change)
099: // (MousePressedReleased);
100: // (Mouse Entered)
101: // (Mouse Motion)
102: // Item State Change
103: // Focus Change
104: // Action Event
105: try {
106: MenuOpened menuOpened = (MenuOpened) MenuOpened.parse(env,
107: recordTokenizer);
108:
109: if (menuOpened == null) {
110: throw new Exception();
111: } else {
112: addedChildren = new Vector(10);
113: addedChildren.add(menuOpened);
114: }
115:
116: StructureElement motion = null;
117:
118: do {
119: motion = MouseMotionOverComponents.parse(env,
120: recordTokenizer);
121:
122: if (motion == null) {
123: motion = KeySequence.parse(env, recordTokenizer);
124: }
125:
126: if (motion != null) {
127: addedChildren.add(motion);
128: }
129: } while (motion != null);
130:
131: FocusChange focusChange = (FocusChange) FocusChange.parse(
132: env, recordTokenizer);
133:
134: if (focusChange != null) {
135: addedChildren.add(focusChange);
136: }
137:
138: MousePressedReleased pressRelease = (MousePressedReleased) MousePressedReleased
139: .parse(env, recordTokenizer);
140:
141: if (pressRelease != null) {
142: addedChildren.add(pressRelease);
143: }
144:
145: StructureElement motion2 = null;
146: MouseEntered mouseEntered = null;
147: motion2 = MouseMotionOverComponents.parse(env,
148: recordTokenizer);
149:
150: if (motion2 == null) {
151: mouseEntered = (MouseEntered) MouseEntered.parse(env,
152: recordTokenizer);
153: motion2 = (MouseMotion) MouseMotion.parse(env,
154: recordTokenizer);
155: }
156:
157: if (mouseEntered != null) {
158: addedChildren.add(mouseEntered);
159: }
160:
161: if (motion2 != null) {
162: addedChildren.add(motion2);
163: }
164:
165: // item state changed
166: ItemStateChange itemChange = (ItemStateChange) ItemStateChange
167: .parse(env, recordTokenizer);
168: ItemEventRecordable itemEvent = null;
169:
170: if (itemChange == null) {
171: itemEvent = (ItemEventRecordable) recordTokenizer
172: .next();
173: }
174:
175: if (itemChange != null) {
176: addedChildren.add(itemChange);
177: } else if (itemEvent != null) {
178: addedChildren.add(itemEvent);
179: }
180:
181: // focus change
182: FocusChange focusChange2 = (FocusChange) FocusChange.parse(
183: env, recordTokenizer);
184:
185: if (focusChange2 != null) {
186: addedChildren.add(focusChange2);
187: }
188:
189: // action event recordable
190: ActionEventRecordable action = (ActionEventRecordable) recordTokenizer
191: .next();
192:
193: if (action == null) {
194: throw new Exception();
195: }
196:
197: addedChildren.add(action);
198:
199: KeyEventRecordable enterReleased = null;
200:
201: try {
202: enterReleased = (KeyEventRecordable) recordTokenizer
203: .get();
204:
205: if ((enterReleased != null)
206: && (enterReleased.getKeyCode() == KeyEvent.VK_ENTER)
207: && (enterReleased.getID() == KeyEvent.KEY_RELEASED)) {
208: recordTokenizer.forward();
209: } else {
210: enterReleased = null;
211: }
212: } catch (Exception ignored) {
213: ;
214: }
215:
216: if (enterReleased != null) {
217: addedChildren.add(enterReleased);
218: }
219:
220: boolean cond = action.getSourceClass().equals(
221: "javax.swing.JMenuItem");
222: cond = cond
223: && ((itemChange == null) || itemChange
224: .getSourceClass().equals(
225: "javax.swing.JMenu"));
226: cond = cond
227: && ((itemEvent == null) || itemEvent
228: .getSourceClass().equals(
229: "javax.swing.JMenu"));
230:
231: if (cond) {
232: String itemName = action.getActionCommand();
233:
234: //result = new MenuItemChosen (env, vectorToArray(addedChildren));
235: result = new MenuItemChosen(env,
236: vectorToArray(addedChildren), itemName);
237: } else {
238: throw new Exception();
239: }
240: } catch (Exception e) {
241: recordTokenizer.restoreState(rtState);
242: }
243:
244: return result;
245: }
246:
247: /**
248: * Returns the name of the element.
249: *
250: * @return the name
251: */
252: public String getElementName() {
253: return language.getString("Structures.MenuItemChosen.Name");
254: }
255:
256: /**
257: * Returns a description of the element.
258: *
259: * @return the description
260: */
261: public String getElementDescription() {
262: return language
263: .getString("Structures.MenuItemChosen.Description");
264: }
265:
266: /**
267: * Returns a String which describes the content of the element shortly.
268: *
269: * @return a string with a short description of the element
270: */
271: public String toShortString() {
272: return getElementName() + " (" + itemName + ")";
273: }
274:
275: /**
276: * Returns the item name of the menu item.
277: *
278: * @return DOCUMENT ME!
279: */
280: public String getMenuItem() {
281: return itemName;
282: }
283:
284: /**
285: * Clones the element.
286: *
287: * @return DOCUMENT ME!
288: */
289: public Object clone() {
290: StructureElement[] clonedChildren = getClonedChildren();
291:
292: return new MenuItemChosen(env, clonedChildren, itemName);
293: }
294: }
|