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

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

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