1 15 package org.eclipse.core.databinding.observable.list; 16 17 import java.util.ArrayList ; 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 import org.eclipse.core.databinding.observable.Diffs; 23 import org.eclipse.core.databinding.observable.Realm; 24 25 36 public class WritableList extends ObservableList { 37 38 43 public WritableList() { 44 this(Realm.getDefault()); 45 } 46 47 52 public WritableList(Realm realm) { 53 this(realm, new ArrayList (), null); 54 } 55 56 63 public WritableList(List toWrap, Object elementType) { 64 this(Realm.getDefault(), toWrap, elementType); 65 } 66 67 77 public WritableList(Realm realm, List toWrap, Object elementType) { 78 super(realm, toWrap, elementType); 79 } 80 81 public Object set(int index, Object element) { 82 checkRealm(); 83 Object oldElement = wrappedList.set(index, element); 84 fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index, 85 false, oldElement), Diffs.createListDiffEntry(index, true, 86 element))); 87 return oldElement; 88 } 89 90 public Object remove(int index) { 91 checkRealm(); 92 Object oldElement = wrappedList.remove(index); 93 fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index, 94 false, oldElement))); 95 return oldElement; 96 } 97 98 public boolean add(Object element) { 99 checkRealm(); 100 boolean added = wrappedList.add(element); 101 if (added) { 102 fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry( 103 wrappedList.size() - 1, true, element))); 104 } 105 return added; 106 } 107 108 public void add(int index, Object element) { 109 checkRealm(); 110 wrappedList.add(index, element); 111 fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index, 112 true, element))); 113 } 114 115 public boolean addAll(Collection c) { 116 checkRealm(); 117 ListDiffEntry[] entries = new ListDiffEntry[c.size()]; 118 int i = 0; 119 int addIndex = wrappedList.size(); 120 for (Iterator it = c.iterator(); it.hasNext();) { 121 Object element = it.next(); 122 entries[i++] = Diffs.createListDiffEntry(addIndex++, true, element); 123 } 124 boolean added = wrappedList.addAll(c); 125 fireListChange(Diffs.createListDiff(entries)); 126 return added; 127 } 128 129 public boolean addAll(int index, Collection c) { 130 checkRealm(); 131 ListDiffEntry[] entries = new ListDiffEntry[c.size()]; 132 int i = 0; 133 int addIndex = index; 134 for (Iterator it = c.iterator(); it.hasNext();) { 135 Object element = it.next(); 136 entries[i++] = Diffs.createListDiffEntry(addIndex++, true, element); 137 } 138 boolean added = wrappedList.addAll(index, c); 139 fireListChange(Diffs.createListDiff(entries)); 140 return added; 141 } 142 143 public boolean remove(Object o) { 144 checkRealm(); 145 int index = wrappedList.indexOf(o); 146 if (index == -1) { 147 return false; 148 } 149 wrappedList.remove(index); 150 fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index, 151 false, o))); 152 return true; 153 } 154 155 public boolean removeAll(Collection c) { 156 checkRealm(); 157 List entries = new ArrayList (); 158 for (Iterator it = c.iterator(); it.hasNext();) { 159 Object element = it.next(); 160 int removeIndex = wrappedList.indexOf(element); 161 if (removeIndex != -1) { 162 wrappedList.remove(removeIndex); 163 entries.add(Diffs.createListDiffEntry(removeIndex, false, 164 element)); 165 } 166 } 167 fireListChange(Diffs.createListDiff((ListDiffEntry[]) entries 168 .toArray(new ListDiffEntry[entries.size()]))); 169 return entries.size() > 0; 170 } 171 172 public boolean retainAll(Collection c) { 173 checkRealm(); 174 List entries = new ArrayList (); 175 int removeIndex = 0; 176 for (Iterator it = wrappedList.iterator(); it.hasNext();) { 177 Object element = it.next(); 178 if (!c.contains(element)) { 179 entries.add(Diffs.createListDiffEntry(removeIndex, false, 180 element)); 181 it.remove(); 182 } else { 183 removeIndex++; 185 } 186 } 187 fireListChange(Diffs.createListDiff((ListDiffEntry[]) entries 188 .toArray(new ListDiffEntry[entries.size()]))); 189 return entries.size() > 0; 190 } 191 192 public void clear() { 193 checkRealm(); 194 List entries = new ArrayList (); 195 for (Iterator it = wrappedList.iterator(); it.hasNext();) { 196 Object element = it.next(); 197 entries.add(Diffs.createListDiffEntry(0, false, element)); 199 it.remove(); 200 } 201 fireListChange(Diffs.createListDiff((ListDiffEntry[]) entries 202 .toArray(new ListDiffEntry[entries.size()]))); 203 } 204 205 210 public static WritableList withElementType(Object elementType) { 211 return new WritableList(Realm.getDefault(), new ArrayList (), 212 elementType); 213 } 214 } 215 | Popular Tags |