KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.dataview;
21
22 import java.util.AbstractList JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.cayenne.DataObject;
29
30 /**
31  * A decorator list for a collection of DataObjects that fires
32  * {@link DataObjectChangeEvent}events on modification, and supports registering
33  * {@link DataObjectChangeListener DataObjectChangeListeners}to recieve these events.
34  * Designed to be used as an active model in Swing applications.
35  *
36  * @since 1.1
37  * @author Andriy Shapochka
38  */

39 public class DataObjectList extends AbstractList JavaDoc {
40
41     protected List JavaDoc dataObjects;
42     protected EventDispatcher changeDispatcher;
43
44     public DataObjectList() {
45         dataObjects = new ArrayList JavaDoc();
46     }
47
48     public DataObjectList(int capacity) {
49         dataObjects = new ArrayList JavaDoc(capacity);
50     }
51
52     public DataObjectList(Collection JavaDoc dataObjects, boolean typeCheck) {
53         if (typeCheck) {
54             for (Iterator JavaDoc i = dataObjects.iterator(); i.hasNext();) {
55                 if (!(i.next() instanceof DataObject)) {
56                     this.dataObjects = new ArrayList JavaDoc(1);
57                     return;
58                 }
59             }
60         }
61         this.dataObjects = new ArrayList JavaDoc(dataObjects);
62     }
63
64     public DataObjectList(Collection JavaDoc 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 JavaDoc o) {
84         if (!(o instanceof DataObject))
85             return -1;
86         return dataObjects.indexOf(o);
87     }
88
89     public int lastIndexOf(Object JavaDoc o) {
90         if (!(o instanceof DataObject))
91             return -1;
92         return dataObjects.lastIndexOf(o);
93     }
94
95     public boolean contains(Object JavaDoc 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 JavaDoc o) {
128         return add((DataObject) o);
129     }
130
131     public void add(int index, Object JavaDoc element) {
132         add(index, (DataObject) element);
133     }
134
135     public Object JavaDoc remove(int index) {
136         Object JavaDoc 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 JavaDoc 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 JavaDoc set(int index, Object JavaDoc element) {
161         return set(index, (DataObject) element);
162     }
163
164     public Object JavaDoc 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 JavaDoc[] toArray(Object JavaDoc[] array) {
173         return dataObjects.toArray(array);
174     }
175
176     public Object JavaDoc[] 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 JavaDoc 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 JavaDoc dataObjects) {
199         return this.dataObjects.containsAll(dataObjects);
200     }
201
202     public boolean addAll(Collection JavaDoc dataObjects, boolean typeCheck) {
203         if (typeCheck) {
204             for (Iterator JavaDoc 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 JavaDoc dataObjects, boolean typeCheck) {
218         if (typeCheck) {
219             for (Iterator JavaDoc 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 JavaDoc dataObjects) {
233         return addAll(dataObjects, true);
234     }
235
236     public boolean addAll(int index, Collection JavaDoc c) {
237         return addAll(index, dataObjects, true);
238     }
239
240     public boolean retainAll(Collection JavaDoc 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