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