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

Last change on this file since 280 was 280, checked in by bjn8, 16 years ago

packages

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