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.system.Environment;
030:
031: import java.util.Vector;
032:
033: /**
034: * This structure element stands for a "popup menu item chosen" structure element.
035: *
036: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
037: * @version 1.0
038: */
039: public class PopupMenuItemChosen extends StructureElement {
040: /** The item name of the chosen menu item. */
041: private String itemName;
042:
043: /**
044: * Creates a new "popup menu opened" structure element. One child must be an action event
045: * recordable.
046: *
047: * @param env the environment
048: * @param children the child structure elements
049: */
050: public PopupMenuItemChosen(Environment env,
051: StructureElement[] children) {
052: super (env, children);
053: itemName = "";
054:
055: for (int i = 0; i < children.length; i++) {
056: if (children[i] instanceof ActionEventRecordable) {
057: itemName = ((ActionEventRecordable) children[i])
058: .getActionCommand();
059:
060: break;
061: }
062: }
063: }
064:
065: /**
066: * Creates a new "popup menu opened" structure element.
067: *
068: * @param env the environment
069: * @param children the child structure elements
070: * @param itemName the name of the chosen menu item
071: */
072: public PopupMenuItemChosen(Environment env,
073: StructureElement[] children, String itemName) {
074: super (env, children);
075: this .itemName = itemName;
076: }
077:
078: /**
079: * Parses a record which is tokenized by the given record tokenizer.
080: *
081: * @param env DOCUMENT ME!
082: * @param recordTokenizer the record tokenizer
083: *
084: * @return a structure element, or <code>null</code> if this class cannot parse the record at
085: * the current position
086: */
087: public static StructureElement parse(Environment env,
088: RecordTokenizer recordTokenizer) {
089: StructureElement result = null;
090: Vector addedChildren = new Vector(5, 5);
091:
092: RecordTokenizerState rtState = recordTokenizer.saveState();
093:
094: try {
095: PopupMenuOpened opened = (PopupMenuOpened) PopupMenuOpened
096: .parse(env, recordTokenizer);
097:
098: if (opened == null) {
099: throw new Exception();
100: } else {
101: addedChildren.add(opened);
102: }
103:
104: MouseMotionOverComponents motion = (MouseMotionOverComponents) MouseMotionOverComponents
105: .parse(env, recordTokenizer);
106:
107: if (motion == null) {
108: throw new Exception();
109: } else {
110: addedChildren.add(motion);
111: }
112:
113: FocusChange focus = (FocusChange) FocusChange.parse(env,
114: recordTokenizer);
115:
116: if (focus == null) {
117: throw new Exception();
118: } else {
119: addedChildren.add(focus);
120: }
121:
122: MouseEntered entered = (MouseEntered) MouseEntered.parse(
123: env, recordTokenizer);
124:
125: if (entered == null) {
126: throw new Exception();
127: } else {
128: addedChildren.add(entered);
129: }
130:
131: MouseMotion motion2 = (MouseMotion) MouseMotion.parse(env,
132: recordTokenizer);
133:
134: if (motion2 == null) {
135: throw new Exception();
136: } else {
137: addedChildren.add(motion2);
138: }
139:
140: int actionCount = 0;
141: ActionEventRecordable action = null;
142:
143: do {
144: action = null;
145:
146: StructureElement tmp = recordTokenizer.get();
147:
148: if ((tmp != null)
149: && tmp instanceof ActionEventRecordable) {
150: action = (ActionEventRecordable) tmp;
151: }
152:
153: if ((action != null)
154: && action.getSourceClass().equals(
155: "javax.swing.JMenuItem")) {
156: recordTokenizer.forward();
157: addedChildren.add(action);
158: actionCount++;
159: } else if (actionCount == 0) {
160: throw new Exception();
161: }
162: } while (action != null);
163:
164: result = new PopupMenuItemChosen(env,
165: vectorToArray(addedChildren));
166: } catch (Throwable t) {
167: recordTokenizer.restoreState(rtState);
168: }
169:
170: return result;
171: }
172:
173: /**
174: * Returns the name of the element.
175: *
176: * @return the name
177: */
178: public String getElementName() {
179: return language
180: .getString("Structures.PopupMenuItemChosen.Name");
181: }
182:
183: /**
184: * Returns a description of the element.
185: *
186: * @return the description
187: */
188: public String getElementDescription() {
189: return language
190: .getString("Structures.PopupMenuItemChosen.Description");
191: }
192:
193: /**
194: * Returns a String which describes the content of the element shortly.
195: *
196: * @return a string with a short description of the element
197: */
198: public String toShortString() {
199: return getElementName() + " (" + itemName + ")";
200: }
201:
202: /**
203: * Returns the item name of the menu item.
204: *
205: * @return DOCUMENT ME!
206: */
207: public String getMenuItem() {
208: return itemName;
209: }
210:
211: /**
212: * Clones the element.
213: *
214: * @return DOCUMENT ME!
215: */
216: public Object clone() {
217: StructureElement[] clonedChildren = getClonedChildren();
218:
219: return new PopupMenuItemChosen(env, clonedChildren, itemName);
220: }
221: }
|