source: trunk/src/org/expeditee/io/ExpWriter.java@ 46

Last change on this file since 46 was 10, checked in by ra33, 16 years ago

Added Interactive Widget Items

File size: 10.5 KB
Line 
1package org.expeditee.io;
2
3import java.io.File;
4import java.io.FileWriter;
5import java.io.IOException;
6import java.lang.reflect.Method;
7import java.util.Iterator;
8import java.util.LinkedHashMap;
9import java.util.LinkedList;
10import java.util.List;
11
12import org.expeditee.gui.Frame;
13import org.expeditee.items.Constraint;
14import org.expeditee.items.Dot;
15import org.expeditee.items.InteractiveWidget;
16import org.expeditee.items.Item;
17import org.expeditee.items.Line;
18import org.expeditee.items.Picture;
19import org.expeditee.items.Text;
20import org.expeditee.items.WidgetCorner;
21import org.expeditee.items.WidgetEdge;
22import org.expeditee.stats.SessionStats;
23
24/**
25 * Writes a Frame out to a KMS format file.
26 *
27 * @author jdm18
28 *
29 */
30public class ExpWriter extends DefaultFrameWriter {
31
32 private ProxyWriter _writer = null;
33
34 private StringBuilder _stringWriter = null;
35
36 // keep track of methods that are put on the same line
37 private static LinkedHashMap<String, Method> _ItemTags = null;
38
39 private static LinkedHashMap<String, Method> _FrameTags = null;
40
41 private static final char TERMINATOR = 'Z';
42
43 public ExpWriter() {
44 if (_ItemTags != null && _FrameTags != null)
45 return;
46
47 _ItemTags = new LinkedHashMap<String, Method>();
48 _FrameTags = new LinkedHashMap<String, Method>();
49
50 Class[] param = {};
51
52 try {
53 _FrameTags.put("V", Frame.class.getMethod("getVersion", param));
54 _FrameTags.put("p", Frame.class.getMethod("getProtection", param));
55 _FrameTags.put("U", Frame.class.getMethod("getOwner", param));
56 _FrameTags.put("D", Frame.class.getMethod("getDateCreated", param));
57 _FrameTags.put("M", Frame.class.getMethod("getLastModifyUser",
58 param));
59 _FrameTags.put("d", Frame.class.getMethod("getLastModifyDate",
60 param));
61 _FrameTags.put("F", Frame.class.getMethod("getFrozenDate", param));
62
63 _FrameTags.put("O", Frame.class.getMethod("getForegroundColor",
64 param));
65 _FrameTags.put("B", Frame.class.getMethod("getBackgroundColor",
66 param));
67
68 _ItemTags.put("S", Item.class.getMethod("getTypeAndID", param));
69 _ItemTags.put("s", Item.class.getMethod("getDateCreated", param));
70 _ItemTags.put("d", Item.class.getMethod("getColor", param));
71 _ItemTags.put("G", Item.class
72 .getMethod("getBackgroundColor", param));
73 _ItemTags.put("P", Item.class.getMethod("getPosition", param));
74 _ItemTags.put("F", Item.class.getMethod("getLink", param));
75 _ItemTags.put("X", Item.class.getMethod("getAction", param));
76 _ItemTags.put("x", Item.class.getMethod("getActionMark", param));
77 _ItemTags.put("U", Item.class.getMethod("getActionCursorEnter",
78 param));
79 _ItemTags.put("V", Item.class.getMethod("getActionCursorLeave",
80 param));
81 _ItemTags.put("W", Item.class.getMethod("getActionEnterFrame",
82 param));
83 _ItemTags.put("Y", Item.class.getMethod("getActionLeaveFrame",
84 param));
85 _ItemTags.put("D", Item.class.getMethod("getData", param));
86 _ItemTags.put("u", Item.class.getMethod("getHighlight", param));
87 _ItemTags.put("e", Item.class.getMethod("getFillColor", param));
88 _ItemTags.put("i", Item.class.getMethod("getFillPattern", param));
89 _ItemTags.put("o", Item.class.getMethod("getOwner", param));
90 _ItemTags.put("n", Item.class.getMethod("getLinkMark", param));
91 _ItemTags.put("q", Item.class.getMethod("getLinkFrameset", param));
92 _ItemTags.put("y", Item.class.getMethod("getLinkTemplate", param));
93 _ItemTags.put("g", Item.class.getMethod("getLinePattern", param));
94
95 _ItemTags.put("j", Item.class.getMethod("getArrow", param));
96
97 _ItemTags.put("f", Text.class.getMethod("getFont", param));
98 _ItemTags.put("t", Text.class.getMethod("getSpacing", param));
99 _ItemTags.put("T", Text.class.getMethod("getText", param));
100
101 _ItemTags.put("a", Text.class.getMethod("getWordSpacing", param));
102 _ItemTags.put("b", Text.class.getMethod("getLetterSpacing", param));
103 _ItemTags
104 .put("m", Text.class.getMethod("getInitialSpacing", param));
105 _ItemTags.put("w", Text.class.getMethod("getWidth", param));
106 _ItemTags.put("k", Text.class.getMethod("getJustification", param));
107
108 _ItemTags.put("h", Dot.class.getMethod("getThickness", param));
109 _ItemTags.put("l", Dot.class.getMethod("getLineIDs", param));
110 _ItemTags.put("c", Dot.class.getMethod("getConstraintIDs", param));
111
112 } catch (Exception e) {
113 e.printStackTrace();
114 }
115 }
116
117 public void initialise(Frame start) throws IOException {
118 String name = start.getFramesetName().toLowerCase();
119
120 if (_filename == null)
121 _filename = start.path + name + File.separator
122 + start.getFrameNumber() + ExpReader.EXTENTION;
123
124 _stringWriter = new StringBuilder();
125 if (_filename.toLowerCase().equals("clipboard")) {
126 _writer = new ProxyWriter(true);
127 _filename = "Clipboard";
128 } else
129 _writer = new ProxyWriter(new FileWriter(_filename));
130 }
131
132 /**
133 * Writes the given Frame (and all items it contains) to a KMS file. Note:
134 * File path and location must be set before calling this or it will do
135 * nothing.
136 *
137 * @param frame
138 * The Frame to write out to the file.
139 * @throws IOException
140 * Any exceptions occured by the BufferedWriter.
141 */
142 public void outputFrame(Frame frame) throws IOException {
143 if (_writer == null)
144 return;
145
146 writeHeader(frame);
147
148 // write out each Item in the Frame.
149 List<Item> items = frame.getItems();
150
151 // iWidgets are handled specially since 8 items are written as one
152 List<InteractiveWidget> seenWidgets = new LinkedList<InteractiveWidget>();
153
154 // do not write items with ID -1, also skip any lines
155 for (Item i : items) {
156
157 // Ensure only one of the WidgetCorners represent a single widget
158 if (i instanceof WidgetCorner) {
159 InteractiveWidget iw = ((WidgetCorner)i).getWidgetSource();
160 if (seenWidgets.contains(iw)) continue;
161 seenWidgets.add(iw);
162 }
163
164 if (i.getID() >= 0 && !(i instanceof Line)) {
165 writeItem(i);
166 }
167 }
168
169 // write any lines or constraints
170 writeTerminator();
171 writeLineData();
172 writeTerminator();
173 writeConstraintData();
174
175 return;
176 }
177
178 private void writeHeader(Frame toWrite) throws IOException {
179 Iterator<String> it = _FrameTags.keySet().iterator();
180 Object[] param = {};
181
182 while (it.hasNext()) {
183 String tag = it.next();
184 try {
185 Object o = _FrameTags.get(tag).invoke(toWrite, param);
186 o = Conversion.ConvertToKMS(_FrameTags.get(tag), o);
187 if (o != null) {
188 writeLine(tag, (String) o);
189 }
190 } catch (Exception e) {
191 e.printStackTrace();
192 }
193 }
194
195 writeTerminator();
196 }
197
198 private void writeLine(String tag, String line) throws IOException {
199 writeLine(tag + " " + line);
200 }
201
202 private void writeTerminator() throws IOException {
203 writeLine(TERMINATOR + "\n");
204 }
205
206 // writes the given line out to the file
207 private void writeLine(String line) throws IOException {
208 // do not write empty lines
209 if (line == null)
210 return;
211
212 String toWrite = line + "\n";
213
214 _writer.write(toWrite);
215 _stringWriter.append(toWrite);
216 }
217
218 // writes the given Item out to the file
219 //This method is not used to write out LINE items
220 public void writeItem(Item item) throws IOException {
221 if (_writer == null)
222 return;
223
224 // Pictures are saved as the corresponding text items that created them
225 if (item instanceof Picture)
226 item = ((Picture) item).getText();
227 else if (item instanceof WidgetCorner)
228 item = ((WidgetCorner) item).getWidgetSource().getSource();
229
230 if (item.getLines().size() > 0)
231 writePoint(item);
232 else if (!(item instanceof Line))
233 writeClass(item);
234 // lines are saved at the end of the file
235 //So dont worry about them in here
236 else
237 System.out.println("Unknown Item: " + item.getID());
238
239 writeLine("");
240 }
241
242 private List<Item> _points = new LinkedList<Item>();
243
244 // Writes out a Dot to the file
245 protected void writePoint(Item point) throws IOException {
246 _points.add(point);
247 writeClass(point);
248 }
249
250 // writes out all lines to the file
251 private void writeLineData() throws IOException {
252 List<Line> seen = new LinkedList<Line>();
253
254 // loop through all points stored
255 for (int i = 0; i < _points.size(); i++) {
256 List<Line> lines = _points.get(i).getLines();
257
258 // if this point is part of one or more lines
259 if (lines != null && lines.size() > 0) {
260 for (Line line : lines) {
261
262 // Brook: widget edges are not saved
263 if (line instanceof WidgetEdge) {
264 seen.add(line);
265 continue;
266 }
267
268 // only output new lines that have not yet been output
269 if (!seen.contains(line)) {
270 writeLine("L", line.getID() + " " + line.getLineType());
271 writeLine("s", line.getLineEnds());
272 writeLine("");
273
274 // add this line to the list of lines that have been
275 // seen
276 seen.add(line);
277 }
278 }
279 }
280 }
281 }
282
283 // writes out any constraints to the file
284 private void writeConstraintData() throws IOException {
285 // outputs any constraints the points have
286
287 // loop through all the points
288 while (_points.size() > 0) {
289 Item i = _points.get(0);
290
291 if (i instanceof Dot) {
292 Dot p = (Dot) i;
293
294 // if there are any constraints to write
295 if (p.getConstraints() != null) {
296 List<Constraint> constraints = p.getConstraints();
297
298 // do not write constraints that have already been
299 // written
300 for (Constraint c : constraints) {
301 if (_points.contains(c.getStart())
302 && _points.contains(c.getEnd())) {
303 writeLine("C", c.getID() + " "
304 + c.getConstraintType());
305 writeLine("s", c.getLineEnds());
306 writeLine("");
307 }
308
309 }
310 }
311 }
312 // remove the point from the list
313 _points.remove(0);
314 }
315 }
316
317 @Override
318 protected String finaliseFrame() throws IOException {
319 writeTerminator();
320
321 writeLine(SessionStats.getFrameEventList());
322
323 _writer.flush();
324 _writer.close();
325 _writer = null;
326
327 return "Frame successfully written to " + _filename;
328 }
329
330 private void writeClass(Object toWrite) throws IOException {
331 Iterator<String> it = _ItemTags.keySet().iterator();
332 Object[] param = {};
333
334 while (it.hasNext()) {
335 String tag = it.next();
336 Method toRun = _ItemTags.get(tag);
337 Class declarer = toRun.getDeclaringClass();
338 if (declarer == toWrite.getClass()
339 || declarer == toWrite.getClass().getSuperclass()
340 || declarer == toWrite.getClass().getSuperclass()
341 .getSuperclass()) {
342 try {
343 Object o = toRun.invoke(toWrite, param);
344 o = Conversion.ConvertToKMS(toRun, o);
345 if (o != null) {
346 if (o instanceof List) {
347 for (Object line : (List) o)
348 writeLine(tag, (String) line);
349 } else
350 writeLine(tag, (String) o);
351 }
352 } catch (Exception e) {
353 e.printStackTrace();
354 }
355 }
356 }
357 }
358
359 /**
360 * Gets a string representation of the frame file contents.
361 */
362 public String getFileContents() {
363 return _stringWriter.toString();
364 }
365}
Note: See TracBrowser for help on using the repository browser.