1 11 12 package org.eclipse.pde.internal.ui.editor.cheatsheet.simple; 13 14 import java.util.Collections ; 15 import java.util.HashMap ; 16 import java.util.Map ; 17 import java.util.Set ; 18 19 import org.eclipse.core.runtime.ListenerList; 20 21 22 26 public class SimpleCSCommandManager { 27 28 private ListenerList fListeners; 29 30 private static SimpleCSCommandManager fPinstance; 31 32 private Map fCommandMap; 33 34 private boolean fBlockEvents; 35 36 39 private SimpleCSCommandManager() { 40 fCommandMap = Collections.synchronizedMap(new HashMap ()); 41 fBlockEvents = false; 42 fListeners = null; 43 } 44 45 48 public void setBlockEvents(boolean block) { 49 fBlockEvents = block; 50 } 51 52 55 public boolean getBlockEvents() { 56 return fBlockEvents; 57 } 58 59 62 public static SimpleCSCommandManager Instance() { 63 if (fPinstance == null) { 64 fPinstance = new SimpleCSCommandManager(); 65 } 66 return fPinstance; 67 } 68 69 74 public synchronized boolean put(String key, String value) { 75 if (fCommandMap.containsKey(key)) { 77 String presentValue = (String )fCommandMap.get(key); 78 if ((presentValue == null) && 79 (value == null)) { 80 return false; 82 } else if ((presentValue != null) && 83 (presentValue.equals(value))) { 84 return false; 86 } 87 } 88 fCommandMap.put(key, value); 90 fireNewCommandKeyEvent(key, value); 92 return true; 94 } 95 96 100 public String get(String key) { 101 return (String )fCommandMap.get(key); 102 } 103 104 108 public boolean hasKey(String key) { 109 return fCommandMap.containsKey(key); 110 } 111 112 115 public Set getKeys() { 116 return fCommandMap.keySet(); 117 } 118 119 122 public int getSize() { 123 return fCommandMap.size(); 124 } 125 126 129 public void addCommandKeyListener(ISimpleCSCommandKeyListener listener) { 130 if (fListeners == null) { 131 fListeners = new ListenerList(); 132 } 133 fListeners.add(listener); 134 } 135 136 139 public void removeCommandKeyListener(ISimpleCSCommandKeyListener listener) { 140 if (fListeners == null) { 141 return; 142 } 143 fListeners.remove(listener); 144 } 145 146 150 private void fireNewCommandKeyEvent(String key, String value) { 151 if ((fBlockEvents == true) || 154 (fListeners == null) || 155 (fListeners.size() == 0)) { 156 return; 157 } 158 NewCommandKeyEvent event = new NewCommandKeyEvent(this, key, value); 160 Object [] listenerList = fListeners.getListeners(); 162 for (int i = 0; i < fListeners.size(); i++) { 163 ISimpleCSCommandKeyListener listener = (ISimpleCSCommandKeyListener)listenerList[i]; 164 listener.newCommandKey(event); 165 } 166 } 167 168 } 169 | Popular Tags |