observablebag.java

来自「本程序是有IBM开发的一个基于数据表格的组件,里面有相关的例子和DOC,本站资料」· Java 代码 · 共 151 行

JAVA
151
字号
package com.ibm.j2x.util.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.collections.bag.HashBag;

import com.ibm.j2x.event.CollectionEvent;
import com.ibm.j2x.event.CollectionListener;

/**
 * The ObservableBag subclasses the HashBag 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 ObservableBag extends HashBag implements ObservableCollection
{
	/** the observers of this collection */
	protected ArrayList listeners = new ArrayList();

	/**
	 * Constructs an empty HashBag.
	 *
	 */
	public ObservableBag()
	{
		super();
	}
	
	/**
	 * Constructs a bag containing all the members of the given collection.
	 * @param c a collection to copy into this bag
	 */
	public ObservableBag(Collection c)
	{
		super(c);
	}

	/**
	 * 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 add(Object o, int count)
	{
		boolean b = super.add(o, count);
		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 remove(Object o, int count)
	{
		boolean b = super.remove(o, count);
		fireCollectionEvent(new CollectionEvent(this, CollectionEvent.REMOVE));	
		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 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 void clear()
	{
		super.clear();
		fireCollectionEvent(new CollectionEvent(this, CollectionEvent.REMOVE));	
	}
	
	/**
	 * 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 + =
减小字号Ctrl + -
显示快捷键?