1 19 20 package org.netbeans.modules.tasklist.core.util; 21 22 import java.util.AbstractList ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import javax.swing.event.EventListenerList ; 26 27 32 public class ObjectList<E> extends AbstractList <E> { 33 private EventListenerList listeners = null; 34 35 36 private List <E> objects = null; 37 38 42 public ObjectList() { 43 } 44 45 50 public ObjectList(List <E> objects) { 51 this.objects = objects; 52 } 53 54 public int size() { 55 int sz; 56 if (objects == null) 57 sz = 0; 58 else 59 sz = objects.size(); 60 return sz; 61 } 62 63 public E set(int index, E obj) { 64 if (objects == null) 65 objects = new ArrayList <E>(); 66 67 E old = objects.set(index, obj); 68 if (hasListeners()) { 69 fireEvent(new ObjectListEvent(this, ObjectListEvent.EVENT_REMOVED, 70 new int[] {index}, new Object []{old})); 71 fireEvent(new ObjectListEvent(this, ObjectListEvent.EVENT_ADDED, 72 new int[] {index}, new Object []{obj})); 73 } 74 return old; 75 } 76 77 82 public void addListener(ObjectListListener listener) { 83 if (listeners == null) 84 listeners = new EventListenerList (); 85 listeners.add(ObjectListListener.class, listener); 86 } 87 88 93 public void removeListener(ObjectListListener listener) { 94 if (listeners != null) 95 listeners.remove(ObjectListListener.class, listener); 96 if (listeners.getListenerCount() == 0) 97 this.listeners = null; 98 } 99 100 105 protected boolean hasListeners() { 106 return listeners != null && listeners.getListenerCount() != 0; 107 } 108 109 114 protected void fireEvent(ObjectListEvent e) { 115 Object [] l = listeners.getListenerList(); 117 118 for (int i = l.length - 2; i >= 0; i -= 2) { 121 ((ObjectListListener) l[i+1]).listChanged(e); 123 } 124 } 125 126 public E get(int index) { 127 if (objects == null) 128 throw new IndexOutOfBoundsException ("Empty list"); 130 return objects.get(index); 131 } 132 133 @Override 134 public void add(int index, E element) { 135 if (objects == null) 136 objects = new ArrayList (); 137 138 objects.add(index, element); 139 140 if (hasListeners() || element instanceof ObjectListListener) { 141 ObjectListEvent e = new ObjectListEvent(this, 142 ObjectListEvent.EVENT_ADDED, new int[] {index}, 143 new Object [] {element}); 144 if (hasListeners()) 145 fireEvent(e); 146 if (element instanceof ObjectListListener) 147 ((ObjectListListener) element).listChanged(e); 148 } 149 } 150 151 public E remove(int index) { 152 if (objects == null) 153 throw new IndexOutOfBoundsException ("Empty list"); E obj = objects.remove(index); 155 if (hasListeners() || obj instanceof ObjectListListener) { 156 ObjectListEvent e = new ObjectListEvent(this, 157 ObjectListEvent.EVENT_REMOVED, 158 new int[] {index}, new Object [] {obj}); 159 if (hasListeners()) 160 fireEvent(e); 161 if (obj instanceof ObjectListListener) 162 ((ObjectListListener) obj).listChanged(e); 163 } 164 if (objects.size() == 0) 165 objects = null; 166 return obj; 167 } 168 169 175 public void move(int index, int newIndex) { 176 if (objects == null) 177 throw new IndexOutOfBoundsException ("Empty list"); objects.add(newIndex, objects.remove(index)); 179 if (hasListeners()) { 180 ObjectListEvent e = new ObjectListEvent(this, 181 ObjectListEvent.EVENT_REORDERED, null, null); 182 fireEvent(e); 183 } 184 } 185 } 186 | Popular Tags |