001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.vfs.context;
020:
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.Iterator;
024:
025: /**
026: * The context handler acts as the hub for context events. They are fired
027: * at the handler and the handler then routes them to all the listeners
028: * for that specific context type and the ALL context type.
029: *
030: * @author Matthew Large
031: * @version $Revision: 1.1 $
032: *
033: */
034: public class ContextHandler {
035:
036: /**
037: * Handler instance, following singleton pattern.
038: */
039: private static ContextHandler m_instance = null;
040:
041: /**
042: * Map of {@link ContextType} to {@link Context} objects.
043: */
044: private HashMap m_contexts = new HashMap(5);
045:
046: /**
047: *
048: */
049: private ContextHandler() {
050: super ();
051: this .setup();
052: }
053:
054: /**
055: * Configures the context handler.
056: *
057: */
058: private void setup() {
059: this .m_contexts.put(ContextType.CONTEXT_ALL, new Context(
060: ContextType.CONTEXT_ALL));
061: this .m_contexts.put(ContextType.CONTEXT_TABS, new Context(
062: ContextType.CONTEXT_TABS));
063: this .m_contexts.put(ContextType.CONTEXT_FILES, new Context(
064: ContextType.CONTEXT_FILES));
065: this .m_contexts.put(ContextType.CONTEXT_DIRS, new Context(
066: ContextType.CONTEXT_DIRS));
067: }
068:
069: /**
070: * Returns the instance of the context handler, following the
071: * singleton patter.
072: *
073: * @return Context handler
074: */
075: public static ContextHandler getInstance() {
076: if (m_instance == null) {
077: m_instance = new ContextHandler();
078: }
079: return m_instance;
080: }
081:
082: /**
083: * Adds a context listener to a specified context type.
084: *
085: * @param contextType Context type
086: * @param listener Context listener
087: */
088: public synchronized void addListener(ContextType contextType,
089: ContextListener listener) {
090: if (!this .m_contexts.keySet().contains(contextType)) {
091: this .m_contexts.put(contextType, new Context(contextType));
092: }
093: ((Context) this .m_contexts.get(contextType))
094: .addListener(listener);
095: }
096:
097: /**
098: * Removes a context listener from a specified context type.
099: *
100: * @param contextType Context type
101: * @param listener Context listener
102: */
103: public void removeListener(ContextType contextType,
104: ContextListener listener) {
105: if (this .m_contexts.keySet().contains(contextType)) {
106: ((Context) this .m_contexts.get(contextType))
107: .removeListener(listener);
108: }
109: }
110:
111: /**
112: * Fires a context event to the relevant context listeners.
113: *
114: * @param ce Context event
115: */
116: public void fireContextEvent(ContextEvent ce) {
117: ContextType contextType = ce.CONTEXT_TYPE;
118: if (ce != null
119: && this .m_contexts.keySet().contains(contextType)) {
120: ((Context) this .m_contexts.get(contextType))
121: .fireContextEvent(ce);
122: ((Context) this .m_contexts.get(contextType))
123: .setLastEvent(ce);
124: }
125: if (contextType == ContextType.CONTEXT_SHUTDOWN) {
126: System.exit(0);
127: }
128: }
129:
130: /**
131: * Returns the last recorded context event for a specified context
132: * type.
133: *
134: * @param contextType Context type
135: * @return Context event
136: */
137: public ContextEvent getLastEvent(ContextType contextType) {
138: ContextEvent lastEvent = null;
139: if (this .m_contexts.keySet().contains(contextType)) {
140: lastEvent = ((Context) this .m_contexts.get(contextType))
141: .getLastEvent();
142: }
143: return lastEvent;
144: }
145:
146: /**
147: * A context is a container for all the information specific to a
148: * context type. It holds the listeners and the last context event.
149: *
150: * @author Matthew Large
151: * @version $Revision: 1.1 $
152: *
153: */
154: private class Context {
155:
156: /**
157: * Context type.
158: */
159: private ContextType m_contextType = null;
160:
161: /**
162: * List of {@link ContextListener} objects.
163: */
164: private ArrayList m_aListeners = new ArrayList(5);
165:
166: /**
167: * Last context event sent to this context.
168: */
169: private ContextEvent m_ce = null;
170:
171: /**
172: * Constructs a new context.
173: *
174: * @param contextType Context type
175: */
176: public Context(ContextType contextType) {
177: super ();
178: this .m_contextType = contextType;
179: }
180:
181: /**
182: * Returns the name of context type.
183: *
184: * @return Name
185: */
186: public String getName() {
187: return this .m_contextType.toString();
188: }
189:
190: /**
191: * Adds a listener to this context.
192: *
193: * @param listener Listener to add
194: */
195: public void addListener(ContextListener listener) {
196: this .m_aListeners.add(listener);
197: }
198:
199: /**
200: * Removes a listener from this context.
201: *
202: * @param listener Listener to remove
203: */
204: public void removeListener(ContextListener listener) {
205: this .m_aListeners.remove(listener);
206: }
207:
208: /**
209: * Fires a context event to all the listeners for this context.
210: *
211: * @param ce Context event
212: */
213: public void fireContextEvent(ContextEvent ce) {
214: Iterator itor = this .m_aListeners.iterator();
215: while (itor.hasNext()) {
216: ((ContextListener) itor.next()).contextMessage(ce);
217: }
218: }
219:
220: public ContextEvent getLastEvent() {
221: return this .m_ce;
222: }
223:
224: public void setLastEvent(ContextEvent ce) {
225: this.m_ce = ce;
226: }
227: }
228:
229: }
|