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