source: trunk/src/org/expeditee/gui/FreeItems.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: 6.0 KB
Line 
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
19package org.expeditee.gui;
20
21import java.util.ArrayList;
22import java.util.Collection;
23import java.util.HashMap;
24import java.util.HashSet;
25import java.util.LinkedHashSet;
26import java.util.LinkedList;
27import java.util.List;
28import java.util.Map;
29
30import org.expeditee.core.bounds.PolygonBounds;
31import org.expeditee.items.Dot;
32import org.expeditee.items.Item;
33import org.expeditee.items.Line;
34import org.expeditee.items.Text;
35
36public 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()) return false;
108
109 for (Item i : getInstance()) {
110 if (!(i instanceof Text)) return false;
111 }
112
113 return true;
114 }
115
116 public static Item getItemAttachedToCursor()
117 {
118 if (hasItemsAttachedToCursor()) return getInstance().get(0);
119
120 return null;
121 }
122
123 public static Text getTextAttachedToCursor()
124 {
125 if (textOnlyAttachedToCursor()) return (Text) getInstance().get(0);
126
127 return null;
128 }
129
130 /**
131 * Creates a list of the free text items.
132 * @return the list of free text items
133 */
134 public static Collection<Text> getTextItems()
135 {
136 Collection<Text> textItems = new LinkedList<Text>();
137
138 for (Item i : getInstance()) {
139 if (i instanceof Text) textItems.add((Text) i);
140 }
141
142 return textItems;
143 }
144
145 public static Map<String, Collection<String>> getGroupedText()
146 {
147 Map<String, Collection<String>> groupedText = new HashMap<String, Collection<String>>();
148
149 // Go throught the lineEnds
150 Collection<Item> addedItems = new HashSet<Item>();
151 for (Item i : getInstance()) {
152 if (!(i instanceof Text) || !i.isLineEnd()) continue;
153
154 // Check for text inside the box
155 Collection<String> textList = new LinkedList<String>();
156 for (Text enclosed : getInstance().getTextWithin(i)) {
157 textList.add(enclosed.getText());
158 addedItems.add(enclosed);
159 }
160
161 if (textList.size() > 0) {
162 groupedText.put(i.getText(), textList);
163 }
164 }
165
166 // Now add the items that were not contained in any of the boxes
167 Collection<String> outsideList = new LinkedList<String>();
168 for (Item i : getInstance()) {
169 if (i instanceof Text && !i.isLineEnd() && !addedItems.contains(i)) {
170 outsideList.add(i.getText());
171 }
172 }
173
174 groupedText.put("", outsideList);
175
176 return groupedText;
177 }
178
179 /**
180 * Gets a list of non-line-end text items within a specified rectangle in
181 * the free items list.
182 *
183 * @param lineEnd
184 * @return
185 */
186 private Collection<Text> getTextWithin(Item lineEnd)
187 {
188 PolygonBounds poly = lineEnd.getEnclosedShape();
189
190 Collection<Text> results = new LinkedHashSet<Text>();
191 for (Item i : this) {
192 if (i.intersects(poly) && i instanceof Text && !i.isLineEnd()) {
193 results.add((Text) i);
194 }
195 }
196
197 return results;
198 }
199
200 public static boolean hasCursor()
201 {
202 return getCursor().size() > 0;
203 }
204
205 public static void setCursor(Collection<Item> cursor)
206 {
207 _cursor.clear();
208 _cursor.addAll(cursor);
209 }
210
211 public static boolean hasMultipleVisibleItems()
212 {
213 List<Item> toCount = new LinkedList<Item>(getInstance());
214
215 int c = 0;
216 while(!toCount.isEmpty()) {
217 Item i = toCount.remove(0);
218 if(i.isVisible()) {
219 toCount.removeAll(i.getAllConnected()); // treat polygons as a single item
220 if(c++ > 0) return true;
221 }
222 }
223
224 return false;
225 }
226
227 public static boolean isDrawingPolyLine()
228 {
229 List<Item> tmp = getInstance();
230
231 return tmp.size() == 2 && tmp.get(0) instanceof Dot && tmp.get(1) instanceof Line;
232 }
233
234 /**
235 * Checks if lines are being rubber banded.
236 *
237 * @return true if the user is rubberBanding one or more lines
238 */
239 public static boolean rubberBanding()
240 {
241 if (getInstance().size() != 2) return false;
242
243 // if rubber-banding, there will be 1 line end and the rest will be lines
244 boolean foundLineEnd = false;
245 for (Item i : getInstance()) {
246 if (i.isLineEnd()) {
247 if (foundLineEnd) return false;
248 foundLineEnd = true;
249 } else if (!(i instanceof Line) || !i.isVisible()) {
250 return false;
251 }
252 }
253
254 return true;
255 }
256
257 @Override
258 public FreeItems clone()
259 {
260 FreeItems ret = new FreeItems();
261 ret.addAll(this);
262 return ret;
263 }
264}
Note: See TracBrowser for help on using the repository browser.