KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > vfs > context > ContextHandler


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (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 http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.vfs.context;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 /**
26  * The context handler acts as the hub for context events. They are fired
27  * at the handler and the handler then routes them to all the listeners
28  * for that specific context type and the ALL context type.
29  *
30  * @author Matthew Large
31  * @version $Revision: 1.1 $
32  *
33  */

34 public class ContextHandler {
35
36     /**
37      * Handler instance, following singleton pattern.
38      */

39     private static ContextHandler m_instance = null;
40     
41     /**
42      * Map of {@link ContextType} to {@link Context} objects.
43      */

44     private HashMap JavaDoc m_contexts = new HashMap JavaDoc(5);
45
46     /**
47      *
48      */

49     private ContextHandler() {
50         super();
51         this.setup();
52     }
53     
54     /**
55      * Configures the context handler.
56      *
57      */

58     private void setup() {
59         this.m_contexts.put(ContextType.CONTEXT_ALL, new Context(ContextType.CONTEXT_ALL));
60         this.m_contexts.put(ContextType.CONTEXT_TABS, new Context(ContextType.CONTEXT_TABS));
61         this.m_contexts.put(ContextType.CONTEXT_FILES, new Context(ContextType.CONTEXT_FILES));
62         this.m_contexts.put(ContextType.CONTEXT_DIRS, new Context(ContextType.CONTEXT_DIRS));
63     }
64
65     /**
66      * Returns the instance of the context handler, following the
67      * singleton patter.
68      *
69      * @return Context handler
70      */

71     public static ContextHandler getInstance() {
72         if(m_instance==null) {
73             m_instance = new ContextHandler();
74         }
75         return m_instance;
76     }
77     
78     /**
79      * Adds a context listener to a specified context type.
80      *
81      * @param contextType Context type
82      * @param listener Context listener
83      */

84     public synchronized void addListener(ContextType contextType, ContextListener listener) {
85         if(!this.m_contexts.keySet().contains(contextType)) {
86             this.m_contexts.put(contextType, new Context(contextType));
87         }
88         ((Context)this.m_contexts.get(contextType)).addListener(listener);
89     }
90     
91     /**
92      * Removes a context listener from a specified context type.
93      *
94      * @param contextType Context type
95      * @param listener Context listener
96      */

97     public void removeListener(ContextType contextType, ContextListener listener) {
98         if(this.m_contexts.keySet().contains(contextType)) {
99             ((Context)this.m_contexts.get(contextType)).removeListener(listener);
100         }
101     }
102     
103     /**
104      * Fires a context event to the relevant context listeners.
105      *
106      * @param ce Context event
107      */

108     public void fireContextEvent(ContextEvent ce) {
109         ContextType contextType = ce.CONTEXT_TYPE;
110         if(ce!=null && this.m_contexts.keySet().contains(contextType)) {
111             ((Context)this.m_contexts.get(contextType)).fireContextEvent(ce);
112             ((Context)this.m_contexts.get(contextType)).setLastEvent(ce);
113         }
114         if(contextType == ContextType.CONTEXT_SHUTDOWN ) {
115             System.exit(0);
116         }
117     }
118     
119     /**
120      * Returns the last recorded context event for a specified context
121      * type.
122      *
123      * @param contextType Context type
124      * @return Context event
125      */

126     public ContextEvent getLastEvent(ContextType contextType) {
127         ContextEvent lastEvent = null;
128         if(this.m_contexts.keySet().contains(contextType)) {
129             lastEvent = ((Context)this.m_contexts.get(contextType)).getLastEvent();
130         }
131         return lastEvent;
132     }
133     
134     /**
135      * A context is a container for all the information specific to a
136      * context type. It holds the listeners and the last context event.
137      *
138      * @author Matthew Large
139      * @version $Revision: 1.1 $
140      *
141      */

142     private class Context {
143         
144         /**
145          * Context type.
146          */

147         private ContextType m_contextType = null;
148         
149         /**
150          * List of {@link ContextListener} objects.
151          */

152         private ArrayList JavaDoc m_aListeners = new ArrayList JavaDoc(5);
153         
154         /**
155          * Last context event sent to this context.
156          */

157         private ContextEvent m_ce = null;
158         
159         /**
160          * Constructs a new context.
161          *
162          * @param contextType Context type
163          */

164         public Context(ContextType contextType) {
165             super();
166             this.m_contextType = contextType;
167         }
168         
169         /**
170          * Returns the name of context type.
171          *
172          * @return Name
173          */

174         public String JavaDoc getName() {
175             return this.m_contextType.toString();
176         }
177         
178         /**
179          * Adds a listener to this context.
180          *
181          * @param listener Listener to add
182          */

183         public void addListener(ContextListener listener) {
184             this.m_aListeners.add(listener);
185         }
186         
187         /**
188          * Removes a listener from this context.
189          *
190          * @param listener Listener to remove
191          */

192         public void removeListener(ContextListener listener) {
193             this.m_aListeners.remove(listener);
194         }
195         
196         /**
197          * Fires a context event to all the listeners for this context.
198          *
199          * @param ce Context event
200          */

201         public void fireContextEvent(ContextEvent ce) {
202             Iterator JavaDoc itor = this.m_aListeners.iterator();
203             while(itor.hasNext()) {
204                 ((ContextListener)itor.next()).contextMessage(ce);
205             }
206         }
207         
208         public ContextEvent getLastEvent() {
209             return this.m_ce;
210         }
211         
212         public void setLastEvent(ContextEvent ce) {
213             this.m_ce = ce;
214         }
215     }
216
217 }
218
Popular Tags