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

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

Items that are completely off the screen to the left or above are no longer saved in the file

File size: 10.6 KB
Line 
1package org.expeditee.io;
2
3import java.awt.Rectangle;
4import java.io.File;
5import java.io.FileWriter;
6import java.io.IOException;
7import java.lang.reflect.Method;
8import java.util.Iterator;
9import java.util.LinkedHashMap;
10import java.util.LinkedList;
11import java.util.List;
12
13import org.expeditee.gui.Frame;
14import org.expeditee.items.Constraint;
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", Item.class.getMethod("getThickness", param));
109 _ItemTags.put("l", Item.class.getMethod("getLineIDs", param));
110 _ItemTags.put("c", Item.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))
161 continue;
162 seenWidgets.add(iw);
163 }
164
165 if (i.getID() >= 0 && !(i instanceof Line)) {
166 writeItem(i);
167 }
168 }
169
170 // write any lines or constraints
171 writeTerminator();
172 writeLineData();
173 writeTerminator();
174 writeConstraintData();
175
176 return;
177 }
178
179 private void writeHeader(Frame toWrite) throws IOException {
180 Iterator<String> it = _FrameTags.keySet().iterator();
181 Object[] param = {};
182
183 while (it.hasNext()) {
184 String tag = it.next();
185 try {
186 Object o = _FrameTags.get(tag).invoke(toWrite, param);
187 o = Conversion.ConvertToKMS(_FrameTags.get(tag), o);
188 if (o != null) {
189 writeLine(tag, (String) o);
190 }
191 } catch (Exception e) {
192 e.printStackTrace();
193 }
194 }
195
196 writeTerminator();
197 }
198
199 private void writeLine(String tag, String line) throws IOException {
200 writeLine(tag + " " + line);
201 }
202
203 private void writeTerminator() throws IOException {
204 writeLine(TERMINATOR + "\n");
205 }
206
207 // writes the given line out to the file
208 private void writeLine(String line) throws IOException {
209 // do not write empty lines
210 if (line == null)
211 return;
212
213 String toWrite = line + "\n";
214
215 _writer.write(toWrite);
216 _stringWriter.append(toWrite);
217 }
218
219 // writes the given Item out to the file
220 // This method is not used to write out LINE items
221 public void writeItem(Item item) throws IOException {
222 if (_writer == null)
223 return;
224
225 // Dont save the item if it is off the screen
226 if (item.offScreen()) {
227 System.out.println("item offscreen!");
228 return;
229 }
230
231 // Pictures are saved as the corresponding text items that created them
232 if (item instanceof Picture)
233 item = ((Picture) item).getText();
234 else if (item instanceof WidgetCorner)
235 item = ((WidgetCorner) item).getWidgetSource().getSource();
236
237 if (item.isLineEnd())
238 writePoint(item);
239 else if (!(item instanceof Line))
240 writeClass(item);
241 // lines are saved at the end of the file
242 // So dont worry about them in here
243 else
244 System.out.println("Unknown Item: " + item.getID());
245
246 writeLine("");
247 }
248
249 private List<Item> _points = new LinkedList<Item>();
250
251 // Writes out a Dot to the file
252 protected void writePoint(Item point) throws IOException {
253 _points.add(point);
254 writeClass(point);
255 }
256
257 // writes out all lines to the file
258 private void writeLineData() throws IOException {
259 List<Line> seen = new LinkedList<Line>();
260
261 // loop through all points stored
262 for (int i = 0; i < _points.size(); i++) {
263 List<Line> lines = _points.get(i).getLines();
264
265 // if this point is part of one or more lines
266 if (lines != null && lines.size() > 0) {
267 for (Line line : lines) {
268
269 // Brook: widget edges are not saved
270 if (line instanceof WidgetEdge) {
271 seen.add(line);
272 continue;
273 }
274
275 // only output new lines that have not yet been output
276 if (!seen.contains(line)) {
277 writeLine("L", line.getID() + " " + line.getLineType());
278 writeLine("s", line.getLineEnds());
279 writeLine("");
280
281 // add this line to the list of lines that have been
282 // seen
283 seen.add(line);
284 }
285 }
286 }
287 }
288 }
289
290 // writes out any constraints to the file
291 private void writeConstraintData() throws IOException {
292 // outputs any constraints the points have
293
294 // loop through all the points
295 while (_points.size() > 0) {
296 Item i = _points.get(0);
297
298 // if there are any constraints to write
299 if (i.getConstraints() != null) {
300 List<Constraint> constraints = i.getConstraints();
301
302 // do not write constraints that have already been
303 // written
304 for (Constraint c : constraints) {
305 if (_points.contains(c.getStart())
306 && _points.contains(c.getEnd())) {
307 writeLine("C", c.getID() + " " + c.getType());
308 writeLine("s", c.getLineEnds());
309 writeLine("");
310 }
311
312 }
313 }
314 // remove the point from the list
315 _points.remove(0);
316 }
317 }
318
319 @Override
320 protected String finaliseFrame() throws IOException {
321 writeTerminator();
322
323 writeLine(SessionStats.getFrameEventList());
324
325 _writer.flush();
326 _writer.close();
327 _writer = null;
328
329 return "Frame successfully written to " + _filename;
330 }
331
332 private void writeClass(Object toWrite) throws IOException {
333 Iterator<String> it = _ItemTags.keySet().iterator();
334 Object[] param = {};
335
336 while (it.hasNext()) {
337 String tag = it.next();
338 Method toRun = _ItemTags.get(tag);
339 Class declarer = toRun.getDeclaringClass();
340 if (declarer == toWrite.getClass()
341 || declarer == toWrite.getClass().getSuperclass()
342 || declarer == toWrite.getClass().getSuperclass()
343 .getSuperclass()) {
344 try {
345 Object o = toRun.invoke(toWrite, param);
346 o = Conversion.ConvertToKMS(toRun, o);
347 if (o != null) {
348 if (o instanceof List) {
349 for (Object line : (List) o)
350 writeLine(tag, (String) line);
351 } else
352 writeLine(tag, (String) o);
353 }
354 } catch (Exception e) {
355 e.printStackTrace();
356 }
357 }
358 }
359 }
360
361 /**
362 * Gets a string representation of the frame file contents.
363 */
364 public String getFileContents() {
365 return _stringWriter.toString();
366 }
367}
Note: See TracBrowser for help on using the repository browser.