📄 observablelist.java
字号:
package com.ibm.j2x.util.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import com.ibm.j2x.event.CollectionEvent;
import com.ibm.j2x.event.CollectionListener;
/**
* The ObservableList subclasses the ArrayList and
* overrides all the functions that change the internally
* stored collection. In the overridden function, it simply
* calls the super class's function and fires a
* CollectionEvent to let observers know the collection
* has changed.
* @author MAbernethy
*
*/
public class ObservableList extends ArrayList implements ObservableCollection
{
/** the observers of this collection */
protected ArrayList listeners = new ArrayList();
/**
* Constructs an empty list with the specified initial capacity.
* @param initialCapacity the initial capacity of the list
*/
public ObservableList(int initialCapacity)
{
super(initialCapacity);
}
/**
* Constructs an empty list with an initial capacity of ten.
*
*/
public ObservableList()
{
this(10);
}
/**
* Constructs a list containing the elements of the specified collection,
* in the order they are returned by the collection's iterator.
* @param c the collection whose elements are to be placed into this list.
*/
public ObservableList(Collection c)
{
super(c);
fireCollectionEvent(new CollectionEvent(this, CollectionEvent.ADD));
}
/**
* Calls the super class's corresponding method
* and fires a CollectionEvent.
*/
public boolean add(Object o)
{
boolean b = super.add(o);
fireCollectionEvent(new CollectionEvent(this, CollectionEvent.ADD));
return b;
}
/**
* Calls the super class's corresponding method
* and fires a CollectionEvent.
*/
public boolean remove(Object o)
{
boolean b = super.remove(o);
fireCollectionEvent(new CollectionEvent(this, CollectionEvent.REMOVE));
return b;
}
/**
* Calls the super class's corresponding method
* and fires a CollectionEvent.
*/
public boolean addAll(Collection c)
{
boolean b = super.addAll(c);
fireCollectionEvent(new CollectionEvent(this, CollectionEvent.ADD));
return b;
}
/**
* Calls the super class's corresponding method
* and fires a CollectionEvent.
*/
public boolean addAll(int index, Collection c)
{
boolean b = super.addAll(index, c);
fireCollectionEvent(new CollectionEvent(this, CollectionEvent.ADD));
return b;
}
/**
* Calls the super class's corresponding method
* and fires a CollectionEvent.
*/
public boolean removeAll(Collection c)
{
boolean b = super.removeAll(c);
fireCollectionEvent(new CollectionEvent(this, CollectionEvent.REMOVE));
return b;
}
/**
* Calls the super class's corresponding method
* and fires a CollectionEvent.
*/
public void clear()
{
super.clear();
fireCollectionEvent(new CollectionEvent(this, CollectionEvent.REMOVE));
}
/**
* Calls the super class's corresponding method
* and fires a CollectionEvent.
*/
public Object set(int index, Object element)
{
Object o = super.set(index, element);
fireCollectionEvent(new CollectionEvent(this, CollectionEvent.MODIFY));
return o;
}
/**
* Calls the super class's corresponding method
* and fires a CollectionEvent.
*/
public void add(int index, Object element)
{
super.add(index, element);
fireCollectionEvent(new CollectionEvent(this, CollectionEvent.ADD));
}
/**
* Calls the super class's corresponding method
* and fires a CollectionEvent.
*/
public Object remove(int index)
{
Object o = super.remove(index);
fireCollectionEvent(new CollectionEvent(this, CollectionEvent.REMOVE));
return o;
}
/**
* Calls the super class's corresponding method
* and fires a CollectionEvent.
*/
public boolean retainAll(Collection c)
{
boolean b = super.retainAll(c);
fireCollectionEvent(new CollectionEvent(this, CollectionEvent.REMOVE));
return b;
}
/**
* Adds a listener to this collection.
* @param l the CollectionListener
*/
public void addCollectionListener(CollectionListener l)
{
if ( ! listeners.contains(l))
listeners.add(l);
}
/**
* Removes the listener from this collection.
* @param l the CollectionListener
*/
public void removeCollectionListener(CollectionListener l)
{
listeners.remove(l);
}
/**
* Fires a CollectionEvent to all listeners.
* @param e the CollectionEvent
*/
protected void fireCollectionEvent(CollectionEvent e)
{
for (Iterator i=listeners.iterator(); i.hasNext();)
((CollectionListener)i.next()).collectionChanged(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -