KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > extensions > NavigatorContentExtension


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.extensions;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.LinkedHashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.ISafeRunnable;
20 import org.eclipse.core.runtime.SafeRunner;
21 import org.eclipse.jface.viewers.ILabelProvider;
22 import org.eclipse.jface.viewers.ILabelProviderListener;
23 import org.eclipse.jface.viewers.ITreeContentProvider;
24 import org.eclipse.ui.IMemento;
25 import org.eclipse.ui.internal.navigator.NavigatorContentService;
26 import org.eclipse.ui.internal.navigator.NavigatorPlugin;
27 import org.eclipse.ui.navigator.ICommonContentProvider;
28 import org.eclipse.ui.navigator.ICommonLabelProvider;
29 import org.eclipse.ui.navigator.IExtensionStateModel;
30 import org.eclipse.ui.navigator.IMementoAware;
31 import org.eclipse.ui.navigator.INavigatorContentDescriptor;
32 import org.eclipse.ui.navigator.INavigatorContentExtension;
33
34 /**
35  * <p>
36  * <strong>EXPERIMENTAL</strong>. This class or interface has been added as
37  * part of a work in progress. There is a guarantee neither that this API will
38  * work nor that it will remain the same. Please do not use this API without
39  * consulting with the Platform/UI team.
40  * </p>
41  *
42  * @since 3.2
43  */

44 public class NavigatorContentExtension implements IMementoAware,
45         INavigatorContentExtension {
46
47     private static final NavigatorContentExtension[] NO_EXTENSIONS = new NavigatorContentExtension[0];
48
49     private NavigatorContentService contentService;
50
51     private NavigatorContentDescriptor descriptor;
52  
53     private ICommonContentProvider contentProvider;
54
55     private ICommonLabelProvider labelProvider;
56
57     private boolean labelProviderInitializationFailed = false;
58
59     private boolean contentProviderInitializationFailed = false;
60
61     private boolean isDisposed = false;
62
63     private IMemento appliedMemento;
64
65     private StructuredViewerManager viewerManager;
66
67     /**
68      * Create an object to manage the instantiated elements from the extension.
69      *
70      * @param aDescriptor
71      * The descriptor that knows how to create elements and knows the
72      * id of the extension
73      * @param aContentService
74      * The content service that will manage this extension
75      * @param aViewerManager
76      * The viewer manager that knows how to initialize the content
77      * provider created by this extension.
78      */

79     public NavigatorContentExtension(NavigatorContentDescriptor aDescriptor,
80             NavigatorContentService aContentService,
81             StructuredViewerManager aViewerManager) {
82         super();
83         Assert.isNotNull(aDescriptor);
84
85         descriptor = aDescriptor;
86         contentService = aContentService;
87         viewerManager = aViewerManager;
88     }
89
90     /*
91      * (non-Javadoc)
92      *
93      * @see org.eclipse.ui.internal.navigator.extensions.INavigatorContentExtension#getId()
94      */

95     public String JavaDoc getId() {
96         return descriptor.getId();
97     }
98
99     /*
100      * (non-Javadoc)
101      *
102      * @see org.eclipse.ui.internal.navigator.extensions.INavigatorContentExtension#getDescriptor()
103      */

104     public INavigatorContentDescriptor getDescriptor() {
105         return descriptor;
106     }
107
108     /*
109      * (non-Javadoc)
110      *
111      * @see org.eclipse.ui.navigator.INavigatorContentExtension#getContentProvider()
112      */

113     public ITreeContentProvider getContentProvider() {
114
115         ITreeContentProvider provider = internalGetContentProvider();
116         if (provider != SkeletonTreeContentProvider.INSTANCE) {
117             return ((SafeDelegateTreeContentProvider) provider)
118                     .getDelegateContentProvider();
119         }
120         return provider;
121     }
122
123     /**
124      *
125      * @return The internal content provider that is wrapped by this extension.
126      */

127     public ITreeContentProvider internalGetContentProvider() {
128         if (contentProvider != null || contentProviderInitializationFailed) {
129             return contentProvider;
130         }
131         synchronized (this) {
132             try {
133                 if (contentProvider == null) {
134                     ITreeContentProvider treeContentProvider = descriptor
135                             .createContentProvider();
136                     if (treeContentProvider != null) {
137                         contentProvider = new SafeDelegateTreeContentProvider(
138                                 treeContentProvider, descriptor, contentService);
139                         contentProvider.init(new CommonContentExtensionSite(
140                                 getId(), contentService, appliedMemento));
141                         viewerManager.initialize(contentProvider);
142                     } else {
143                         contentProvider = SkeletonTreeContentProvider.INSTANCE;
144                     }
145                 }
146             } catch (CoreException e) {
147                 contentProviderInitializationFailed = true;
148                 e.printStackTrace();
149             } catch (RuntimeException JavaDoc e) {
150                 contentProviderInitializationFailed = true;
151                 e.printStackTrace();
152             }
153             if (contentProviderInitializationFailed) {
154                 contentProvider = SkeletonTreeContentProvider.INSTANCE;
155             }
156         }
157         return contentProvider;
158     }
159
160     /*
161      * (non-Javadoc)
162      *
163      * @see org.eclipse.ui.internal.navigator.extensions.INavigatorContentExtension#getLabelProvider()
164      */

165     public ICommonLabelProvider getLabelProvider() {
166         if (labelProvider != null || labelProviderInitializationFailed) {
167             return labelProvider;
168         }
169         synchronized (this) {
170             try {
171
172                 if (labelProvider == null) {
173                     ILabelProvider tempLabelProvider = descriptor
174                             .createLabelProvider();
175
176                     if (tempLabelProvider instanceof ICommonLabelProvider) {
177                         labelProvider = (ICommonLabelProvider) tempLabelProvider;
178                             labelProvider.init(new CommonContentExtensionSite(
179                                     getId(), contentService, appliedMemento));
180                     } else {
181                         labelProvider = new SafeDelegateCommonLabelProvider(
182                                 tempLabelProvider);
183                     }
184                     
185                     labelProvider.addListener( (ILabelProviderListener)contentService.createCommonLabelProvider() );
186                 }
187             } catch (CoreException e) {
188                 labelProviderInitializationFailed = true;
189                 e.printStackTrace();
190             } catch (RuntimeException JavaDoc e) {
191                 labelProviderInitializationFailed = true;
192                 e.printStackTrace();
193             }
194
195             if (labelProviderInitializationFailed) {
196                 labelProvider = SkeletonLabelProvider.INSTANCE;
197             }
198         }
199         return labelProvider;
200     }
201
202     /**
203      * Dispose of any resources acquired during the lifecycle of the extension.
204      *
205      */

206     public void dispose() {
207         try {
208             synchronized (this) {
209                     
210                 SafeRunner.run(new ISafeRunnable() {
211
212                     public void handleException(Throwable JavaDoc exception) {
213                         String JavaDoc msg = exception.getMessage() != null ? exception.getMessage() : exception.toString() ;
214                         NavigatorPlugin.logError(0, msg, exception);
215                         
216                     }
217
218                     public void run() throws Exception JavaDoc {
219                         if (contentProvider != null) {
220                             contentProvider.dispose();
221                         }
222
223                     }
224                     
225                 });
226                 
227                 SafeRunner.run(new ISafeRunnable() {
228
229                     public void handleException(Throwable JavaDoc exception) {
230                         String JavaDoc msg = exception.getMessage() != null ? exception.getMessage() : exception.toString() ;
231                         NavigatorPlugin.logError(0, msg, exception);
232                         
233                     }
234
235                     public void run() throws Exception JavaDoc {
236                         if (labelProvider != null) {
237                             labelProvider.removeListener((ILabelProviderListener)contentService.createCommonLabelProvider());
238                             labelProvider.dispose();
239                         }
240
241                     }
242                     
243                 });
244     
245             }
246         } finally {
247             isDisposed = true;
248         }
249     }
250
251     /*
252      * (non-Javadoc)
253      *
254      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
255      */

256     /*
257      * (non-Javadoc)
258      *
259      * @see org.eclipse.ui.internal.navigator.extensions.INavigatorContentExtension#getAdapter(java.lang.Class)
260      */

261     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
262         return null;
263     }
264
265     /**
266      * @return Returns the contentProviderInitializationFailed.
267      */

268     public boolean hasContentProviderInitializationFailed() {
269         return contentProviderInitializationFailed;
270     }
271
272     /**
273      * @return Returns the labelProviderInitializationFailed.
274      */

275     public boolean hasLabelProviderInitializationFailed() {
276         return labelProviderInitializationFailed;
277     }
278
279     /**
280      *
281      * @return True if the loading of the content provider has failed.
282      */

283     public boolean hasLoadingFailed() {
284         return contentProviderInitializationFailed;
285     }
286
287     /*
288      * (non-Javadoc)
289      *
290      * @see org.eclipse.ui.internal.navigator.extensions.INavigatorContentExtension#isLoaded()
291      */

292     public boolean isLoaded() {
293         return contentProvider != null;
294     }
295
296     public void restoreState(IMemento aMemento) {
297         synchronized (this) {
298             appliedMemento = aMemento;
299             applyMemento(contentProvider);
300             applyMemento(labelProvider);
301
302         }
303     }
304
305     public void saveState(IMemento aMemento) {
306         synchronized (this) {
307             if(contentProvider != null && contentProvider instanceof IMementoAware)
308                 ((IMementoAware)contentProvider).saveState(aMemento);
309             if(labelProvider != null && labelProvider instanceof IMementoAware)
310                 ((IMementoAware)labelProvider).saveState(aMemento);
311             
312         }
313     }
314
315     private void applyMemento(IMementoAware target) {
316         if (target != null) {
317             target.restoreState(appliedMemento);
318         }
319
320     }
321
322     protected final void complainDisposedIfNecessary() {
323         if (isDisposed) {
324             throw new IllegalStateException JavaDoc("INavigatorContentExtension " //$NON-NLS-1$
325
+ descriptor.getId() + " is disposed!"); //$NON-NLS-1$
326
}
327     }
328
329     /*
330      * (non-Javadoc)
331      *
332      * @see org.eclipse.ui.internal.navigator.extensions.INavigatorContentExtension#getStateModel()
333      */

334     public IExtensionStateModel getStateModel() {
335         return contentService.getExtensionStateService().getExtensionStateModel(getDescriptor());
336     }
337
338     /**
339      * @param anElement
340      * The element for the query.
341      * @return Returns the overridingExtensions.
342      */

343     public NavigatorContentExtension[] getOverridingExtensionsForTriggerPoint(
344             Object JavaDoc anElement) {
345         if (!descriptor.hasOverridingExtensions()) {
346             return NO_EXTENSIONS;
347         }
348
349         NavigatorContentDescriptor overriddingDescriptor;
350         Set JavaDoc overridingExtensions = new LinkedHashSet JavaDoc();
351         for (Iterator JavaDoc contentDescriptorsItr = descriptor
352                 .getOverriddingExtensions().iterator(); contentDescriptorsItr
353                 .hasNext();) {
354             overriddingDescriptor = (NavigatorContentDescriptor) contentDescriptorsItr
355                     .next();
356
357             if (contentService.isActive(overriddingDescriptor.getId())
358                     && contentService.isVisible(overriddingDescriptor.getId())
359                     && overriddingDescriptor.isTriggerPoint(anElement)) {
360                 overridingExtensions.add(contentService
361                         .getExtension(overriddingDescriptor));
362             }
363         }
364         if (overridingExtensions.size() == 0) {
365             return NO_EXTENSIONS;
366         }
367         return (NavigatorContentExtension[]) overridingExtensions
368                 .toArray(new NavigatorContentExtension[overridingExtensions
369                         .size()]);
370     }
371
372     /**
373      *
374      * @param anElement
375      * The element for the query.
376      * @return Returns the overridingExtensions.
377      */

378     public NavigatorContentExtension[] getOverridingExtensionsForPossibleChild(
379             Object JavaDoc anElement) {
380         if (!descriptor.hasOverridingExtensions()) {
381             return NO_EXTENSIONS;
382         }
383
384         NavigatorContentDescriptor overriddingDescriptor;
385         Set JavaDoc overridingExtensions = new LinkedHashSet JavaDoc();
386         for (Iterator JavaDoc contentDescriptorsItr = descriptor
387                 .getOverriddingExtensions().iterator(); contentDescriptorsItr
388                 .hasNext();) {
389             overriddingDescriptor = (NavigatorContentDescriptor) contentDescriptorsItr
390                     .next();
391
392             if (contentService.isActive(overriddingDescriptor.getId())
393                     && contentService.isVisible(overriddingDescriptor.getId())
394                     && overriddingDescriptor.isPossibleChild(anElement)) {
395                 overridingExtensions.add(contentService
396                         .getExtension(overriddingDescriptor));
397             }
398         }
399         if (overridingExtensions.size() == 0) {
400             return NO_EXTENSIONS;
401         }
402         return (NavigatorContentExtension[]) overridingExtensions
403                 .toArray(new NavigatorContentExtension[overridingExtensions
404                         .size()]);
405     }
406
407     /*
408      * (non-Javadoc)
409      *
410      * @see java.lang.Object#toString()
411      */

412     public String JavaDoc toString() {
413         return descriptor.toString() + " Instance"; //$NON-NLS-1$
414
}
415 }
416
Popular Tags