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.convert.xml;
025:
026: import jacareto.record.ListSelectionEventRecordable;
027: import jacareto.struct.StructureElement;
028: import jacareto.system.Environment;
029: import jacareto.toolkit.UUIDGen;
030:
031: import org.jdom.Element;
032:
033: /**
034: * A converter which is able to convert a {@link jacareto.record.ListSelectionEventRecordable} to a
035: * xml element, and vice versa.
036: *
037: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
038: * @version 1.00
039: */
040: public class XMLListSelectionEventConverter extends
041: XMLEventObjectConverter {
042: /**
043: * Creates a new converter.
044: *
045: * @param env the environment
046: */
047: public XMLListSelectionEventConverter(Environment env) {
048: super (env);
049: }
050:
051: /**
052: * Returns whether this converter is able to transform the specified structure element to an
053: * other representation. This converter is responsible for the given structure element if it
054: * is of type {@link jacareto.record.ListSelectionEventRecordable}.
055: *
056: * @param element the structure element
057: *
058: * @return <code>true</code> if this converter is responsible for this structure element;
059: * otherwise <code>false</code>.
060: */
061: public boolean handlesElement(StructureElement element) {
062: return (element != null)
063: && (element instanceof ListSelectionEventRecordable);
064: }
065:
066: /**
067: * Converts the specified element to an other representation, if this converter is responsible
068: * for it. For responsibility see {@link #handlesElement(StructureElement)}.
069: *
070: * @param element the structure element to convert
071: *
072: * @return the other representation
073: */
074: public Object convertElement(StructureElement element) {
075: ListSelectionEventRecordable lsEventRecordable = (ListSelectionEventRecordable) element;
076:
077: Element result = (Element) super .convertElement(element);
078:
079: result.setName("ListSelectionEvent");
080:
081: result.setAttribute("firstIndex", ""
082: + lsEventRecordable.getFirstIndex());
083: result.setAttribute("lastIndex", ""
084: + lsEventRecordable.getLastIndex());
085: result.setAttribute("valueIsAdjusting", ""
086: + lsEventRecordable.getValueIsAdjusting());
087:
088: return result;
089: }
090:
091: /**
092: * Returns whether this converter is able to transform the specified other representation to a
093: * structure element. This converter is responsible if the other representation is of type
094: * <code>jdom.org.Element</code> and the name of the xml element is
095: * "ListSelectionEvent"
096: *
097: * @param other the other representation
098: *
099: * @return <code>true</code> if this converter is responsible for this other representation;
100: * otherwise <code>false</code>.
101: */
102: public boolean handlesOther(Object other) {
103: try {
104: Element e = (Element) other;
105:
106: return e.getName().equals("ListSelectionEvent");
107: } catch (Throwable t) {
108: return false;
109: }
110: }
111:
112: /**
113: * Converts the specified other representation to a structure element, if this converter is
114: * responsible for it (reverses {@link #convertElement(StructureElement)}). For responsibility
115: * see {@link #handlesOther(Object)}.
116: *
117: * @param other the other representation to convert
118: *
119: * @return the key event element
120: */
121: public StructureElement convertOther(Object other) {
122: Element element = (Element) other;
123:
124: String sourceName = element.getAttributeValue("source");
125: String sourceClass = element.getAttributeValue("class");
126:
127: if (sourceClass == null) {
128: sourceClass = "";
129: }
130:
131: // workaround because of the changes relativeTime -> duration
132: String duration;
133:
134: if ((duration = element.getAttributeValue("duration")) == null) {
135: duration = element.getAttributeValue("relativeTime");
136: }
137:
138: String procTime = element.getAttributeValue("procTime");
139:
140: long durationTime = Long.parseLong(duration);
141: long procTimeTime = 0;
142:
143: if (procTime != null) {
144: procTimeTime = Long.parseLong(procTime);
145: }
146:
147: int firstIndex = Integer.parseInt(element
148: .getAttributeValue("firstIndex"));
149: int lastIndex = Integer.parseInt(element
150: .getAttributeValue("lastIndex"));
151: boolean valueIsAdjusting = (Boolean.valueOf(element
152: .getAttributeValue("valueIsAdjusting").toString()))
153: .booleanValue();
154:
155: ListSelectionEventRecordable returnValue = new ListSelectionEventRecordable(
156: env, sourceName, sourceClass, durationTime,
157: procTimeTime, firstIndex, lastIndex, valueIsAdjusting);
158:
159: String uuidString = element.getAttributeValue("uuid");
160:
161: if (uuidString == null) {
162: returnValue.setUUIDString(UUIDGen.getInstance()
163: .generateUUID());
164: } else {
165: returnValue.setUUIDString(uuidString);
166: }
167:
168: return returnValue;
169: }
170: }
|