1    package com.instantbank.lettertemplate.control.web;
2    
3    import java.io.Serializable;
4    import java.util.HashMap;
5    import java.util.ArrayList;
6    import java.util.Collection;
7    import java.util.Iterator;
8    import com.instantbank.common.utilcomponents.Debug;
9    
10   /**
11    *  This class is responsible for providing methods to add objects as listeners
12    *  for a particular model update event and for notifying the listeners when the
13    *  event actually occurs.
14    *
15    * @author Instant-bank (Consuelo Franky)
16    * @created August 2002
17    */
18   public class ModelUpdateNotifier
19       implements Serializable {
20   
21     private HashMap listenerMap;
22     private Debug debug = null;
23   
24   
25     /**
26      *  constructor
27      */
28     public ModelUpdateNotifier() {
29       debug = new Debug();
30       debug.setDebugginOn(true);
31       debug.setPreMessage("** ModelUpdateNotifier");
32       listenerMap = new HashMap();
33     }
34   
35   
36     /**
37      *  notify to javabeans that are view of Model for updating;
38      *  only javabeans corresponding to processed event are notified.
39      *
40      * @param updatedModelList list of names of Model objects that changed
41      */
42     public void notifyListeners(Collection updatedModelList) {
43       debug.println("notifyListeners");
44       for(Iterator it1 = updatedModelList.iterator(); it1.hasNext(); ) {
45         String modelType = (String)it1.next();
46         debug.println("updating views of modelType " + modelType);
47         Collection listeners = (Collection)listenerMap.get(modelType);
48         if(listeners != null) {
49           for(Iterator it2 = listeners.iterator(); it2.hasNext(); ) {
50             ((ModelUpdateListener)it2.next()).performUpdate();
51           }
52         }
53       }
54     }
55   
56   
57     /**
58      *  registers a javabean that is view of Model corresponding to an event type
59      *
60      * @param modelType event type
61      * @param listener view javabean
62      */
63     public void addListener(String modelType, Object listener) {
64   
65       if(listenerMap.get(modelType) == null) {
66         ArrayList listeners = new ArrayList();
67         listeners.add(listener);
68         listenerMap.put(modelType, listeners);
69       }
70       else {
71         ((ArrayList)listenerMap.get(modelType)).add(listener);
72       }
73     }
74   }
75   
76