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