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.KeyEventRecordable;
029: import jacareto.system.Environment;
030: import jacareto.toolkit.StringToolkit;
031:
032: import java.awt.event.KeyEvent;
033:
034: import java.util.Enumeration;
035: import java.util.Vector;
036:
037: /**
038: * This structure element stands for a key input sequence.
039: *
040: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
041: * @version 1.0
042: */
043: public class KeySequence extends StructureElement implements
044: KeyStringOwner {
045: /**
046: * Creates a new "key sequence" structure element.
047: *
048: * @param env the environment
049: * @param children the child structure elements
050: */
051: public KeySequence(Environment env, StructureElement[] children) {
052: super (env, children);
053: }
054:
055: /**
056: * Parses a record which is tokenized by the given record tokenizer.
057: *
058: * @param env DOCUMENT ME!
059: * @param recordTokenizer the record tokenizer
060: *
061: * @return a structure element, or <code>null</code> if this class cannot parse the record at
062: * the current position
063: */
064: public static StructureElement parse(Environment env,
065: RecordTokenizer recordTokenizer) {
066: try {
067: // Test if the next part could be interesting
068: StructureElement next = recordTokenizer.get();
069:
070: if ((next == null) || !(next instanceof KeyEventRecordable)) {
071: return null;
072: }
073:
074: Vector addedChildren = new Vector(5, 5);
075:
076: do {
077: next = null;
078:
079: next = ShiftAndKeys.parse(env, recordTokenizer);
080:
081: if (next == null) {
082: RecordTokenizerState StateBeforeMod = recordTokenizer
083: .saveState();
084: next = ModifiedKeySequence.parse(env,
085: recordTokenizer);
086:
087: if (next != null) {
088: recordTokenizer.restoreState(StateBeforeMod);
089:
090: break;
091: }
092: }
093:
094: if (next != null) {
095: addedChildren.add(next);
096: } else {
097: next = KeyTyped.parse(env, recordTokenizer);
098:
099: if (next != null) {
100: addedChildren.add(next);
101: } else {
102: next = recordTokenizer.get();
103:
104: if ((next != null)
105: && next instanceof KeyEventRecordable) {
106: addedChildren.add(next);
107: recordTokenizer.forward();
108: } else {
109: next = null;
110: }
111: }
112: }
113: } while ((next != null) && recordTokenizer.hasMore());
114:
115: if (addedChildren.size() == 0) {
116: throw new Exception();
117: } else if (addedChildren.size() == 1) {
118: return (StructureElement) addedChildren.get(0);
119: } else {
120: return new KeySequence(env,
121: vectorToArray(addedChildren));
122: }
123: } catch (Throwable t) {
124: return null;
125: }
126: }
127:
128: /**
129: * Returns the name of the element.
130: *
131: * @return the name
132: */
133: public String getElementName() {
134: return language.getString("Structures.KeySequence.Name");
135: }
136:
137: /**
138: * Returns a description of the element.
139: *
140: * @return the description
141: */
142: public String getElementDescription() {
143: return language.getString("Structures.KeySequence.Description");
144: }
145:
146: /**
147: * Returns a String which describes the content of the element shortly.
148: *
149: * @return a string with a short description of the element
150: */
151: public String toShortString() {
152: return getInterpretedKeyString() + " "
153: + language.getString("Structures.KeySequence.Typed");
154: }
155:
156: /**
157: * Returns a key string.
158: *
159: * @return the key sequence as string
160: */
161: public String getInterpretedKeyString() {
162: String result = "";
163: String alternativeResult = "";
164: boolean usesAlternativeResult = false;
165: Enumeration enumeration = children();
166:
167: while (enumeration.hasMoreElements()) {
168: StructureElement element = (StructureElement) enumeration
169: .nextElement();
170:
171: if (element instanceof KeyStringOwner
172: && !((element instanceof KeyEventRecordable) && (((KeyEventRecordable) element)
173: .getID() == KeyEvent.KEY_RELEASED))) {
174: result += ((KeyStringOwner) element)
175: .getInterpretedKeyString();
176: alternativeResult += ("[:"
177: + ((KeyStringOwner) element).getKeyString() + ":]");
178: }
179: }
180:
181: if (!StringToolkit.isDefined(result)) {
182: result = alternativeResult;
183: usesAlternativeResult = true;
184: }
185:
186: if (result.length() > 30) {
187: result = result.substring(0, 30) + "...";
188: }
189:
190: if (!usesAlternativeResult) {
191: result = "\"" + result + "\"";
192: }
193:
194: return result;
195: }
196:
197: /**
198: * Returns a key string.
199: *
200: * @return the key sequence as string
201: */
202: public String getKeyString() {
203: return getInterpretedKeyString();
204: }
205:
206: /**
207: * Clones the element.
208: *
209: * @return DOCUMENT ME!
210: */
211: public Object clone() {
212: StructureElement[] clonedChildren = getClonedChildren();
213:
214: return new KeySequence(env, clonedChildren);
215: }
216:
217: public boolean hasProcTime() {
218: return false;
219: }
220: }
|