1 /*******************************************************************************
2 * Copyright (c) 2010, 2011, 2012 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
;
15 import java
.util
.ArrayList
;
16 import java
.util
.HashMap
;
17 import java
.util
.List
;
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
;
28 import org
.eclipse
.ui
.navigator
.IPipelinedTreeContentProvider
;
29 import org
.eclipse
.ui
.navigator
.PipelinedShapeModification
;
30 import org
.eclipse
.ui
.navigator
.PipelinedViewerUpdate
;
33 * The TMF project content provider for the tree viewer in the project explorer view.
36 * @author Francois Chouinard
38 public class TmfNavigatorContentProvider
implements IPipelinedTreeContentProvider
{
40 // ------------------------------------------------------------------------
41 // ICommonContentProvider
42 // ------------------------------------------------------------------------
45 * @see org.eclipse.jface.viewers.ITreeContentProvider#getElements(java.lang.Object)
48 public Object
[] getElements(Object inputElement
) {
54 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
57 public Object
getParent(Object element
) {
63 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
66 public boolean hasChildren(Object element
) {
67 if (element
instanceof IProject
) {
68 IProject project
= (IProject
) element
;
69 return project
.isAccessible();
71 if (element
instanceof TmfTraceFolder
) {
72 TmfTraceFolder folder
= (TmfTraceFolder
) element
;
73 return folder
.hasChildren();
75 if (element
instanceof TmfExperimentFolder
) {
76 TmfExperimentFolder folder
= (TmfExperimentFolder
) element
;
77 return folder
.hasChildren();
79 if (element
instanceof TmfExperimentElement
) {
80 TmfExperimentElement folder
= (TmfExperimentElement
) element
;
81 return folder
.hasChildren();
88 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
91 public void dispose() {
96 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
99 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
104 * @see org.eclipse.ui.navigator.IMementoAware#restoreState(org.eclipse.ui.IMemento)
107 public void restoreState(IMemento aMemento
) {
112 * @see org.eclipse.ui.navigator.IMementoAware#saveState(org.eclipse.ui.IMemento)
115 public void saveState(IMemento aMemento
) {
120 * @see org.eclipse.ui.navigator.ICommonContentProvider#init(org.eclipse.ui.navigator.ICommonContentExtensionSite)
123 public void init(ICommonContentExtensionSite aConfig
) {
126 // ------------------------------------------------------------------------
127 // ICommonContentProvider - getChildren()
128 // ------------------------------------------------------------------------
131 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
134 public synchronized Object
[] getChildren(Object parentElement
) {
136 // Tracing project level
137 if (parentElement
instanceof IProject
) {
138 return getProjectChildren((IProject
) parentElement
);
141 // Traces "folder" level
142 if (parentElement
instanceof TmfTraceFolder
) {
143 return getTraceFolderChildren((TmfTraceFolder
) parentElement
);
146 // Experiments "folder" level
147 if (parentElement
instanceof TmfExperimentFolder
) {
148 return getExperimentFolderChildren((TmfExperimentFolder
) parentElement
);
152 if (parentElement
instanceof TmfExperimentElement
) {
153 return getExperimentChildren((TmfExperimentElement
) parentElement
);
156 return new Object
[0];
159 // ------------------------------------------------------------------------
161 // ------------------------------------------------------------------------
162 private Object
[] getProjectChildren(IProject project
) {
163 // The children structure
164 List
<Object
> children
= new ArrayList
<Object
>();
166 // Get the children from the model
167 Map
<String
, ITmfProjectModelElement
> childrenMap
= new HashMap
<String
, ITmfProjectModelElement
>();
168 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, true);
169 for (ITmfProjectModelElement element
: tmfProject
.getChildren()) {
170 if (element
instanceof TmfTraceFolder
) {
171 TmfTraceFolder child
= (TmfTraceFolder
) element
;
172 childrenMap
.put(child
.getResource().getName(), child
);
174 if (element
instanceof TmfExperimentFolder
) {
175 TmfExperimentFolder child
= (TmfExperimentFolder
) element
;
176 childrenMap
.put(child
.getResource().getName(), child
);
180 // Add the model folder if the corresponding resource exists and is not
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
);
189 children
.add(element
);
190 childrenMap
.remove(name
);
191 getTraceFolderChildren((TmfTraceFolder
) element
);
194 // Add the model folder if the corresponding resource exists and is not
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
);
203 children
.add(element
);
204 childrenMap
.remove(name
);
205 getExperimentFolderChildren((TmfExperimentFolder
) element
);
208 // Remove the leftovers (what was in the model but removed from the
210 cleanupModel(tmfProject
, childrenMap
);
212 return children
.toArray();
215 private Object
[] getTraceFolderChildren(TmfTraceFolder tmfTraceFolder
) {
216 // The children structure
217 List
<Object
> children
= new ArrayList
<Object
>();
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
);
228 IFolder folder
= tmfTraceFolder
.getResource();
230 IResource
[] members
= folder
.members();
231 for (IResource resource
: members
) {
232 String name
= resource
.getName();
233 ITmfProjectModelElement trace
= childrenMap
.get(name
);
235 trace
= new TmfTraceElement(name
, resource
, tmfTraceFolder
);
238 childrenMap
.remove(name
);
240 } catch (CoreException e
) {
243 // Remove the leftovers (what was in the model but removed from the
245 cleanupModel(tmfTraceFolder
, childrenMap
);
247 return children
.toArray();
250 private Object
[] getExperimentFolderChildren(TmfExperimentFolder tmfExperimentFolder
) {
251 // The children structure
252 List
<Object
> children
= new ArrayList
<Object
>();
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
);
263 IFolder folder
= tmfExperimentFolder
.getResource();
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
);
274 children
.add(experiment
);
275 childrenMap
.remove(name
);
276 getExperimentChildren((TmfExperimentElement
) experiment
);
279 } catch (CoreException e
) {
282 // Remove the leftovers (what was in the model but removed from the
284 cleanupModel(tmfExperimentFolder
, childrenMap
);
286 return children
.toArray();
289 private Object
[] getExperimentChildren(TmfExperimentElement tmfExperiment
) {
290 // The children structure
291 List
<Object
> children
= new ArrayList
<Object
>();
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
);
302 IFolder folder
= tmfExperiment
.getResource();
304 IResource
[] members
= folder
.members();
305 for (IResource resource
: members
) {
306 String name
= resource
.getName();
307 ITmfProjectModelElement trace
= childrenMap
.get(name
);
308 if (trace
== null && !resource
.isHidden()) {
309 // exclude hidden resources (e.g. bookmarks file)
310 trace
= new TmfTraceElement(name
, resource
, tmfExperiment
);
313 childrenMap
.remove(name
);
315 } catch (CoreException e
) {
318 // Remove the leftovers (what was in the model but removed from the
320 cleanupModel(tmfExperiment
, childrenMap
);
322 return children
.toArray();
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
);
333 cleanupModel(child
, grandChildren
);
334 parent
.removeChild(child
);
339 // ------------------------------------------------------------------------
340 // IPipelinedTreeContentProvider
341 // ------------------------------------------------------------------------
345 * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedChildren(java.lang.Object, java.util.Set)
348 @SuppressWarnings({ "rawtypes", "unchecked" })
349 public void getPipelinedChildren(Object parent
, Set currentChildren
) {
350 customizeTmfElements(getChildren(parent
), currentChildren
);
355 * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedElements(java.lang.Object, java.util.Set)
358 @SuppressWarnings({ "rawtypes", "unchecked" })
359 public void getPipelinedElements(Object input
, Set currentElements
) {
360 customizeTmfElements(getElements(input
), currentElements
);
364 * Add/replace the ITmfProjectElement to the list of children
367 * the list returned by getChildren()
369 * the current children
371 private static void customizeTmfElements(Object
[] elements
,
372 Set
<Object
> children
) {
373 if (elements
!= null && children
!= null) {
374 for (Object element
: elements
) {
375 if (element
instanceof ITmfProjectModelElement
) {
376 ITmfProjectModelElement tmfElement
= (ITmfProjectModelElement
) element
;
377 IResource resource
= tmfElement
.getResource();
378 if (resource
!= null) {
379 children
.remove(resource
);
381 children
.add(element
);
383 else if (element
!= null) {
384 children
.add(element
);
392 * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedParent(java.lang.Object, java.lang.Object)
395 public Object
getPipelinedParent(Object anObject
, Object aSuggestedParent
) {
396 return aSuggestedParent
;
401 * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptAdd(org.eclipse.ui.navigator.PipelinedShapeModification)
404 public PipelinedShapeModification
interceptAdd(PipelinedShapeModification anAddModification
) {
405 return anAddModification
;
410 * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptRemove(org.eclipse.ui.navigator.PipelinedShapeModification)
413 public PipelinedShapeModification
interceptRemove(PipelinedShapeModification aRemoveModification
) {
419 * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptRefresh(org.eclipse.ui.navigator.PipelinedViewerUpdate)
422 public boolean interceptRefresh(PipelinedViewerUpdate aRefreshSynchronization
) {
428 * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#interceptUpdate(org.eclipse.ui.navigator.PipelinedViewerUpdate)
431 public boolean interceptUpdate(PipelinedViewerUpdate anUpdateSynchronization
) {