1 19 20 package org.apache.cayenne.dataview; 21 22 import java.util.AbstractList ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.apache.cayenne.DataObject; 29 30 39 public class DataObjectList extends AbstractList { 40 41 protected List dataObjects; 42 protected EventDispatcher changeDispatcher; 43 44 public DataObjectList() { 45 dataObjects = new ArrayList (); 46 } 47 48 public DataObjectList(int capacity) { 49 dataObjects = new ArrayList (capacity); 50 } 51 52 public DataObjectList(Collection dataObjects, boolean typeCheck) { 53 if (typeCheck) { 54 for (Iterator i = dataObjects.iterator(); i.hasNext();) { 55 if (!(i.next() instanceof DataObject)) { 56 this.dataObjects = new ArrayList (1); 57 return; 58 } 59 } 60 } 61 this.dataObjects = new ArrayList (dataObjects); 62 } 63 64 public DataObjectList(Collection dataObjects) { 65 this(dataObjects, true); 66 } 67 68 public void addDataObjectChangeListener(DataObjectChangeListener listener) { 69 changeDispatcher = EventDispatcher.add(changeDispatcher, listener); 70 } 71 72 public void removeDataObjectChangeListener(DataObjectChangeListener listener) { 73 changeDispatcher = EventDispatcher.remove(changeDispatcher, listener); 74 } 75 76 public void clearDataObjectChangeListeners() { 77 if (changeDispatcher != null) { 78 changeDispatcher.clear(); 79 changeDispatcher = null; 80 } 81 } 82 83 public int indexOf(Object o) { 84 if (!(o instanceof DataObject)) 85 return -1; 86 return dataObjects.indexOf(o); 87 } 88 89 public int lastIndexOf(Object o) { 90 if (!(o instanceof DataObject)) 91 return -1; 92 return dataObjects.lastIndexOf(o); 93 } 94 95 public boolean contains(Object o) { 96 return (indexOf(o) >= 0); 97 } 98 99 public boolean isEmpty() { 100 return dataObjects.isEmpty(); 101 } 102 103 public int size() { 104 return dataObjects.size(); 105 } 106 107 public boolean add(DataObject dataObject) { 108 boolean success = dataObjects.add(dataObject); 109 int index = dataObjects.size() - 1; 110 if (success && changeDispatcher != null) 111 changeDispatcher.dispatch(new DataObjectChangeEvent( 112 this, 113 DataObjectChangeEvent.DATAOBJECT_ADDED, 114 index)); 115 return success; 116 } 117 118 public void add(int index, DataObject dataObject) { 119 dataObjects.add(index, dataObject); 120 if (changeDispatcher != null) 121 changeDispatcher.dispatch(new DataObjectChangeEvent( 122 this, 123 DataObjectChangeEvent.DATAOBJECT_ADDED, 124 index)); 125 } 126 127 public boolean add(Object o) { 128 return add((DataObject) o); 129 } 130 131 public void add(int index, Object element) { 132 add(index, (DataObject) element); 133 } 134 135 public Object remove(int index) { 136 Object dataObject = dataObjects.remove(index); 137 if (dataObject != null && changeDispatcher != null) 138 changeDispatcher.dispatch(new DataObjectChangeEvent( 139 this, 140 DataObjectChangeEvent.DATAOBJECT_REMOVED, 141 index)); 142 return dataObject; 143 } 144 145 public boolean remove(Object o) { 146 int index = indexOf(o); 147 return remove(index) != null; 148 } 149 150 public DataObject set(int index, DataObject dataObject) { 151 DataObject oldObject = (DataObject) dataObjects.set(index, dataObject); 152 if (changeDispatcher != null) 153 changeDispatcher.dispatch(new DataObjectChangeEvent( 154 this, 155 DataObjectChangeEvent.DATAOBJECT_CHANGED, 156 index)); 157 return oldObject; 158 } 159 160 public Object set(int index, Object element) { 161 return set(index, (DataObject) element); 162 } 163 164 public Object get(int index) { 165 return dataObjects.get(index); 166 } 167 168 public DataObject getDataObject(int index) { 169 return (DataObject) get(index); 170 } 171 172 public Object [] toArray(Object [] array) { 173 return dataObjects.toArray(array); 174 } 175 176 public Object [] toArray() { 177 return dataObjects.toArray(); 178 } 179 180 public void clear() { 181 boolean empty = isEmpty(); 182 dataObjects.clear(); 183 if (!empty && changeDispatcher != null) 184 changeDispatcher.dispatch(new DataObjectChangeEvent( 185 this, 186 DataObjectChangeEvent.DATAOBJECT_REMOVED)); 187 } 188 189 public boolean removeAll(Collection dataObjects) { 190 boolean success = this.dataObjects.removeAll(dataObjects); 191 if (success && changeDispatcher != null) 192 changeDispatcher.dispatch(new DataObjectChangeEvent( 193 this, 194 DataObjectChangeEvent.DATAOBJECT_REMOVED)); 195 return success; 196 } 197 198 public boolean containsAll(Collection dataObjects) { 199 return this.dataObjects.containsAll(dataObjects); 200 } 201 202 public boolean addAll(Collection dataObjects, boolean typeCheck) { 203 if (typeCheck) { 204 for (Iterator i = dataObjects.iterator(); i.hasNext();) { 205 if (!(i.next() instanceof DataObject)) 206 return false; 207 } 208 } 209 boolean success = this.dataObjects.addAll(dataObjects); 210 if (success && changeDispatcher != null) 211 changeDispatcher.dispatch(new DataObjectChangeEvent( 212 this, 213 DataObjectChangeEvent.DATAOBJECT_ADDED)); 214 return success; 215 } 216 217 public boolean addAll(int index, Collection dataObjects, boolean typeCheck) { 218 if (typeCheck) { 219 for (Iterator i = dataObjects.iterator(); i.hasNext();) { 220 if (!(i.next() instanceof DataObject)) 221 return false; 222 } 223 } 224 boolean success = this.dataObjects.addAll(index, dataObjects); 225 if (success && changeDispatcher != null) 226 changeDispatcher.dispatch(new DataObjectChangeEvent( 227 this, 228 DataObjectChangeEvent.DATAOBJECT_ADDED)); 229 return success; 230 } 231 232 public boolean addAll(Collection dataObjects) { 233 return addAll(dataObjects, true); 234 } 235 236 public boolean addAll(int index, Collection c) { 237 return addAll(index, dataObjects, true); 238 } 239 240 public boolean retainAll(Collection dataObjects) { 241 boolean success = this.dataObjects.retainAll(dataObjects); 242 if (success && changeDispatcher != null) 243 changeDispatcher.dispatch(new DataObjectChangeEvent( 244 this, 245 DataObjectChangeEvent.DATAOBJECT_REMOVED)); 246 return success; 247 } 248 249 } 250 | Popular Tags |