1 11 package org.eclipse.jface.internal.databinding.provisional.observable.list; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.jface.internal.databinding.provisional.observable.Diffs; 19 20 25 public class WritableList extends ObservableList { 26 27 30 public WritableList() { 31 this(Object .class); 32 } 33 34 39 public WritableList(Object elementType) { 40 super(new ArrayList (), elementType); 41 } 42 43 public Object set(int index, Object element) { 44 getterCalled(); 45 Object 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 remove(int index) { 53 getterCalled(); 54 Object oldElement = wrappedList.remove(index); 55 fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index, 56 false, oldElement))); 57 return oldElement; 58 } 59 60 public boolean add(Object 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 element) { 71 wrappedList.add(index, element); 72 fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index, 73 true, element))); 74 } 75 76 public boolean addAll(Collection c) { 77 ListDiffEntry[] entries = new ListDiffEntry[c.size()]; 78 int i = 0; 79 int addIndex = c.size(); 80 for (Iterator it = c.iterator(); it.hasNext();) { 81 Object 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 c) { 90 ListDiffEntry[] entries = new ListDiffEntry[c.size()]; 91 int i = 0; 92 int addIndex = index; 93 for (Iterator it = c.iterator(); it.hasNext();) { 94 Object 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 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 c) { 114 List entries = new ArrayList (); 115 for (Iterator it = c.iterator(); it.hasNext();) { 116 Object 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 c) { 130 List entries = new ArrayList (); 131 int removeIndex = 0; 132 for (Iterator it = wrappedList.iterator(); it.hasNext();) { 133 Object element = it.next(); 134 if (!c.contains(element)) { 135 entries.add(Diffs.createListDiffEntry(removeIndex, false, 136 element)); 137 it.remove(); 138 } else { 139 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 entries = new ArrayList (); 150 for (Iterator it = wrappedList.iterator(); it.hasNext();) { 151 Object element = it.next(); 152 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 |