1 56 package org.objectstyle.cayenne.dataview; 57 58 import java.util.AbstractList ; 59 import java.util.ArrayList ; 60 import java.util.Collection ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 64 import org.objectstyle.cayenne.DataObject; 65 66 75 public class DataObjectList extends AbstractList { 76 77 protected List dataObjects; 78 protected EventDispatcher changeDispatcher; 79 80 public DataObjectList() { 81 dataObjects = new ArrayList (); 82 } 83 84 public DataObjectList(int capacity) { 85 dataObjects = new ArrayList (capacity); 86 } 87 88 public DataObjectList(Collection dataObjects, boolean typeCheck) { 89 if (typeCheck) { 90 for (Iterator i = dataObjects.iterator(); i.hasNext();) { 91 if (!(i.next() instanceof DataObject)) { 92 this.dataObjects = new ArrayList (1); 93 return; 94 } 95 } 96 } 97 this.dataObjects = new ArrayList (dataObjects); 98 } 99 100 public DataObjectList(Collection dataObjects) { 101 this(dataObjects, true); 102 } 103 104 public void addDataObjectChangeListener(DataObjectChangeListener listener) { 105 changeDispatcher = EventDispatcher.add(changeDispatcher, listener); 106 } 107 108 public void removeDataObjectChangeListener(DataObjectChangeListener listener) { 109 changeDispatcher = EventDispatcher.remove(changeDispatcher, listener); 110 } 111 112 public void clearDataObjectChangeListeners() { 113 if (changeDispatcher != null) { 114 changeDispatcher.clear(); 115 changeDispatcher = null; 116 } 117 } 118 119 public int indexOf(Object o) { 120 if (!(o instanceof DataObject)) 121 return -1; 122 return dataObjects.indexOf(o); 123 } 124 125 public int lastIndexOf(Object o) { 126 if (!(o instanceof DataObject)) 127 return -1; 128 return dataObjects.lastIndexOf(o); 129 } 130 131 public boolean contains(Object o) { 132 return (indexOf(o) >= 0); 133 } 134 135 public boolean isEmpty() { 136 return dataObjects.isEmpty(); 137 } 138 139 public int size() { 140 return dataObjects.size(); 141 } 142 143 public boolean add(DataObject dataObject) { 144 boolean success = dataObjects.add(dataObject); 145 int index = dataObjects.size() - 1; 146 if (success && changeDispatcher != null) 147 changeDispatcher.dispatch(new DataObjectChangeEvent( 148 this, 149 DataObjectChangeEvent.DATAOBJECT_ADDED, 150 index)); 151 return success; 152 } 153 154 public void add(int index, DataObject dataObject) { 155 dataObjects.add(index, dataObject); 156 if (changeDispatcher != null) 157 changeDispatcher.dispatch(new DataObjectChangeEvent( 158 this, 159 DataObjectChangeEvent.DATAOBJECT_ADDED, 160 index)); 161 } 162 163 public boolean add(Object o) { 164 return add((DataObject) o); 165 } 166 167 public void add(int index, Object element) { 168 add(index, (DataObject) element); 169 } 170 171 public Object remove(int index) { 172 Object dataObject = dataObjects.remove(index); 173 if (dataObject != null && changeDispatcher != null) 174 changeDispatcher.dispatch(new DataObjectChangeEvent( 175 this, 176 DataObjectChangeEvent.DATAOBJECT_REMOVED, 177 index)); 178 return dataObject; 179 } 180 181 public boolean remove(Object o) { 182 int index = indexOf(o); 183 return remove(index) != null; 184 } 185 186 public DataObject set(int index, DataObject dataObject) { 187 DataObject oldObject = (DataObject) dataObjects.set(index, dataObject); 188 if (changeDispatcher != null) 189 changeDispatcher.dispatch(new DataObjectChangeEvent( 190 this, 191 DataObjectChangeEvent.DATAOBJECT_CHANGED, 192 index)); 193 return oldObject; 194 } 195 196 public Object set(int index, Object element) { 197 return set(index, (DataObject) element); 198 } 199 200 public Object get(int index) { 201 return dataObjects.get(index); 202 } 203 204 public DataObject getDataObject(int index) { 205 return (DataObject) get(index); 206 } 207 208 public Object [] toArray(Object [] array) { 209 return dataObjects.toArray(array); 210 } 211 212 public Object [] toArray() { 213 return dataObjects.toArray(); 214 } 215 216 public void clear() { 217 boolean empty = isEmpty(); 218 dataObjects.clear(); 219 if (!empty && changeDispatcher != null) 220 changeDispatcher.dispatch(new DataObjectChangeEvent( 221 this, 222 DataObjectChangeEvent.DATAOBJECT_REMOVED)); 223 } 224 225 public boolean removeAll(Collection dataObjects) { 226 boolean success = this.dataObjects.removeAll(dataObjects); 227 if (success && changeDispatcher != null) 228 changeDispatcher.dispatch(new DataObjectChangeEvent( 229 this, 230 DataObjectChangeEvent.DATAOBJECT_REMOVED)); 231 return success; 232 } 233 234 public boolean containsAll(Collection dataObjects) { 235 return this.dataObjects.containsAll(dataObjects); 236 } 237 238 public boolean addAll(Collection dataObjects, boolean typeCheck) { 239 if (typeCheck) { 240 for (Iterator i = dataObjects.iterator(); i.hasNext();) { 241 if (!(i.next() instanceof DataObject)) 242 return false; 243 } 244 } 245 boolean success = this.dataObjects.addAll(dataObjects); 246 if (success && changeDispatcher != null) 247 changeDispatcher.dispatch(new DataObjectChangeEvent( 248 this, 249 DataObjectChangeEvent.DATAOBJECT_ADDED)); 250 return success; 251 } 252 253 public boolean addAll(int index, Collection dataObjects, boolean typeCheck) { 254 if (typeCheck) { 255 for (Iterator i = dataObjects.iterator(); i.hasNext();) { 256 if (!(i.next() instanceof DataObject)) 257 return false; 258 } 259 } 260 boolean success = this.dataObjects.addAll(index, dataObjects); 261 if (success && changeDispatcher != null) 262 changeDispatcher.dispatch(new DataObjectChangeEvent( 263 this, 264 DataObjectChangeEvent.DATAOBJECT_ADDED)); 265 return success; 266 } 267 268 public boolean addAll(Collection dataObjects) { 269 return addAll(dataObjects, true); 270 } 271 272 public boolean addAll(int index, Collection c) { 273 return addAll(index, dataObjects, true); 274 } 275 276 public boolean retainAll(Collection dataObjects) { 277 boolean success = this.dataObjects.retainAll(dataObjects); 278 if (success && changeDispatcher != null) 279 changeDispatcher.dispatch(new DataObjectChangeEvent( 280 this, 281 DataObjectChangeEvent.DATAOBJECT_REMOVED)); 282 return success; 283 } 284 285 } | Popular Tags |