KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > table > model > common > AbstractTableDataModel


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.contrib.table.model.common;
16
17 import java.lang.ref.WeakReference JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
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 /**
27  * An implementation of the listener support in the ITableDataModel interface
28  *
29  * @author mindbridge
30  */

31 public abstract class AbstractTableDataModel implements ITableDataModel
32 {
33     private List JavaDoc m_arrListeners;
34
35     public AbstractTableDataModel()
36     {
37         m_arrListeners = new ArrayList JavaDoc();
38     }
39
40     /**
41      * Method fireTableDataModelEvent.
42      * Fires a change event to all listeners
43      * @param objEvent the event to pass to the listeners
44      */

45     protected void fireTableDataModelEvent(CTableDataModelEvent objEvent)
46     {
47         synchronized (m_arrListeners) {
48             List JavaDoc arrEmptyReferences = null;
49         
50             for (Iterator JavaDoc it = m_arrListeners.iterator(); it.hasNext();)
51             {
52                 WeakReference JavaDoc objRef = (WeakReference JavaDoc) 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 JavaDoc();
60                     arrEmptyReferences.add(objRef);
61                 }
62             }
63
64             if (arrEmptyReferences != null)
65                 m_arrListeners.removeAll(arrEmptyReferences);
66         }
67     }
68
69     /**
70      * @see org.apache.tapestry.contrib.table.model.ITableDataModel#addTableDataModelListener(ITableDataModelListener)
71      */

72     public void addTableDataModelListener(ITableDataModelListener objListener)
73     {
74         synchronized (m_arrListeners) {
75             m_arrListeners.add(new WeakReference JavaDoc(objListener));
76         }
77     }
78
79     /**
80      * @see org.apache.tapestry.contrib.table.model.ITableDataModel#removeTableDataModelListener(ITableDataModelListener)
81      */

82     public void removeTableDataModelListener(ITableDataModelListener objListener)
83     {
84         synchronized (m_arrListeners) {
85             List JavaDoc arrEmptyReferences = null;
86         
87             for (Iterator JavaDoc it = m_arrListeners.iterator(); it.hasNext();)
88             {
89                 WeakReference JavaDoc objRef = (WeakReference JavaDoc) it.next();
90                 ITableDataModelListener objStoredListener =
91                     (ITableDataModelListener) objRef.get();
92                 if (objListener == objStoredListener || objStoredListener == null) {
93                     if (arrEmptyReferences == null)
94                         arrEmptyReferences = new ArrayList JavaDoc();
95                     arrEmptyReferences.add(objRef);
96                 }
97             }
98
99             if (arrEmptyReferences != null)
100                 m_arrListeners.removeAll(arrEmptyReferences);
101         }
102     }
103
104 }
105
Popular Tags