Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
b544077e | 2 | * Copyright (c) 2010, 2011, 2012 Ericsson |
013a5f1c | 3 | * |
12c155f5 FC |
4 | * All rights reserved. This program and the accompanying materials are |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
013a5f1c | 8 | * |
12c155f5 FC |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.project.model; | |
14 | ||
15 | import java.util.ArrayList; | |
16 | import java.util.HashMap; | |
17 | import java.util.List; | |
18 | import java.util.Map; | |
19 | import java.util.Set; | |
20 | ||
21 | import org.eclipse.core.resources.IFolder; | |
22 | import org.eclipse.core.resources.IProject; | |
23 | import org.eclipse.core.resources.IResource; | |
24 | import org.eclipse.core.runtime.CoreException; | |
25 | import org.eclipse.jface.viewers.Viewer; | |
26 | import org.eclipse.ui.IMemento; | |
27 | import org.eclipse.ui.navigator.ICommonContentExtensionSite; | |
12c155f5 FC |
28 | import org.eclipse.ui.navigator.IPipelinedTreeContentProvider; |
29 | import org.eclipse.ui.navigator.PipelinedShapeModification; | |
30 | import org.eclipse.ui.navigator.PipelinedViewerUpdate; | |
31 | ||
32 | /** | |
013a5f1c | 33 | * The TMF project content provider for the tree viewer in the project explorer view. |
12c155f5 | 34 | * <p> |
b544077e BH |
35 | * @version 1.0 |
36 | * @author Francois Chouinard | |
12c155f5 | 37 | */ |
013a5f1c | 38 | public class TmfNavigatorContentProvider implements IPipelinedTreeContentProvider { |
12c155f5 FC |
39 | |
40 | // ------------------------------------------------------------------------ | |
41 | // ICommonContentProvider | |
42 | // ------------------------------------------------------------------------ | |
b544077e BH |
43 | /* |
44 | * (non-Javadoc) | |
45 | * @see org.eclipse.jface.viewers.ITreeContentProvider#getElements(java.lang.Object) | |
46 | */ | |
12c155f5 FC |
47 | @Override |
48 | public Object[] getElements(Object inputElement) { | |
49 | return null; | |
50 | } | |
51 | ||
b544077e BH |
52 | /* |
53 | * (non-Javadoc) | |
54 | * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) | |
55 | */ | |
12c155f5 FC |
56 | @Override |
57 | public Object getParent(Object element) { | |
58 | return null; | |
59 | } | |
60 | ||
b544077e BH |
61 | /* |
62 | * (non-Javadoc) | |
63 | * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) | |
64 | */ | |
12c155f5 FC |
65 | @Override |
66 | public boolean hasChildren(Object element) { | |
67 | if (element instanceof IProject) { | |
68 | IProject project = (IProject) element; | |
69 | return project.isAccessible(); | |
70 | } | |
71 | if (element instanceof TmfTraceFolder) { | |
72 | TmfTraceFolder folder = (TmfTraceFolder) element; | |
73 | return folder.hasChildren(); | |
74 | } | |
75 | if (element instanceof TmfExperimentFolder) { | |
76 | TmfExperimentFolder folder = (TmfExperimentFolder) element; | |
77 | return folder.hasChildren(); | |
78 | } | |
79 | if (element instanceof TmfExperimentElement) { | |
80 | TmfExperimentElement folder = (TmfExperimentElement) element; | |
81 | return folder.hasChildren(); | |
82 | } | |
83 | return false; | |
84 | } | |
85 | ||
b544077e BH |
86 | /* |
87 | * (non-Javadoc) | |
88 | * @see org.eclipse.jface.viewers.IContentProvider#dispose() | |
89 | */ | |
12c155f5 FC |
90 | @Override |
91 | public void dispose() { | |
92 | } | |
93 | ||
b544077e BH |
94 | /* |
95 | * (non-Javadoc) | |
96 | * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) | |
97 | */ | |
12c155f5 FC |
98 | @Override |
99 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { | |
100 | } | |
013a5f1c | 101 | |
b544077e BH |
102 | /* |
103 | * (non-Javadoc) | |
104 | * @see org.eclipse.ui.navigator.IMementoAware#restoreState(org.eclipse.ui.IMemento) | |
105 | */ | |
12c155f5 FC |
106 | @Override |
107 | public void restoreState(IMemento aMemento) { | |
108 | } | |
013a5f1c | 109 | |
b544077e BH |
110 | /* |
111 | * (non-Javadoc) | |
112 | * @see org.eclipse.ui.navigator.IMementoAware#saveState(org.eclipse.ui.IMemento) | |
113 | */ | |
12c155f5 FC |
114 | @Override |
115 | public void saveState(IMemento aMemento) { | |
116 | } | |
117 | ||
b544077e BH |
118 | /* |
119 | * (non-Javadoc) | |
120 | * @see org.eclipse.ui.navigator.ICommonContentProvider#init(org.eclipse.ui.navigator.ICommonContentExtensionSite) | |
121 | */ | |
12c155f5 FC |
122 | @Override |
123 | public void init(ICommonContentExtensionSite aConfig) { | |
124 | } | |
125 | ||
126 | // ------------------------------------------------------------------------ | |
127 | // ICommonContentProvider - getChildren() | |
128 | // ------------------------------------------------------------------------ | |
b544077e BH |
129 | /* |
130 | * (non-Javadoc) | |
131 | * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) | |
132 | */ | |
12c155f5 FC |
133 | @Override |
134 | public synchronized Object[] getChildren(Object parentElement) { | |
135 | ||
136 | // Tracing project level | |
137 | if (parentElement instanceof IProject) { | |
138 | return getProjectChildren((IProject) parentElement); | |
139 | } | |
140 | ||
141 | // Traces "folder" level | |
142 | if (parentElement instanceof TmfTraceFolder) { | |
143 | return getTraceFolderChildren((TmfTraceFolder) parentElement); | |
144 | } | |
145 | ||
146 | // Experiments "folder" level | |
147 | if (parentElement instanceof TmfExperimentFolder) { | |
148 | return getExperimentFolderChildren((TmfExperimentFolder) parentElement); | |
149 | } | |
150 | ||
151 | // Experiment | |
152 | if (parentElement instanceof TmfExperimentElement) { | |
153 | return getExperimentChildren((TmfExperimentElement) parentElement); | |
154 | } | |
155 | ||
156 | return new Object[0]; | |
157 | } | |
158 | ||
b544077e BH |
159 | // ------------------------------------------------------------------------ |
160 | // Helper method | |
161 | // ------------------------------------------------------------------------ | |
12c155f5 FC |
162 | private Object[] getProjectChildren(IProject project) { |
163 | // The children structure | |
164 | List<Object> children = new ArrayList<Object>(); | |
165 | ||
166 | // Get the children from the model | |
167 | Map<String, ITmfProjectModelElement> childrenMap = new HashMap<String, ITmfProjectModelElement>(); | |
828e5592 | 168 | TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, true); |
12c155f5 FC |
169 | for (ITmfProjectModelElement element : tmfProject.getChildren()) { |
170 | if (element instanceof TmfTraceFolder) { | |
171 | TmfTraceFolder child = (TmfTraceFolder) element; | |
172 | childrenMap.put(child.getResource().getName(), child); | |
173 | } | |
174 | if (element instanceof TmfExperimentFolder) { | |
175 | TmfExperimentFolder child = (TmfExperimentFolder) element; | |
176 | childrenMap.put(child.getResource().getName(), child); | |
177 | } | |
178 | } | |
179 | ||
180 | // Add the model folder if the corresponding resource exists and is not | |
181 | // accounted for | |
182 | IFolder folder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME); | |
183 | if (folder != null) { | |
184 | String name = folder.getName(); | |
185 | ITmfProjectModelElement element = childrenMap.get(name); | |
186 | if (element == null) { | |
187 | element = new TmfTraceFolder(TmfTraceFolder.TRACE_FOLDER_NAME, folder, tmfProject); | |
188 | } | |
189 | children.add(element); | |
190 | childrenMap.remove(name); | |
191 | getTraceFolderChildren((TmfTraceFolder) element); | |
192 | } | |
193 | ||
194 | // Add the model folder if the corresponding resource exists and is not | |
195 | // accounted for | |
196 | folder = project.getFolder(TmfExperimentFolder.EXPER_FOLDER_NAME); | |
197 | if (folder != null) { | |
198 | String name = folder.getName(); | |
199 | ITmfProjectModelElement element = childrenMap.get(name); | |
200 | if (element == null) { | |
201 | element = new TmfExperimentFolder(TmfExperimentFolder.EXPER_FOLDER_NAME, folder, tmfProject); | |
202 | } | |
203 | children.add(element); | |
204 | childrenMap.remove(name); | |
205 | getExperimentFolderChildren((TmfExperimentFolder) element); | |
206 | } | |
207 | ||
208 | // Remove the leftovers (what was in the model but removed from the | |
209 | // project) | |
210 | cleanupModel(tmfProject, childrenMap); | |
211 | ||
212 | return children.toArray(); | |
213 | } | |
214 | ||
215 | private Object[] getTraceFolderChildren(TmfTraceFolder tmfTraceFolder) { | |
216 | // The children structure | |
217 | List<Object> children = new ArrayList<Object>(); | |
218 | ||
219 | // Get the children from the model | |
220 | Map<String, ITmfProjectModelElement> childrenMap = new HashMap<String, ITmfProjectModelElement>(); | |
221 | for (ITmfProjectModelElement element : tmfTraceFolder.getChildren()) { | |
222 | if (element instanceof TmfTraceElement) { | |
223 | String name = element.getResource().getName(); | |
224 | childrenMap.put(name, element); | |
225 | } | |
226 | } | |
227 | ||
228 | IFolder folder = tmfTraceFolder.getResource(); | |
229 | try { | |
230 | IResource[] members = folder.members(); | |
231 | for (IResource resource : members) { | |
232 | String name = resource.getName(); | |
233 | ITmfProjectModelElement trace = childrenMap.get(name); | |
e12ecd30 BH |
234 | if (trace == null) { |
235 | trace = new TmfTraceElement(name, resource, tmfTraceFolder); | |
12c155f5 | 236 | } |
e12ecd30 | 237 | children.add(trace); |
12c155f5 FC |
238 | childrenMap.remove(name); |
239 | } | |
240 | } catch (CoreException e) { | |
241 | } | |
242 | ||
243 | // Remove the leftovers (what was in the model but removed from the | |
244 | // project) | |
245 | cleanupModel(tmfTraceFolder, childrenMap); | |
246 | ||
247 | return children.toArray(); | |
248 | } | |
249 | ||
250 | private Object[] getExperimentFolderChildren(TmfExperimentFolder tmfExperimentFolder) { | |
251 | // The children structure | |
252 | List<Object> children = new ArrayList<Object>(); | |
253 | ||
254 | // Get the children from the model | |
255 | Map<String, ITmfProjectModelElement> childrenMap = new HashMap<String, ITmfProjectModelElement>(); | |
256 | for (ITmfProjectModelElement element : tmfExperimentFolder.getChildren()) { | |
257 | if (element instanceof TmfExperimentElement) { | |
258 | String name = element.getResource().getName(); | |
259 | childrenMap.put(name, element); | |
260 | } | |
261 | } | |
262 | ||
263 | IFolder folder = tmfExperimentFolder.getResource(); | |
264 | try { | |
265 | IResource[] members = folder.members(); | |
266 | for (IResource resource : members) { | |
267 | if (resource instanceof IFolder) { | |
268 | IFolder expFolder = (IFolder) resource; | |
269 | String name = resource.getName(); | |
270 | ITmfProjectModelElement experiment = childrenMap.get(name); | |
271 | if (experiment == null) { | |
272 | experiment = new TmfExperimentElement(name, expFolder, tmfExperimentFolder); | |
273 | } | |
274 | children.add(experiment); | |
275 | childrenMap.remove(name); | |
276 | getExperimentChildren((TmfExperimentElement) experiment); | |
277 | } | |
278 | } | |
279 | } catch (CoreException e) { | |
280 | } | |
281 | ||
282 | // Remove the leftovers (what was in the model but removed from the | |
283 | // project) | |
284 | cleanupModel(tmfExperimentFolder, childrenMap); | |
285 | ||
286 | return children.toArray(); | |
287 | } | |
288 | ||
289 | private Object[] getExperimentChildren(TmfExperimentElement tmfExperiment) { | |
290 | // The children structure | |
291 | List<Object> children = new ArrayList<Object>(); | |
292 | ||
293 | // Get the children from the model | |
294 | Map<String, ITmfProjectModelElement> childrenMap = new HashMap<String, ITmfProjectModelElement>(); | |
295 | for (ITmfProjectModelElement element : tmfExperiment.getChildren()) { | |
296 | if (element instanceof TmfTraceElement) { | |
297 | String name = element.getResource().getName(); | |
298 | childrenMap.put(name, element); | |
299 | } | |
300 | } | |
301 | ||
302 | IFolder folder = tmfExperiment.getResource(); | |
303 | try { | |
304 | IResource[] members = folder.members(); | |
305 | for (IResource resource : members) { | |
306 | String name = resource.getName(); | |
307 | ITmfProjectModelElement trace = childrenMap.get(name); | |
828e5592 PT |
308 | if (trace == null && !resource.isHidden()) { |
309 | // exclude hidden resources (e.g. bookmarks file) | |
12c155f5 FC |
310 | trace = new TmfTraceElement(name, resource, tmfExperiment); |
311 | } | |
312 | children.add(trace); | |
313 | childrenMap.remove(name); | |
314 | } | |
315 | } catch (CoreException e) { | |
316 | } | |
317 | ||
318 | // Remove the leftovers (what was in the model but removed from the | |
319 | // project) | |
320 | cleanupModel(tmfExperiment, childrenMap); | |
321 | ||
322 | return children.toArray(); | |
323 | } | |
324 | ||
325 | private void cleanupModel(ITmfProjectModelElement parent, Map<String, ITmfProjectModelElement> danglingChildren) { | |
326 | if (parent != null) { | |
327 | for (ITmfProjectModelElement child : danglingChildren.values()) { | |
328 | Map<String, ITmfProjectModelElement> grandChildren = new HashMap<String, ITmfProjectModelElement>(); | |
329 | for (ITmfProjectModelElement element : child.getChildren()) { | |
330 | String name = element.getResource().getName(); | |
331 | grandChildren.put(name, element); | |
332 | } | |
333 | cleanupModel(child, grandChildren); | |
334 | parent.removeChild(child); | |
335 | } | |
336 | } | |
337 | } | |
338 | ||
339 | // ------------------------------------------------------------------------ | |
340 | // IPipelinedTreeContentProvider | |
341 | // ------------------------------------------------------------------------ | |
342 | ||
b544077e BH |
343 | /* |
344 | * (non-Javadoc) | |
345 | * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedChildren(java.lang.Object, java.util.Set) | |
346 | */ | |
12c155f5 FC |
347 | @Override |
348 | @SuppressWarnings({ "rawtypes", "unchecked" }) | |
349 | public void getPipelinedChildren(Object parent, Set currentChildren) { | |
350 | customizeTmfElements(getChildren(parent), currentChildren); | |
351 | } | |
352 | ||
b544077e BH |
353 | /* |
354 | * (non-Javadoc) | |
355 | * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedElements(java.lang.Object, java.util.Set) | |
356 | */ | |
12c155f5 FC |
357 | @Override |
358 | @SuppressWarnings({ "rawtypes", "unchecked" }) | |
359 | public void getPipelinedElements(Object input, Set currentElements) { | |
360 | customizeTmfElements(getElements(input), currentElements); | |
361 | } | |
362 | ||
363 | /** | |
364 | * Add/replace the ITmfProjectElement to the list of children | |
013a5f1c | 365 | * |
12c155f5 FC |
366 | * @param elements |
367 | * the list returned by getChildren() | |
368 | * @param children | |
369 | * the current children | |
370 | */ | |
371 | private void customizeTmfElements(Object[] elements, Set<Object> children) { | |
372 | if (elements != null && children != null) { | |
373 | for (Object element : elements) { | |
374 | if (element instanceof ITmfProjectModelElement) { | |
375 | ITmfProjectModelElement tmfElement = (ITmfProjectModelElement) element; | |
376 | IResource resource = tmfElement.getResource(); | |
377 | if (resource != null) { | |
378 | children.remove(resource); | |
379 | } | |
380 | children.add(element); | |
381 | } | |
382 | else if (element != null) { | |
383 | children.add(element); | |
384 | } | |
385 | } | |
386 | } | |
387 | } | |
388 | ||
b544077e BH |
389 | /* |
390 | * (non-Javadoc) | |
391 | * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedParent(java.lang.Object, java.lang.Object) | |
392 | */ | |
12c155f5 FC |
393 | @Override |
394 | public Object getPipelinedParent(Object anObject, Object aSuggestedParent) { | |
bfa12ff0 | 395 | return aSuggestedParent; |
12c155f5 FC |
396 | } |
397 | ||
b544077e BH |
398 | /* |
399 | * (non-Javadoc) | |
400 | * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptAdd(org.eclipse.ui.navigator.PipelinedShapeModification) | |
401 | */ | |
12c155f5 FC |
402 | @Override |
403 | public PipelinedShapeModification interceptAdd(PipelinedShapeModification anAddModification) { | |
bfa12ff0 | 404 | return anAddModification; |
12c155f5 FC |
405 | } |
406 | ||
b544077e BH |
407 | /* |
408 | * (non-Javadoc) | |
409 | * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptRemove(org.eclipse.ui.navigator.PipelinedShapeModification) | |
410 | */ | |
12c155f5 FC |
411 | @Override |
412 | public PipelinedShapeModification interceptRemove(PipelinedShapeModification aRemoveModification) { | |
413 | return null; | |
414 | } | |
415 | ||
b544077e BH |
416 | /* |
417 | * (non-Javadoc) | |
418 | * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptRefresh(org.eclipse.ui.navigator.PipelinedViewerUpdate) | |
419 | */ | |
12c155f5 FC |
420 | @Override |
421 | public boolean interceptRefresh(PipelinedViewerUpdate aRefreshSynchronization) { | |
422 | return false; | |
423 | } | |
424 | ||
b544077e BH |
425 | /* |
426 | * (non-Javadoc) | |
427 | * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptUpdate(org.eclipse.ui.navigator.PipelinedViewerUpdate) | |
428 | */ | |
12c155f5 FC |
429 | @Override |
430 | public boolean interceptUpdate(PipelinedViewerUpdate anUpdateSynchronization) { | |
431 | return false; | |
432 | } | |
12c155f5 | 433 | } |