1 15 package org.apache.tapestry.contrib.table.model.common; 16 17 import java.lang.ref.WeakReference ; 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 import org.apache.tapestry.contrib.table.model.CTableDataModelEvent; 23 import org.apache.tapestry.contrib.table.model.ITableDataModel; 24 import org.apache.tapestry.contrib.table.model.ITableDataModelListener; 25 26 31 public abstract class AbstractTableDataModel implements ITableDataModel 32 { 33 private List m_arrListeners; 34 35 public AbstractTableDataModel() 36 { 37 m_arrListeners = new ArrayList (); 38 } 39 40 45 protected void fireTableDataModelEvent(CTableDataModelEvent objEvent) 46 { 47 synchronized (m_arrListeners) { 48 List arrEmptyReferences = null; 49 50 for (Iterator it = m_arrListeners.iterator(); it.hasNext();) 51 { 52 WeakReference objRef = (WeakReference ) it.next(); 53 ITableDataModelListener objListener = 54 (ITableDataModelListener) objRef.get(); 55 if (objListener != null) 56 objListener.tableDataChanged(objEvent); 57 else { 58 if (arrEmptyReferences == null) 59 arrEmptyReferences = new ArrayList (); 60 arrEmptyReferences.add(objRef); 61 } 62 } 63 64 if (arrEmptyReferences != null) 65 m_arrListeners.removeAll(arrEmptyReferences); 66 } 67 } 68 69 72 public void addTableDataModelListener(ITableDataModelListener objListener) 73 { 74 synchronized (m_arrListeners) { 75 m_arrListeners.add(new WeakReference (objListener)); 76 } 77 } 78 79 82 public void removeTableDataModelListener(ITableDataModelListener objListener) 83 { 84 synchronized (m_arrListeners) { 85 List arrEmptyReferences = null; 86 87 for (Iterator it = m_arrListeners.iterator(); it.hasNext();) 88 { 89 WeakReference objRef = (WeakReference ) it.next(); 90 ITableDataModelListener objStoredListener = 91 (ITableDataModelListener) objRef.get(); 92 if (objListener == objStoredListener || objStoredListener == null) { 93 if (arrEmptyReferences == null) 94 arrEmptyReferences = new ArrayList (); 95 arrEmptyReferences.add(objRef); 96 } 97 } 98 99 if (arrEmptyReferences != null) 100 m_arrListeners.removeAll(arrEmptyReferences); 101 } 102 } 103 104 } 105 | Popular Tags |