source: trunk/src/org/expeditee/io/ExpClipReader.java@ 1102

Last change on this file since 1102 was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

File size: 7.1 KB
Line 
1/**
2 * ExpClipReader.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.BufferedReader;
22import java.io.IOException;
23import java.util.ArrayList;
24import java.util.HashMap;
25import java.util.List;
26
27import org.expeditee.core.Point;
28import org.expeditee.gui.DisplayController;
29import org.expeditee.gui.Frame;
30import org.expeditee.gui.FrameGraphics;
31import org.expeditee.items.Circle;
32import org.expeditee.items.Constraint;
33import org.expeditee.items.Dot;
34import org.expeditee.items.FrameBitmap;
35import org.expeditee.items.FrameImage;
36import org.expeditee.items.Item;
37import org.expeditee.items.ItemUtils;
38import org.expeditee.items.Line;
39import org.expeditee.items.Text;
40import org.expeditee.items.widgets.Widget;
41
42/**
43 * Takes a String containing data produced by ExpClipWriter,
44 * and generates a list of Items
45 *
46 * Most of this code is copy+pasted and modified from ExpReader, since it's too different to just subclass
47 *
48 */
49public class ExpClipReader extends ExpReader {
50
51 private HashMap<Integer, Item> _linePoints;
52 private ArrayList<Item> _items;
53 private int dX, dY;
54
55 public ExpClipReader(Point p)
56 {
57 this(p.x, p.y);
58 }
59
60 public ExpClipReader(int dX, int dY) {
61 super("");
62 this.dX = dX;
63 this.dY = dY;
64 }
65
66 /**
67 * Makes images, circles and widgets display without having to toggle xray mode
68 *
69 */
70 public static void updateItems(List<Item> items) {
71 if(DisplayController.isXRayMode()) {
72 return;
73 }
74 // make an array to iterate over instead of the list so we don't get stuck when we remove items from the list
75 Item[] tmpitems = items.toArray(new Item[0]);
76 for(Item item : tmpitems) {
77 if(!(item instanceof Text)) {
78 continue;
79 }
80 if (ItemUtils.startsWithTag(item, ItemUtils.TAG_IMAGE, true)) {
81 if (!item.hasEnclosures()) {
82 items.add(ItemUtils.CreatePicture((Text) item));
83 }
84 // check for frame images
85 } else if (ItemUtils.startsWithTag(item,
86 ItemUtils.TAG_FRAME_IMAGE)
87 && item.getLink() != null
88 && !item.getAbsoluteLink()
89 .equalsIgnoreCase(
90 DisplayController.getCurrentFrame().getName())) {
91 if (item.hasEnclosures()) {
92 // item.setHidden(true);
93 // image =
94 // item.getEnclosures().iterator().next();
95 // image.refresh();
96 } else {
97 items.add(new FrameImage((Text) item, null));
98 }
99 } else if (ItemUtils.startsWithTag(item,
100 ItemUtils.TAG_BITMAP_IMAGE)
101 && item.getLink() != null
102 && !item.getAbsoluteLink()
103 .equalsIgnoreCase(
104 DisplayController.getCurrentFrame().getName())) {
105 if (item.hasEnclosures()) {
106 // image =
107 // item.getEnclosures().iterator().next();
108 // image.refresh();
109 // item.setHidden(true);
110 } else {
111 // If a new bitmap is created for a
112 // frame which already has a bitmap dont
113 // recreate the bitmap
114 items.add(new FrameBitmap((Text) item, null));
115 }
116 } else if (ItemUtils.startsWithTag(item, "@c")) {
117 // Can only have a @c
118 if (!item.hasEnclosures()
119 && item.getLines().size() == 1) {
120 items.add(new Circle((Text) item));
121 }
122 // Check for interactive widgets
123 } else if (ItemUtils.startsWithTag(item, ItemUtils.TAG_IWIDGET)) {
124 items.remove(item);
125 item.setParent(DisplayController.getCurrentFrame());
126 try {
127 items.addAll(Widget.createWidget((Text) item).getItems());
128 } catch (Exception e) {
129 System.err.println("Failed to create widget");
130 e.printStackTrace();
131 }
132 } else {
133 // System.out.println("None of the above");
134 }
135 }
136 }
137
138 public ArrayList<Item> read(String data) {
139
140 _linePoints = new HashMap<Integer, Item>();
141 _items = new ArrayList<Item>();
142
143 String[] lines = data.split("\n");
144 int index = 0;
145
146 try {
147 // Read all the items
148 Item currentItem = null;
149 while(index < lines.length && !(lines[index].equals("Z"))) {
150 // if this is the start of a new item add a new item
151 if (isValidLine(lines[index])) {
152 if (getTag(lines[index]) == 'S') {
153 if(currentItem != null) {
154 currentItem.setPosition(currentItem.getX() + dX, currentItem.getY() + dY);
155 }
156 String value = getValue(lines[index]);
157 int id = Integer.parseInt(value.substring(2));
158
159 switch (value.charAt(0)) {
160 case 'P': // check if its a point
161 currentItem = new Dot(id);
162 break;
163 default:
164 currentItem = new Text(id);
165 break;
166 }
167 _linePoints.put(currentItem.getID(), currentItem);
168 _items.add(currentItem);
169 } else if (currentItem != null) {
170 processBodyLine(currentItem, lines[index]);
171 }
172 }
173 ++index;
174 }
175 if(currentItem != null) {
176 currentItem.setPosition(currentItem.getX() + dX, currentItem.getY() + dY);
177 }
178
179 // Read the lines
180 while(index < lines.length && !(lines[++index].equals("Z"))) {
181 if (isValidLine(lines[index])) {
182 Point idtype = separateValues(lines[index].substring(2));
183 // The next line must be the endpoints
184 if (index >= lines.length)
185 throw new Exception("Unexpected end of file");
186 ++index;
187 Point startend = separateValues(lines[index].substring(2));
188 int start = startend.x;
189 int end = startend.y;
190
191 if (_linePoints.get(start) != null
192 && _linePoints.get(end) != null) {
193 _items.add(new Line(_linePoints.get(start),
194 _linePoints.get(end), idtype.x));
195 } else {
196 System.out
197 .println("Error reading line with unknown end points");
198 }
199 }
200 }
201
202 // Read the constraints
203 while(index < lines.length && !(lines[++index].equals("Z"))) {
204 if (isValidLine(lines[index])) {
205 Point idtype = separateValues(lines[index].substring(2));
206 // The next line must be the endpoints
207 if (index >= lines.length)
208 throw new Exception("Unexpected end of file");
209 ++index;
210 Point startend = separateValues(lines[index].substring(2));
211
212 Item a = _linePoints.get(startend.x);
213 Item b = _linePoints.get(startend.y);
214
215 // System.out.println("adding constraint" + a + " " + b + " " + idtype.x + " " + idtype.y);
216
217 new Constraint(a, b, idtype.x, idtype.y);
218 }
219 }
220 } catch (Exception e) {
221 e.printStackTrace();
222 System.out.println("Error reading expeditee data line: " + lines[index] + " "
223 + e.getMessage());
224 }
225
226 updateItems(_items);
227 return _items;
228 }
229
230 @Override
231 public Frame readFrame(BufferedReader frameContents) throws IOException {
232 // Does nothing, just stops ExpReader's readFrame from running
233 return null;
234 }
235}
Note: See TracBrowser for help on using the repository browser.