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