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.record;
025:
026: import jacareto.comp.Components;
027: import jacareto.struct.KeyStringOwner;
028: import jacareto.system.Environment;
029: import jacareto.toolkit.CharacterToolkit;
030:
031: import java.awt.AWTEvent;
032: import java.awt.Component;
033: import java.awt.event.KeyEvent;
034:
035: import javax.swing.JLabel;
036:
037: /**
038: * This class represents key events which can be recorded.
039: *
040: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
041: * @version 1.05
042: */
043: public class KeyEventRecordable extends InputEventRecordable implements
044: KeyStringOwner {
045: /** The key code. */
046: private int keyCode;
047:
048: /** The key char. */
049: private char keyChar;
050:
051: /**
052: * Creates a new key event recordable with the specified values.
053: *
054: * @param env the environment
055: * @param ID the event id
056: * @param sourceName the name of the events source
057: * @param sourceClass the classname of the event's source
058: * @param duration the duration
059: * @param procTime DOCUMENT ME!
060: * @param componentName the component's name
061: * @param rootName the name of the root component
062: * @param modifiers the modifiers of the event
063: * @param when the time when the event has occured
064: * @param isConsumed whether the event is consumed or not
065: * @param keyCode the key code
066: * @param keyChar the key char
067: */
068: public KeyEventRecordable(Environment env, int ID,
069: String sourceName, String sourceClass, long duration,
070: long procTime, String componentName, String rootName,
071: int modifiers, long when, boolean isConsumed, int keyCode,
072: char keyChar) {
073: super (env, ID, sourceName, sourceClass, duration, procTime,
074: componentName, rootName, modifiers, when, isConsumed);
075: setKeyCode(keyCode);
076: setKeyChar(keyChar);
077: }
078:
079: /**
080: * Creates a new key event recordable with the values of the given event and additional
081: * information.
082: *
083: * @param env the environment
084: * @param keyEvent the key event to take the main information from
085: * @param components the instance which knows the components
086: * @param duration the duration
087: * @param procTime DOCUMENT ME!
088: */
089: public KeyEventRecordable(Environment env, KeyEvent keyEvent,
090: Components components, long duration, long procTime) {
091: this (env,
092: keyEvent.getID(),
093: components.getName((Component) keyEvent.getSource()), // The internally generated name of the event source
094: Components.getClassName(keyEvent.getSource()),
095: duration, procTime,
096: keyEvent.getComponent().getName(), // The name of the component set by the programmer
097:
098: // of the captured application
099: components.getName(components.getRoot(keyEvent
100: .getComponent())), keyEvent.getModifiers(),
101: keyEvent.getWhen(), keyEvent.isConsumed(), keyEvent
102: .getKeyCode(), keyEvent.getKeyChar());
103:
104: //
105: // in swing not all key events will be consumed,
106: // so they must be consumed manually (otherwise they would
107: // be recorded twice)
108: //
109: int eventId = keyEvent.getID();
110: int keyCode = keyEvent.getKeyCode();
111: int modifiers = keyEvent.getModifiers();
112:
113: boolean isSpecialKey = (keyCode == KeyEvent.VK_LEFT)
114: || (keyCode == KeyEvent.VK_RIGHT)
115: || (keyCode == KeyEvent.VK_UP)
116: || (keyCode == KeyEvent.VK_DOWN)
117: || (keyCode == KeyEvent.VK_DELETE)
118: || (keyCode == KeyEvent.VK_INSERT)
119: || (keyCode == KeyEvent.VK_HOME)
120: || (keyCode == KeyEvent.VK_END)
121: || (keyCode == KeyEvent.VK_ENTER)
122: || (keyCode == KeyEvent.VK_TAB)
123: || (keyCode == KeyEvent.VK_PAGE_DOWN)
124: || (keyCode == KeyEvent.VK_PAGE_UP)
125: || (keyCode == KeyEvent.VK_BACK_SPACE);
126:
127: // At first backspace was in this list, then somewhere occured
128: // problems, then backspace has been removed from the list,
129: // then in the text area of JEdit backspaces have not been
130: // submitted to the target application during the capture
131: // process, so it has been added again.
132: boolean isModifiingKey = (keyCode == KeyEvent.VK_CONTROL)
133: || (keyCode == KeyEvent.VK_ALT)
134: || (keyCode == KeyEvent.VK_ALT_GRAPH)
135: || (keyCode == KeyEvent.VK_META)
136: || (keyCode == KeyEvent.VK_SHIFT);
137:
138: // CTRL-C, CTRL-V, ... for example
139: boolean isModifiedNormal = ((modifiers & KeyEvent.CTRL_MASK) > 0)
140: && !isSpecialKey && !isModifiingKey;
141:
142: if (!isModifiedNormal
143: && ((eventId == KeyEvent.KEY_RELEASED) || !isSpecialKey)) {
144: keyEvent.consume();
145: }
146: }
147:
148: /**
149: * Creates a key event recordable with default values and no environment. The environment
150: * should be defined with the method {@link
151: * jacareto.system.EnvironmentMember#setEnvironment(Environment)} before environment instances
152: * will be accessed.
153: */
154: public KeyEventRecordable() {
155: this (null, 0, "", "", 0, 0, "", "", 0, 0, false, 0, ' ');
156: }
157:
158: /**
159: * Sets the key code of the key event.
160: *
161: * @param keyCode the key code
162: */
163: public void setKeyCode(int keyCode) {
164: this .keyCode = keyCode;
165: fireValuesChanged();
166: }
167:
168: /**
169: * Returns the key code.
170: *
171: * @return the key code
172: */
173: public int getKeyCode() {
174: return keyCode;
175: }
176:
177: /**
178: * Sets the key cchar of the key event.
179: *
180: * @param keyChar the key char
181: */
182: public void setKeyChar(char keyChar) {
183: this .keyChar = keyChar;
184: fireValuesChanged();
185: }
186:
187: /**
188: * Returns the key char.
189: *
190: * @return the key char
191: */
192: public char getKeyChar() {
193: return keyChar;
194: }
195:
196: /**
197: * Returns the name of the key event recordable
198: *
199: * @return the name
200: */
201: public String getElementName() {
202: return getLanguage().getString(
203: "Recordables.KeyEventRecordable.Name");
204: }
205:
206: /**
207: * Returns a String which describes the content of the recordable shortly.
208: *
209: * @return a string with a short description of the object
210: */
211: public String toShortString() {
212: String eventString = null;
213:
214: switch (getID()) {
215: case KeyEvent.KEY_PRESSED:
216: eventString = language
217: .getString("Events.KeyEvent.KeyPressed");
218:
219: break;
220:
221: case KeyEvent.KEY_RELEASED:
222: eventString = language
223: .getString("Events.KeyEvent.KeyReleased");
224:
225: break;
226:
227: case KeyEvent.KEY_TYPED:
228: eventString = language
229: .getString("Events.KeyEvent.KeyTyped");
230:
231: break;
232: }
233:
234: String result = eventString + " (" + getKeyString() + ")";
235:
236: return result;
237: }
238:
239: /**
240: * Returns a key string.
241: *
242: * @return the key sequence as string
243: */
244: public String getKeyString() {
245: return CharacterToolkit.getString(keyChar, keyCode);
246: }
247:
248: /**
249: * Returns a key string.
250: *
251: * @return the key sequence as string
252: */
253: public String getInterpretedKeyString() {
254: return CharacterToolkit.getInterpretedString(keyChar, keyCode);
255: }
256:
257: /**
258: * Returns a string representation of the key event recordable.
259: *
260: * @return the string representation
261: */
262: public String toString() {
263: return "KeyEventRecordable[" + getSourceName() + ",ID="
264: + getID() + ",when=" + getWhen() + ",modifiers="
265: + getModifiers() + "," + getDuration() + ",keyCode="
266: + getKeyCode() + ",keyChar=" + getKeyChar()
267: + ",isConsumed=" + isConsumed() + "]";
268: }
269:
270: /**
271: * Returns a description of the recordable.
272: *
273: * @return the description
274: */
275: public String getElementDescription() {
276: return getLanguage().getString(
277: "Recordables.KeyEventRecordable.Description");
278: }
279:
280: /**
281: * Returns the event with the attributes specified by this recordable (except for the component
282: * attributes which will be dummy components).
283: *
284: * @return DOCUMENT ME!
285: */
286: public AWTEvent getEvent() {
287: return new KeyEvent(new JLabel(), getID(), getWhen(),
288: getModifiers(), getKeyCode(), getKeyChar());
289: }
290: }
|