001: /*
002: @COPYRIGHT@
003: */
004: package demo.sharededitor.models;
005:
006: import demo.sharededitor.events.ListListener;
007:
008: import java.util.ArrayList;
009: import java.util.List;
010: import java.util.Collections;
011: import java.util.Iterator;
012:
013: public class ObjectManager implements ListListener {
014: private List objList;
015:
016: private transient List grabList;
017:
018: private transient ListListener listener;
019:
020: private transient BaseObject lastGrabbed;
021:
022: public ObjectManager() {
023: objList = Collections.synchronizedList(new ArrayList());
024: init_transients();
025: notifyListener(null);
026: }
027:
028: public void init_transients() {
029: listener = null;
030: grabList = Collections.synchronizedList(new ArrayList());
031: }
032:
033: public void setListener(ListListener listener) {
034: this .listener = listener;
035: notifyListener(null);
036: }
037:
038: private void notifyListener(Object obj) {
039: if (listener != null) {
040: listener.changed(this , obj);
041: }
042: }
043:
044: public synchronized void add(BaseObject obj) {
045: synchronized (objList) {
046: if (objList.contains(obj))
047: return;
048:
049: obj.addListener(this );
050: objList.add(obj);
051: obj.notifyListeners(obj);
052:
053: notifyListener(obj);
054: }
055: }
056:
057: public synchronized void remove(BaseObject obj) {
058: synchronized (objList) {
059: if (!objList.contains(obj))
060: return;
061:
062: objList.remove(obj);
063: obj.notifyListeners(obj);
064: obj.removeListener(this );
065:
066: notifyListener(obj);
067: }
068: }
069:
070: public BaseObject[] reversedList() {
071: synchronized (objList) {
072: List list = new ArrayList(objList);
073: Collections.reverse(list);
074: return (BaseObject[]) list.toArray(new BaseObject[0]);
075: }
076: }
077:
078: public BaseObject[] list() {
079: synchronized (objList) {
080: return (BaseObject[]) objList.toArray(new BaseObject[0]);
081: }
082: }
083:
084: public boolean canGrabAt(int x, int y) {
085: BaseObject[] list = reversedList();
086: for (int i = 0; i < list.length; i++) {
087: BaseObject obj = list[i];
088: if (obj.isAt(x, y))
089: return true;
090: }
091: return false;
092: }
093:
094: public BaseObject create(int x, int y, String name) {
095: BaseObject obj = BaseObject.createObject(name);
096: obj.move(x, y);
097: add(obj);
098: ungrabAll();
099: grab(obj, x, y);
100: return obj;
101: }
102:
103: public BaseObject grabAt(int x, int y, boolean ungrabAll) {
104: BaseObject[] list = reversedList();
105: for (int i = 0; i < list.length; i++) {
106: BaseObject obj = list[i];
107: if (obj.isAt(x, y)) {
108: if (ungrabAll)
109: ungrabAll();
110: grab(obj, x, y);
111: return obj;
112: }
113: }
114: if (ungrabAll)
115: ungrabAll();
116: return null;
117: }
118:
119: public BaseObject lastGrabbed() {
120: return lastGrabbed;
121: }
122:
123: private void ungrabAll() {
124: synchronized (grabList) {
125: lastGrabbed = null;
126: grabList.clear();
127: notifyListener(null);
128: }
129: }
130:
131: private void grab(BaseObject obj, int x, int y) {
132: synchronized (grabList) {
133: obj.selectAction(true);
134:
135: if (grabList.contains(obj))
136: return;
137:
138: grabList.add(obj);
139: obj.setGrabbedAnchorAt(x, y);
140:
141: notifyListener(obj);
142: lastGrabbed = obj;
143: }
144: }
145:
146: private void ungrab(BaseObject obj) {
147: synchronized (grabList) {
148: obj.selectAction(false);
149:
150: if (!grabList.contains(obj))
151: return;
152:
153: grabList.remove(obj);
154: notifyListener(obj);
155: lastGrabbed = null;
156: }
157: }
158:
159: private void grab(BaseObject obj) {
160: synchronized (grabList) {
161: if (grabList.contains(obj))
162: return;
163:
164: obj.selectAction(true);
165: grabList.add(obj);
166: notifyListener(obj);
167: lastGrabbed = obj;
168: }
169: }
170:
171: public void deleteSelection() {
172: synchronized (grabList) {
173: Iterator i = grabList.iterator();
174: while (i.hasNext())
175: remove((BaseObject) i.next());
176: }
177: }
178:
179: public void selectAll() {
180: BaseObject[] list = list();
181: for (int i = 0; i < list.length; i++)
182: grab(list[i]);
183: }
184:
185: public void selectAllWithin(BaseObject boundary) {
186: BaseObject[] list = list();
187: for (int i = 0; i < list.length; i++) {
188: java.awt.Shape s1 = boundary.getShape();
189: java.awt.Shape s2 = list[i].getShape();
190: if (s1.contains(s2.getBounds2D()))
191: grab(list[i]);
192: }
193: }
194:
195: public void clearSelection() {
196: BaseObject[] list = list();
197: for (int i = 0; i < list.length; i++)
198: ungrab(list[i]);
199: }
200:
201: public void invertSelection() {
202: BaseObject[] list = list();
203: for (int i = 0; i < list.length; i++) {
204: BaseObject obj = list[i];
205: if (isGrabbed(obj))
206: ungrab(obj);
207: else
208: grab(obj);
209: }
210: }
211:
212: public void toggleSelection() {
213: synchronized (objList) {
214: synchronized (grabList) {
215: if (objList.size() == grabList.size())
216: clearSelection();
217: else
218: selectAll();
219: }
220: }
221: }
222:
223: public boolean isGrabbed(BaseObject obj) {
224: synchronized (grabList) {
225: return grabList.contains(obj);
226: }
227: }
228:
229: public void changed(Object source, Object obj) {
230: notifyListener(obj);
231: }
232: }
|