@Override public void addCollectionListener(CollectionListener<T> l) { ls.addCollectionListener(l); }
public void add(int index, T element) { list.add(index, element); collectionSupport.fireAdded(this,Collections.singleton(element), index, index); } public boolean addAll(int index, Collection<? extends T> c) {
public void clear() { if (!collectionSupport.hasListeners()) { collection.clear(); return; } Collection<T> copy = new ArrayList<T>(collection); collection.clear(); collectionSupport.fireRemoved(this, copy); } public boolean contains(Object o) {
void propagateUpdated(K key, V oldValue, V newValue) { if (keySet != null) keySet.collectionSupport.fireUpdated(keySet, Collections.singleton(key)); boolean different = !oldValue.equals(newValue); if (entrySet != null) { Collection<Map.Entry<K, V>> newEntries = Collections.singleton((Map.Entry<K, V>)new UnmodifiableEntry<K,V>(key, newValue)); if (different) { Collection<Map.Entry<K, V>> oldEntries = Collections.singleton((Map.Entry<K, V>)new UnmodifiableEntry<K,V>(key, oldValue)); entrySet.collectionSupport.fireRemoved(entrySet, oldEntries); entrySet.collectionSupport.fireAdded(entrySet, newEntries); } else { entrySet.collectionSupport.fireUpdated(entrySet, newEntries); } } if (values != null) { if (different) { valuesSupport.fireRemoved(values, Collections.singleton(oldValue)); valuesSupport.fireAdded(values, Collections.singleton(newValue)); } else { valuesSupport.fireUpdated(values, Collections.singleton(newValue)); } } }
public boolean addAll(Collection<? extends T> c) { if (!collectionSupport.hasListeners()) return collection.addAll(c); int max = c.size(); Collection<T> addedElements = new ArrayList<T>(max), updatedElements = new ArrayList<T>(max); for (T t : c) { (collection.add(t) ? addedElements : updatedElements).add(t); } int firstIndex = collection instanceof List<?> ? size() : -1, lastIndex = firstIndex < 0 ? -1 : firstIndex + addedElements.size() - 1; collectionSupport.fireAdded(this, addedElements, firstIndex, lastIndex); collectionSupport.fireUpdated(this, updatedElements, firstIndex, lastIndex); return !addedElements.isEmpty(); } public void clear() {
public boolean add(T o) { boolean added = collection.add(o); int index = collection instanceof List<?> ? size() : -1; if (added) { collectionSupport.fireAdded(this,Collections.singleton(o), index, index); } else { collectionSupport.fireUpdated(this,Collections.singleton(o), index, index); } return added; } public boolean addAll(Collection<? extends T> c) {
@Override public void collectionChanged(CollectionEvent<ListenableCollection<T>> e) { switch(e.getType()){ case ADDED: for(ListenableCollection<T> coll:e.getElements()){ delegate.addComponent(coll); coll.addCollectionListener(innerListener); ls.fireAdded(CompoundListenableCollection.this, coll); }//end for(collections added) break; case REMOVED: for(ListenableCollection<T> coll:e.getElements()){ delegate.removeComponent(coll); coll.removeCollectionListener(innerListener); ls.fireRemoved(CompoundListenableCollection.this, coll); }//end for(collections removed) break; case UPDATED: // ???? break; default: break; }//end EventTypes }};
@SuppressWarnings("unchecked") public boolean remove(Object o) { boolean removed = collection.remove(o); if (removed) { collectionSupport.fireRemoved(this,Collections.singleton((T)o)); } return removed; } @SuppressWarnings("unchecked")
@Override public boolean add(V value) { if (backwardAdapter == null) throw new UnsupportedOperationException("No backward adapter in this AdapterCollection"); try { currentlyCausingChange = true; if (collection.add(backwardAdapter.adapt(value))) { if (collectionSupport != null && collectionSupport.hasListeners()) collectionSupport.fireAdded(this, Collections.singleton(value)); return true; } return false; } finally { currentlyCausingChange = false; } }
public void addCollectionListener(CollectionListener<V> l) { if (collectionSupport == null) { collectionSupport = new ListenableSupport<V>(); if (collection instanceof ListenableCollection<?>) { ((ListenableCollection<U>)collection).addCollectionListener(new CollectionListener<U>() { public void collectionChanged(CollectionEvent<U> e) { // Do not propagate the event if we triggered it if (currentlyCausingChange) return; // Only propagate if someone is listening (CollectionSupport already tries not to fire anything when there is no listener, but here we are trying to avoid to create the wrapped elements collection) if (collectionSupport == null || !collectionSupport.hasListeners()) return; // Adapt the collection of changed / added / removed elements in the event collectionSupport.fireEvent( AdaptedCollection.this, new AdaptedCollection<U, V>(e.getElements(), AdaptedCollection.this.forwardAdapter, AdaptedCollection.this.backwardAdapter), e.getType(), e.getFirstIndex(), e.getLastIndex()); } }); } } collectionSupport.addCollectionListener(l); }
public void removeCollectionListener(CollectionListener<V> l) { if (collectionSupport == null) return; collectionSupport.removeCollectionListener(l); } }
public T set(int index, T element) { T value = list.set(index, element); collectionSupport.fireUpdated(this, Collections.singleton(element), index, index); return value; }
public void fireAdded(ListenableCollection<T> source, Collection<T> elements, int firstIndex, int lastIndex) { fireEvent(source, elements, CollectionEvent.EventType.ADDED, firstIndex, lastIndex); }
public void collectionChanged(CollectionEvent<U> e) { // Do not propagate the event if we triggered it if (currentlyCausingChange) return; // Only propagate if someone is listening (CollectionSupport already tries not to fire anything when there is no listener, but here we are trying to avoid to create the wrapped elements collection) if (collectionSupport == null || !collectionSupport.hasListeners()) return; // Adapt the collection of changed / added / removed elements in the event collectionSupport.fireEvent( AdaptedCollection.this, new AdaptedCollection<U, V>(e.getElements(), AdaptedCollection.this.forwardAdapter, AdaptedCollection.this.backwardAdapter), e.getType(), e.getFirstIndex(), e.getLastIndex()); } });
public DefaultListenableCollection(Collection<T> collection) { this(collection,new ListenableSupport<T>()); } public DefaultListenableCollection(Collection<T> collection, ListenableSupport<T> collectionSupport) {
@Override public boolean removeAll(Collection<?> c) { if (!collectionSupport.hasListeners()) return collection.removeAll(c); boolean changed = false; for (Object o : c) changed = remove(o) || changed; return changed; } @Override
@Override public void collectionChanged(CollectionEvent<T> e) { switch(e.getType()){ case ADDED: ls.fireAdded(CompoundListenableCollection.this, e.getElements()); break; case REMOVED: ls.fireRemoved(CompoundListenableCollection.this, e.getElements()); break; case UPDATED: ls.fireUpdated(CompoundListenableCollection.this, e.getElements()); break; default: break; }//end EventTypes }}; private final CollectionListener<ListenableCollection<T>> outerListener = new CollectionListener<ListenableCollection<T>>(){
public T remove(int index) { T removed = list.remove(index); if (removed != null) { collectionSupport.fireRemoved(this,Collections.singleton(removed), index, index); } return removed; } public boolean remove(Object o) {
@Override public void removeCollectionListener(CollectionListener<T> l) { ls.removeCollectionListener(l); }
public void fireRemoved(ListenableCollection<T> source, Collection<T> elements) { fireEvent(source, elements, CollectionEvent.EventType.REMOVED, -1, -1); } public void fireRemoved(ListenableCollection<T> source, Collection<T> elements, int firstIndex, int lastIndex) {