1 17 18 package swingwtx.swing; 19 20 import java.util.Enumeration ; 21 import java.util.Vector ; 22 23 public class DefaultListModel extends AbstractListModel { 24 private Vector v = new Vector (); 25 public int getSize() { 26 return v.size(); 27 } 28 public Object getElementAt(int index) { 29 return v.elementAt(index); 30 } 31 public void copyInto(Object anArray[]) { 32 v.copyInto(anArray); 33 } 34 public void trimToSize() { 35 v.trimToSize(); 36 } 37 public void ensureCapacity(int minCapacity) { 38 v.ensureCapacity(minCapacity); 39 } 40 public void setSize(int newSize) { 41 int oldSize = v.size(); 42 v.setSize(newSize); 43 if (oldSize > newSize) { 44 fireIntervalRemoved(this, newSize, oldSize-1); 45 } 46 else if (oldSize < newSize) { 47 fireIntervalAdded(this, oldSize, newSize-1); 48 } 49 } 50 public int capacity() { 51 return v.capacity(); 52 } 53 public int size() { 54 return v.size(); 55 } 56 public boolean isEmpty() { 57 return v.isEmpty(); 58 } 59 public Enumeration elements() { 60 return v.elements(); 61 } 62 public boolean contains(Object elem) { 63 return v.contains(elem); 64 } 65 public int indexOf(Object elem) { 66 return v.indexOf(elem); 67 } 68 public int indexOf(Object elem, int index) { 69 return v.indexOf(elem, index); 70 } 71 public int lastIndexOf(Object elem) { 72 return v.lastIndexOf(elem); 73 } 74 public int lastIndexOf(Object elem, int index) { 75 return v.lastIndexOf(elem, index); 76 } 77 public Object elementAt(int index) { 78 return v.elementAt(index); 79 } 80 public Object firstElement() { 81 return v.firstElement(); 82 } 83 public Object lastElement() { 84 return v.lastElement(); 85 } 86 public void setElementAt(Object obj, int index) { 87 v.setElementAt(obj, index); 88 fireContentsChanged(this, index, index); 89 } 90 public void removeElementAt(int index) { 91 v.removeElementAt(index); 92 fireIntervalRemoved(this, index, index); 93 } 94 public void insertElementAt(Object obj, int index) { 95 v.insertElementAt(obj, index); 96 fireIntervalAdded(this, index, index); 97 } 98 public void addElement(Object obj) { 99 int index = v.size(); 100 v.addElement(obj); 101 fireIntervalAdded(this, index, index); 102 } 103 public boolean removeElement(Object obj) { 104 int index = indexOf(obj); 105 boolean rv = v.removeElement(obj); 106 if (index >= 0) { 107 fireIntervalRemoved(this, index, index); 108 } 109 return rv; 110 } 111 public void removeAllElements() { 112 int index1 = v.size()-1; 113 v.removeAllElements(); 114 if (index1 >= 0) { 115 fireIntervalRemoved(this, 0, index1); 116 } 117 } 118 public String toString() { 119 return v.toString(); 120 } 121 public Object [] toArray() { 122 Object [] rv = new Object [v.size()]; 123 v.copyInto(rv); 124 return rv; 125 } 126 public Object get(int index) { 127 return v.elementAt(index); 128 } 129 public Object set(int index, Object element) { 130 Object rv = v.elementAt(index); 131 v.setElementAt(element, index); 132 fireContentsChanged(this, index, index); 133 return rv; 134 } 135 public void add(int index, Object element) { 136 v.insertElementAt(element, index); 137 fireIntervalAdded(this, index, index); 138 } 139 public Object remove(int index) { 140 Object rv = v.elementAt(index); 141 v.removeElementAt(index); 142 fireIntervalRemoved(this, index, index); 143 return rv; 144 } 145 public void clear() { 146 int index1 = v.size()-1; 147 v.removeAllElements(); 148 if (index1 >= 0) { 149 fireIntervalRemoved(this, 0, index1); 150 } 151 } 152 public void removeRange(int fromIndex, int toIndex) { 153 for(int i = toIndex; i >= fromIndex; i--) { 154 v.removeElementAt(i); 155 } 156 fireIntervalRemoved(this, fromIndex, toIndex); 157 } 158 } 159 | Popular Tags |