1 | /**
|
---|
2 | * ExpClipWriter.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 |
|
---|
19 | package org.expeditee.io;
|
---|
20 |
|
---|
21 | import java.io.IOException;
|
---|
22 | import java.util.LinkedList;
|
---|
23 | import java.util.List;
|
---|
24 |
|
---|
25 | import org.expeditee.core.Point;
|
---|
26 | import org.expeditee.gui.Frame;
|
---|
27 | import org.expeditee.items.Item;
|
---|
28 | import org.expeditee.items.Line;
|
---|
29 | import org.expeditee.items.XRayable;
|
---|
30 | import org.expeditee.items.widgets.Widget;
|
---|
31 | import org.expeditee.items.widgets.WidgetCorner;
|
---|
32 | import org.expeditee.items.widgets.WidgetEdge;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Subclass of ExpWriter that takes a list of items and writes it to a String
|
---|
36 | * i.e. the changes are: - doesn't write frame header/data
|
---|
37 | * - doesn't write to a file
|
---|
38 | *
|
---|
39 | */
|
---|
40 | public class ExpClipWriter extends ExpWriter {
|
---|
41 |
|
---|
42 | private int dX, dY;
|
---|
43 |
|
---|
44 | public ExpClipWriter(Point p)
|
---|
45 | {
|
---|
46 | this(p.x, p.y);
|
---|
47 | }
|
---|
48 |
|
---|
49 | public ExpClipWriter(int dX, int dY) {
|
---|
50 | super();
|
---|
51 | this.dX = dX;
|
---|
52 | this.dY = dY;
|
---|
53 | _stringWriter = new StringBuilder();
|
---|
54 | }
|
---|
55 |
|
---|
56 | // writes the line to the stringbuilder
|
---|
57 | @Override
|
---|
58 | protected void writeLine(String line) throws IOException {
|
---|
59 | // do not write empty lines
|
---|
60 | if (line == null)
|
---|
61 | return;
|
---|
62 |
|
---|
63 | _stringWriter.append(line + "\n");
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void output(List<Item> items) throws IOException {
|
---|
67 | // switch to savable items
|
---|
68 | LinkedList<Widget> widgets = new LinkedList<Widget>();
|
---|
69 | // make an array to iterate over instead of the list so we don't get stuck when we remove items from the list
|
---|
70 | Item[] tmpitems = items.toArray(new Item[0]);
|
---|
71 | for(Item i : tmpitems) {
|
---|
72 | if (i instanceof XRayable) {
|
---|
73 | items.remove(i);
|
---|
74 | // Show the items
|
---|
75 | for (Item item : ((XRayable) i).getConnected()) {
|
---|
76 | item.setVisible(true);
|
---|
77 | item.removeEnclosure(i);
|
---|
78 | }
|
---|
79 | } else if (i instanceof WidgetCorner) {
|
---|
80 | Widget iw = ((WidgetCorner)i).getWidgetSource();
|
---|
81 | if(!widgets.contains(iw)) {
|
---|
82 | widgets.add(iw);
|
---|
83 | }
|
---|
84 | items.remove(i);
|
---|
85 | } else if (i instanceof WidgetEdge) {
|
---|
86 | Widget iw = ((WidgetEdge)i).getWidgetSource();
|
---|
87 | if(!widgets.contains(iw)) {
|
---|
88 | widgets.add(iw);
|
---|
89 | }
|
---|
90 | items.remove(i);
|
---|
91 | } else if (i.hasFormula()) {
|
---|
92 | i.setText(i.getFormula());
|
---|
93 | } else if (i.hasOverlay()) {
|
---|
94 | i.setVisible(true);
|
---|
95 | // int x = i.getBoundsHeight();
|
---|
96 | }
|
---|
97 | }
|
---|
98 | for (Widget iw : widgets) {
|
---|
99 | items.add(iw.getSource());
|
---|
100 | }
|
---|
101 | widgets.clear();
|
---|
102 | // write each item in the frame
|
---|
103 | for (Item i : items) {
|
---|
104 | assert (!(i instanceof Line));
|
---|
105 | i.setPosition(i.getX() - dX, i.getY() - dY);
|
---|
106 | writeItemAlways(i);
|
---|
107 | }
|
---|
108 |
|
---|
109 | // write any lines or constraints
|
---|
110 | writeTerminator();
|
---|
111 | writeLineData();
|
---|
112 | writeTerminator();
|
---|
113 | writeConstraintData();
|
---|
114 | writeTerminator();
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | public void outputFrame(Frame frame) throws IOException {
|
---|
119 | // Does nothing, just stops ExpWriter's outputFrame from running
|
---|
120 | }
|
---|
121 | }
|
---|