View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2005 Christian W. Hargraves (engrean@hotmail.com)
4       
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111AssertLevel.NO_FUNCTION07 USA
18  */
19  package net.sf.jameleon.event;
20  
21  import net.sf.jameleon.data.DataDrivable;
22  
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.Map;
26  import java.util.LinkedList;
27  import java.util.List;
28  
29  public class DataDrivableEventHandler{
30  
31      private static DataDrivableEventHandler eventHandler;
32      private final List dataDrivableListeners = Collections.synchronizedList(new LinkedList());
33  
34      private DataDrivableEventHandler(){}
35  
36      public static DataDrivableEventHandler getInstance(){
37          if (eventHandler == null) {
38              eventHandler = new DataDrivableEventHandler();
39          }
40          return eventHandler;
41      }
42  
43      public void clearInstance(){
44          eventHandler = null;
45      }
46  
47      public void addDataDrivableListener(DataDrivableListener ddl){
48          if (ddl != null && !dataDrivableListeners.contains(ddl)){
49              dataDrivableListeners.add(ddl);
50          }
51      }
52  
53      public List getDataDrivableListeners(){
54          return dataDrivableListeners;
55      }
56  
57      public void removeDataDrivableListener(DataDrivableListener ddl){
58          dataDrivableListeners.remove(ddl);
59      }
60  
61      public void openEvent(DataDrivable dd){
62          DataDrivableEvent dde = new DataDrivableEvent(dd);
63          synchronized(dataDrivableListeners){
64              Iterator it = dataDrivableListeners.iterator();
65              DataDrivableListener ddl;
66              while (it.hasNext()) {
67                  ddl = (DataDrivableListener)it.next();
68                  ddl.openEvent(dde);
69              }
70          }
71      }
72  
73      public void closeEvent(DataDrivable dd){
74          DataDrivableEvent dde = new DataDrivableEvent(dd);
75          synchronized(dataDrivableListeners){
76              Iterator it = dataDrivableListeners.iterator();
77              DataDrivableListener ddl;
78              while (it.hasNext()) {
79                  ddl = (DataDrivableListener)it.next();
80                  ddl.closeEvent(dde);
81              }
82          }
83      }
84  
85      public void executeRowEvent(Map vars, DataDrivable dd, int rowNum){
86          DataDrivableEvent dde = new DataDrivableEvent(dd);
87          dde.setRowData(vars);
88          synchronized(dataDrivableListeners){
89              Iterator it = dataDrivableListeners.iterator();
90              DataDrivableListener ddl;
91              while (it.hasNext()) {
92                  ddl = (DataDrivableListener)it.next();
93                  ddl.executeRowEvent(dde, rowNum);
94              }
95          }
96      }
97  
98  }