1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2003, 2010 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;
|
14
|
import java.util.Set;
|
15
|
|
16
|
import org.eclipse.core.runtime.SafeRunner;
|
17
|
import org.eclipse.jface.viewers.ITreeContentProvider;
|
18
|
import org.eclipse.jface.viewers.ITreePathContentProvider;
|
19
|
import org.eclipse.jface.viewers.StructuredViewer;
|
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.NavigatorSafeRunnable;
|
24
|
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
|
25
|
import org.eclipse.ui.navigator.ICommonContentProvider;
|
26
|
import org.eclipse.ui.navigator.IMementoAware;
|
27
|
import org.eclipse.ui.navigator.IPipelinedTreeContentProvider;
|
28
|
import org.eclipse.ui.navigator.IPipelinedTreeContentProvider2;
|
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
|
IPipelinedTreeContentProvider2, ITreePathContentProvider {
|
37
|
|
38
|
private static final TreePath[] NO_PATHS = new TreePath[0];
|
39
|
|
40
|
private final ITreeContentProvider contentProvider;
|
41
|
|
42
|
private StructuredViewer viewer;
|
43
|
|
44
|
SafeDelegateTreeContentProvider(ITreeContentProvider aContentProvider) {
|
45
|
super();
|
46
|
contentProvider = aContentProvider;
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* @return true if the underlying content provider implements IPipelinedTreeContentProvider
|
51
|
*/
|
52
|
public boolean isPipelined() {
|
53
|
return contentProvider instanceof IPipelinedTreeContentProvider;
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* @return true if the underlying content provider implements IPipelinedTreeContentProviderHasChildren
|
58
|
*/
|
59
|
public boolean isPipelinedHasChildren() {
|
60
|
return contentProvider instanceof IPipelinedTreeContentProvider2;
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* @return true if the underlying content provider implements ITreePathContentProvider
|
65
|
*/
|
66
|
public boolean isTreePath() {
|
67
|
return contentProvider instanceof ITreePathContentProvider;
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
*
|
72
|
*/
|
73
|
public void dispose() {
|
74
|
SafeRunner.run(new NavigatorSafeRunnable() {
|
75
|
public void run() throws Exception {
|
76
|
contentProvider.dispose();
|
77
|
}
|
78
|
});
|
79
|
|
80
|
}
|
81
|
|
82
|
/*
|
83
|
* (non-Javadoc)
|
84
|
*
|
85
|
* @see java.lang.Object#equals(java.lang.Object)
|
86
|
*/
|
87
|
public boolean equals(Object anObject) {
|
88
|
return contentProvider.equals(anObject);
|
89
|
}
|
90
|
|
91
|
public Object[] getChildren(Object aParentElement) {
|
92
|
if (aParentElement instanceof TreePath) {
|
93
|
TreePath tp = (TreePath) aParentElement;
|
94
|
return getChildren(tp);
|
95
|
}
|
96
|
Object[] children = contentProvider.getChildren(aParentElement);
|
97
|
return children;
|
98
|
}
|
99
|
|
100
|
public Object[] getElements(Object anInputElement) {
|
101
|
Object[] elements = contentProvider.getElements(anInputElement);
|
102
|
return elements;
|
103
|
}
|
104
|
|
105
|
public Object getParent(Object anElement) {
|
106
|
return contentProvider.getParent(anElement);
|
107
|
}
|
108
|
|
109
|
public boolean hasChildren(Object anElement) {
|
110
|
return contentProvider.hasChildren(anElement);
|
111
|
}
|
112
|
|
113
|
/*
|
114
|
* (non-Javadoc)
|
115
|
*
|
116
|
* @see java.lang.Object#hashCode()
|
117
|
*/
|
118
|
public int hashCode() {
|
119
|
return contentProvider.hashCode();
|
120
|
}
|
121
|
|
122
|
public void inputChanged(final Viewer aViewer, final Object anOldInput, final Object aNewInput) {
|
123
|
viewer = (StructuredViewer) aViewer;
|
124
|
|
125
|
SafeRunner.run(new NavigatorSafeRunnable() {
|
126
|
public void run() throws Exception {
|
127
|
contentProvider.inputChanged(aViewer, anOldInput, aNewInput);
|
128
|
}
|
129
|
});
|
130
|
}
|
131
|
|
132
|
/*
|
133
|
* (non-Javadoc)
|
134
|
*
|
135
|
* @see java.lang.Object#toString()
|
136
|
*/
|
137
|
public String toString() {
|
138
|
return contentProvider.toString();
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
*
|
143
|
* @return The real content provider.
|
144
|
*/
|
145
|
public ITreeContentProvider getDelegateContentProvider() {
|
146
|
return contentProvider;
|
147
|
}
|
148
|
|
149
|
public void restoreState(IMemento aMemento) {
|
150
|
if (contentProvider != null && contentProvider instanceof IMementoAware) {
|
151
|
((IMementoAware) contentProvider).restoreState(aMemento);
|
152
|
}
|
153
|
|
154
|
}
|
155
|
|
156
|
public void saveState(IMemento aMemento) {
|
157
|
if (contentProvider != null && contentProvider instanceof IMementoAware) {
|
158
|
((IMementoAware) contentProvider).saveState(aMemento);
|
159
|
}
|
160
|
|
161
|
}
|
162
|
|
163
|
public void init(ICommonContentExtensionSite aConfig) {
|
164
|
if (contentProvider instanceof ICommonContentProvider) {
|
165
|
((ICommonContentProvider) contentProvider).init(aConfig);
|
166
|
}
|
167
|
}
|
168
|
|
169
|
/*
|
170
|
* (non-Javadoc)
|
171
|
*
|
172
|
* @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedChildren(java.lang.Object,
|
173
|
* java.util.Set)
|
174
|
*/
|
175
|
public void getPipelinedChildren(Object aParent, Set theCurrentChildren) {
|
176
|
if (contentProvider instanceof IPipelinedTreeContentProvider) {
|
177
|
((IPipelinedTreeContentProvider) contentProvider)
|
178
|
.getPipelinedChildren(aParent, theCurrentChildren);
|
179
|
}
|
180
|
|
181
|
}
|
182
|
|
183
|
/*
|
184
|
* (non-Javadoc)
|
185
|
*
|
186
|
* @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedElements(java.lang.Object,
|
187
|
* java.util.Set)
|
188
|
*/
|
189
|
public void getPipelinedElements(Object anInput, Set theCurrentElements) {
|
190
|
if (contentProvider instanceof IPipelinedTreeContentProvider) {
|
191
|
((IPipelinedTreeContentProvider) contentProvider)
|
192
|
.getPipelinedElements(anInput, theCurrentElements);
|
193
|
}
|
194
|
}
|
195
|
|
196
|
/*
|
197
|
* (non-Javadoc)
|
198
|
*
|
199
|
* @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedParent(java.lang.Object,
|
200
|
* java.lang.Object)
|
201
|
*/
|
202
|
public Object getPipelinedParent(Object anObject, Object aSuggestedParent) {
|
203
|
if (contentProvider instanceof IPipelinedTreeContentProvider) {
|
204
|
return ((IPipelinedTreeContentProvider) contentProvider)
|
205
|
.getPipelinedParent(anObject, aSuggestedParent);
|
206
|
}
|
207
|
return anObject;
|
208
|
}
|
209
|
|
210
|
/*
|
211
|
* (non-Javadoc)
|
212
|
*
|
213
|
* @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptAdd(org.eclipse.ui.navigator.PipelinedShapeModification)
|
214
|
*/
|
215
|
public PipelinedShapeModification interceptAdd(
|
216
|
PipelinedShapeModification anAddModification) {
|
217
|
if (contentProvider instanceof IPipelinedTreeContentProvider) {
|
218
|
return ((IPipelinedTreeContentProvider) contentProvider)
|
219
|
.interceptAdd(anAddModification);
|
220
|
}
|
221
|
return anAddModification;
|
222
|
}
|
223
|
|
224
|
/*
|
225
|
* (non-Javadoc)
|
226
|
*
|
227
|
* @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptRemove(org.eclipse.ui.navigator.PipelinedShapeModification)
|
228
|
*/
|
229
|
public PipelinedShapeModification interceptRemove(
|
230
|
PipelinedShapeModification aRemoveModification) {
|
231
|
if (contentProvider instanceof IPipelinedTreeContentProvider) {
|
232
|
return ((IPipelinedTreeContentProvider) contentProvider)
|
233
|
.interceptRemove(aRemoveModification);
|
234
|
}
|
235
|
return aRemoveModification;
|
236
|
}
|
237
|
|
238
|
/*
|
239
|
* (non-Javadoc)
|
240
|
*
|
241
|
* @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptRefresh(org.eclipse.ui.navigator.PipelinedViewerUpdate)
|
242
|
*/
|
243
|
public boolean interceptRefresh(
|
244
|
PipelinedViewerUpdate aRefreshSynchronization) {
|
245
|
if (contentProvider instanceof IPipelinedTreeContentProvider) {
|
246
|
return ((IPipelinedTreeContentProvider) contentProvider)
|
247
|
.interceptRefresh(aRefreshSynchronization);
|
248
|
}
|
249
|
return false;
|
250
|
}
|
251
|
|
252
|
/*
|
253
|
* (non-Javadoc)
|
254
|
*
|
255
|
* @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptUpdate(org.eclipse.ui.navigator.PipelinedViewerUpdate)
|
256
|
*/
|
257
|
public boolean interceptUpdate(
|
258
|
PipelinedViewerUpdate anUpdateSynchronization) {
|
259
|
if (contentProvider instanceof IPipelinedTreeContentProvider) {
|
260
|
return ((IPipelinedTreeContentProvider) contentProvider)
|
261
|
.interceptUpdate(anUpdateSynchronization);
|
262
|
}
|
263
|
return false;
|
264
|
}
|
265
|
|
266
|
/* (non-Javadoc)
|
267
|
* @see org.eclipse.jface.viewers.ITreePathContentProvider#getChildren(org.eclipse.jface.viewers.TreePath)
|
268
|
*/
|
269
|
public Object[] getChildren(TreePath parentPath) {
|
270
|
if (contentProvider instanceof ITreePathContentProvider) {
|
271
|
ITreePathContentProvider tpcp = (ITreePathContentProvider) contentProvider;
|
272
|
Object[] children = tpcp.getChildren(parentPath);
|
273
|
return children;
|
274
|
}
|
275
|
return getChildren(parentPath.getLastSegment());
|
276
|
}
|
277
|
|
278
|
/* (non-Javadoc)
|
279
|
* @see org.eclipse.jface.viewers.ITreePathContentProvider#hasChildren(org.eclipse.jface.viewers.TreePath)
|
280
|
*/
|
281
|
public boolean hasChildren(TreePath path) {
|
282
|
if (contentProvider instanceof ITreePathContentProvider) {
|
283
|
ITreePathContentProvider tpcp = (ITreePathContentProvider) contentProvider;
|
284
|
return tpcp.hasChildren(path);
|
285
|
}
|
286
|
return hasChildren(path.getLastSegment());
|
287
|
}
|
288
|
|
289
|
/* (non-Javadoc)
|
290
|
* @see org.eclipse.jface.viewers.ITreePathContentProvider#getParents(java.lang.Object)
|
291
|
*/
|
292
|
public TreePath[] getParents(Object element) {
|
293
|
if (contentProvider instanceof ITreePathContentProvider) {
|
294
|
ITreePathContentProvider tpcp = (ITreePathContentProvider) contentProvider;
|
295
|
return tpcp.getParents(element);
|
296
|
}
|
297
|
ArrayList segments = new ArrayList();
|
298
|
Object parent = element;
|
299
|
do {
|
300
|
parent = contentProvider.getParent(parent);
|
301
|
if (parent != null && parent != viewer.getInput())
|
302
|
segments.add(0, parent);
|
303
|
} while (parent != null && parent != viewer.getInput());
|
304
|
if (!segments.isEmpty()) {
|
305
|
// Loop backwards over the array to create the path.
|
306
|
return new TreePath[] { new TreePath(segments.toArray()) };
|
307
|
}
|
308
|
return NO_PATHS;
|
309
|
}
|
310
|
|
311
|
/* (non-Javadoc)
|
312
|
* @see org.eclipse.ui.navigator.IPipelinedTreeContentProviderHasChildren#hasPipelinedChildren(java.lang.Object, boolean)
|
313
|
*/
|
314
|
public boolean hasPipelinedChildren(Object anInput, boolean currentHasChildren) {
|
315
|
if (contentProvider instanceof IPipelinedTreeContentProvider2) {
|
316
|
return ((IPipelinedTreeContentProvider2) contentProvider)
|
317
|
.hasPipelinedChildren(anInput, currentHasChildren);
|
318
|
}
|
319
|
return currentHasChildren;
|
320
|
}
|
321
|
|
322
|
}
|