source: trunk/src/org/expeditee/gui/FreeItems.java@ 242

Last change on this file since 242 was 242, checked in by ra33, 16 years ago
File size: 3.6 KB
Line 
1package org.expeditee.gui;
2
3import java.awt.Polygon;
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.HashMap;
7import java.util.HashSet;
8import java.util.LinkedHashSet;
9import java.util.LinkedList;
10import java.util.Map;
11
12import org.expeditee.items.Item;
13import org.expeditee.items.Text;
14import org.expeditee.items.XRayable;
15
16public class FreeItems extends ArrayList<Item> {
17
18 private static final long serialVersionUID = 1L;
19
20 private static FreeItems _instance = new FreeItems();
21
22 private FreeItems() {
23 }
24
25 public static FreeItems getInstance() {
26 return _instance;
27 }
28
29 @Override
30 public void clear() {
31 for (Item i : this) {
32 i.invalidateAll();
33 i.invalidateFill();
34 }
35 super.clear();
36 }
37
38 @Override
39 public Item remove(int index) {
40 Item i = get(index);
41 remove(i);
42 return i;
43 }
44
45 @Override
46 public boolean remove(Object o) {
47 if (o instanceof Item) {
48 ((Item) o).invalidateAll();
49 ((Item) o).invalidateFill();
50 }
51 return super.remove(o);
52 }
53
54 /**
55 * Return true if there is at least one item attached to the end of the
56 * cursor.
57 *
58 * @return true if at least one item is attached to the cursor.
59 */
60 public static boolean itemsAttachedToCursor() {
61 return getInstance().size() > 0;
62 }
63
64 /**
65 * Checks if only text items are attached to the cursor.
66 *
67 * @return true if at least one item is attached to the cursor and all items
68 * attached are text items.
69 */
70 public static boolean textOnlyAttachedToCursor() {
71 if (!itemsAttachedToCursor())
72 return false;
73 for (Item i : getInstance()) {
74 if (!(i instanceof Text)) {
75 return false;
76 }
77 }
78 return true;
79 }
80
81 public static Item getItemAttachedToCursor() {
82 if (itemsAttachedToCursor())
83 return getInstance().get(0);
84 return null;
85 }
86
87 public static Text getTextAttachedToCursor() {
88 if (textOnlyAttachedToCursor())
89 return (Text) getInstance().get(0);
90 return null;
91 }
92
93 /**
94 * Creates a list of the free text items.
95 * @return the list of free text items
96 */
97 public static Collection<Text> getTextItems() {
98 Collection<Text> textItems = new LinkedList<Text>();
99 for (Item i : getInstance()) {
100 if (i instanceof Text) {
101 textItems.add((Text) i);
102 }
103 }
104
105 return textItems;
106 }
107
108 public static Map<String, Collection<String>> getGroupedText() {
109 Map<String, Collection<String>> groupedText = new HashMap<String, Collection<String>>();
110 // Go throught the lineEnds
111 Collection<Item> addedItems = new HashSet<Item>();
112 for (Item i : getInstance()) {
113 if (!(i instanceof Text) || !i.isLineEnd()) {
114 continue;
115 }
116 // Check for text inside the box
117 Collection<String> textList = new LinkedList<String>();
118 for (Text enclosed : getInstance().getTextWithin(i)) {
119 textList.add(enclosed.getText());
120 addedItems.add(enclosed);
121 }
122 if (textList.size() > 0) {
123 groupedText.put(i.getText(), textList);
124 }
125 }
126 // Now add the items that were not contained in any of the boxes
127 Collection<String> outsideList = new LinkedList<String>();
128 for (Item i : getInstance()) {
129 if (i instanceof Text && !i.isLineEnd() && !addedItems.contains(i)) {
130 outsideList.add(i.getText());
131 }
132 }
133 groupedText.put("", outsideList);
134
135 return groupedText;
136 }
137
138 /**
139 * Gets a list of non-line-end text items within a specified rectangle in
140 * the free items list.
141 *
142 * @param lineEnd
143 * @return
144 */
145 private Collection<Text> getTextWithin(Item lineEnd) {
146 Polygon poly = lineEnd.getEnclosedShape();
147 Collection<Text> results = new LinkedHashSet<Text>();
148 for (Item i : this) {
149 if (i.intersects(poly) && i instanceof Text && !i.isLineEnd()) {
150 results.add((Text) i);
151 }
152 }
153 return results;
154 }
155}
Note: See TracBrowser for help on using the repository browser.