1 11 package org.eclipse.ui.internal.navigator.extensions; 12 13 import java.util.ArrayList ; 14 import java.util.Set ; 15 16 import org.eclipse.core.runtime.ISafeRunnable; 17 import org.eclipse.core.runtime.SafeRunner; 18 import org.eclipse.jface.viewers.ITreeContentProvider; 19 import org.eclipse.jface.viewers.ITreePathContentProvider; 20 import org.eclipse.jface.viewers.TreePath; 21 import org.eclipse.jface.viewers.Viewer; 22 import org.eclipse.ui.IMemento; 23 import org.eclipse.ui.internal.navigator.NavigatorContentService; 24 import org.eclipse.ui.internal.navigator.NavigatorPlugin; 25 import org.eclipse.ui.navigator.ICommonContentExtensionSite; 26 import org.eclipse.ui.navigator.ICommonContentProvider; 27 import org.eclipse.ui.navigator.IMementoAware; 28 import org.eclipse.ui.navigator.IPipelinedTreeContentProvider; 29 import org.eclipse.ui.navigator.PipelinedShapeModification; 30 import org.eclipse.ui.navigator.PipelinedViewerUpdate; 31 32 35 public class SafeDelegateTreeContentProvider implements 36 IPipelinedTreeContentProvider, ITreePathContentProvider { 37 38 private static final TreePath[] NO_PATHS = new TreePath[0]; 39 40 private final ITreeContentProvider contentProvider; 41 42 private NavigatorContentService contentService; 43 44 private NavigatorContentDescriptor descriptor; 45 46 private Viewer viewer; 47 48 SafeDelegateTreeContentProvider(ITreeContentProvider aContentProvider, 49 NavigatorContentDescriptor aDescriptor, 50 NavigatorContentService theContentService) { 51 super(); 52 contentProvider = aContentProvider; 53 contentService = theContentService; 54 descriptor = aDescriptor; 55 } 56 57 60 public void dispose() { 61 SafeRunner.run(new ISafeRunnable() { 62 63 public void handleException(Throwable exception) { 64 String msg = exception.getMessage() != null ? exception.getMessage() : exception.toString() ; 65 NavigatorPlugin.logError(0, msg, exception); 66 67 } 68 69 public void run() throws Exception { 70 contentProvider.dispose(); 71 } 72 73 }); 74 75 } 76 77 82 public boolean equals(Object anObject) { 83 return contentProvider.equals(anObject); 84 } 85 86 public Object [] getChildren(Object aParentElement) { 87 if (aParentElement instanceof TreePath) { 88 TreePath tp = (TreePath) aParentElement; 89 return getChildren(tp); 90 } 91 Object [] children = contentProvider.getChildren(aParentElement); 92 contentService.rememberContribution(descriptor, children); 93 return children; 94 } 95 96 public Object [] getElements(Object anInputElement) { 97 Object [] elements = contentProvider.getElements(anInputElement); 98 contentService.rememberContribution(descriptor, elements); 99 return elements; 100 } 101 102 public Object getParent(Object anElement) { 103 return contentProvider.getParent(anElement); 104 } 105 106 public boolean hasChildren(Object anElement) { 107 return contentProvider.hasChildren(anElement); 108 } 109 110 115 public int hashCode() { 116 return contentProvider.hashCode(); 117 } 118 119 public void inputChanged(final Viewer aViewer, final Object anOldInput, final Object aNewInput) { 120 viewer = aViewer; 121 122 SafeRunner.run(new ISafeRunnable() { 123 124 public void handleException(Throwable exception) { 125 String msg = exception.getMessage() != null ? exception.getMessage() : exception.toString() ; 126 NavigatorPlugin.logError(0, msg, exception); 127 128 } 129 130 public void run() throws Exception { 131 contentProvider.inputChanged(aViewer, anOldInput, aNewInput); 132 } 133 134 }); 135 } 136 137 142 public String toString() { 143 return contentProvider.toString(); 144 } 145 146 150 public ITreeContentProvider getDelegateContentProvider() { 151 return contentProvider; 152 } 153 154 public void restoreState(IMemento aMemento) { 155 if (contentProvider != null && contentProvider instanceof IMementoAware) { 156 ((IMementoAware) contentProvider).restoreState(aMemento); 157 } 158 159 } 160 161 public void saveState(IMemento aMemento) { 162 if (contentProvider != null && contentProvider instanceof IMementoAware) { 163 ((IMementoAware) contentProvider).saveState(aMemento); 164 } 165 166 } 167 168 public void init(ICommonContentExtensionSite aConfig) { 169 if (contentProvider instanceof ICommonContentProvider) { 170 ((ICommonContentProvider) contentProvider).init(aConfig); 171 } 172 } 173 174 180 public void getPipelinedChildren(Object aParent, Set theCurrentChildren) { 181 if (contentProvider instanceof IPipelinedTreeContentProvider) { 182 ((IPipelinedTreeContentProvider) contentProvider) 183 .getPipelinedChildren(aParent, theCurrentChildren); 184 } 185 186 } 187 188 194 public void getPipelinedElements(Object anInput, Set theCurrentElements) { 195 if (contentProvider instanceof IPipelinedTreeContentProvider) { 196 ((IPipelinedTreeContentProvider) contentProvider) 197 .getPipelinedElements(anInput, theCurrentElements); 198 } 199 } 200 201 207 public Object getPipelinedParent(Object anObject, Object aSuggestedParent) { 208 if (contentProvider instanceof IPipelinedTreeContentProvider) { 209 return ((IPipelinedTreeContentProvider) contentProvider) 210 .getPipelinedParent(anObject, aSuggestedParent); 211 } 212 return anObject; 213 } 214 215 220 public PipelinedShapeModification interceptAdd( 221 PipelinedShapeModification anAddModification) { 222 if (contentProvider instanceof IPipelinedTreeContentProvider) { 223 return ((IPipelinedTreeContentProvider) contentProvider) 224 .interceptAdd(anAddModification); 225 } 226 return anAddModification; 227 } 228 229 234 public PipelinedShapeModification interceptRemove( 235 PipelinedShapeModification aRemoveModification) { 236 if (contentProvider instanceof IPipelinedTreeContentProvider) { 237 return ((IPipelinedTreeContentProvider) contentProvider) 238 .interceptRemove(aRemoveModification); 239 } 240 return aRemoveModification; 241 } 242 243 248 public boolean interceptRefresh( 249 PipelinedViewerUpdate aRefreshSynchronization) { 250 if (contentProvider instanceof IPipelinedTreeContentProvider) { 251 return ((IPipelinedTreeContentProvider) contentProvider) 252 .interceptRefresh(aRefreshSynchronization); 253 } 254 return false; 255 } 256 257 262 public boolean interceptUpdate( 263 PipelinedViewerUpdate anUpdateSynchronization) { 264 if (contentProvider instanceof IPipelinedTreeContentProvider) { 265 return ((IPipelinedTreeContentProvider) contentProvider) 266 .interceptUpdate(anUpdateSynchronization); 267 } 268 return false; 269 } 270 271 274 public Object [] getChildren(TreePath parentPath) { 275 if (contentProvider instanceof ITreePathContentProvider) { 276 ITreePathContentProvider tpcp = (ITreePathContentProvider) contentProvider; 277 Object [] children = tpcp.getChildren(parentPath); 278 contentService.rememberContribution(descriptor, children); 279 return children; 280 } 281 return getChildren(parentPath.getLastSegment()); 282 } 283 284 287 public boolean hasChildren(TreePath path) { 288 if (contentProvider instanceof ITreePathContentProvider) { 289 ITreePathContentProvider tpcp = (ITreePathContentProvider) contentProvider; 290 return tpcp.hasChildren(path); 291 } 292 return hasChildren(path.getLastSegment()); 293 } 294 295 298 public TreePath[] getParents(Object element) { 299 if (contentProvider instanceof ITreePathContentProvider) { 300 ITreePathContentProvider tpcp = (ITreePathContentProvider) contentProvider; 301 return tpcp.getParents(element); 302 } 303 ArrayList segments = new ArrayList (); 304 Object parent = element; 305 do { 306 parent = contentProvider.getParent(parent); 307 if (parent != null && parent != viewer.getInput()) 308 segments.add(0, parent); 309 } while (parent != null && parent != viewer.getInput()); 310 if (!segments.isEmpty()) { 311 return new TreePath[] { new TreePath(segments.toArray()) }; 313 } 314 return NO_PATHS; 315 } 316 317 } 318 | Popular Tags |