1 /*******************************************************************************
2 * Copyright (c) 2010, 2011 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 * <b><u>TmfNavigatorContentProvider</u></b>
37 public class TmfNavigatorContentProvider
implements ICommonContentProvider
, IPipelinedTreeContentProvider
{
39 // ------------------------------------------------------------------------
40 // ICommonContentProvider
41 // ------------------------------------------------------------------------
44 public Object
[] getElements(Object inputElement
) {
49 public Object
getParent(Object element
) {
54 public boolean hasChildren(Object element
) {
55 if (element
instanceof IProject
) {
56 IProject project
= (IProject
) element
;
57 return project
.isAccessible();
59 if (element
instanceof TmfTraceFolder
) {
60 TmfTraceFolder folder
= (TmfTraceFolder
) element
;
61 return folder
.hasChildren();
63 if (element
instanceof TmfExperimentFolder
) {
64 TmfExperimentFolder folder
= (TmfExperimentFolder
) element
;
65 return folder
.hasChildren();
67 if (element
instanceof TmfExperimentElement
) {
68 TmfExperimentElement folder
= (TmfExperimentElement
) element
;
69 return folder
.hasChildren();
75 public void dispose() {
79 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
83 public void restoreState(IMemento aMemento
) {
87 public void saveState(IMemento aMemento
) {
91 public void init(ICommonContentExtensionSite aConfig
) {
94 // ------------------------------------------------------------------------
95 // ICommonContentProvider - getChildren()
96 // ------------------------------------------------------------------------
99 public synchronized Object
[] getChildren(Object parentElement
) {
101 // Tracing project level
102 if (parentElement
instanceof IProject
) {
103 return getProjectChildren((IProject
) parentElement
);
106 // Traces "folder" level
107 if (parentElement
instanceof TmfTraceFolder
) {
108 return getTraceFolderChildren((TmfTraceFolder
) parentElement
);
111 // Experiments "folder" level
112 if (parentElement
instanceof TmfExperimentFolder
) {
113 return getExperimentFolderChildren((TmfExperimentFolder
) parentElement
);
117 if (parentElement
instanceof TmfExperimentElement
) {
118 return getExperimentChildren((TmfExperimentElement
) parentElement
);
121 return new Object
[0];
124 private Object
[] getProjectChildren(IProject project
) {
125 // The children structure
126 List
<Object
> children
= new ArrayList
<Object
>();
128 // Get the children from the model
129 Map
<String
, ITmfProjectModelElement
> childrenMap
= new HashMap
<String
, ITmfProjectModelElement
>();
130 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(project
);
131 for (ITmfProjectModelElement element
: tmfProject
.getChildren()) {
132 if (element
instanceof TmfTraceFolder
) {
133 TmfTraceFolder child
= (TmfTraceFolder
) element
;
134 childrenMap
.put(child
.getResource().getName(), child
);
136 if (element
instanceof TmfExperimentFolder
) {
137 TmfExperimentFolder child
= (TmfExperimentFolder
) element
;
138 childrenMap
.put(child
.getResource().getName(), child
);
142 // Add the model folder if the corresponding resource exists and is not
144 IFolder folder
= project
.getFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
);
145 if (folder
!= null) {
146 String name
= folder
.getName();
147 ITmfProjectModelElement element
= childrenMap
.get(name
);
148 if (element
== null) {
149 element
= new TmfTraceFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
, folder
, tmfProject
);
151 children
.add(element
);
152 childrenMap
.remove(name
);
153 getTraceFolderChildren((TmfTraceFolder
) element
);
156 // Add the model folder if the corresponding resource exists and is not
158 folder
= project
.getFolder(TmfExperimentFolder
.EXPER_FOLDER_NAME
);
159 if (folder
!= null) {
160 String name
= folder
.getName();
161 ITmfProjectModelElement element
= childrenMap
.get(name
);
162 if (element
== null) {
163 element
= new TmfExperimentFolder(TmfExperimentFolder
.EXPER_FOLDER_NAME
, folder
, tmfProject
);
165 children
.add(element
);
166 childrenMap
.remove(name
);
167 getExperimentFolderChildren((TmfExperimentFolder
) element
);
170 // Remove the leftovers (what was in the model but removed from the
172 cleanupModel(tmfProject
, childrenMap
);
174 return children
.toArray();
177 private Object
[] getTraceFolderChildren(TmfTraceFolder tmfTraceFolder
) {
178 // The children structure
179 List
<Object
> children
= new ArrayList
<Object
>();
181 // Get the children from the model
182 Map
<String
, ITmfProjectModelElement
> childrenMap
= new HashMap
<String
, ITmfProjectModelElement
>();
183 for (ITmfProjectModelElement element
: tmfTraceFolder
.getChildren()) {
184 if (element
instanceof TmfTraceElement
) {
185 String name
= element
.getResource().getName();
186 childrenMap
.put(name
, element
);
190 IFolder folder
= tmfTraceFolder
.getResource();
192 IResource
[] members
= folder
.members();
193 for (IResource resource
: members
) {
194 String name
= resource
.getName();
195 ITmfProjectModelElement trace
= childrenMap
.get(name
);
197 trace
= new TmfTraceElement(name
, resource
, tmfTraceFolder
);
200 childrenMap
.remove(name
);
202 } catch (CoreException e
) {
205 // Remove the leftovers (what was in the model but removed from the
207 cleanupModel(tmfTraceFolder
, childrenMap
);
209 return children
.toArray();
212 private Object
[] getExperimentFolderChildren(TmfExperimentFolder tmfExperimentFolder
) {
213 // The children structure
214 List
<Object
> children
= new ArrayList
<Object
>();
216 // Get the children from the model
217 Map
<String
, ITmfProjectModelElement
> childrenMap
= new HashMap
<String
, ITmfProjectModelElement
>();
218 for (ITmfProjectModelElement element
: tmfExperimentFolder
.getChildren()) {
219 if (element
instanceof TmfExperimentElement
) {
220 String name
= element
.getResource().getName();
221 childrenMap
.put(name
, element
);
225 IFolder folder
= tmfExperimentFolder
.getResource();
227 IResource
[] members
= folder
.members();
228 for (IResource resource
: members
) {
229 if (resource
instanceof IFolder
) {
230 IFolder expFolder
= (IFolder
) resource
;
231 String name
= resource
.getName();
232 ITmfProjectModelElement experiment
= childrenMap
.get(name
);
233 if (experiment
== null) {
234 experiment
= new TmfExperimentElement(name
, expFolder
, tmfExperimentFolder
);
236 children
.add(experiment
);
237 childrenMap
.remove(name
);
238 getExperimentChildren((TmfExperimentElement
) experiment
);
241 } catch (CoreException e
) {
244 // Remove the leftovers (what was in the model but removed from the
246 cleanupModel(tmfExperimentFolder
, childrenMap
);
248 return children
.toArray();
251 private Object
[] getExperimentChildren(TmfExperimentElement tmfExperiment
) {
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
: tmfExperiment
.getChildren()) {
258 if (element
instanceof TmfTraceElement
) {
259 String name
= element
.getResource().getName();
260 childrenMap
.put(name
, element
);
264 IFolder folder
= tmfExperiment
.getResource();
266 IResource
[] members
= folder
.members();
267 for (IResource resource
: members
) {
268 String name
= resource
.getName();
269 ITmfProjectModelElement trace
= childrenMap
.get(name
);
271 trace
= new TmfTraceElement(name
, resource
, tmfExperiment
);
274 childrenMap
.remove(name
);
276 } catch (CoreException e
) {
279 // Remove the leftovers (what was in the model but removed from the
281 cleanupModel(tmfExperiment
, childrenMap
);
283 return children
.toArray();
286 private void cleanupModel(ITmfProjectModelElement parent
, Map
<String
, ITmfProjectModelElement
> danglingChildren
) {
287 if (parent
!= null) {
288 for (ITmfProjectModelElement child
: danglingChildren
.values()) {
289 Map
<String
, ITmfProjectModelElement
> grandChildren
= new HashMap
<String
, ITmfProjectModelElement
>();
290 for (ITmfProjectModelElement element
: child
.getChildren()) {
291 String name
= element
.getResource().getName();
292 grandChildren
.put(name
, element
);
294 cleanupModel(child
, grandChildren
);
295 parent
.removeChild(child
);
300 // ------------------------------------------------------------------------
301 // IPipelinedTreeContentProvider
302 // ------------------------------------------------------------------------
305 @SuppressWarnings({ "rawtypes", "unchecked" })
306 public void getPipelinedChildren(Object parent
, Set currentChildren
) {
307 customizeTmfElements(getChildren(parent
), currentChildren
);
311 @SuppressWarnings({ "rawtypes", "unchecked" })
312 public void getPipelinedElements(Object input
, Set currentElements
) {
313 customizeTmfElements(getElements(input
), currentElements
);
317 * Add/replace the ITmfProjectElement to the list of children
320 * the list returned by getChildren()
322 * the current children
324 private void customizeTmfElements(Object
[] elements
, Set
<Object
> children
) {
325 if (elements
!= null && children
!= null) {
326 for (Object element
: elements
) {
327 if (element
instanceof ITmfProjectModelElement
) {
328 ITmfProjectModelElement tmfElement
= (ITmfProjectModelElement
) element
;
329 IResource resource
= tmfElement
.getResource();
330 if (resource
!= null) {
331 children
.remove(resource
);
333 children
.add(element
);
335 else if (element
!= null) {
336 children
.add(element
);
343 public Object
getPipelinedParent(Object anObject
, Object aSuggestedParent
) {
344 return aSuggestedParent
;
348 public PipelinedShapeModification
interceptAdd(PipelinedShapeModification anAddModification
) {
349 return anAddModification
;
353 public PipelinedShapeModification
interceptRemove(PipelinedShapeModification aRemoveModification
) {
358 public boolean interceptRefresh(PipelinedViewerUpdate aRefreshSynchronization
) {
363 public boolean interceptUpdate(PipelinedViewerUpdate anUpdateSynchronization
) {