KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > observable > list > WritableList


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.internal.databinding.provisional.observable.list;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jface.internal.databinding.provisional.observable.Diffs;
19
20 /**
21  * Mutable observable list backed by an ArrayList.
22  *
23  * @since 1.0
24  */

25 public class WritableList extends ObservableList {
26
27     /**
28      * Creates an empty writable list containing elements of type Object.
29      */

30     public WritableList() {
31         this(Object JavaDoc.class);
32     }
33
34     /**
35      * Creates an empty writable list containing elements of the given type.
36      *
37      * @param elementType
38      */

39     public WritableList(Object JavaDoc elementType) {
40         super(new ArrayList JavaDoc(), elementType);
41     }
42
43     public Object JavaDoc set(int index, Object JavaDoc element) {
44         getterCalled();
45         Object JavaDoc oldElement = wrappedList.set(index, element);
46         fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
47                 false, oldElement), Diffs.createListDiffEntry(index, true,
48                 element)));
49         return oldElement;
50     }
51
52     public Object JavaDoc remove(int index) {
53         getterCalled();
54         Object JavaDoc oldElement = wrappedList.remove(index);
55         fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
56                 false, oldElement)));
57         return oldElement;
58     }
59
60     public boolean add(Object JavaDoc element) {
61         getterCalled();
62         boolean added = wrappedList.add(element);
63         if (added) {
64             fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
65                     wrappedList.size() - 1, true, element)));
66         }
67         return added;
68     }
69
70     public void add(int index, Object JavaDoc element) {
71         wrappedList.add(index, element);
72         fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
73                 true, element)));
74     }
75
76     public boolean addAll(Collection JavaDoc c) {
77         ListDiffEntry[] entries = new ListDiffEntry[c.size()];
78         int i = 0;
79         int addIndex = c.size();
80         for (Iterator JavaDoc it = c.iterator(); it.hasNext();) {
81             Object JavaDoc element = it.next();
82             entries[i++] = Diffs.createListDiffEntry(addIndex++, true, element);
83         }
84         boolean added = wrappedList.addAll(c);
85         fireListChange(Diffs.createListDiff(entries));
86         return added;
87     }
88
89     public boolean addAll(int index, Collection JavaDoc c) {
90         ListDiffEntry[] entries = new ListDiffEntry[c.size()];
91         int i = 0;
92         int addIndex = index;
93         for (Iterator JavaDoc it = c.iterator(); it.hasNext();) {
94             Object JavaDoc element = it.next();
95             entries[i++] = Diffs.createListDiffEntry(addIndex++, true, element);
96         }
97         boolean added = wrappedList.addAll(index, c);
98         fireListChange(Diffs.createListDiff(entries));
99         return added;
100     }
101
102     public boolean remove(Object JavaDoc o) {
103         int index = wrappedList.indexOf(o);
104         if (index == -1) {
105             return false;
106         }
107         wrappedList.remove(index);
108         fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
109                 false, o)));
110         return true;
111     }
112
113     public boolean removeAll(Collection JavaDoc c) {
114         List JavaDoc entries = new ArrayList JavaDoc();
115         for (Iterator JavaDoc it = c.iterator(); it.hasNext();) {
116             Object JavaDoc element = it.next();
117             int removeIndex = wrappedList.indexOf(element);
118             if (removeIndex != -1) {
119                 wrappedList.remove(removeIndex);
120                 entries.add(Diffs.createListDiffEntry(removeIndex, true,
121                         element));
122             }
123         }
124         fireListChange(Diffs.createListDiff((ListDiffEntry[]) entries
125                 .toArray(new ListDiffEntry[entries.size()])));
126         return entries.size() > 0;
127     }
128
129     public boolean retainAll(Collection JavaDoc c) {
130         List JavaDoc entries = new ArrayList JavaDoc();
131         int removeIndex = 0;
132         for (Iterator JavaDoc it = wrappedList.iterator(); it.hasNext();) {
133             Object JavaDoc element = it.next();
134             if (!c.contains(element)) {
135                 entries.add(Diffs.createListDiffEntry(removeIndex, false,
136                         element));
137                 it.remove();
138             } else {
139                 // only increment if we haven't removed the current element
140
removeIndex++;
141             }
142         }
143         fireListChange(Diffs.createListDiff((ListDiffEntry[]) entries
144                 .toArray(new ListDiffEntry[entries.size()])));
145         return entries.size() > 0;
146     }
147
148     public void clear() {
149         List JavaDoc entries = new ArrayList JavaDoc();
150         for (Iterator JavaDoc it = wrappedList.iterator(); it.hasNext();) {
151             Object JavaDoc element = it.next();
152             // always report 0 as the remove index
153
entries.add(Diffs.createListDiffEntry(0, false, element));
154         }
155         fireListChange(Diffs.createListDiff((ListDiffEntry[]) entries
156                 .toArray(new ListDiffEntry[entries.size()])));
157     }
158
159 }
160
Popular Tags