001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.cheatsheet.simple;
011:
012: import java.util.Collections;
013: import java.util.HashMap;
014: import java.util.Map;
015: import java.util.Set;
016:
017: import org.eclipse.core.runtime.ListenerList;
018:
019: /**
020: * SimpleCSCommandManager
021: *
022: */
023: public class SimpleCSCommandManager {
024:
025: private ListenerList fListeners;
026:
027: private static SimpleCSCommandManager fPinstance;
028:
029: private Map fCommandMap;
030:
031: private boolean fBlockEvents;
032:
033: /**
034: *
035: */
036: private SimpleCSCommandManager() {
037: fCommandMap = Collections.synchronizedMap(new HashMap());
038: fBlockEvents = false;
039: fListeners = null;
040: }
041:
042: /**
043: * @param block
044: */
045: public void setBlockEvents(boolean block) {
046: fBlockEvents = block;
047: }
048:
049: /**
050: * @return
051: */
052: public boolean getBlockEvents() {
053: return fBlockEvents;
054: }
055:
056: /**
057: * @return
058: */
059: public static SimpleCSCommandManager Instance() {
060: if (fPinstance == null) {
061: fPinstance = new SimpleCSCommandManager();
062: }
063: return fPinstance;
064: }
065:
066: /**
067: * @param key
068: * @param value
069: * @return
070: */
071: public synchronized boolean put(String key, String value) {
072: // Do not add the key-value pair if it is already in the map
073: if (fCommandMap.containsKey(key)) {
074: String presentValue = (String) fCommandMap.get(key);
075: if ((presentValue == null) && (value == null)) {
076: // Key-value pair not added
077: return false;
078: } else if ((presentValue != null)
079: && (presentValue.equals(value))) {
080: // Key-value pair not added
081: return false;
082: }
083: }
084: // Insert the key-value pair into the map
085: fCommandMap.put(key, value);
086: // Notify all listeners of the new key-value pair
087: fireNewCommandKeyEvent(key, value);
088: // Key-value pair added
089: return true;
090: }
091:
092: /**
093: * @param key
094: * @return
095: */
096: public String get(String key) {
097: return (String) fCommandMap.get(key);
098: }
099:
100: /**
101: * @param key
102: * @return
103: */
104: public boolean hasKey(String key) {
105: return fCommandMap.containsKey(key);
106: }
107:
108: /**
109: * @return
110: */
111: public Set getKeys() {
112: return fCommandMap.keySet();
113: }
114:
115: /**
116: * @return
117: */
118: public int getSize() {
119: return fCommandMap.size();
120: }
121:
122: /**
123: * @param listener
124: */
125: public void addCommandKeyListener(
126: ISimpleCSCommandKeyListener listener) {
127: if (fListeners == null) {
128: fListeners = new ListenerList();
129: }
130: fListeners.add(listener);
131: }
132:
133: /**
134: * @param listener
135: */
136: public void removeCommandKeyListener(
137: ISimpleCSCommandKeyListener listener) {
138: if (fListeners == null) {
139: return;
140: }
141: fListeners.remove(listener);
142: }
143:
144: /**
145: * @param key
146: * @param value
147: */
148: private void fireNewCommandKeyEvent(String key, String value) {
149: // Do not fire the event if there are no listeners or we are blocking
150: // events
151: if ((fBlockEvents == true) || (fListeners == null)
152: || (fListeners.size() == 0)) {
153: return;
154: }
155: // Create the event
156: NewCommandKeyEvent event = new NewCommandKeyEvent(this , key,
157: value);
158: // Notify all listeners
159: Object[] listenerList = fListeners.getListeners();
160: for (int i = 0; i < fListeners.size(); i++) {
161: ISimpleCSCommandKeyListener listener = (ISimpleCSCommandKeyListener) listenerList[i];
162: listener.newCommandKey(event);
163: }
164: }
165:
166: }
|