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.HyperlinkEventRecordable;
029: import jacareto.system.Environment;
030:
031: import java.util.Vector;
032:
033: /**
034: * A hyperlink has been entered and activated.
035: *
036: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
037: * @version 1.01
038: */
039: public class HyperlinkChosen extends StructureElement {
040: /** The url. */
041: private String URL;
042:
043: /** The index (just for cloning). */
044: private int index;
045:
046: /**
047: * Creates a new "hyperlink chosen" structure element. One of its children must be a hyperlink
048: * event recordable of type ACTIVATED.
049: *
050: * @param env the environment
051: * @param children the child structure elements
052: */
053: public HyperlinkChosen(Environment env, StructureElement[] children) {
054: super (env, children);
055:
056: for (int i = 0; i < children.length; i++) {
057: if ((children[i] instanceof HyperlinkEventRecordable)
058: && ((HyperlinkEventRecordable) children[i])
059: .getType().equals("ACTIVATED")) {
060: URL = ((HyperlinkEventRecordable) children[i]).getURL();
061: index = i;
062:
063: break;
064: }
065: }
066: }
067:
068: /**
069: * Creates a new "hyperlink chosen" structure element.
070: *
071: * @param env the environment
072: * @param children the child structure elements
073: * @param activatedIndex the index of the "Hyperlink activated" recordable
074: */
075: public HyperlinkChosen(Environment env,
076: StructureElement[] children, int activatedIndex) {
077: super (env, children);
078: this .index = activatedIndex;
079: this .URL = ((HyperlinkEventRecordable) children[activatedIndex])
080: .getURL();
081: }
082:
083: /**
084: * Parses a record which is tokenized by the given record tokenizer.
085: *
086: * @param env DOCUMENT ME!
087: * @param recordTokenizer the record tokenizer
088: *
089: * @return a structure element, or <code>null</code> if this class cannot parse the record at
090: * the current position
091: */
092: public static StructureElement parse(Environment env,
093: RecordTokenizer recordTokenizer) {
094: StructureElement result = null;
095: StructureElement[] children = null;
096:
097: RecordTokenizerState rtState = recordTokenizer.saveState();
098:
099: // (hyper link entered,mouse motion), focus change | mouse click, hyperlink activated, (mouse motion, hyperlink exited)
100: try {
101: // Entering the link
102: HyperlinkEventRecordable entered = null;
103: MouseMotion motion = null;
104: RecordTokenizerState stateBeforeEnter = recordTokenizer
105: .saveState();
106:
107: try {
108: entered = (HyperlinkEventRecordable) recordTokenizer
109: .next();
110: motion = (MouseMotion) MouseMotion.parse(env,
111: recordTokenizer);
112: } catch (Exception e) {
113: recordTokenizer.restoreState(stateBeforeEnter);
114: }
115:
116: // Click and activate
117: StructureElement fm = FocusChange.parse(env,
118: recordTokenizer);
119:
120: if (fm == null) {
121: fm = MouseClick.parse(env, recordTokenizer);
122: }
123:
124: HyperlinkEventRecordable activated = (HyperlinkEventRecordable) recordTokenizer
125: .next();
126:
127: // Exiting the link
128: MouseMotion motion2 = null;
129: HyperlinkEventRecordable exited = null;
130: RecordTokenizerState stateBeforeExit = recordTokenizer
131: .saveState();
132:
133: try {
134: motion2 = (MouseMotion) MouseMotion.parse(env,
135: recordTokenizer);
136: exited = (HyperlinkEventRecordable) recordTokenizer
137: .next();
138: } catch (Exception e) {
139: recordTokenizer.restoreState(stateBeforeExit);
140: }
141:
142: boolean cond = activated.getType().equals("ACTIVATED")
143: && ((entered == null) || (entered.getURL().equals(
144: activated.getURL()) && entered.getType()
145: .equals("ENTERED")))
146: && ((exited == null) || exited.getType().equals(
147: "EXITED"));
148:
149: // (The url of the exited event must not be equal to the activated url, because the mouse pointer
150: // could be on another hyperlink on the activated page.)
151: if (cond) {
152: Vector addedChildren = new Vector(6);
153:
154: if (entered != null) {
155: addedChildren.add(entered);
156: }
157:
158: if (motion != null) {
159: addedChildren.add(motion);
160: }
161:
162: if (fm != null) {
163: addedChildren.add(fm);
164: }
165:
166: addedChildren.add(activated);
167:
168: if (motion2 != null) {
169: addedChildren.add(motion2);
170: }
171:
172: if (exited != null) {
173: addedChildren.add(exited);
174: }
175:
176: children = vectorToArray(addedChildren);
177:
178: //result = new HyperlinkChosen (env, children);
179: result = new HyperlinkChosen(env, children,
180: addedChildren.indexOf(activated));
181: } else {
182: throw new Exception();
183: }
184: } catch (Throwable t) {
185: recordTokenizer.restoreState(rtState);
186: }
187:
188: return result;
189: }
190:
191: /**
192: * Returns the name of the element.
193: *
194: * @return the name
195: */
196: public String getElementName() {
197: return language.getString("Structures.HyperlinkChosen.Name");
198: }
199:
200: /**
201: * Returns a description of the element.
202: *
203: * @return the description
204: */
205: public String getElementDescription() {
206: return language
207: .getString("Structures.HyperlinkChosen.Description");
208: }
209:
210: /**
211: * Returns a String which describes the content of the element shortly.
212: *
213: * @return a string with a short description of the element
214: */
215: public String toShortString() {
216: String insertedText = null;
217:
218: if (getURL().length() < 40) {
219: insertedText = getURL();
220: } else {
221: int length = getURL().length();
222: insertedText = getURL().substring(0, 9) + "..."
223: + getURL().substring(length - 28, length - 1);
224: }
225:
226: return getElementName() + " (" + insertedText + ")";
227: }
228:
229: /**
230: * Returns the URL.
231: *
232: * @return DOCUMENT ME!
233: */
234: public String getURL() {
235: return URL;
236: }
237:
238: /**
239: * Clones the element.
240: *
241: * @return DOCUMENT ME!
242: */
243: public Object clone() {
244: StructureElement[] clonedChildren = getClonedChildren();
245:
246: return new HyperlinkChosen(env, clonedChildren, index);
247: }
248:
249: public boolean hasProcTime() {
250: return true;
251: }
252: }
|