KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > app > context > UIContextService


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.app.context;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.faces.context.FacesContext;
23
24 /**
25  * Beans supporting the IContextListener interface are registered against this class. Then Beans
26  * which wish to indicate that the UI should refresh itself i.e. dump all cached data and settings,
27  * call the UIContextService.notifyBeans() to inform all registered instances of the change.
28  *
29  * @author Kevin Roast
30  */

31 public final class UIContextService
32 {
33    /**
34     * Private constructor
35     */

36    private UIContextService()
37    {
38    }
39    
40    /**
41     * Returns a Session local instance of the UIContextService
42     *
43     * @return UIContextService for this Thread
44     */

45    public static UIContextService getInstance(FacesContext fc)
46    {
47       Map JavaDoc session = fc.getExternalContext().getSessionMap();
48       UIContextService service = (UIContextService)session.get(CONTEXT_KEY);
49       if (service == null)
50       {
51          service = new UIContextService();
52          session.put(CONTEXT_KEY, service);
53       }
54       
55       return service;
56    }
57    
58    /**
59     * Register a bean to be informed of context events
60     *
61     * @param bean Conforming to the IContextListener interface
62     */

63    public void registerBean(IContextListener bean)
64    {
65       if (bean == null)
66       {
67          throw new IllegalArgumentException JavaDoc("Bean reference specified cannot be null!");
68       }
69       
70       this.registeredBeans.put(bean.getClass(), bean);
71    }
72    
73    /**
74     * Remove a bean reference from those notified of changes
75     *
76     * @param bean Conforming to the IContextListener interface
77     */

78    public void unregisterBean(IContextListener bean)
79    {
80       if (bean == null)
81       {
82          throw new IllegalArgumentException JavaDoc("Bean reference specified cannot be null!");
83       }
84       
85       this.registeredBeans.remove(bean);
86    }
87    
88    /**
89     * Call to notify all register beans that the UI context has changed and they should
90     * refresh themselves as appropriate.
91     */

92    public void notifyBeans()
93    {
94       for (IContextListener listener: this.registeredBeans.values())
95       {
96          listener.contextUpdated();
97       }
98    }
99    
100    
101    /** key for the UI context service in the session */
102    private final static String JavaDoc CONTEXT_KEY = "__uiContextService";
103    
104    /** Map of bean registered against the context service */
105    private Map JavaDoc<Class JavaDoc, IContextListener> registeredBeans = new HashMap JavaDoc<Class JavaDoc, IContextListener>(7, 1.0f);
106 }
107
Popular Tags