KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > NavigatorActivationService


1 /*******************************************************************************
2  * Copyright (c) 2003, 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 package org.eclipse.ui.internal.navigator;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.eclipse.core.runtime.ListenerList;
23 import org.eclipse.core.runtime.Preferences;
24 import org.eclipse.ui.internal.navigator.extensions.NavigatorContentDescriptor;
25 import org.eclipse.ui.internal.navigator.extensions.NavigatorContentDescriptorManager;
26 import org.eclipse.ui.navigator.IExtensionActivationListener;
27 import org.eclipse.ui.navigator.INavigatorActivationService;
28 import org.eclipse.ui.navigator.INavigatorContentDescriptor;
29 import org.eclipse.ui.navigator.INavigatorContentService;
30
31 /**
32  *
33  * The activation service determines if an extension is <i>active</i> within the
34  * context of a given viewer. If an extension is <i>active</i> then the extension
35  * will contribute functionality to the viewer. If an extension is not <i>active</i>,
36  * then the extension will not be given opportunities to contribute
37  * functionality to the given viewer. See {@link INavigatorContentService} for
38  * more detail on what states are associated with a content extension.
39  *
40  *
41  * @since 3.2
42  */

43 public final class NavigatorActivationService implements
44         INavigatorActivationService {
45
46     private static final String JavaDoc ACTIVATED_EXTENSIONS = ".activatedExtensions"; //$NON-NLS-1$
47

48     private static final NavigatorContentDescriptorManager CONTENT_DESCRIPTOR_REGISTRY = NavigatorContentDescriptorManager
49             .getInstance();
50
51     private static final INavigatorContentDescriptor[] NO_DESCRIPTORS = new INavigatorContentDescriptor[0];
52
53     private static final String JavaDoc DELIM = ";"; //$NON-NLS-1$
54

55     private static final char EQUALS = '=';
56
57     /*
58      * Set of ids of activated extensions.
59      */

60     //private final Set activatedExtensions = new HashSet();
61

62     /*
63      * Set of ids of activated extensions.
64      */

65     private final Map JavaDoc/*<String, Boolean>*/ activatedExtensionsMap = new HashMap JavaDoc();
66
67     /*
68      * IExtensionActivationListeners
69      */

70     private final ListenerList listeners = new ListenerList();
71
72     private INavigatorContentService contentService;
73
74     /**
75      * Create an instance of the service.
76      *
77      * @param aContentService
78      * The associated content service.
79      */

80     public NavigatorActivationService(INavigatorContentService aContentService) {
81         contentService = aContentService;
82         revertExtensionActivations();
83     }
84
85     /**
86      *
87      * Checks the known activation state for the given viewer id to determine if
88      * the given navigator extension is 'active'.
89      *
90      * @param aNavigatorExtensionId
91      * The unique identifier associated with a given extension.
92      *
93      * @return True if the extension is active in the context of the viewer id.
94      */

95     public boolean isNavigatorExtensionActive(String JavaDoc aNavigatorExtensionId) {
96         Boolean JavaDoc b = (Boolean JavaDoc) activatedExtensionsMap.get(aNavigatorExtensionId);
97         if(b != null)
98             return b.booleanValue();
99         synchronized (activatedExtensionsMap) {
100             NavigatorContentDescriptor descriptor = CONTENT_DESCRIPTOR_REGISTRY.getContentDescriptor(aNavigatorExtensionId);
101             if(descriptor.isActiveByDefault())
102                 activatedExtensionsMap.put(aNavigatorExtensionId, Boolean.TRUE);
103             else
104                 activatedExtensionsMap.put(aNavigatorExtensionId, Boolean.FALSE);
105             return descriptor.isActiveByDefault();
106         }
107         //return activatedExtensions.contains(aNavigatorExtensionId);
108
}
109
110     /**
111      * Set the activation state for the given extension in the context of the
112      * given viewer id. Each instance of an INavigatorContentService listens for
113      * the activation service to update; and if those instances were created
114      * with viewers, they will issue a refresh. Otherwise, clients are
115      * responsible for refreshing the viewers.
116      *
117      * <p>
118      * Clients must call {@link #persistExtensionActivations()} to save
119      * the the activation state.
120      * </p>
121      *
122      * <p>
123      * When clients are updating a batch of extensions, consider using
124      * {@link #setActive(String[], boolean)} when
125      * possible to avoid unnecessary notifications.
126      * </p>
127      *
128      * @param aNavigatorExtensionId
129      * The unique identifier associated with a given extension.
130      * @param toEnable
131      * True indicates the extension should be enabled; False
132      * indicates otherwise.
133      *
134      */

135     public void setActive(
136             String JavaDoc aNavigatorExtensionId, boolean toEnable) {
137
138         boolean currentlyActive = isNavigatorExtensionActive(aNavigatorExtensionId);
139         if (currentlyActive == toEnable) {
140             return;
141         }
142
143         if (toEnable) {
144             //activatedExtensions.add(aNavigatorExtensionId);
145
activatedExtensionsMap.put(aNavigatorExtensionId, Boolean.TRUE);
146         } else {
147             //activatedExtensions.remove(aNavigatorExtensionId);
148
activatedExtensionsMap.put(aNavigatorExtensionId, Boolean.FALSE);
149         }
150         notifyListeners(new String JavaDoc[] { aNavigatorExtensionId }, toEnable);
151
152     }
153
154     /**
155      * Set the activation state for the given extension in the context of the
156      * given viewer id. Each instance of an INavigatorContentService listens for
157      * the activation service to update; and if those instances were created
158      * with viewers, they will issue a refresh. Otherwise, clients are
159      * responsible for refreshing the viewers.
160      *
161      * <p>
162      * Clients must call {@link #persistExtensionActivations()} to save
163      * the the activation state.
164      * </p>
165      *
166      * @param aNavigatorExtensionIds
167      * An array of unique identifiers associated with existing
168      * extension.
169      * @param toEnable
170      * True indicates the extension should be enabled; False
171      * indicates otherwise.
172      *
173      */

174     public void setActive(String JavaDoc[] aNavigatorExtensionIds,
175             boolean toEnable) {
176
177         if (toEnable) {
178             for (int i = 0; i < aNavigatorExtensionIds.length; i++) {
179                 //activatedExtensions.add(aNavigatorExtensionIds[i]);
180
activatedExtensionsMap.put(aNavigatorExtensionIds[i], Boolean.TRUE);
181             }
182         } else {
183             for (int i = 0; i < aNavigatorExtensionIds.length; i++) {
184                 //activatedExtensions.remove(aNavigatorExtensionIds[i]);
185
activatedExtensionsMap.put(aNavigatorExtensionIds[i], Boolean.FALSE);
186             }
187         }
188         notifyListeners(aNavigatorExtensionIds, toEnable);
189
190     }
191
192     /**
193      * Save the activation state for the given viewer.
194      *
195      */

196     public void persistExtensionActivations() {
197
198         Preferences preferences = NavigatorPlugin.getDefault()
199                 .getPluginPreferences();
200
201         //synchronized (activatedExtensions) {
202
synchronized (activatedExtensionsMap) {
203             //Iterator activatedExtensionsIterator = activatedExtensions.iterator();
204
Iterator JavaDoc activatedExtensionsIterator = activatedExtensionsMap.keySet().iterator();
205             
206             /* ensure that the preference will be non-empty */
207             StringBuffer JavaDoc preferenceValue = new StringBuffer JavaDoc();
208             String JavaDoc navigatorExtensionId = null;
209             boolean isActive = false;
210             while (activatedExtensionsIterator.hasNext()) {
211                 navigatorExtensionId = (String JavaDoc) activatedExtensionsIterator.next();
212                 isActive = isNavigatorExtensionActive(navigatorExtensionId);
213                 preferenceValue.append(navigatorExtensionId)
214                                     .append(EQUALS)
215                                         .append( isActive ? Boolean.TRUE : Boolean.FALSE )
216                                             .append(DELIM);
217             }
218             preferences.setValue(getPreferenceKey(), preferenceValue.toString());
219         }
220         NavigatorPlugin.getDefault().savePluginPreferences();
221     }
222
223     /**
224      * Request notification when the activation state changes for the given
225      * viewer id.
226      *
227      * @param aListener
228      * An implementation of {@link IExtensionActivationListener}
229      */

230     public void addExtensionActivationListener(
231             IExtensionActivationListener aListener) {
232         listeners.add(aListener);
233     }
234
235     /**
236      * No longer receive notification when activation state changes.
237      *
238      * @param aListener
239      * An implementation of {@link IExtensionActivationListener}
240      */

241     public void removeExtensionActivationListener(
242             IExtensionActivationListener aListener) {
243         listeners.remove(aListener);
244     }
245
246     private void notifyListeners(String JavaDoc[] navigatorExtensionIds,
247             boolean toEnable) {
248         
249         if(navigatorExtensionIds != null) { // should really never be null, but just in case
250
if(navigatorExtensionIds.length > 1)
251                 Arrays.sort(navigatorExtensionIds);
252             
253             Object JavaDoc[] listenerArray = listeners.getListeners();
254             for (int i = 0; i < listenerArray.length; i++) {
255                 ((IExtensionActivationListener) listenerArray[i])
256                         .onExtensionActivation(contentService.getViewerId(),
257                                 navigatorExtensionIds, toEnable);
258             }
259         }
260
261     }
262
263     private void revertExtensionActivations() {
264
265         Preferences preferences = NavigatorPlugin.getDefault()
266                 .getPluginPreferences();
267
268         String JavaDoc activatedExtensionsString = preferences
269                 .getString(getPreferenceKey());
270
271         if (activatedExtensionsString != null
272                 && activatedExtensionsString.length() > 0) {
273             String JavaDoc[] contentExtensionIds = activatedExtensionsString
274                     .split(DELIM);
275             
276             String JavaDoc id = null;
277             String JavaDoc booleanString = null;
278             int indx=0;
279             for (int i = 0; i < contentExtensionIds.length; i++) {
280                 //activatedExtensions.add(contentExtensionIds[i]);
281
if( (indx = contentExtensionIds[i].indexOf(EQUALS)) > -1) {
282                     // up to but not including the equals
283
id = contentExtensionIds[i].substring(0, indx);
284                     booleanString = contentExtensionIds[i].substring(indx+1, contentExtensionIds[i].length());
285                     activatedExtensionsMap.put(id, Boolean.valueOf(booleanString));
286                 } else {
287                     // IS THIS THE RIGHT WAY TO HANDLE THIS CASE?
288
NavigatorContentDescriptor descriptor = CONTENT_DESCRIPTOR_REGISTRY.getContentDescriptor(contentExtensionIds[i]);
289                     if(descriptor != null)
290                         activatedExtensionsMap.put(id, Boolean.valueOf(descriptor.isActiveByDefault()));
291                 }
292             }
293
294         } else {
295             /*
296              * We add the default activation of every known extension, even
297              * though some may not be bound to the associated content service;
298              * this is because they could be bound at a later time through the
299              * programmatic binding mechanism in INavigatorContentService.
300              */

301             INavigatorContentDescriptor[] contentDescriptors = CONTENT_DESCRIPTOR_REGISTRY
302                     .getAllContentDescriptors();
303             for (int i = 0; i < contentDescriptors.length; i++) {
304                 if (contentDescriptors[i].isActiveByDefault()) {
305                     //activatedExtensions.add(contentDescriptors[i].getId());
306
activatedExtensionsMap.put(contentDescriptors[i].getId(), Boolean.TRUE);
307                 }
308             }
309         }
310     }
311
312     private String JavaDoc getPreferenceKey() {
313         return contentService.getViewerId() + ACTIVATED_EXTENSIONS;
314     }
315
316
317     public INavigatorContentDescriptor[] activateExtensions(
318             String JavaDoc[] extensionIds, boolean toDeactivateAllOthers) {
319
320         Set JavaDoc activatedDescriptors = new HashSet JavaDoc();
321         setActive(extensionIds, true);
322         for (int extId = 0; extId < extensionIds.length; extId++) {
323             activatedDescriptors.add(CONTENT_DESCRIPTOR_REGISTRY
324                     .getContentDescriptor(extensionIds[extId]));
325         }
326
327         if (toDeactivateAllOthers) {
328             NavigatorContentDescriptor[] descriptors = CONTENT_DESCRIPTOR_REGISTRY
329                     .getAllContentDescriptors();
330             List JavaDoc descriptorList = new ArrayList JavaDoc(Arrays.asList(descriptors));
331
332             for (int descriptorIndx = 0; descriptorIndx < descriptors.length; descriptorIndx++) {
333                 for (int extId = 0; extId < extensionIds.length; extId++) {
334                     if (descriptors[descriptorIndx].getId().equals(
335                             extensionIds[extId])) {
336                         descriptorList.remove(descriptors[descriptorIndx]);
337                     }
338                 }
339             }
340
341             String JavaDoc[] deactivatedExtensions = new String JavaDoc[descriptorList.size()];
342             for (int i = 0; i < descriptorList.size(); i++) {
343                 INavigatorContentDescriptor descriptor = (INavigatorContentDescriptor) descriptorList
344                         .get(i);
345                 deactivatedExtensions[i] = descriptor.getId();
346             }
347             setActive(deactivatedExtensions, false);
348         }
349
350         if (activatedDescriptors.size() == 0) {
351             return NO_DESCRIPTORS;
352         }
353         return (INavigatorContentDescriptor[]) activatedDescriptors
354                 .toArray(new NavigatorContentDescriptor[activatedDescriptors
355                         .size()]);
356     }
357
358     public INavigatorContentDescriptor[] deactivateExtensions(
359             String JavaDoc[] extensionIds, boolean toEnableAllOthers) {
360
361         Set JavaDoc activatedDescriptors = new HashSet JavaDoc();
362         setActive(extensionIds, false);
363
364         if (toEnableAllOthers) {
365             NavigatorContentDescriptor[] descriptors = CONTENT_DESCRIPTOR_REGISTRY
366                     .getAllContentDescriptors();
367             List JavaDoc descriptorList = new ArrayList JavaDoc(Arrays.asList(descriptors));
368
369             for (int descriptorIndx = 0; descriptorIndx < descriptors.length; descriptorIndx++) {
370                 for (int extId = 0; extId < extensionIds.length; extId++) {
371                     if (descriptors[descriptorIndx].getId().equals(
372                             extensionIds[extId])) {
373                         descriptorList.remove(descriptors[descriptorIndx]);
374                     }
375                 }
376             }
377
378             String JavaDoc[] activatedExtensions = new String JavaDoc[descriptorList.size()];
379             for (int i = 0; i < descriptorList.size(); i++) {
380                 NavigatorContentDescriptor descriptor = (NavigatorContentDescriptor) descriptorList
381                         .get(i);
382                 activatedExtensions[i] = descriptor.getId();
383                 activatedDescriptors.add(descriptor);
384             }
385             setActive(activatedExtensions, true);
386         }
387         if (activatedDescriptors.size() == 0) {
388             return NO_DESCRIPTORS;
389         }
390
391         return (INavigatorContentDescriptor[]) activatedDescriptors
392                 .toArray(new NavigatorContentDescriptor[activatedDescriptors
393                         .size()]);
394     }
395
396
397 }
398
Popular Tags