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.LinkedHashMap;
|
---|
8 |
|
---|
9 | import org.expeditee.gui.Frame;
|
---|
10 | import org.expeditee.gui.FrameIO;
|
---|
11 | import org.expeditee.items.Item;
|
---|
12 | import org.expeditee.items.Text;
|
---|
13 |
|
---|
14 | public abstract class DefaultFrameWriter extends ItemWriter implements
|
---|
15 | FrameWriter {
|
---|
16 |
|
---|
17 | protected String _filename = null;
|
---|
18 |
|
---|
19 | protected String _output = null;
|
---|
20 |
|
---|
21 | protected ProxyWriter _writer = null;
|
---|
22 |
|
---|
23 | protected String _format = "";
|
---|
24 |
|
---|
25 | protected boolean _running = true;
|
---|
26 |
|
---|
27 | protected boolean _stop = false;
|
---|
28 |
|
---|
29 | // keep track of methods that are put on the same line
|
---|
30 | protected static LinkedHashMap<String, Method> _ItemTags = null;
|
---|
31 |
|
---|
32 | protected static LinkedHashMap<String, Method> _FrameTags = null;
|
---|
33 |
|
---|
34 | public DefaultFrameWriter() {
|
---|
35 | if (_ItemTags != null && _FrameTags != null)
|
---|
36 | return;
|
---|
37 |
|
---|
38 | _ItemTags = new LinkedHashMap<String, Method>();
|
---|
39 | _FrameTags = new LinkedHashMap<String, Method>();
|
---|
40 |
|
---|
41 | Class[] param = {};
|
---|
42 |
|
---|
43 | try {
|
---|
44 | _FrameTags.put("A", Frame.class.getMethod("getFrameName", param));
|
---|
45 | _FrameTags.put("V", Frame.class.getMethod("getVersion", param));
|
---|
46 | _FrameTags.put("p", Frame.class.getMethod("getProtection", param));
|
---|
47 | _FrameTags.put("U", Frame.class.getMethod("getOwner", param));
|
---|
48 | _FrameTags.put("D", Frame.class.getMethod("getDateCreated", param));
|
---|
49 | _FrameTags.put("M", Frame.class.getMethod("getLastModifyUser",
|
---|
50 | param));
|
---|
51 | _FrameTags.put("d", Frame.class.getMethod("getLastModifyDate",
|
---|
52 | param));
|
---|
53 | _FrameTags.put("F", Frame.class.getMethod("getFrozenDate", param));
|
---|
54 |
|
---|
55 | _FrameTags.put("O", Frame.class.getMethod("getForegroundColor",
|
---|
56 | param));
|
---|
57 | _FrameTags.put("B", Frame.class.getMethod("getBackgroundColor",
|
---|
58 | param));
|
---|
59 | _ItemTags.put("S", Item.class.getMethod("getTypeAndID", param));
|
---|
60 | _ItemTags.put("s", Item.class.getMethod("getDateCreated", param));
|
---|
61 | _ItemTags.put("d", Item.class.getMethod("getColor", param));
|
---|
62 | _ItemTags.put("G", Item.class
|
---|
63 | .getMethod("getBackgroundColor", param));
|
---|
64 | _ItemTags.put("P", Item.class.getMethod("getPosition", param));
|
---|
65 | _ItemTags.put("F", Item.class.getMethod("getLink", param));
|
---|
66 | _ItemTags.put("X", Item.class.getMethod("getAction", param));
|
---|
67 | _ItemTags.put("x", Item.class.getMethod("getActionMark", param));
|
---|
68 | _ItemTags.put("U", Item.class.getMethod("getActionCursorEnter",
|
---|
69 | param));
|
---|
70 | _ItemTags.put("V", Item.class.getMethod("getActionCursorLeave",
|
---|
71 | param));
|
---|
72 | _ItemTags.put("W", Item.class.getMethod("getActionEnterFrame",
|
---|
73 | param));
|
---|
74 | _ItemTags.put("Y", Item.class.getMethod("getActionLeaveFrame",
|
---|
75 | param));
|
---|
76 | _ItemTags.put("D", Item.class.getMethod("getData", param));
|
---|
77 | _ItemTags.put("u", Item.class.getMethod("getHighlight", param));
|
---|
78 | _ItemTags.put("e", Item.class.getMethod("getFillColor", param));
|
---|
79 | _ItemTags.put("i", Item.class.getMethod("getFillPattern", param));
|
---|
80 | _ItemTags.put("o", Item.class.getMethod("getOwner", param));
|
---|
81 | _ItemTags.put("n", Item.class.getMethod("getLinkMark", param));
|
---|
82 | _ItemTags.put("q", Item.class.getMethod("getLinkFrameset", param));
|
---|
83 | _ItemTags.put("y", Item.class.getMethod("getLinkTemplate", param));
|
---|
84 | _ItemTags.put("g", Item.class.getMethod("getLinePattern", param));
|
---|
85 |
|
---|
86 | _ItemTags.put("j", Item.class.getMethod("getArrow", param));
|
---|
87 |
|
---|
88 | _ItemTags.put("f", Text.class.getMethod("getFont", param));
|
---|
89 | _ItemTags.put("t", Text.class.getMethod("getSpacing", param));
|
---|
90 | _ItemTags.put("T", Text.class.getMethod("getText", param));
|
---|
91 |
|
---|
92 | _ItemTags.put("a", Text.class.getMethod("getWordSpacing", param));
|
---|
93 | _ItemTags.put("b", Text.class.getMethod("getLetterSpacing", param));
|
---|
94 | _ItemTags
|
---|
95 | .put("m", Text.class.getMethod("getInitialSpacing", param));
|
---|
96 | _ItemTags.put("w", Text.class.getMethod("getWidth", param));
|
---|
97 | _ItemTags.put("k", Text.class.getMethod("getJustification", param));
|
---|
98 |
|
---|
99 | _ItemTags.put("h", Item.class.getMethod("getThickness", param));
|
---|
100 | _ItemTags.put("l", Item.class.getMethod("getLineIDs", param));
|
---|
101 | _ItemTags.put("c", Item.class.getMethod("getConstraintIDs", param));
|
---|
102 |
|
---|
103 | } catch (Exception e) {
|
---|
104 | e.printStackTrace();
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | public void setOutputLocation(String filename) {
|
---|
109 | _filename = filename;
|
---|
110 | }
|
---|
111 |
|
---|
112 | public String writeFrame(Frame toWrite) throws IOException {
|
---|
113 | try {
|
---|
114 | initialise(toWrite);
|
---|
115 |
|
---|
116 | outputFrame(toWrite);
|
---|
117 |
|
---|
118 | _running = false;
|
---|
119 | return finaliseFrame();
|
---|
120 | } catch (IOException ioe) {
|
---|
121 | _running = false;
|
---|
122 | throw ioe;
|
---|
123 | }
|
---|
124 |
|
---|
125 | }
|
---|
126 |
|
---|
127 | protected void writeStartFrame(Frame starting) throws IOException {
|
---|
128 | if (starting.getTitle() != null) {
|
---|
129 | if (starting.getTitle().isAnnotation())
|
---|
130 | this.writeAnnotationTitle(starting.getTitle());
|
---|
131 | else
|
---|
132 | this.writeTitle(starting.getTitle(), starting.getItems());
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | protected void writeEndFrame(Frame ending) throws IOException {
|
---|
137 | }
|
---|
138 |
|
---|
139 | protected void initialise(Frame start) throws IOException {
|
---|
140 | if (_filename == null)
|
---|
141 | _filename = FrameIO.EXPORTS_DIR + start.getFrameName() + _format;
|
---|
142 |
|
---|
143 | if (_filename.toLowerCase().equals("clipboard")) {
|
---|
144 | _writer = new ProxyWriter(true);
|
---|
145 | _output = "Clipboard";
|
---|
146 | } else {
|
---|
147 | if (_filename.contains(File.separator)) {
|
---|
148 | String extTest = _filename.substring(_filename
|
---|
149 | .lastIndexOf(File.separator) + 1);
|
---|
150 | if (!extTest.contains("."))
|
---|
151 | _filename += _format;
|
---|
152 | } else if (!_filename.contains("."))
|
---|
153 | _filename += _format;
|
---|
154 |
|
---|
155 | if (!_filename.contains(File.separator))
|
---|
156 | _filename = FrameIO.EXPORTS_DIR + _filename;
|
---|
157 |
|
---|
158 | File test = new File(_filename);
|
---|
159 |
|
---|
160 | test = test.getParentFile();
|
---|
161 | if (test != null && !test.exists()) {
|
---|
162 | test.mkdirs();
|
---|
163 | }
|
---|
164 |
|
---|
165 | _writer = new ProxyWriter(new FileWriter(_filename));
|
---|
166 | _output = _filename;
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | protected String finalise() throws IOException {
|
---|
171 | try {
|
---|
172 | _writer.flush();
|
---|
173 | _writer.close();
|
---|
174 | } catch (IOException ioe) {
|
---|
175 | } finally {
|
---|
176 | _writer.close();
|
---|
177 | }
|
---|
178 |
|
---|
179 | return " exported to " + _output;
|
---|
180 | }
|
---|
181 |
|
---|
182 | protected String finaliseFrame() throws IOException {
|
---|
183 | return "Frame" + finalise();
|
---|
184 | }
|
---|
185 |
|
---|
186 | protected void outputFrame(Frame toWrite) throws IOException {
|
---|
187 | writeStartFrame(toWrite);
|
---|
188 |
|
---|
189 | for (Item i : toWrite.getItems()) {
|
---|
190 | if (_stop)
|
---|
191 | return;
|
---|
192 | writeItem(i);
|
---|
193 | }
|
---|
194 |
|
---|
195 | writeEndFrame(toWrite);
|
---|
196 | }
|
---|
197 |
|
---|
198 | public boolean isRunning() {
|
---|
199 | return _running;
|
---|
200 | }
|
---|
201 |
|
---|
202 | public void stop() {
|
---|
203 | _stop = true;
|
---|
204 | }
|
---|
205 |
|
---|
206 | public String getFileContents() {
|
---|
207 | return "Not Supported";
|
---|
208 | }
|
---|
209 |
|
---|
210 | }
|
---|