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