KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > dataview > DataObjectList


1 /* ====================================================================
2 *
3 * The ObjectStyle Group Software License, version 1.1
4 * ObjectStyle Group - http://objectstyle.org/
5 *
6 * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7 * of the software. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution, if any,
22 * must include the following acknowlegement:
23 * "This product includes software developed by independent contributors
24 * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25 * Alternately, this acknowlegement may appear in the software itself,
26 * if and wherever such third-party acknowlegements normally appear.
27 *
28 * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29 * or promote products derived from this software without prior written
30 * permission. For written permission, email
31 * "andrus at objectstyle dot org".
32 *
33 * 5. Products derived from this software may not be called "ObjectStyle"
34 * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35 * names without prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals and hosted on ObjectStyle Group web site. For more
53 * information on the ObjectStyle Group, please see
54 * <http://objectstyle.org/>.
55 */

56 package org.objectstyle.cayenne.dataview;
57
58 import java.util.AbstractList JavaDoc;
59 import java.util.ArrayList JavaDoc;
60 import java.util.Collection JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.List JavaDoc;
63
64 import org.objectstyle.cayenne.DataObject;
65
66 /**
67  * A decorator list for a collection of DataObjects that fires
68  * {@link DataObjectChangeEvent}events on modification, and supports registering
69  * {@link DataObjectChangeListener DataObjectChangeListeners}to recieve these events.
70  * Designed to be used as an active model in Swing applications.
71  *
72  * @since 1.1
73  * @author Andriy Shapochka
74  */

75 public class DataObjectList extends AbstractList JavaDoc {
76
77     protected List JavaDoc dataObjects;
78     protected EventDispatcher changeDispatcher;
79
80     public DataObjectList() {
81         dataObjects = new ArrayList JavaDoc();
82     }
83
84     public DataObjectList(int capacity) {
85         dataObjects = new ArrayList JavaDoc(capacity);
86     }
87
88     public DataObjectList(Collection JavaDoc dataObjects, boolean typeCheck) {
89         if (typeCheck) {
90             for (Iterator JavaDoc i = dataObjects.iterator(); i.hasNext();) {
91                 if (!(i.next() instanceof DataObject)) {
92                     this.dataObjects = new ArrayList JavaDoc(1);
93                     return;
94                 }
95             }
96         }
97         this.dataObjects = new ArrayList JavaDoc(dataObjects);
98     }
99
100     public DataObjectList(Collection JavaDoc 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 JavaDoc o) {
120         if (!(o instanceof DataObject))
121             return -1;
122         return dataObjects.indexOf(o);
123     }
124
125     public int lastIndexOf(Object JavaDoc o) {
126         if (!(o instanceof DataObject))
127             return -1;
128         return dataObjects.lastIndexOf(o);
129     }
130
131     public boolean contains(Object JavaDoc 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 JavaDoc o) {
164         return add((DataObject) o);
165     }
166
167     public void add(int index, Object JavaDoc element) {
168         add(index, (DataObject) element);
169     }
170
171     public Object JavaDoc remove(int index) {
172         Object JavaDoc 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 JavaDoc 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 JavaDoc set(int index, Object JavaDoc element) {
197         return set(index, (DataObject) element);
198     }
199
200     public Object JavaDoc 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 JavaDoc[] toArray(Object JavaDoc[] array) {
209         return dataObjects.toArray(array);
210     }
211
212     public Object JavaDoc[] 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 JavaDoc 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 JavaDoc dataObjects) {
235         return this.dataObjects.containsAll(dataObjects);
236     }
237
238     public boolean addAll(Collection JavaDoc dataObjects, boolean typeCheck) {
239         if (typeCheck) {
240             for (Iterator JavaDoc 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 JavaDoc dataObjects, boolean typeCheck) {
254         if (typeCheck) {
255             for (Iterator JavaDoc 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 JavaDoc dataObjects) {
269         return addAll(dataObjects, true);
270     }
271
272     public boolean addAll(int index, Collection JavaDoc c) {
273         return addAll(index, dataObjects, true);
274     }
275
276     public boolean retainAll(Collection JavaDoc 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