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.record.KeyEventRecordable;
028: import jacareto.system.Environment;
029: import jacareto.toolkit.CharacterToolkit;
030:
031: import java.awt.event.KeyEvent;
032:
033: /**
034: * This structure element stands for a typed key.
035: *
036: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
037: * @version 1.0
038: */
039: public class KeyTyped extends StructureElement implements
040: KeyStringOwner {
041: /** The key code. */
042: private int keyCode;
043:
044: /** The key char. */
045: private char keyChar;
046:
047: /** The index (just for cloning). */
048: private int index;
049:
050: /**
051: * Creates a new "key typed" structure element. The first child must be a key event recordable.
052: *
053: * @param env the environment
054: * @param children the child structure elements
055: */
056: public KeyTyped(Environment env, StructureElement[] children) {
057: super (env, children);
058:
059: this .index = 0;
060:
061: KeyEventRecordable keyEvent = (KeyEventRecordable) children[0];
062: keyCode = keyEvent.getKeyCode();
063: keyChar = keyEvent.getKeyChar();
064: }
065:
066: /**
067: * Creates a new "key typed" structure element.
068: *
069: * @param env the environment
070: * @param children the child structure elements
071: * @param index the index of the key event recordable int the children array where to take the
072: * data (keyChar, keyCode) from
073: */
074: public KeyTyped(Environment env, StructureElement[] children,
075: int index) {
076: super (env, children);
077:
078: this .index = index;
079:
080: KeyEventRecordable keyEvent = (KeyEventRecordable) children[index];
081: keyCode = keyEvent.getKeyCode();
082: keyChar = keyEvent.getKeyChar();
083: }
084:
085: /**
086: * Parses a record which is tokenized by the given record tokenizer.
087: *
088: * @param env DOCUMENT ME!
089: * @param recordTokenizer the record tokenizer
090: *
091: * @return a structure element, or <code>null</code> if this class cannot parse the record at
092: * the current position
093: */
094: public static StructureElement parse(Environment env,
095: RecordTokenizer recordTokenizer) {
096: try {
097: KeyEventRecordable first = (KeyEventRecordable) recordTokenizer
098: .get();
099: KeyEventRecordable second = (KeyEventRecordable) recordTokenizer
100: .get(1);
101:
102: boolean cond = true;
103:
104: cond = cond && (first.getID() == KeyEvent.KEY_PRESSED);
105: cond = cond && (second.getID() == KeyEvent.KEY_RELEASED);
106: cond = cond && (first.getKeyCode() == second.getKeyCode());
107:
108: if (cond) {
109: StructureElement[] children = new StructureElement[2];
110: children[0] = first;
111: children[1] = second;
112: recordTokenizer.forward(2);
113:
114: return new KeyTyped(env, children, 0);
115: } else {
116: return null;
117: }
118: } catch (Throwable t) {
119: return null;
120: }
121: }
122:
123: /**
124: * Returns the name of the element.
125: *
126: * @return the name
127: */
128: public String getElementName() {
129: return language.getString("Structures.KeyTyped.Name");
130: }
131:
132: /**
133: * Returns a description of the element.
134: *
135: * @return the description
136: */
137: public String getElementDescription() {
138: return language.getString("Structures.KeyTyped.Description");
139: }
140:
141: /**
142: * Returns a String which describes the content of the element shortly.
143: *
144: * @return a string with a short description of the element
145: */
146: public String toShortString() {
147: return getElementName() + " (" + getKeyString() + ")";
148: }
149:
150: /**
151: * Returns a key string.
152: *
153: * @return the key sequence as string
154: */
155: public String getKeyString() {
156: return CharacterToolkit.getString(keyChar, keyCode);
157: }
158:
159: /**
160: * Returns a key string.
161: *
162: * @return the key sequence as string
163: */
164: public String getInterpretedKeyString() {
165: return CharacterToolkit.getInterpretedString(keyChar, keyCode);
166: }
167:
168: /**
169: * Returns the key code.
170: *
171: * @return DOCUMENT ME!
172: */
173: public int getKeyCode() {
174: return keyCode;
175: }
176:
177: /**
178: * Returns the key char.
179: *
180: * @return DOCUMENT ME!
181: */
182: public char getKeyChar() {
183: return keyChar;
184: }
185:
186: /**
187: * Clones the element.
188: *
189: * @return DOCUMENT ME!
190: */
191: public Object clone() {
192: StructureElement[] clonedChildren = getClonedChildren();
193:
194: return new KeyTyped(env, clonedChildren, index);
195: }
196:
197: public boolean hasProcTime() {
198: return true;
199: }
200: }
|