1 | package org.expeditee.io;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileWriter;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.lang.reflect.Method;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.HashSet;
|
---|
9 | import java.util.LinkedHashMap;
|
---|
10 |
|
---|
11 | import org.expeditee.agents.WriteTree;
|
---|
12 | import org.expeditee.gui.Frame;
|
---|
13 | import org.expeditee.gui.FrameIO;
|
---|
14 | import org.expeditee.items.Item;
|
---|
15 | import org.expeditee.items.Line;
|
---|
16 | import org.expeditee.items.Text;
|
---|
17 |
|
---|
18 | public abstract class DefaultFrameWriter extends ItemWriter implements
|
---|
19 | FrameWriter {
|
---|
20 |
|
---|
21 | protected String _filename = null;
|
---|
22 |
|
---|
23 | protected String _output = null;
|
---|
24 |
|
---|
25 | protected ProxyWriter _writer = null;
|
---|
26 |
|
---|
27 | protected String _format = "";
|
---|
28 |
|
---|
29 | protected boolean _running = true;
|
---|
30 |
|
---|
31 | protected boolean _stop = false;
|
---|
32 |
|
---|
33 | // keep track of methods that are put on the same line
|
---|
34 | protected static LinkedHashMap<String, Method> _ItemTags = null;
|
---|
35 |
|
---|
36 | protected static LinkedHashMap<String, Method> _FrameTags = null;
|
---|
37 |
|
---|
38 | public DefaultFrameWriter() {
|
---|
39 | if (_ItemTags != null && _FrameTags != null)
|
---|
40 | return;
|
---|
41 |
|
---|
42 | _ItemTags = new LinkedHashMap<String, Method>();
|
---|
43 | _FrameTags = new LinkedHashMap<String, Method>();
|
---|
44 |
|
---|
45 | Class[] param = {};
|
---|
46 |
|
---|
47 | try {
|
---|
48 | _FrameTags.put("V", Frame.class.getMethod("getVersion", param));
|
---|
49 | _FrameTags.put("p", Frame.class.getMethod("getPermission", param));
|
---|
50 | _FrameTags.put("U", Frame.class.getMethod("getOwner", param));
|
---|
51 | _FrameTags.put("D", Frame.class.getMethod("getDateCreated", param));
|
---|
52 | _FrameTags.put("M", Frame.class.getMethod("getLastModifyUser",
|
---|
53 | param));
|
---|
54 | _FrameTags.put("d", Frame.class.getMethod("getLastModifyDate",
|
---|
55 | param));
|
---|
56 | _FrameTags.put("F", Frame.class.getMethod("getFrozenDate", param));
|
---|
57 |
|
---|
58 | _FrameTags.put("O", Frame.class.getMethod("getForegroundColor",
|
---|
59 | param));
|
---|
60 | _FrameTags.put("B", Frame.class.getMethod("getBackgroundColor",
|
---|
61 | param));
|
---|
62 | _ItemTags.put("S", Item.class.getMethod("getTypeAndID", param));
|
---|
63 | _ItemTags.put("s", Item.class.getMethod("getDateCreated", param));
|
---|
64 | _ItemTags.put("d", Item.class.getMethod("getColor", param));
|
---|
65 | _ItemTags.put("G", Item.class
|
---|
66 | .getMethod("getBackgroundColor", param));
|
---|
67 | _ItemTags.put("K", Item.class.getMethod("getBorderColor", param));
|
---|
68 | _ItemTags.put("H", Item.class.getMethod("getAnchorRight", param));
|
---|
69 | _ItemTags.put("I", Item.class.getMethod("getAnchorBottom", param));
|
---|
70 | _ItemTags.put("P", Item.class.getMethod("getPosition", param));
|
---|
71 | _ItemTags.put("F", Item.class.getMethod("getLink", param));
|
---|
72 | _ItemTags.put("X", Item.class.getMethod("getAction", param));
|
---|
73 | _ItemTags.put("x", Item.class.getMethod("getActionMark", param));
|
---|
74 | _ItemTags.put("U", Item.class.getMethod("getActionCursorEnter",
|
---|
75 | param));
|
---|
76 | _ItemTags.put("V", Item.class.getMethod("getActionCursorLeave",
|
---|
77 | param));
|
---|
78 | _ItemTags.put("W", Item.class.getMethod("getActionEnterFrame",
|
---|
79 | param));
|
---|
80 | _ItemTags.put("Y", Item.class.getMethod("getActionLeaveFrame",
|
---|
81 | param));
|
---|
82 | _ItemTags.put("D", Item.class.getMethod("getData", param));
|
---|
83 | _ItemTags.put("u", Item.class.getMethod("getHighlight", param));
|
---|
84 | _ItemTags.put("e", Item.class.getMethod("getFillColor", param));
|
---|
85 | _ItemTags.put("E", Item.class.getMethod("getGradientColor", param));
|
---|
86 |
|
---|
87 | _ItemTags.put("i", Item.class.getMethod("getFillPattern", param));
|
---|
88 | _ItemTags.put("o", Item.class.getMethod("getOwner", param));
|
---|
89 | _ItemTags.put("n", Item.class.getMethod("getLinkMark", param));
|
---|
90 | _ItemTags.put("q", Item.class.getMethod("getLinkFrameset", param));
|
---|
91 | _ItemTags.put("y", Item.class.getMethod("getLinkTemplate", param));
|
---|
92 | _ItemTags.put("g", Item.class.getMethod("getLinePattern", param));
|
---|
93 |
|
---|
94 | _ItemTags.put("j", Item.class.getMethod("getArrow", param));
|
---|
95 |
|
---|
96 | _ItemTags.put("f", Text.class.getMethod("getFont", param));
|
---|
97 | _ItemTags.put("t", Text.class.getMethod("getSpacing", param));
|
---|
98 |
|
---|
99 | // TODO set a boolean flag to indicate that the text is a formula
|
---|
100 | // Store the formula in the text property NOT the answer
|
---|
101 | _ItemTags.put("J", Item.class.getMethod("getFormula", param));
|
---|
102 | _ItemTags.put("T", Text.class.getMethod("getText", param));
|
---|
103 |
|
---|
104 | _ItemTags.put("a", Text.class.getMethod("getWordSpacing", param));
|
---|
105 | _ItemTags.put("b", Text.class.getMethod("getLetterSpacing", param));
|
---|
106 | _ItemTags
|
---|
107 | .put("m", Text.class.getMethod("getInitialSpacing", param));
|
---|
108 | _ItemTags.put("w", Text.class.getMethod("getWidth", param));
|
---|
109 | _ItemTags.put("k", Text.class.getMethod("getJustification", param));
|
---|
110 |
|
---|
111 | _ItemTags.put("h", Item.class.getMethod("getThickness", param));
|
---|
112 | _ItemTags.put("l", Item.class.getMethod("getLineIDs", param));
|
---|
113 | _ItemTags.put("c", Item.class.getMethod("getConstraintIDs", param));
|
---|
114 |
|
---|
115 | } catch (Exception e) {
|
---|
116 | e.printStackTrace();
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | public void setOutputLocation(String filename) {
|
---|
121 | _filename = filename;
|
---|
122 | }
|
---|
123 |
|
---|
124 | public String writeFrame(Frame toWrite) throws IOException {
|
---|
125 | try {
|
---|
126 | initialise(toWrite);
|
---|
127 |
|
---|
128 | outputFrame(toWrite);
|
---|
129 |
|
---|
130 | _running = false;
|
---|
131 | return finaliseFrame();
|
---|
132 | } catch (IOException ioe) {
|
---|
133 | _running = false;
|
---|
134 | throw ioe;
|
---|
135 | }
|
---|
136 |
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Called before writing out the body items of each frame.
|
---|
141 | *
|
---|
142 | * @param starting
|
---|
143 | * the name of the frame currently being written out.
|
---|
144 | * @throws IOException
|
---|
145 | */
|
---|
146 | protected void writeStartFrame(Frame starting) throws IOException {
|
---|
147 | if (starting.getTitleItem() != null) {
|
---|
148 | if (starting.getTitleItem().isAnnotation())
|
---|
149 | this.writeAnnotationTitle(starting.getTitleItem());
|
---|
150 | else
|
---|
151 | this.writeTitle(starting.getTitleItem(), starting.getItems());
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Called after writing out the body items of each frame.
|
---|
157 | *
|
---|
158 | * @param ending
|
---|
159 | * the name of the frame currently being written out.
|
---|
160 | * @throws IOException
|
---|
161 | */
|
---|
162 | protected void writeEndFrame(Frame ending) throws IOException {
|
---|
163 | }
|
---|
164 |
|
---|
165 | protected void initialise(Frame start) throws IOException {
|
---|
166 | if (_filename == null)
|
---|
167 | _filename = FrameIO.EXPORTS_DIR
|
---|
168 | + getFileName(start) + _format;
|
---|
169 |
|
---|
170 | if (_filename.equalsIgnoreCase(WriteTree.CLIPBOARD)) {
|
---|
171 | _writer = new ProxyWriter(true);
|
---|
172 | _output = WriteTree.CLIPBOARD;
|
---|
173 | } else {
|
---|
174 | if (_filename.contains(File.separator)) {
|
---|
175 | String extTest = _filename.substring(_filename
|
---|
176 | .lastIndexOf(File.separator) + 1);
|
---|
177 | if (!extTest.contains("."))
|
---|
178 | _filename += _format;
|
---|
179 | } else if (!_filename.contains("."))
|
---|
180 | _filename += _format;
|
---|
181 |
|
---|
182 | if (!_filename.contains(File.separator))
|
---|
183 | _filename = FrameIO.EXPORTS_DIR + _filename;
|
---|
184 |
|
---|
185 | File test = new File(_filename);
|
---|
186 |
|
---|
187 | test = test.getParentFile();
|
---|
188 | if (test != null && !test.exists()) {
|
---|
189 | test.mkdirs();
|
---|
190 | }
|
---|
191 |
|
---|
192 | _writer = new ProxyWriter(new FileWriter(_filename));
|
---|
193 | _output = _filename;
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | protected String getFileName(Frame start) {
|
---|
198 | return getValidFilename(start.getTitle());
|
---|
199 | }
|
---|
200 |
|
---|
201 | public static String getValidFilename(String filename) {
|
---|
202 | return filename.replaceAll("[ \\.]", "_");
|
---|
203 | }
|
---|
204 |
|
---|
205 | protected String finalise() throws IOException {
|
---|
206 | try {
|
---|
207 | _writer.flush();
|
---|
208 | _writer.close();
|
---|
209 | } catch (IOException ioe) {
|
---|
210 | } finally {
|
---|
211 | _writer.close();
|
---|
212 | }
|
---|
213 |
|
---|
214 | return " exported to " + _output;
|
---|
215 | }
|
---|
216 |
|
---|
217 | protected String finaliseFrame() throws IOException {
|
---|
218 | return "Frame" + finalise();
|
---|
219 | }
|
---|
220 |
|
---|
221 | protected void outputFrame(Frame toWrite) throws IOException {
|
---|
222 | writeStartFrame(toWrite);
|
---|
223 |
|
---|
224 | Collection<Item> done = new HashSet<Item>();
|
---|
225 |
|
---|
226 | for (Item i : getItemsToWrite(toWrite)) {
|
---|
227 | if (_stop) {
|
---|
228 | return;
|
---|
229 | }
|
---|
230 |
|
---|
231 | if (i instanceof Line) {
|
---|
232 | if (done.contains(i)) {
|
---|
233 | continue;
|
---|
234 | }
|
---|
235 | done.addAll(i.getAllConnected());
|
---|
236 | }
|
---|
237 | writeItem(i);
|
---|
238 | }
|
---|
239 | writeEndFrame(toWrite);
|
---|
240 | }
|
---|
241 |
|
---|
242 | protected Collection<Item> getItemsToWrite(Frame toWrite) {
|
---|
243 | return toWrite.getItemsToSave();
|
---|
244 | }
|
---|
245 |
|
---|
246 | public boolean isRunning() {
|
---|
247 | return _running;
|
---|
248 | }
|
---|
249 |
|
---|
250 | public void stop() {
|
---|
251 | _stop = true;
|
---|
252 | }
|
---|
253 |
|
---|
254 | public String getFileContents() {
|
---|
255 | return "Not Supported";
|
---|
256 | }
|
---|
257 |
|
---|
258 | }
|
---|