1 11 package org.eclipse.ui.internal.navigator.extensions; 12 13 import java.util.Iterator ; 14 import java.util.LinkedHashSet ; 15 import java.util.Set ; 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 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 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 95 public String getId() { 96 return descriptor.getId(); 97 } 98 99 104 public INavigatorContentDescriptor getDescriptor() { 105 return descriptor; 106 } 107 108 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 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 e) { 150 contentProviderInitializationFailed = true; 151 e.printStackTrace(); 152 } 153 if (contentProviderInitializationFailed) { 154 contentProvider = SkeletonTreeContentProvider.INSTANCE; 155 } 156 } 157 return contentProvider; 158 } 159 160 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 e) { 191 labelProviderInitializationFailed = true; 192 e.printStackTrace(); 193 } 194 195 if (labelProviderInitializationFailed) { 196 labelProvider = SkeletonLabelProvider.INSTANCE; 197 } 198 } 199 return labelProvider; 200 } 201 202 206 public void dispose() { 207 try { 208 synchronized (this) { 209 210 SafeRunner.run(new ISafeRunnable() { 211 212 public void handleException(Throwable exception) { 213 String msg = exception.getMessage() != null ? exception.getMessage() : exception.toString() ; 214 NavigatorPlugin.logError(0, msg, exception); 215 216 } 217 218 public void run() throws Exception { 219 if (contentProvider != null) { 220 contentProvider.dispose(); 221 } 222 223 } 224 225 }); 226 227 SafeRunner.run(new ISafeRunnable() { 228 229 public void handleException(Throwable exception) { 230 String msg = exception.getMessage() != null ? exception.getMessage() : exception.toString() ; 231 NavigatorPlugin.logError(0, msg, exception); 232 233 } 234 235 public void run() throws Exception { 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 256 261 public Object getAdapter(Class adapter) { 262 return null; 263 } 264 265 268 public boolean hasContentProviderInitializationFailed() { 269 return contentProviderInitializationFailed; 270 } 271 272 275 public boolean hasLabelProviderInitializationFailed() { 276 return labelProviderInitializationFailed; 277 } 278 279 283 public boolean hasLoadingFailed() { 284 return contentProviderInitializationFailed; 285 } 286 287 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 ("INavigatorContentExtension " + descriptor.getId() + " is disposed!"); } 327 } 328 329 334 public IExtensionStateModel getStateModel() { 335 return contentService.getExtensionStateService().getExtensionStateModel(getDescriptor()); 336 } 337 338 343 public NavigatorContentExtension[] getOverridingExtensionsForTriggerPoint( 344 Object anElement) { 345 if (!descriptor.hasOverridingExtensions()) { 346 return NO_EXTENSIONS; 347 } 348 349 NavigatorContentDescriptor overriddingDescriptor; 350 Set overridingExtensions = new LinkedHashSet (); 351 for (Iterator 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 378 public NavigatorContentExtension[] getOverridingExtensionsForPossibleChild( 379 Object anElement) { 380 if (!descriptor.hasOverridingExtensions()) { 381 return NO_EXTENSIONS; 382 } 383 384 NavigatorContentDescriptor overriddingDescriptor; 385 Set overridingExtensions = new LinkedHashSet (); 386 for (Iterator 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 412 public String toString() { 413 return descriptor.toString() + " Instance"; } 415 } 416 | Popular Tags |