1 19 package org.openharmonise.vfs.context; 20 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 25 34 public class ContextHandler { 35 36 39 private static ContextHandler m_instance = null; 40 41 44 private HashMap m_contexts = new HashMap (5); 45 46 49 private ContextHandler() { 50 super(); 51 this.setup(); 52 } 53 54 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 71 public static ContextHandler getInstance() { 72 if(m_instance==null) { 73 m_instance = new ContextHandler(); 74 } 75 return m_instance; 76 } 77 78 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 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 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 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 142 private class Context { 143 144 147 private ContextType m_contextType = null; 148 149 152 private ArrayList m_aListeners = new ArrayList (5); 153 154 157 private ContextEvent m_ce = null; 158 159 164 public Context(ContextType contextType) { 165 super(); 166 this.m_contextType = contextType; 167 } 168 169 174 public String getName() { 175 return this.m_contextType.toString(); 176 } 177 178 183 public void addListener(ContextListener listener) { 184 this.m_aListeners.add(listener); 185 } 186 187 192 public void removeListener(ContextListener listener) { 193 this.m_aListeners.remove(listener); 194 } 195 196 201 public void fireContextEvent(ContextEvent ce) { 202 Iterator 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 |