KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006 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.ui.internal.navigator;
13
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.ListenerList;
19 import org.eclipse.ui.navigator.IExtensionActivationListener;
20 import org.eclipse.ui.navigator.INavigatorActivationService;
21 import org.eclipse.ui.navigator.INavigatorContentDescriptor;
22 import org.eclipse.ui.navigator.INavigatorContentService;
23 import org.eclipse.ui.navigator.INavigatorViewerDescriptor;
24
25 /**
26  * Stores information about programmatic bindings and activation settings.
27  *
28  */

29 public class VisibilityAssistant implements IExtensionActivationListener {
30
31     private final INavigatorViewerDescriptor viewerDescriptor;
32
33     private final Set JavaDoc programmaticVisibilityBindings = new HashSet JavaDoc();
34
35     private final Set JavaDoc programmaticRootBindings = new HashSet JavaDoc();
36
37     private final ListenerList listeners = new ListenerList();
38
39     private final INavigatorActivationService activationService;
40
41     /**
42      * Notifies clients of changes in extension visibility or activation.
43      *
44      */

45     public interface VisibilityListener {
46
47         /**
48          * Respond to the change in visibility or activation.
49          *
50          */

51         void onVisibilityOrActivationChange();
52     }
53
54     /**
55      * Create a visibility assistant for the given viewer descriptor.
56      *
57      * @param aViewerDescriptor
58      * A non-nullviewer descriptor.
59      * @param anActivationService
60      * The activation service associated with the content service.
61      */

62     public VisibilityAssistant(INavigatorViewerDescriptor aViewerDescriptor,
63             INavigatorActivationService anActivationService) {
64         Assert.isNotNull(aViewerDescriptor);
65         viewerDescriptor = aViewerDescriptor;
66
67         activationService = anActivationService;
68         activationService.addExtensionActivationListener(this);
69     }
70
71     /**
72      * Dispose of any resources held onto by this assistant.
73      *
74      */

75     public void dispose() {
76         activationService.removeExtensionActivationListener(this);
77     }
78
79     /**
80      *
81      * @param theExtensions
82      * The extensions that should be made visible to the viewer.
83      * @param isRoot
84      */

85     public void bindExtensions(String JavaDoc[] theExtensions, boolean isRoot) {
86         if (theExtensions == null) {
87             return;
88         }
89         for (int i = 0; i < theExtensions.length; i++) {
90             programmaticVisibilityBindings.add(theExtensions[i]);
91             if (isRoot) {
92                 programmaticRootBindings.add(theExtensions[i]);
93             }
94         }
95         notifyClients();
96     }
97
98     /**
99      * Add a listener to be notified when the visibility or activation state
100      * associated with this assistant changes.
101      *
102      * @param aListener
103      * a listener to be notified when the visibility or activation
104      * state associated with this assistant changes.
105      */

106     public void addListener(VisibilityListener aListener) {
107         listeners.add(aListener);
108     }
109
110     /**
111      * Remove a listener to be notified when the visibility or activation state
112      * associated with this assistant changes.
113      *
114      * @param aListener
115      * a listener to be notified when the visibility or activation
116      * state associated with this assistant changes.
117      */

118     public void removeListener(VisibilityListener aListener) {
119         listeners.remove(aListener);
120     }
121
122     private void notifyClients() {
123         Object JavaDoc[] clients = listeners.getListeners();
124         for (int i = 0; i < clients.length; i++) {
125             ((VisibilityListener) clients[i]).onVisibilityOrActivationChange();
126         }
127     }
128
129     /**
130      *
131      * @param aContentDescriptor
132      * The content descriptor of inquiry
133      * @return True if and only if the content descriptor is <i>active</i> and
134      * <i>visible</i> for the viewer descriptor and enabled for the
135      * given element.
136      */

137     public boolean isVisibleAndActive(
138             INavigatorContentDescriptor aContentDescriptor) {
139         return isActive(aContentDescriptor) && isVisible(aContentDescriptor);
140     }
141
142     /**
143      *
144      * @param aContentDescriptor
145      * The content descriptor of inquiry
146      * @return True if and only if the given extension is <i>active</i>
147      *
148      * @see INavigatorContentService For more information on what <i>active</i>
149      * means.
150      * @see INavigatorActivationService#activateExtensions(String[], boolean)
151      * @see INavigatorActivationService#deactivateExtensions(String[], boolean)
152      */

153     public boolean isActive(INavigatorContentDescriptor aContentDescriptor) {
154         return activationService.isNavigatorExtensionActive(aContentDescriptor
155                 .getId());
156     }
157
158     /**
159      *
160      * @param aContentExtensionId
161      * The unique id of the content extension
162      * @return True if and only if the given extension is <i>active</i>
163      * @see INavigatorContentService For more information on what <i>active</i>
164      * means.
165      * @see INavigatorActivationService#activateExtensions(String[], boolean)
166      * @see INavigatorActivationService#deactivateExtensions(String[], boolean)
167      */

168     public boolean isActive(String JavaDoc aContentExtensionId) {
169         return activationService
170                 .isNavigatorExtensionActive(aContentExtensionId);
171     }
172
173     /**
174      *
175      * @param aContentDescriptor
176      * The content descriptor of inquiry
177      * @return True if and only if the given content extension is declaratively
178      * or programmatically made visible to the viewer.
179      * @see INavigatorContentService#bindExtensions(String[], boolean) For more
180      * information on what <i>visible</i> means.
181      */

182     public boolean isVisible(INavigatorContentDescriptor aContentDescriptor) {
183         return programmaticVisibilityBindings.contains(aContentDescriptor
184                 .getId())
185                 || viewerDescriptor
186                         .isVisibleContentExtension(aContentDescriptor.getId());
187     }
188
189     /**
190      *
191      * @param aContentExtensionId
192      * The unique id of the content extension
193      * @return True if and only if the given content extension is declaratively
194      * or programmatically made visible to the viewer.
195      * @see INavigatorContentService#bindExtensions(String[], boolean) For more
196      * information on what <i>visible</i> means.
197      */

198     public boolean isVisible(String JavaDoc aContentExtensionId) {
199         return programmaticVisibilityBindings.contains(aContentExtensionId)
200                 || viewerDescriptor
201                         .isVisibleContentExtension(aContentExtensionId);
202     }
203
204     /**
205      * Return whether the given content extension is a root extension.
206      *
207      * @param aContentExtensionId
208      * the id of the content extension.
209      * @return whether the given content extension is a root extension
210      */

211     public boolean isRootExtension(String JavaDoc aContentExtensionId) {
212         return programmaticRootBindings.contains(aContentExtensionId)
213                 || viewerDescriptor.isRootExtension(aContentExtensionId);
214     }
215
216     /*
217      * (non-Javadoc)
218      *
219      * @see org.eclipse.ui.navigator.IExtensionActivationListener#onExtensionActivation(java.lang.String,
220      * java.lang.String[], boolean)
221      */

222     public void onExtensionActivation(String JavaDoc aViewerId,
223             String JavaDoc[] theNavigatorExtensionIds, boolean isActive) {
224         if (aViewerId.equals(viewerDescriptor.getViewerId())) {
225             notifyClients();
226         }
227
228     }
229
230 }
231
Popular Tags