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