KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > cheatsheet > simple > SimpleCSCommandManager


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.ui.editor.cheatsheet.simple;
13
14 import java.util.Collections JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.runtime.ListenerList;
20
21
22 /**
23  * SimpleCSCommandManager
24  *
25  */

26 public class SimpleCSCommandManager {
27
28     private ListenerList fListeners;
29     
30     private static SimpleCSCommandManager fPinstance;
31     
32     private Map JavaDoc fCommandMap;
33     
34     private boolean fBlockEvents;
35     
36     /**
37      *
38      */

39     private SimpleCSCommandManager() {
40         fCommandMap = Collections.synchronizedMap(new HashMap JavaDoc());
41         fBlockEvents = false;
42         fListeners = null;
43     }
44     
45     /**
46      * @param block
47      */

48     public void setBlockEvents(boolean block) {
49         fBlockEvents = block;
50     }
51     
52     /**
53      * @return
54      */

55     public boolean getBlockEvents() {
56         return fBlockEvents;
57     }
58     
59     /**
60      * @return
61      */

62     public static SimpleCSCommandManager Instance() {
63         if (fPinstance == null) {
64             fPinstance = new SimpleCSCommandManager();
65         }
66         return fPinstance;
67     }
68     
69     /**
70      * @param key
71      * @param value
72      * @return
73      */

74     public synchronized boolean put(String JavaDoc key, String JavaDoc value) {
75         // Do not add the key-value pair if it is already in the map
76
if (fCommandMap.containsKey(key)) {
77             String JavaDoc presentValue = (String JavaDoc)fCommandMap.get(key);
78             if ((presentValue == null) &&
79                     (value == null)) {
80                 // Key-value pair not added
81
return false;
82             } else if ((presentValue != null) &&
83                     (presentValue.equals(value))) {
84                 // Key-value pair not added
85
return false;
86             }
87         }
88         // Insert the key-value pair into the map
89
fCommandMap.put(key, value);
90         // Notify all listeners of the new key-value pair
91
fireNewCommandKeyEvent(key, value);
92         // Key-value pair added
93
return true;
94     }
95     
96     /**
97      * @param key
98      * @return
99      */

100     public String JavaDoc get(String JavaDoc key) {
101         return (String JavaDoc)fCommandMap.get(key);
102     }
103     
104     /**
105      * @param key
106      * @return
107      */

108     public boolean hasKey(String JavaDoc key) {
109         return fCommandMap.containsKey(key);
110     }
111     
112     /**
113      * @return
114      */

115     public Set JavaDoc getKeys() {
116         return fCommandMap.keySet();
117     }
118     
119     /**
120      * @return
121      */

122     public int getSize() {
123         return fCommandMap.size();
124     }
125     
126     /**
127      * @param listener
128      */

129     public void addCommandKeyListener(ISimpleCSCommandKeyListener listener) {
130         if (fListeners == null) {
131             fListeners = new ListenerList();
132         }
133         fListeners.add(listener);
134     }
135
136     /**
137      * @param listener
138      */

139     public void removeCommandKeyListener(ISimpleCSCommandKeyListener listener) {
140         if (fListeners == null) {
141             return;
142         }
143         fListeners.remove(listener);
144     }
145
146     /**
147      * @param key
148      * @param value
149      */

150     private void fireNewCommandKeyEvent(String JavaDoc key, String JavaDoc value) {
151         // Do not fire the event if there are no listeners or we are blocking
152
// events
153
if ((fBlockEvents == true) ||
154                 (fListeners == null) ||
155                 (fListeners.size() == 0)) {
156             return;
157         }
158         // Create the event
159
NewCommandKeyEvent event = new NewCommandKeyEvent(this, key, value);
160         // Notify all listeners
161
Object JavaDoc[] 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