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
.ICommonContentProvider
;
29 import org
.eclipse
.ui
.navigator
.IPipelinedTreeContentProvider
;
30 import org
.eclipse
.ui
.navigator
.PipelinedShapeModification
;
31 import org
.eclipse
.ui
.navigator
.PipelinedViewerUpdate
;
34 * The TMF project content provider for the tree viewer in the project explorer view.
37 * @author Francois Chouinard
39 public class TmfNavigatorContentProvider
implements ICommonContentProvider
, IPipelinedTreeContentProvider
{
41 // ------------------------------------------------------------------------
42 // ICommonContentProvider
43 // ------------------------------------------------------------------------
46 * @see org.eclipse.jface.viewers.ITreeContentProvider#getElements(java.lang.Object)
49 public Object
[] getElements(Object inputElement
) {
55 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
58 public Object
getParent(Object element
) {
64 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
67 public boolean hasChildren(Object element
) {
68 if (element
instanceof IProject
) {
69 IProject project
= (IProject
) element
;
70 return project
.isAccessible();
72 if (element
instanceof TmfTraceFolder
) {
73 TmfTraceFolder folder
= (TmfTraceFolder
) element
;
74 return folder
.hasChildren();
76 if (element
instanceof TmfExperimentFolder
) {
77 TmfExperimentFolder folder
= (TmfExperimentFolder
) element
;
78 return folder
.hasChildren();
80 if (element
instanceof TmfExperimentElement
) {
81 TmfExperimentElement folder
= (TmfExperimentElement
) element
;
82 return folder
.hasChildren();
89 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
92 public void dispose() {
97 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
100 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
105 * @see org.eclipse.ui.navigator.IMementoAware#restoreState(org.eclipse.ui.IMemento)
108 public void restoreState(IMemento aMemento
) {
113 * @see org.eclipse.ui.navigator.IMementoAware#saveState(org.eclipse.ui.IMemento)
116 public void saveState(IMemento aMemento
) {
121 * @see org.eclipse.ui.navigator.ICommonContentProvider#init(org.eclipse.ui.navigator.ICommonContentExtensionSite)
124 public void init(ICommonContentExtensionSite aConfig
) {
127 // ------------------------------------------------------------------------
128 // ICommonContentProvider - getChildren()
129 // ------------------------------------------------------------------------
132 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
135 public synchronized Object
[] getChildren(Object parentElement
) {
137 // Tracing project level
138 if (parentElement
instanceof IProject
) {
139 return getProjectChildren((IProject
) parentElement
);
142 // Traces "folder" level
143 if (parentElement
instanceof TmfTraceFolder
) {
144 return getTraceFolderChildren((TmfTraceFolder
) parentElement
);
147 // Experiments "folder" level
148 if (parentElement
instanceof TmfExperimentFolder
) {
149 return getExperimentFolderChildren((TmfExperimentFolder
) parentElement
);
153 if (parentElement
instanceof TmfExperimentElement
) {
154 return getExperimentChildren((TmfExperimentElement
) parentElement
);
157 return new Object
[0];
160 // ------------------------------------------------------------------------
162 // ------------------------------------------------------------------------
163 private Object
[] getProjectChildren(IProject project
) {
164 // The children structure
165 List
<Object
> children
= new ArrayList
<Object
>();
167 // Get the children from the model
168 Map
<String
, ITmfProjectModelElement
> childrenMap
= new HashMap
<String
, ITmfProjectModelElement
>();
169 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
, true);
170 for (ITmfProjectModelElement element
: tmfProject
.getChildren()) {
171 if (element
instanceof TmfTraceFolder
) {
172 TmfTraceFolder child
= (TmfTraceFolder
) element
;
173 childrenMap
.put(child
.getResource().getName(), child
);
175 if (element
instanceof TmfExperimentFolder
) {
176 TmfExperimentFolder child
= (TmfExperimentFolder
) element
;
177 childrenMap
.put(child
.getResource().getName(), child
);
181 // Add the model folder if the corresponding resource exists and is not
183 IFolder folder
= project
.getFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
);
184 if (folder
!= null) {
185 String name
= folder
.getName();
186 ITmfProjectModelElement element
= childrenMap
.get(name
);
187 if (element
== null) {
188 element
= new TmfTraceFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
, folder
, tmfProject
);
190 children
.add(element
);
191 childrenMap
.remove(name
);
192 getTraceFolderChildren((TmfTraceFolder
) element
);
195 // Add the model folder if the corresponding resource exists and is not
197 folder
= project
.getFolder(TmfExperimentFolder
.EXPER_FOLDER_NAME
);
198 if (folder
!= null) {
199 String name
= folder
.getName();
200 ITmfProjectModelElement element
= childrenMap
.get(name
);
201 if (element
== null) {
202 element
= new TmfExperimentFolder(TmfExperimentFolder
.EXPER_FOLDER_NAME
, folder
, tmfProject
);
204 children
.add(element
);
205 childrenMap
.remove(name
);
206 getExperimentFolderChildren((TmfExperimentFolder
) element
);
209 // Remove the leftovers (what was in the model but removed from the
211 cleanupModel(tmfProject
, childrenMap
);
213 return children
.toArray();
216 private Object
[] getTraceFolderChildren(TmfTraceFolder tmfTraceFolder
) {
217 // The children structure
218 List
<Object
> children
= new ArrayList
<Object
>();
220 // Get the children from the model
221 Map
<String
, ITmfProjectModelElement
> childrenMap
= new HashMap
<String
, ITmfProjectModelElement
>();
222 for (ITmfProjectModelElement element
: tmfTraceFolder
.getChildren()) {
223 if (element
instanceof TmfTraceElement
) {
224 String name
= element
.getResource().getName();
225 childrenMap
.put(name
, element
);
229 IFolder folder
= tmfTraceFolder
.getResource();
231 IResource
[] members
= folder
.members();
232 for (IResource resource
: members
) {
233 String name
= resource
.getName();
234 ITmfProjectModelElement trace
= childrenMap
.get(name
);
236 trace
= new TmfTraceElement(name
, resource
, tmfTraceFolder
);
239 childrenMap
.remove(name
);
241 } catch (CoreException e
) {
244 // Remove the leftovers (what was in the model but removed from the
246 cleanupModel(tmfTraceFolder
, childrenMap
);
248 return children
.toArray();
251 private Object
[] getExperimentFolderChildren(TmfExperimentFolder tmfExperimentFolder
) {
252 // The children structure
253 List
<Object
> children
= new ArrayList
<Object
>();
255 // Get the children from the model
256 Map
<String
, ITmfProjectModelElement
> childrenMap
= new HashMap
<String
, ITmfProjectModelElement
>();
257 for (ITmfProjectModelElement element
: tmfExperimentFolder
.getChildren()) {
258 if (element
instanceof TmfExperimentElement
) {
259 String name
= element
.getResource().getName();
260 childrenMap
.put(name
, element
);
264 IFolder folder
= tmfExperimentFolder
.getResource();
266 IResource
[] members
= folder
.members();
267 for (IResource resource
: members
) {
268 if (resource
instanceof IFolder
) {
269 IFolder expFolder
= (IFolder
) resource
;
270 String name
= resource
.getName();
271 ITmfProjectModelElement experiment
= childrenMap
.get(name
);
272 if (experiment
== null) {
273 experiment
= new TmfExperimentElement(name
, expFolder
, tmfExperimentFolder
);
275 children
.add(experiment
);
276 childrenMap
.remove(name
);
277 getExperimentChildren((TmfExperimentElement
) experiment
);
280 } catch (CoreException e
) {
283 // Remove the leftovers (what was in the model but removed from the
285 cleanupModel(tmfExperimentFolder
, childrenMap
);
287 return children
.toArray();
290 private Object
[] getExperimentChildren(TmfExperimentElement tmfExperiment
) {
291 // The children structure
292 List
<Object
> children
= new ArrayList
<Object
>();
294 // Get the children from the model
295 Map
<String
, ITmfProjectModelElement
> childrenMap
= new HashMap
<String
, ITmfProjectModelElement
>();
296 for (ITmfProjectModelElement element
: tmfExperiment
.getChildren()) {
297 if (element
instanceof TmfTraceElement
) {
298 String name
= element
.getResource().getName();
299 childrenMap
.put(name
, element
);
303 IFolder folder
= tmfExperiment
.getResource();
305 IResource
[] members
= folder
.members();
306 for (IResource resource
: members
) {
307 String name
= resource
.getName();
308 ITmfProjectModelElement trace
= childrenMap
.get(name
);
309 if (trace
== null && !resource
.isHidden()) {
310 // exclude hidden resources (e.g. bookmarks file)
311 trace
= new TmfTraceElement(name
, resource
, tmfExperiment
);
314 childrenMap
.remove(name
);
316 } catch (CoreException e
) {
319 // Remove the leftovers (what was in the model but removed from the
321 cleanupModel(tmfExperiment
, childrenMap
);
323 return children
.toArray();
326 private void cleanupModel(ITmfProjectModelElement parent
, Map
<String
, ITmfProjectModelElement
> danglingChildren
) {
327 if (parent
!= null) {
328 for (ITmfProjectModelElement child
: danglingChildren
.values()) {
329 Map
<String
, ITmfProjectModelElement
> grandChildren
= new HashMap
<String
, ITmfProjectModelElement
>();
330 for (ITmfProjectModelElement element
: child
.getChildren()) {
331 String name
= element
.getResource().getName();
332 grandChildren
.put(name
, element
);
334 cleanupModel(child
, grandChildren
);
335 parent
.removeChild(child
);
340 // ------------------------------------------------------------------------
341 // IPipelinedTreeContentProvider
342 // ------------------------------------------------------------------------
346 * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedChildren(java.lang.Object, java.util.Set)
349 @SuppressWarnings({ "rawtypes", "unchecked" })
350 public void getPipelinedChildren(Object parent
, Set currentChildren
) {
351 customizeTmfElements(getChildren(parent
), currentChildren
);
356 * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedElements(java.lang.Object, java.util.Set)
359 @SuppressWarnings({ "rawtypes", "unchecked" })
360 public void getPipelinedElements(Object input
, Set currentElements
) {
361 customizeTmfElements(getElements(input
), currentElements
);
365 * Add/replace the ITmfProjectElement to the list of children
368 * the list returned by getChildren()
370 * the current children
372 private void customizeTmfElements(Object
[] elements
, 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
) {