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.FocusEventRecordable;
028: import jacareto.record.WindowEventRecordable;
029: import jacareto.system.Environment;
030:
031: import java.awt.event.FocusEvent;
032: import java.awt.event.WindowEvent;
033:
034: /**
035: * A window has been deactivated. A structure element can parse a part of a record.
036: *
037: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
038: * @version 1.01
039: */
040: public class WindowDeactivation extends StructureElement {
041: /** The window name. */
042: private String windowName;
043:
044: /** The window title. */
045: private String windowTitle;
046:
047: /**
048: * Creates a new "window deactivation" structure element. One of the children must be a window
049: * event recordable with the id WINDOW_DEACTIVATED.
050: *
051: * @param env the environment
052: * @param children the child structure elements
053: */
054: public WindowDeactivation(Environment env,
055: StructureElement[] children) {
056: super (env, children);
057:
058: StructureElement[] filteredChildren = getChildren("jacareto.record.WindowEventRecordable");
059: this .windowName = ((WindowEventRecordable) filteredChildren[0])
060: .getWindowName();
061: this .windowTitle = ((WindowEventRecordable) filteredChildren[0])
062: .getWindowTitle();
063: }
064:
065: /**
066: * Creates a new "window deactivation" structure element.
067: *
068: * @param env the environment
069: * @param children the child structure elements
070: * @param windowName the name of the window
071: * @param windowTitle the title of the window
072: */
073: public WindowDeactivation(Environment env,
074: StructureElement[] children, String windowName,
075: String windowTitle) {
076: super (env, children);
077: this .windowName = windowName;
078: this .windowTitle = windowTitle;
079: }
080:
081: /**
082: * Parses a record which is tokenized by the given record tokenizer.
083: *
084: * @param env DOCUMENT ME!
085: * @param recordTokenizer the record tokenizer
086: *
087: * @return a structure element, or <code>null</code> if this class cannot parse the record at
088: * the current position
089: */
090: public static StructureElement parse(Environment env,
091: RecordTokenizer recordTokenizer) {
092: StructureElement result = null;
093: StructureElement[] children = null;
094: boolean cond;
095:
096: WindowEventRecordable windowEvent = null;
097: FocusEventRecordable focusEvent = null;
098: int windowIndex = 0;
099: int focusIndex = 1;
100:
101: try {
102: windowEvent = (WindowEventRecordable) recordTokenizer.get();
103: focusEvent = (FocusEventRecordable) recordTokenizer.get(1);
104: } catch (Exception e) {
105: try {
106: focusEvent = (FocusEventRecordable) recordTokenizer
107: .get();
108: windowEvent = (WindowEventRecordable) recordTokenizer
109: .get(1);
110: focusIndex = 0;
111: windowIndex = 1;
112: } catch (Exception i) {
113: windowEvent = null;
114: focusEvent = null;
115: }
116: }
117:
118: if ((windowEvent != null) && (focusEvent != null)) {
119: cond = true;
120: cond = cond
121: && (windowEvent.getID() == WindowEvent.WINDOW_DEACTIVATED);
122: cond = cond
123: && (focusEvent.getID() == FocusEvent.FOCUS_LOST);
124:
125: if (cond) {
126: children = new StructureElement[2];
127: children[windowIndex] = windowEvent;
128: children[focusIndex] = focusEvent;
129:
130: //result = new WindowDeactivation (env, children);
131: result = new WindowDeactivation(env, children,
132: windowEvent.getWindowName(), windowEvent
133: .getWindowTitle());
134: recordTokenizer.forward(2);
135: }
136: } else {
137: try {
138: windowEvent = (WindowEventRecordable) recordTokenizer
139: .get();
140:
141: if (windowEvent.getID() == WindowEvent.WINDOW_DEACTIVATED) {
142: children = new StructureElement[1];
143: children[0] = windowEvent;
144: recordTokenizer.forward();
145:
146: //result = new WindowDeactivation (env, children);
147: result = new WindowDeactivation(env, children,
148: windowEvent.getWindowName(), windowEvent
149: .getWindowTitle());
150: }
151: } catch (Exception e) {
152: ;
153: }
154: }
155:
156: return result;
157: }
158:
159: /**
160: * Returns the name of the element.
161: *
162: * @return the name
163: */
164: public String getElementName() {
165: return language.getString("Structures.WindowDeactivation.Name");
166: }
167:
168: /**
169: * Returns a description of the element.
170: *
171: * @return the description
172: */
173: public String getElementDescription() {
174: return language
175: .getString("Structures.WindowDeactivation.Description");
176: }
177:
178: /**
179: * Returns a String which describes the content of the element shortly.
180: *
181: * @return a string with a short description of the element
182: */
183: public String toShortString() {
184: String result = getElementName();
185:
186: if (windowName != null) {
187: String windowIdentifier = (((windowTitle == null) || windowTitle
188: .equals("")) ? windowName : windowTitle);
189: String insertedText = ((windowIdentifier.length() < 40) ? windowIdentifier
190: : (windowIdentifier.substring(0, 37) + "..."));
191: result += (" (" + insertedText + ")");
192: }
193:
194: return result;
195: }
196:
197: /**
198: * The name of the activated window.
199: *
200: * @return DOCUMENT ME!
201: */
202: public String getWindowName() {
203: return windowName;
204: }
205:
206: /**
207: * The title of the activated window.
208: *
209: * @return DOCUMENT ME!
210: */
211: public String getWindowTitle() {
212: return windowTitle;
213: }
214:
215: /**
216: * Clones the element.
217: *
218: * @return DOCUMENT ME!
219: */
220: public Object clone() {
221: StructureElement[] clonedChildren = getClonedChildren();
222:
223: return new WindowDeactivation(env, clonedChildren, windowName,
224: windowTitle);
225: }
226: }
|