001: /*******************************************************************************
002: * Copyright (c) 2006 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.ui.internal.navigator;
011:
012: import java.util.HashSet;
013: import java.util.Set;
014:
015: import org.eclipse.core.runtime.Assert;
016: import org.eclipse.core.runtime.ListenerList;
017: import org.eclipse.ui.navigator.IExtensionActivationListener;
018: import org.eclipse.ui.navigator.INavigatorActivationService;
019: import org.eclipse.ui.navigator.INavigatorContentDescriptor;
020: import org.eclipse.ui.navigator.INavigatorContentService;
021: import org.eclipse.ui.navigator.INavigatorViewerDescriptor;
022:
023: /**
024: * Stores information about programmatic bindings and activation settings.
025: *
026: */
027: public class VisibilityAssistant implements
028: IExtensionActivationListener {
029:
030: private final INavigatorViewerDescriptor viewerDescriptor;
031:
032: private final Set programmaticVisibilityBindings = new HashSet();
033:
034: private final Set programmaticRootBindings = new HashSet();
035:
036: private final ListenerList listeners = new ListenerList();
037:
038: private final INavigatorActivationService activationService;
039:
040: /**
041: * Notifies clients of changes in extension visibility or activation.
042: *
043: */
044: public interface VisibilityListener {
045:
046: /**
047: * Respond to the change in visibility or activation.
048: *
049: */
050: void onVisibilityOrActivationChange();
051: }
052:
053: /**
054: * Create a visibility assistant for the given viewer descriptor.
055: *
056: * @param aViewerDescriptor
057: * A non-nullviewer descriptor.
058: * @param anActivationService
059: * The activation service associated with the content service.
060: */
061: public VisibilityAssistant(
062: INavigatorViewerDescriptor aViewerDescriptor,
063: INavigatorActivationService anActivationService) {
064: Assert.isNotNull(aViewerDescriptor);
065: viewerDescriptor = aViewerDescriptor;
066:
067: activationService = anActivationService;
068: activationService.addExtensionActivationListener(this );
069: }
070:
071: /**
072: * Dispose of any resources held onto by this assistant.
073: *
074: */
075: public void dispose() {
076: activationService.removeExtensionActivationListener(this );
077: }
078:
079: /**
080: *
081: * @param theExtensions
082: * The extensions that should be made visible to the viewer.
083: * @param isRoot
084: */
085: public void bindExtensions(String[] theExtensions, boolean isRoot) {
086: if (theExtensions == null) {
087: return;
088: }
089: for (int i = 0; i < theExtensions.length; i++) {
090: programmaticVisibilityBindings.add(theExtensions[i]);
091: if (isRoot) {
092: programmaticRootBindings.add(theExtensions[i]);
093: }
094: }
095: notifyClients();
096: }
097:
098: /**
099: * 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[] clients = listeners.getListeners();
124: for (int i = 0; i < clients.length; i++) {
125: ((VisibilityListener) clients[i])
126: .onVisibilityOrActivationChange();
127: }
128: }
129:
130: /**
131: *
132: * @param aContentDescriptor
133: * The content descriptor of inquiry
134: * @return True if and only if the content descriptor is <i>active</i> and
135: * <i>visible</i> for the viewer descriptor and enabled for the
136: * given element.
137: */
138: public boolean isVisibleAndActive(
139: INavigatorContentDescriptor aContentDescriptor) {
140: return isActive(aContentDescriptor)
141: && isVisible(aContentDescriptor);
142: }
143:
144: /**
145: *
146: * @param aContentDescriptor
147: * The content descriptor of inquiry
148: * @return True if and only if the given extension is <i>active</i>
149: *
150: * @see INavigatorContentService For more information on what <i>active</i>
151: * means.
152: * @see INavigatorActivationService#activateExtensions(String[], boolean)
153: * @see INavigatorActivationService#deactivateExtensions(String[], boolean)
154: */
155: public boolean isActive(
156: INavigatorContentDescriptor aContentDescriptor) {
157: return activationService
158: .isNavigatorExtensionActive(aContentDescriptor.getId());
159: }
160:
161: /**
162: *
163: * @param aContentExtensionId
164: * The unique id of the content extension
165: * @return True if and only if the given extension is <i>active</i>
166: * @see INavigatorContentService For more information on what <i>active</i>
167: * means.
168: * @see INavigatorActivationService#activateExtensions(String[], boolean)
169: * @see INavigatorActivationService#deactivateExtensions(String[], boolean)
170: */
171: public boolean isActive(String aContentExtensionId) {
172: return activationService
173: .isNavigatorExtensionActive(aContentExtensionId);
174: }
175:
176: /**
177: *
178: * @param aContentDescriptor
179: * The content descriptor of inquiry
180: * @return True if and only if the given content extension is declaratively
181: * or programmatically made visible to the viewer.
182: * @see INavigatorContentService#bindExtensions(String[], boolean) For more
183: * information on what <i>visible</i> means.
184: */
185: public boolean isVisible(
186: INavigatorContentDescriptor aContentDescriptor) {
187: return programmaticVisibilityBindings
188: .contains(aContentDescriptor.getId())
189: || viewerDescriptor
190: .isVisibleContentExtension(aContentDescriptor
191: .getId());
192: }
193:
194: /**
195: *
196: * @param aContentExtensionId
197: * The unique id of the content extension
198: * @return True if and only if the given content extension is declaratively
199: * or programmatically made visible to the viewer.
200: * @see INavigatorContentService#bindExtensions(String[], boolean) For more
201: * information on what <i>visible</i> means.
202: */
203: public boolean isVisible(String aContentExtensionId) {
204: return programmaticVisibilityBindings
205: .contains(aContentExtensionId)
206: || viewerDescriptor
207: .isVisibleContentExtension(aContentExtensionId);
208: }
209:
210: /**
211: * Return whether the given content extension is a root extension.
212: *
213: * @param aContentExtensionId
214: * the id of the content extension.
215: * @return whether the given content extension is a root extension
216: */
217: public boolean isRootExtension(String aContentExtensionId) {
218: return programmaticRootBindings.contains(aContentExtensionId)
219: || viewerDescriptor
220: .isRootExtension(aContentExtensionId);
221: }
222:
223: /*
224: * (non-Javadoc)
225: *
226: * @see org.eclipse.ui.navigator.IExtensionActivationListener#onExtensionActivation(java.lang.String,
227: * java.lang.String[], boolean)
228: */
229: public void onExtensionActivation(String aViewerId,
230: String[] theNavigatorExtensionIds, boolean isActive) {
231: if (aViewerId.equals(viewerDescriptor.getViewerId())) {
232: notifyClients();
233: }
234:
235: }
236:
237: }
|