ContextHandler.java :  » Content-Management-System » harmonise » org » openharmonise » vfs » context » Java Open Source

Java Open Source » Content Management System » harmonise 
harmonise » org » openharmonise » vfs » context » ContextHandler.java
/*
 * The contents of this file are subject to the 
 * Mozilla Public License Version 1.1 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
 * See the License for the specific language governing rights and 
 * limitations under the License.
 *
 * The Initial Developer of the Original Code is Simulacra Media Ltd.
 * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
 *
 * All Rights Reserved.
 *
 * Contributor(s):
 */
package org.openharmonise.vfs.context;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 * The context handler acts as the hub for context events. They are fired
 * at the handler and the handler then routes them to all the listeners
 * for that specific context type and the ALL context type.
 * 
 * @author Matthew Large
 * @version $Revision: 1.1 $
 *
 */
public class ContextHandler {

  /**
   * Handler instance, following singleton pattern.
   */
  private static ContextHandler m_instance = null;
  
  /**
   * Map of {@link ContextType} to {@link Context} objects.
   */
  private HashMap m_contexts = new HashMap(5);

  /**
   * 
   */
  private ContextHandler() {
    super();
    this.setup();
  }
  
  /**
   * Configures the context handler.
   *
   */
  private void setup() {
    this.m_contexts.put(ContextType.CONTEXT_ALL, new Context(ContextType.CONTEXT_ALL));
    this.m_contexts.put(ContextType.CONTEXT_TABS, new Context(ContextType.CONTEXT_TABS));
    this.m_contexts.put(ContextType.CONTEXT_FILES, new Context(ContextType.CONTEXT_FILES));
    this.m_contexts.put(ContextType.CONTEXT_DIRS, new Context(ContextType.CONTEXT_DIRS));
  }

  /**
   * Returns the instance of the context handler, following the
   * singleton patter.
   * 
   * @return Context handler
   */
  public static ContextHandler getInstance() {
    if(m_instance==null) {
      m_instance = new ContextHandler();
    }
    return m_instance;
  }
  
  /**
   * Adds a context listener to a specified context type.
   * 
   * @param contextType Context type
   * @param listener Context listener
   */
  public synchronized void addListener(ContextType contextType, ContextListener listener) {
    if(!this.m_contexts.keySet().contains(contextType)) {
      this.m_contexts.put(contextType, new Context(contextType));
    }
    ((Context)this.m_contexts.get(contextType)).addListener(listener);
  }
  
  /**
   * Removes a context listener from a specified context type.
   * 
   * @param contextType Context type
   * @param listener Context listener
   */
  public void removeListener(ContextType contextType, ContextListener listener) {
    if(this.m_contexts.keySet().contains(contextType)) {
      ((Context)this.m_contexts.get(contextType)).removeListener(listener);
    }
  }
  
  /**
   * Fires a context event to the relevant context listeners.
   * 
   * @param ce Context event
   */
  public void fireContextEvent(ContextEvent ce) {
    ContextType contextType = ce.CONTEXT_TYPE;
    if(ce!=null && this.m_contexts.keySet().contains(contextType)) {
      ((Context)this.m_contexts.get(contextType)).fireContextEvent(ce);
      ((Context)this.m_contexts.get(contextType)).setLastEvent(ce);
    }
    if(contextType == ContextType.CONTEXT_SHUTDOWN ) {
      System.exit(0);
    }
  }
  
  /**
   * Returns the last recorded context event for a specified context
   * type.
   * 
   * @param contextType Context type
   * @return Context event
   */
  public ContextEvent getLastEvent(ContextType contextType) {
    ContextEvent lastEvent = null;
    if(this.m_contexts.keySet().contains(contextType)) {
      lastEvent = ((Context)this.m_contexts.get(contextType)).getLastEvent();
    }
    return lastEvent;
  }
  
  /**
   * A context is a container for all the information specific to a
   * context type. It holds the listeners and the last context event.
   * 
   * @author Matthew Large
   * @version $Revision: 1.1 $
   *
   */
  private class Context {
    
    /**
     * Context type.
     */
    private ContextType m_contextType = null;
    
    /**
     * List of {@link ContextListener} objects.
     */
    private ArrayList m_aListeners = new ArrayList(5);
    
    /**
     * Last context event sent to this context.
     */
    private ContextEvent m_ce = null;
    
    /**
     * Constructs a new context.
     * 
     * @param contextType Context type
     */
    public Context(ContextType contextType) {
      super();
      this.m_contextType = contextType;
    }
    
    /**
     * Returns the name of context type.
     * 
     * @return Name
     */
    public String getName() {
      return this.m_contextType.toString();
    }
    
    /**
     * Adds a listener to this context.
     * 
     * @param listener Listener to add
     */
    public void addListener(ContextListener listener) {
      this.m_aListeners.add(listener);
    }
    
    /**
     * Removes a listener from this context.
     * 
     * @param listener Listener to remove
     */
    public void removeListener(ContextListener listener) {
      this.m_aListeners.remove(listener);
    }
    
    /**
     * Fires a context event to all the listeners for this context.
     * 
     * @param ce Context event
     */
    public void fireContextEvent(ContextEvent ce) {
      Iterator itor = this.m_aListeners.iterator();
      while(itor.hasNext()) {
        ((ContextListener)itor.next()).contextMessage(ce);
      }
    }
    
    public ContextEvent getLastEvent() {
      return this.m_ce;
    }
    
    public void setLastEvent(ContextEvent ce) {
      this.m_ce = ce;
    }
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.