KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 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 package org.eclipse.ui.internal.navigator.extensions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Set JavaDoc;
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 /**
33  * @since 3.2
34  */

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     /**
58      *
59      */

60     public void dispose() {
61         SafeRunner.run(new ISafeRunnable() {
62
63             public void handleException(Throwable JavaDoc exception) {
64                 String JavaDoc msg = exception.getMessage() != null ? exception.getMessage() : exception.toString() ;
65                 NavigatorPlugin.logError(0, msg, exception);
66                 
67             }
68
69             public void run() throws Exception JavaDoc {
70                 contentProvider.dispose();
71             }
72             
73         });
74         
75     }
76
77     /*
78      * (non-Javadoc)
79      *
80      * @see java.lang.Object#equals(java.lang.Object)
81      */

82     public boolean equals(Object JavaDoc anObject) {
83         return contentProvider.equals(anObject);
84     }
85
86     public Object JavaDoc[] getChildren(Object JavaDoc aParentElement) {
87         if (aParentElement instanceof TreePath) {
88             TreePath tp = (TreePath) aParentElement;
89             return getChildren(tp);
90         }
91         Object JavaDoc[] children = contentProvider.getChildren(aParentElement);
92         contentService.rememberContribution(descriptor, children);
93         return children;
94     }
95
96     public Object JavaDoc[] getElements(Object JavaDoc anInputElement) {
97         Object JavaDoc[] elements = contentProvider.getElements(anInputElement);
98         contentService.rememberContribution(descriptor, elements);
99         return elements;
100     }
101
102     public Object JavaDoc getParent(Object JavaDoc anElement) {
103         return contentProvider.getParent(anElement);
104     }
105
106     public boolean hasChildren(Object JavaDoc anElement) {
107         return contentProvider.hasChildren(anElement);
108     }
109
110     /*
111      * (non-Javadoc)
112      *
113      * @see java.lang.Object#hashCode()
114      */

115     public int hashCode() {
116         return contentProvider.hashCode();
117     }
118
119     public void inputChanged(final Viewer aViewer, final Object JavaDoc anOldInput, final Object JavaDoc aNewInput) {
120         viewer = aViewer;
121         
122         SafeRunner.run(new ISafeRunnable() {
123
124             public void handleException(Throwable JavaDoc exception) {
125                 String JavaDoc msg = exception.getMessage() != null ? exception.getMessage() : exception.toString() ;
126                 NavigatorPlugin.logError(0, msg, exception);
127                 
128             }
129
130             public void run() throws Exception JavaDoc {
131                 contentProvider.inputChanged(aViewer, anOldInput, aNewInput);
132             }
133             
134         });
135     }
136
137     /*
138      * (non-Javadoc)
139      *
140      * @see java.lang.Object#toString()
141      */

142     public String JavaDoc toString() {
143         return contentProvider.toString();
144     }
145
146     /**
147      *
148      * @return The real content provider.
149      */

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     /*
175      * (non-Javadoc)
176      *
177      * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedChildren(java.lang.Object,
178      * java.util.Set)
179      */

180     public void getPipelinedChildren(Object JavaDoc aParent, Set JavaDoc theCurrentChildren) {
181         if (contentProvider instanceof IPipelinedTreeContentProvider) {
182             ((IPipelinedTreeContentProvider) contentProvider)
183                     .getPipelinedChildren(aParent, theCurrentChildren);
184         }
185
186     }
187
188     /*
189      * (non-Javadoc)
190      *
191      * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedElements(java.lang.Object,
192      * java.util.Set)
193      */

194     public void getPipelinedElements(Object JavaDoc anInput, Set JavaDoc theCurrentElements) {
195         if (contentProvider instanceof IPipelinedTreeContentProvider) {
196             ((IPipelinedTreeContentProvider) contentProvider)
197                     .getPipelinedElements(anInput, theCurrentElements);
198         }
199     }
200
201     /*
202      * (non-Javadoc)
203      *
204      * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedParent(java.lang.Object,
205      * java.lang.Object)
206      */

207     public Object JavaDoc getPipelinedParent(Object JavaDoc anObject, Object JavaDoc aSuggestedParent) {
208         if (contentProvider instanceof IPipelinedTreeContentProvider) {
209             return ((IPipelinedTreeContentProvider) contentProvider)
210                     .getPipelinedParent(anObject, aSuggestedParent);
211         }
212         return anObject;
213     }
214
215     /*
216      * (non-Javadoc)
217      *
218      * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptAdd(org.eclipse.ui.navigator.PipelinedShapeModification)
219      */

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     /*
230      * (non-Javadoc)
231      *
232      * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptRemove(org.eclipse.ui.navigator.PipelinedShapeModification)
233      */

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     /*
244      * (non-Javadoc)
245      *
246      * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptRefresh(org.eclipse.ui.navigator.PipelinedViewerUpdate)
247      */

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     /*
258      * (non-Javadoc)
259      *
260      * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptUpdate(org.eclipse.ui.navigator.PipelinedViewerUpdate)
261      */

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     /* (non-Javadoc)
272      * @see org.eclipse.jface.viewers.ITreePathContentProvider#getChildren(org.eclipse.jface.viewers.TreePath)
273      */

274     public Object JavaDoc[] getChildren(TreePath parentPath) {
275         if (contentProvider instanceof ITreePathContentProvider) {
276             ITreePathContentProvider tpcp = (ITreePathContentProvider) contentProvider;
277             Object JavaDoc[] children = tpcp.getChildren(parentPath);
278             contentService.rememberContribution(descriptor, children);
279             return children;
280         }
281         return getChildren(parentPath.getLastSegment());
282     }
283
284     /* (non-Javadoc)
285      * @see org.eclipse.jface.viewers.ITreePathContentProvider#hasChildren(org.eclipse.jface.viewers.TreePath)
286      */

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     /* (non-Javadoc)
296      * @see org.eclipse.jface.viewers.ITreePathContentProvider#getParents(java.lang.Object)
297      */

298     public TreePath[] getParents(Object JavaDoc element) {
299         if (contentProvider instanceof ITreePathContentProvider) {
300             ITreePathContentProvider tpcp = (ITreePathContentProvider) contentProvider;
301             return tpcp.getParents(element);
302         }
303         ArrayList JavaDoc segments = new ArrayList JavaDoc();
304         Object JavaDoc 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             // Loop backwards over the array to create the path.
312
return new TreePath[] { new TreePath(segments.toArray()) };
313         }
314         return NO_PATHS;
315     }
316
317 }
318
Popular Tags