source: trunk/src/org/expeditee/io/ExpClipWriter.java@ 821

Last change on this file since 821 was 504, checked in by jts21, 11 years ago

New copy/paste handling, add pdfImporter, add dictionary and documentation to resources, other small changes

File size: 2.6 KB
Line 
1package org.expeditee.io;
2
3import java.io.IOException;
4import java.util.LinkedList;
5import java.util.List;
6
7import org.expeditee.gui.Frame;
8import org.expeditee.items.Item;
9import org.expeditee.items.Line;
10import org.expeditee.items.XRayable;
11import org.expeditee.items.widgets.InteractiveWidget;
12import org.expeditee.items.widgets.WidgetCorner;
13import 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 */
21public 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}
Note: See TracBrowser for help on using the repository browser.