source: trunk/src/org/expeditee/io/DefaultFrameWriter.java@ 1379

Last change on this file since 1379 was 1379, checked in by bln4, 5 years ago

Reimplementation of the functionality that limits a text item to one line by reducing font size. The user must now specify inject a width property (eg Width: 300) and inject the property 'SingleLineOnly: true' to cause the functionality to happen. This new version of the functionality also now works with pasting large amounts of content rather than adding a single character at the time. Also, this new version dynamically returns to the larger font size as the length of the text item decreases.

File size: 10.4 KB
Line 
1/**
2 * DefaultFrameWriter.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.io;
20
21import java.io.BufferedOutputStream;
22import java.io.File;
23import java.io.FileOutputStream;
24import java.io.IOException;
25import java.io.OutputStream;
26import java.io.OutputStreamWriter;
27import java.io.Writer;
28import java.lang.reflect.Method;
29import java.util.Collection;
30import java.util.HashSet;
31import java.util.LinkedHashMap;
32
33import org.expeditee.agents.WriteTree;
34import org.expeditee.gio.EcosystemManager;
35import org.expeditee.gui.Frame;
36import org.expeditee.gui.FrameIO;
37import org.expeditee.items.Item;
38import org.expeditee.items.Line;
39import org.expeditee.items.Text;
40
41public abstract class DefaultFrameWriter extends ItemWriter implements
42 FrameWriter {
43
44 protected String _filename = null;
45
46 protected String _output = null;
47
48 protected ProxyWriter _writer = null;
49
50 protected String _format = "";
51
52 protected boolean _running = true;
53
54 protected boolean _stop = false;
55
56 // keep track of methods that are put on the same line
57 protected static LinkedHashMap<Character, Method> _ItemTags = null;
58 // IMPORTANT: keys in _ItemTagsExt must start with underscore as their first character
59 protected static LinkedHashMap<String, Method> _ItemTagsExt = null;
60
61 protected static LinkedHashMap<Character, Method> _FrameTags = null;
62
63 public DefaultFrameWriter() {
64
65 if (_ItemTags != null && _FrameTags != null)
66 return;
67
68 _ItemTags = new LinkedHashMap<Character, Method>();
69 _ItemTagsExt = new LinkedHashMap<String, Method>();
70 _FrameTags = new LinkedHashMap<Character, Method>();
71
72 try {
73 _FrameTags.put('V', Frame.class.getMethod("getVersion"));
74 _FrameTags.put('p', Frame.class.getMethod("getPermission"));
75 _FrameTags.put('U', Frame.class.getMethod("getOwner"));
76 _FrameTags.put('D', Frame.class.getMethod("getDateCreated"));
77 _FrameTags.put('M', Frame.class.getMethod("getLastModifyUser"));
78 _FrameTags.put('d', Frame.class.getMethod("getLastModifyDate"));
79 _FrameTags.put('F', Frame.class.getMethod("getFrozenDate"));
80 _FrameTags.put('O', Frame.class.getMethod("getForegroundColor"));
81 _FrameTags.put('B', Frame.class.getMethod("getBackgroundColor"));
82 _FrameTags.put('K', Frame.class.getMethod("getEncryptionLabel"));
83 _FrameTags.put('T', Frame.class.getMethod("getData"));
84 _FrameTags.put('E', Frame.class.getMethod("getEncryptionPermission"));
85
86 // Note: As of 26/11/18 there are no unused letter item tags. Use other characters.
87 _ItemTags.put('S', Item.class.getMethod("getTypeAndID"));
88 _ItemTags.put('s', Item.class.getMethod("getDateCreated"));
89 _ItemTags.put('d', Item.class.getMethod("getColor"));
90 _ItemTags.put('G', Item.class.getMethod("getBackgroundColor"));
91 _ItemTags.put('K', Item.class.getMethod("getBorderColor"));
92
93 _ItemTags.put('P', Item.class.getMethod("getPosition"));
94 _ItemTags.put('T', Text.class.getMethod("getText"));
95 _ItemTags.put('F', Item.class.getMethod("getLink"));
96 _ItemTags.put('X', Item.class.getMethod("getAction"));
97 _ItemTags.put('x', Item.class.getMethod("getActionMark"));
98 _ItemTags.put('U', Item.class.getMethod("getActionCursorEnter"));
99 _ItemTags.put('V', Item.class.getMethod("getActionCursorLeave"));
100 _ItemTags.put('W', Item.class.getMethod("getActionEnterFrame"));
101 _ItemTags.put('Y', Item.class.getMethod("getActionLeaveFrame"));
102 _ItemTags.put('D', Item.class.getMethod("getData"));
103 _ItemTags.put('u', Item.class.getMethod("getHighlight"));
104 _ItemTags.put('e', Item.class.getMethod("getFillColor"));
105 _ItemTags.put('E', Item.class.getMethod("getGradientColor"));
106 _ItemTags.put('Q', Item.class.getMethod("getGradientAngle"));
107
108 _ItemTags.put('R', Item.class.getMethod("getAnchorLeft"));
109 _ItemTags.put('H', Item.class.getMethod("getAnchorRight"));
110 _ItemTags.put('N', Item.class.getMethod("getAnchorTop"));
111 _ItemTags.put('I', Item.class.getMethod("getAnchorBottom"));
112
113 _ItemTags.put('i', Item.class.getMethod("getFillPattern"));
114 _ItemTags.put('o', Item.class.getMethod("getOwner"));
115 _ItemTags.put('n', Item.class.getMethod("getLinkMark"));
116 _ItemTags.put('q', Item.class.getMethod("getLinkFrameset"));
117 _ItemTags.put('y', Item.class.getMethod("getLinkTemplate"));
118 _ItemTags.put('g', Item.class.getMethod("getLinePattern"));
119
120 _ItemTags.put('j', Item.class.getMethod("getArrow"));
121
122 _ItemTags.put('v', Item.class.getMethod("getDotType"));
123 _ItemTags.put('z', Item.class.getMethod("getFilled"));
124
125 _ItemTags.put('f', Text.class.getMethod("getFont"));
126 _ItemTags.put('t', Text.class.getMethod("getSpacing"));
127
128 _ItemTags.put('O', Text.class.getMethod("getMask"));
129
130 // TODO set a boolean flag to indicate that the text is a formula
131 // Store the formula in the text property NOT the answer
132 _ItemTags.put('J', Item.class.getMethod("getFormula"));
133
134 _ItemTags.put('a', Text.class.getMethod("getWordSpacing"));
135 _ItemTags.put('b', Text.class.getMethod("getLetterSpacing"));
136 _ItemTags.put('m', Text.class.getMethod("getInitialSpacing"));
137 _ItemTags.put('w', Text.class.getMethod("getWidthToSave"));
138 _ItemTags.put('M', Text.class.getMethod("getMinWidthToSave"));
139 _ItemTags.put('k', Text.class.getMethod("getJustification"));
140 _ItemTags.put('r', Text.class.getMethod("getAutoWrapToSave"));
141
142 _ItemTags.put('h', Item.class.getMethod("getThickness"));
143 _ItemTags.put('l', Item.class.getMethod("getLineIDs"));
144 _ItemTags.put('c', Item.class.getMethod("getConstraintIDs"));
145
146 _ItemTags.put('A', Item.class.getMethod("getTooltip"));
147 _ItemTags.put('B', Item.class.getMethod("getLinkHistory"));
148
149 _ItemTags.put('p', Item.class.getMethod("getPermission"));
150
151 _ItemTags.put('[', Item.class.getMethod("getMagnetizedItemLeft"));
152 _ItemTags.put(']', Item.class.getMethod("getMagnetizedItemRight"));
153 _ItemTags.put('^', Item.class.getMethod("getMagnetizedItemTop"));
154 _ItemTags.put('/', Item.class.getMethod("getMagnetizedItemBottom"));
155
156 _ItemTagsExt.put("_ph", Text.class.getMethod("getPlaceholder"));
157 _ItemTagsExt.put("_sl", Text.class.getMethod("isSingleLineOnly"));
158 } catch (Exception e) {
159 e.printStackTrace();
160 }
161 }
162
163 public void setOutputLocation(String filename) {
164 _filename = filename;
165 }
166
167 public String writeFrame(Frame toWrite) throws IOException {
168 return writeFrame(toWrite, null);
169 }
170
171 public String writeFrame(Frame toWrite, Writer writer) throws IOException {
172 try {
173 initialise(toWrite, writer);
174
175 outputFrame(toWrite);
176
177 _running = false;
178 return finaliseFrame();
179 } catch (IOException ioe) {
180 _running = false;
181 throw ioe;
182 }
183
184 }
185
186 /**
187 * Called before writing out the body items of each frame.
188 *
189 * @param starting
190 * the name of the frame currently being written out.
191 * @throws IOException
192 */
193 protected void writeStartFrame(Frame starting) throws IOException {
194 if (starting.getTitleItem() != null) {
195 if (starting.getTitleItem().isAnnotation())
196 this.writeAnnotationTitle(starting.getTitleItem());
197 else
198 this.writeTitle(starting.getTitleItem(), starting.getItems());
199 }
200 }
201
202 /**
203 * Called after writing out the body items of each frame.
204 *
205 * @param ending
206 * the name of the frame currently being written out.
207 * @throws IOException
208 */
209 protected void writeEndFrame(Frame ending) throws IOException {
210 }
211
212 protected final void initialise(Frame start) throws IOException {
213 initialise(start, null);
214 }
215
216 protected void initialise(Frame start, Writer writer) throws IOException {
217 if (_filename == null)
218 _filename = FrameIO.EXPORTS_PATH + getFileName(start) + _format;
219
220 if (writer != null) {
221 _writer = new ProxyWriter(writer);
222 _output = writer.toString();
223 } else if (_filename.equalsIgnoreCase(WriteTree.CLIPBOARD)) {
224 _writer = new ProxyWriter(true);
225 _output = WriteTree.CLIPBOARD;
226 } else {
227 if (_filename.contains(File.separator)) {
228 String extTest = _filename.substring(_filename
229 .lastIndexOf(File.separator) + 1);
230 if (!extTest.contains("."))
231 _filename += _format;
232 } else if (!_filename.contains("."))
233 _filename += _format;
234
235 if (!_filename.contains(File.separator))
236 _filename = FrameIO.EXPORTS_PATH + _filename;
237
238 File test = new File(_filename);
239
240 File parent = test.getParentFile();
241 if (parent != null && !parent.exists()) {
242 parent.mkdirs();
243 }
244
245 // Open an Output Stream Writer to set encoding
246 OutputStream fout = new FileOutputStream(_filename);
247 OutputStream bout = new BufferedOutputStream(fout);
248 Writer out = new OutputStreamWriter(bout, "UTF-8");
249
250 _writer = new ProxyWriter(out);
251 _output = _filename;
252 }
253 }
254
255 protected String getFileName(Frame start) {
256 return getValidFilename(start.getTitle());
257 }
258
259 public static String getValidFilename(String filename) {
260 return filename.replaceAll("[ \\.]", "_");
261 }
262
263 protected String finalise() throws IOException {
264 try {
265 _writer.flush();
266 _writer.close();
267 } catch (IOException ioe) {
268 } finally {
269 _writer.close();
270 }
271 String errorMessage = EcosystemManager.getMiscManager().open(_output);
272 if (errorMessage != null) System.err.println(errorMessage);
273 return " exported to " + _output;
274 }
275
276 protected String finaliseFrame() throws IOException {
277 return "Frame" + finalise();
278 }
279
280 protected void outputFrame(Frame toWrite) throws IOException {
281 writeStartFrame(toWrite);
282
283 Collection<Item> done = new HashSet<Item>();
284
285 for (Item i : getItemsToWrite(toWrite)) {
286 if (_stop) {
287 return;
288 }
289
290 if (i instanceof Line) {
291 if (done.contains(i)) {
292 continue;
293 }
294 done.addAll(i.getAllConnected());
295 }
296 writeItem(i);
297 }
298 writeEndFrame(toWrite);
299 }
300
301 protected Collection<Item> getItemsToWrite(Frame toWrite) {
302 return toWrite.getItemsToSave();
303 }
304
305 public boolean isRunning() {
306 return _running;
307 }
308
309 public void stop() {
310 _stop = true;
311 }
312
313 public String getFileContents() {
314 return "Not Supported";
315 }
316
317}
Note: See TracBrowser for help on using the repository browser.