1 | /**
|
---|
2 | * FreeItems.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.gui;
|
---|
20 |
|
---|
21 | import java.util.ArrayList;
|
---|
22 | import java.util.Collection;
|
---|
23 | import java.util.HashMap;
|
---|
24 | import java.util.HashSet;
|
---|
25 | import java.util.LinkedHashSet;
|
---|
26 | import java.util.LinkedList;
|
---|
27 | import java.util.List;
|
---|
28 | import java.util.Map;
|
---|
29 |
|
---|
30 | import org.expeditee.core.bounds.PolygonBounds;
|
---|
31 | import org.expeditee.items.Dot;
|
---|
32 | import org.expeditee.items.Item;
|
---|
33 | import org.expeditee.items.Line;
|
---|
34 | import org.expeditee.items.Text;
|
---|
35 |
|
---|
36 | public class FreeItems extends ArrayList<Item> {
|
---|
37 |
|
---|
38 | private static final long serialVersionUID = 1L;
|
---|
39 |
|
---|
40 | private static FreeItems _instance = new FreeItems();
|
---|
41 |
|
---|
42 | private static FreeItems _cursor = new FreeItems();
|
---|
43 |
|
---|
44 | public static FreeItems getCursor()
|
---|
45 | {
|
---|
46 | return _cursor;
|
---|
47 | }
|
---|
48 |
|
---|
49 | public static FreeItems getInstance()
|
---|
50 | {
|
---|
51 | return _instance;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /** Singleton class (although there are two of them). */
|
---|
55 | private FreeItems()
|
---|
56 | {
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public void clear()
|
---|
61 | {
|
---|
62 | for (Item i : this) {
|
---|
63 | i.invalidateAll();
|
---|
64 | i.invalidateFill();
|
---|
65 | }
|
---|
66 | super.clear();
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public Item remove(int index)
|
---|
71 | {
|
---|
72 | Item i = get(index);
|
---|
73 | remove(i);
|
---|
74 | return i;
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public boolean remove(Object o)
|
---|
79 | {
|
---|
80 | if (o instanceof Item) {
|
---|
81 | ((Item) o).invalidateAll();
|
---|
82 | ((Item) o).invalidateFill();
|
---|
83 | }
|
---|
84 |
|
---|
85 | return super.remove(o);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Return true if there is at least one item attached to the end of the
|
---|
90 | * cursor.
|
---|
91 | *
|
---|
92 | * @return true if at least one item is attached to the cursor.
|
---|
93 | */
|
---|
94 | public static boolean hasItemsAttachedToCursor()
|
---|
95 | {
|
---|
96 | return getInstance().size() > 0;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Checks if only text items are attached to the cursor.
|
---|
101 | *
|
---|
102 | * @return true if at least one item is attached to the cursor and all items
|
---|
103 | * attached are text items.
|
---|
104 | */
|
---|
105 | public static boolean textOnlyAttachedToCursor()
|
---|
106 | {
|
---|
107 | if (!hasItemsAttachedToCursor()) {
|
---|
108 | return false;
|
---|
109 | }
|
---|
110 |
|
---|
111 | for (Item i : getInstance()) {
|
---|
112 | if (!(i instanceof Text)) {
|
---|
113 | return false;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | return true;
|
---|
118 | }
|
---|
119 |
|
---|
120 | public static Item getItemAttachedToCursor()
|
---|
121 | {
|
---|
122 | if (hasItemsAttachedToCursor()) {
|
---|
123 | return getInstance().get(0);
|
---|
124 | }
|
---|
125 |
|
---|
126 | return null;
|
---|
127 | }
|
---|
128 |
|
---|
129 | public static Text getTextAttachedToCursor()
|
---|
130 | {
|
---|
131 | if (textOnlyAttachedToCursor()) {
|
---|
132 | return (Text) getInstance().get(0);
|
---|
133 | }
|
---|
134 |
|
---|
135 | return null;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Creates a list of the free text items.
|
---|
140 | * @return the list of free text items
|
---|
141 | */
|
---|
142 | public static Collection<Text> getTextItems()
|
---|
143 | {
|
---|
144 | Collection<Text> textItems = new LinkedList<Text>();
|
---|
145 |
|
---|
146 | for (Item i : getInstance()) {
|
---|
147 | if (i instanceof Text) {
|
---|
148 | textItems.add((Text) i);
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | return textItems;
|
---|
153 | }
|
---|
154 |
|
---|
155 | public static Map<String, Collection<String>> getGroupedText()
|
---|
156 | {
|
---|
157 | Map<String, Collection<String>> groupedText = new HashMap<String, Collection<String>>();
|
---|
158 |
|
---|
159 | // Go throught the lineEnds
|
---|
160 | Collection<Item> addedItems = new HashSet<Item>();
|
---|
161 | for (Item i : getInstance()) {
|
---|
162 | if (!(i instanceof Text) || !i.isLineEnd()) {
|
---|
163 | continue;
|
---|
164 | }
|
---|
165 |
|
---|
166 | // Check for text inside the box
|
---|
167 | Collection<String> textList = new LinkedList<String>();
|
---|
168 | for (Text enclosed : getInstance().getTextWithin(i)) {
|
---|
169 | textList.add(enclosed.getText());
|
---|
170 | addedItems.add(enclosed);
|
---|
171 | }
|
---|
172 |
|
---|
173 | if (textList.size() > 0) {
|
---|
174 | groupedText.put(i.getText(), textList);
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | // Now add the items that were not contained in any of the boxes
|
---|
179 | Collection<String> outsideList = new LinkedList<String>();
|
---|
180 | for (Item i : getInstance()) {
|
---|
181 | if (i instanceof Text && !i.isLineEnd() && !addedItems.contains(i)) {
|
---|
182 | outsideList.add(i.getText());
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | groupedText.put("", outsideList);
|
---|
187 |
|
---|
188 | return groupedText;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Gets a list of non-line-end text items within a specified rectangle in
|
---|
193 | * the free items list.
|
---|
194 | *
|
---|
195 | * @param lineEnd
|
---|
196 | * @return
|
---|
197 | */
|
---|
198 | private Collection<Text> getTextWithin(Item lineEnd)
|
---|
199 | {
|
---|
200 | PolygonBounds poly = lineEnd.getEnclosedShape();
|
---|
201 |
|
---|
202 | Collection<Text> results = new LinkedHashSet<Text>();
|
---|
203 | for (Item i : this) {
|
---|
204 | if (i.intersects(poly) && i instanceof Text && !i.isLineEnd()) {
|
---|
205 | results.add((Text) i);
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | return results;
|
---|
210 | }
|
---|
211 |
|
---|
212 | public static boolean hasCursor()
|
---|
213 | {
|
---|
214 | return getCursor().size() > 0;
|
---|
215 | }
|
---|
216 |
|
---|
217 | public static void setCursor(Collection<Item> cursor)
|
---|
218 | {
|
---|
219 | _cursor.clear();
|
---|
220 | _cursor.addAll(cursor);
|
---|
221 | }
|
---|
222 |
|
---|
223 | public static boolean hasMultipleVisibleItems()
|
---|
224 | {
|
---|
225 | List<Item> toCount = new LinkedList<Item>(getInstance());
|
---|
226 |
|
---|
227 | int c = 0;
|
---|
228 | while(!toCount.isEmpty()) {
|
---|
229 | Item i = toCount.remove(0);
|
---|
230 | if(i.isVisible()) {
|
---|
231 | toCount.removeAll(i.getAllConnected()); // treat polygons as a single item
|
---|
232 | if(c++ > 0) {
|
---|
233 | return true;
|
---|
234 | }
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | return false;
|
---|
239 | }
|
---|
240 |
|
---|
241 | public static boolean isDrawingPolyLine()
|
---|
242 | {
|
---|
243 | List<Item> tmp = getInstance();
|
---|
244 |
|
---|
245 | return tmp.size() == 2 && tmp.get(0) instanceof Dot && tmp.get(1) instanceof Line;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Checks if lines are being rubber banded.
|
---|
250 | *
|
---|
251 | * @return true if the user is rubberBanding one or more lines
|
---|
252 | */
|
---|
253 | public static boolean rubberBanding()
|
---|
254 | {
|
---|
255 | if (getInstance().size() != 2) {
|
---|
256 | return false;
|
---|
257 | }
|
---|
258 |
|
---|
259 | // if rubber-banding, there will be 1 line end and the rest will be lines
|
---|
260 | boolean foundLineEnd = false;
|
---|
261 | for (Item i : getInstance()) {
|
---|
262 | if (i.isLineEnd()) {
|
---|
263 | if (foundLineEnd) {
|
---|
264 | return false;
|
---|
265 | }
|
---|
266 | foundLineEnd = true;
|
---|
267 | } else if (!(i instanceof Line) || !i.isVisible()) {
|
---|
268 | return false;
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | return true;
|
---|
273 | }
|
---|
274 |
|
---|
275 | @Override
|
---|
276 | public FreeItems clone()
|
---|
277 | {
|
---|
278 | FreeItems ret = new FreeItems();
|
---|
279 | ret.addAll(this);
|
---|
280 | return ret;
|
---|
281 | }
|
---|
282 | }
|
---|