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.awt.Polygon;
|
---|
22 | import java.util.ArrayList;
|
---|
23 | import java.util.Collection;
|
---|
24 | import java.util.HashMap;
|
---|
25 | import java.util.HashSet;
|
---|
26 | import java.util.LinkedHashSet;
|
---|
27 | import java.util.LinkedList;
|
---|
28 | import java.util.List;
|
---|
29 | import java.util.Map;
|
---|
30 |
|
---|
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 | private FreeItems() {
|
---|
45 | }
|
---|
46 |
|
---|
47 | public static FreeItems getCursor() {
|
---|
48 | return _cursor;
|
---|
49 | }
|
---|
50 |
|
---|
51 | public static FreeItems getInstance() {
|
---|
52 | return _instance;
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public void clear() {
|
---|
57 | for (Item i : this) {
|
---|
58 | i.invalidateAll();
|
---|
59 | i.invalidateFill();
|
---|
60 | }
|
---|
61 | super.clear();
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public Item remove(int index) {
|
---|
66 | Item i = get(index);
|
---|
67 | remove(i);
|
---|
68 | return i;
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public boolean remove(Object o) {
|
---|
73 | if (o instanceof Item) {
|
---|
74 | ((Item) o).invalidateAll();
|
---|
75 | ((Item) o).invalidateFill();
|
---|
76 | }
|
---|
77 | return super.remove(o);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Return true if there is at least one item attached to the end of the
|
---|
82 | * cursor.
|
---|
83 | *
|
---|
84 | * @return true if at least one item is attached to the cursor.
|
---|
85 | */
|
---|
86 | public static boolean itemsAttachedToCursor() {
|
---|
87 | return getInstance().size() > 0;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Checks if only text items are attached to the cursor.
|
---|
92 | *
|
---|
93 | * @return true if at least one item is attached to the cursor and all items
|
---|
94 | * attached are text items.
|
---|
95 | */
|
---|
96 | public static boolean textOnlyAttachedToCursor() {
|
---|
97 | if (!itemsAttachedToCursor())
|
---|
98 | return false;
|
---|
99 | for (Item i : getInstance()) {
|
---|
100 | if (!(i instanceof Text)) {
|
---|
101 | return false;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 |
|
---|
107 | public static Item getItemAttachedToCursor() {
|
---|
108 | if (itemsAttachedToCursor())
|
---|
109 | return getInstance().get(0);
|
---|
110 | return null;
|
---|
111 | }
|
---|
112 |
|
---|
113 | public static Text getTextAttachedToCursor() {
|
---|
114 | if (textOnlyAttachedToCursor())
|
---|
115 | return (Text) getInstance().get(0);
|
---|
116 | return null;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Creates a list of the free text items.
|
---|
121 | * @return the list of free text items
|
---|
122 | */
|
---|
123 | public static Collection<Text> getTextItems() {
|
---|
124 | Collection<Text> textItems = new LinkedList<Text>();
|
---|
125 | for (Item i : getInstance()) {
|
---|
126 | if (i instanceof Text) {
|
---|
127 | textItems.add((Text) i);
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | return textItems;
|
---|
132 | }
|
---|
133 |
|
---|
134 | public static Map<String, Collection<String>> getGroupedText() {
|
---|
135 | Map<String, Collection<String>> groupedText = new HashMap<String, Collection<String>>();
|
---|
136 | // Go throught the lineEnds
|
---|
137 | Collection<Item> addedItems = new HashSet<Item>();
|
---|
138 | for (Item i : getInstance()) {
|
---|
139 | if (!(i instanceof Text) || !i.isLineEnd()) {
|
---|
140 | continue;
|
---|
141 | }
|
---|
142 | // Check for text inside the box
|
---|
143 | Collection<String> textList = new LinkedList<String>();
|
---|
144 | for (Text enclosed : getInstance().getTextWithin(i)) {
|
---|
145 | textList.add(enclosed.getText());
|
---|
146 | addedItems.add(enclosed);
|
---|
147 | }
|
---|
148 | if (textList.size() > 0) {
|
---|
149 | groupedText.put(i.getText(), textList);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | // Now add the items that were not contained in any of the boxes
|
---|
153 | Collection<String> outsideList = new LinkedList<String>();
|
---|
154 | for (Item i : getInstance()) {
|
---|
155 | if (i instanceof Text && !i.isLineEnd() && !addedItems.contains(i)) {
|
---|
156 | outsideList.add(i.getText());
|
---|
157 | }
|
---|
158 | }
|
---|
159 | groupedText.put("", outsideList);
|
---|
160 |
|
---|
161 | return groupedText;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Gets a list of non-line-end text items within a specified rectangle in
|
---|
166 | * the free items list.
|
---|
167 | *
|
---|
168 | * @param lineEnd
|
---|
169 | * @return
|
---|
170 | */
|
---|
171 | private Collection<Text> getTextWithin(Item lineEnd) {
|
---|
172 | Polygon poly = lineEnd.getEnclosedShape();
|
---|
173 | Collection<Text> results = new LinkedHashSet<Text>();
|
---|
174 | for (Item i : this) {
|
---|
175 | if (i.intersects(poly) && i instanceof Text && !i.isLineEnd()) {
|
---|
176 | results.add((Text) i);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | return results;
|
---|
180 | }
|
---|
181 |
|
---|
182 | public static boolean hasCursor() {
|
---|
183 | return getCursor().size() > 0;
|
---|
184 | }
|
---|
185 |
|
---|
186 | public static void setCursor(Collection<Item> cursor) {
|
---|
187 | _cursor.clear();
|
---|
188 | _cursor.addAll(cursor);
|
---|
189 | }
|
---|
190 |
|
---|
191 | public static boolean hasMultipleVisibleItems() {
|
---|
192 | List<Item> toCount = new LinkedList<Item>(getInstance());
|
---|
193 | int c = 0;
|
---|
194 | while(!toCount.isEmpty()) {
|
---|
195 | Item i = toCount.remove(0);
|
---|
196 | if(i.isVisible()) {
|
---|
197 | toCount.removeAll(i.getAllConnected()); // treat polygons as a single item
|
---|
198 | if(c++ > 0) return true;
|
---|
199 | }
|
---|
200 | }
|
---|
201 | return false;
|
---|
202 | }
|
---|
203 |
|
---|
204 | public static boolean isDrawingPolyLine() {
|
---|
205 | List<Item> tmp = getInstance();
|
---|
206 | return tmp.size() == 2 && tmp.get(0) instanceof Dot && tmp.get(1) instanceof Line;
|
---|
207 | }
|
---|
208 | }
|
---|