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

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

Fixed a bunch of problems with rectangles and resizing the window, as well as adding some more unit tests etc.

File size: 7.3 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.LinkedList;
9import java.util.List;
10
11import org.expeditee.gui.Frame;
12import org.expeditee.items.Constraint;
13import org.expeditee.items.InteractiveWidget;
14import org.expeditee.items.Item;
15import org.expeditee.items.Line;
16import org.expeditee.items.Picture;
17import org.expeditee.items.WidgetCorner;
18import org.expeditee.items.WidgetEdge;
19import org.expeditee.stats.SessionStats;
20
21/**
22 * Writes a Frame out to a KMS format file.
23 *
24 * @author jdm18
25 *
26 */
27public class ExpWriter extends DefaultFrameWriter {
28
29 private ProxyWriter _writer = null;
30
31 private StringBuilder _stringWriter = null;
32
33 private static final char TERMINATOR = 'Z';
34
35 public ExpWriter() {
36 super();
37 }
38
39 public void initialise(Frame start) throws IOException {
40 String name = start.getFramesetName().toLowerCase();
41
42 if (_filename == null)
43 _filename = start.path + name + File.separator
44 + start.getFrameNumber() + ExpReader.EXTENTION;
45
46 _stringWriter = new StringBuilder();
47 if (_filename.toLowerCase().equals("clipboard")) {
48 _writer = new ProxyWriter(true);
49 _filename = "Clipboard";
50 } else
51 _writer = new ProxyWriter(new FileWriter(_filename));
52
53 try {
54 _ItemTags.put("S", Item.class.getMethod("getTypeAndID", new Class[] {}));
55 } catch (Exception e) {
56
57 }
58 }
59
60 /**
61 * Writes the given Frame (and all items it contains) to a KMS file. Note:
62 * File path and location must be set before calling this or it will do
63 * nothing.
64 *
65 * @param frame
66 * The Frame to write out to the file.
67 * @throws IOException
68 * Any exceptions occured by the BufferedWriter.
69 */
70 public void outputFrame(Frame frame) throws IOException {
71 if (_writer == null)
72 return;
73
74 writeHeader(frame);
75
76 // write out each Item in the Frame.
77 List<Item> items = frame.getItems();
78
79 // iWidgets are handled specially since 8 items are written as one
80 List<InteractiveWidget> seenWidgets = new LinkedList<InteractiveWidget>();
81
82 // do not write items with ID -1, also skip any lines
83 for (Item i : items) {
84
85 // Ensure only one of the WidgetCorners represent a single widget
86 if (i instanceof WidgetCorner) {
87 InteractiveWidget iw = ((WidgetCorner) i).getWidgetSource();
88 if (seenWidgets.contains(iw))
89 continue;
90 seenWidgets.add(iw);
91 }
92
93 if (i.getID() >= 0 && !(i instanceof Line)) {
94 writeItem(i);
95 }
96 }
97
98 // write any lines or constraints
99 writeTerminator();
100 writeLineData();
101 writeTerminator();
102 writeConstraintData();
103
104 return;
105 }
106
107 private void writeHeader(Frame toWrite) throws IOException {
108 Iterator<String> it = _FrameTags.keySet().iterator();
109 Object[] param = {};
110
111 while (it.hasNext()) {
112 String tag = it.next();
113 try {
114 Object o = _FrameTags.get(tag).invoke(toWrite, param);
115 o = Conversion.ConvertToKMS(_FrameTags.get(tag), o);
116 if (o != null) {
117 writeLine(tag, (String) o);
118 }
119 } catch (Exception e) {
120 e.printStackTrace();
121 }
122 }
123
124 writeTerminator();
125 }
126
127 private void writeLine(String tag, String line) throws IOException {
128 writeLine(tag + " " + line);
129 }
130
131 private void writeTerminator() throws IOException {
132 writeLine(TERMINATOR + "\n");
133 }
134
135 // writes the given line out to the file
136 private void writeLine(String line) throws IOException {
137 // do not write empty lines
138 if (line == null)
139 return;
140
141 String toWrite = line + "\n";
142
143 _writer.write(toWrite);
144 _stringWriter.append(toWrite);
145 }
146
147 // writes the given Item out to the file
148 // This method is not used to write out LINE items
149 public void writeItem(Item item) throws IOException {
150 if (_writer == null)
151 return;
152
153 // Dont save the item if it is off the screen
154 if (item.offScreen()) {
155 //System.out.println("item offscreen!");
156 return;
157 }
158
159 // Pictures are saved as the corresponding text items that created them
160 if (item instanceof Picture)
161 item = ((Picture) item).getText();
162 else if (item instanceof WidgetCorner)
163 item = ((WidgetCorner) item).getWidgetSource().getSource();
164
165 if (item.isLineEnd())
166 writePoint(item);
167 else if (!(item instanceof Line))
168 writeClass(item);
169 // lines are saved at the end of the file
170 // So dont worry about them in here
171 else
172 System.out.println("Unknown Item: " + item.getID());
173
174 writeLine("");
175 }
176
177 private List<Item> _points = new LinkedList<Item>();
178
179 // Writes out a Dot to the file
180 protected void writePoint(Item point) throws IOException {
181 _points.add(point);
182 writeClass(point);
183 }
184
185 // writes out all lines to the file
186 private void writeLineData() throws IOException {
187 List<Line> seen = new LinkedList<Line>();
188
189 // loop through all points stored
190 for (int i = 0; i < _points.size(); i++) {
191 List<Line> lines = _points.get(i).getLines();
192
193 // if this point is part of one or more lines
194 if (lines != null && lines.size() > 0) {
195 for (Line line : lines) {
196
197 // Brook: widget edges are not saved
198 if (line instanceof WidgetEdge) {
199 seen.add(line);
200 continue;
201 }
202
203 // only output new lines that have not yet been output
204 if (!seen.contains(line)) {
205 writeLine("L", line.getID() + " " + line.getLineType());
206 writeLine("s", line.getLineEnds());
207 writeLine("");
208
209 // add this line to the list of lines that have been
210 // seen
211 seen.add(line);
212 }
213 }
214 }
215 }
216 }
217
218 // writes out any constraints to the file
219 private void writeConstraintData() throws IOException {
220 // outputs any constraints the points have
221
222 // loop through all the points
223 while (_points.size() > 0) {
224 Item i = _points.get(0);
225
226 // if there are any constraints to write
227 if (i.getConstraints() != null) {
228 List<Constraint> constraints = i.getConstraints();
229
230 // do not write constraints that have already been
231 // written
232 for (Constraint c : constraints) {
233 if (_points.contains(c.getStart())
234 && _points.contains(c.getEnd())) {
235 writeLine("C", c.getID() + " " + c.getType());
236 writeLine("s", c.getLineEnds());
237 writeLine("");
238 }
239
240 }
241 }
242 // remove the point from the list
243 _points.remove(0);
244 }
245 }
246
247 @Override
248 protected String finaliseFrame() throws IOException {
249 writeTerminator();
250
251 writeLine(SessionStats.getFrameEventList());
252
253 _writer.flush();
254 _writer.close();
255 _writer = null;
256
257 return "Frame successfully written to " + _filename;
258 }
259
260 private void writeClass(Object toWrite) throws IOException {
261 Iterator<String> it = _ItemTags.keySet().iterator();
262 Object[] param = {};
263
264 while (it.hasNext()) {
265 String tag = it.next();
266 Method toRun = _ItemTags.get(tag);
267 Class declarer = toRun.getDeclaringClass();
268 if (declarer == toWrite.getClass()
269 || declarer == toWrite.getClass().getSuperclass()
270 || declarer == toWrite.getClass().getSuperclass()
271 .getSuperclass()) {
272 try {
273 Object o = toRun.invoke(toWrite, param);
274 o = Conversion.ConvertToKMS(toRun, o);
275 if (o != null) {
276 if (o instanceof List) {
277 for (Object line : (List) o)
278 writeLine(tag, (String) line);
279 } else
280 writeLine(tag, (String) o);
281 }
282 } catch (Exception e) {
283 e.printStackTrace();
284 }
285 }
286 }
287 }
288
289 /**
290 * Gets a string representation of the frame file contents.
291 */
292 public String getFileContents() {
293 return _stringWriter.toString();
294 }
295}
Note: See TracBrowser for help on using the repository browser.