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
, true);
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
);
196 if (!name
.startsWith(TmfTraceElement
.SUPPLEMENATARY_FILES_PREFIX
)) {
198 trace
= new TmfTraceElement(name
, resource
, tmfTraceFolder
);
202 childrenMap
.remove(name
);
204 } catch (CoreException e
) {
207 // Remove the leftovers (what was in the model but removed from the
209 cleanupModel(tmfTraceFolder
, childrenMap
);
211 return children
.toArray();
214 private Object
[] getExperimentFolderChildren(TmfExperimentFolder tmfExperimentFolder
) {
215 // The children structure
216 List
<Object
> children
= new ArrayList
<Object
>();
218 // Get the children from the model
219 Map
<String
, ITmfProjectModelElement
> childrenMap
= new HashMap
<String
, ITmfProjectModelElement
>();
220 for (ITmfProjectModelElement element
: tmfExperimentFolder
.getChildren()) {
221 if (element
instanceof TmfExperimentElement
) {
222 String name
= element
.getResource().getName();
223 childrenMap
.put(name
, element
);
227 IFolder folder
= tmfExperimentFolder
.getResource();
229 IResource
[] members
= folder
.members();
230 for (IResource resource
: members
) {
231 if (resource
instanceof IFolder
) {
232 IFolder expFolder
= (IFolder
) resource
;
233 String name
= resource
.getName();
234 ITmfProjectModelElement experiment
= childrenMap
.get(name
);
235 if (experiment
== null) {
236 experiment
= new TmfExperimentElement(name
, expFolder
, tmfExperimentFolder
);
238 children
.add(experiment
);
239 childrenMap
.remove(name
);
240 getExperimentChildren((TmfExperimentElement
) experiment
);
243 } catch (CoreException e
) {
246 // Remove the leftovers (what was in the model but removed from the
248 cleanupModel(tmfExperimentFolder
, childrenMap
);
250 return children
.toArray();
253 private Object
[] getExperimentChildren(TmfExperimentElement tmfExperiment
) {
254 // The children structure
255 List
<Object
> children
= new ArrayList
<Object
>();
257 // Get the children from the model
258 Map
<String
, ITmfProjectModelElement
> childrenMap
= new HashMap
<String
, ITmfProjectModelElement
>();
259 for (ITmfProjectModelElement element
: tmfExperiment
.getChildren()) {
260 if (element
instanceof TmfTraceElement
) {
261 String name
= element
.getResource().getName();
262 childrenMap
.put(name
, element
);
266 IFolder folder
= tmfExperiment
.getResource();
268 IResource
[] members
= folder
.members();
269 for (IResource resource
: members
) {
270 String name
= resource
.getName();
271 ITmfProjectModelElement trace
= childrenMap
.get(name
);
272 if (trace
== null && !resource
.isHidden()) {
273 // exclude hidden resources (e.g. bookmarks file)
274 trace
= new TmfTraceElement(name
, resource
, tmfExperiment
);
277 childrenMap
.remove(name
);
279 } catch (CoreException e
) {
282 // Remove the leftovers (what was in the model but removed from the
284 cleanupModel(tmfExperiment
, childrenMap
);
286 return children
.toArray();
289 private void cleanupModel(ITmfProjectModelElement parent
, Map
<String
, ITmfProjectModelElement
> danglingChildren
) {
290 if (parent
!= null) {
291 for (ITmfProjectModelElement child
: danglingChildren
.values()) {
292 Map
<String
, ITmfProjectModelElement
> grandChildren
= new HashMap
<String
, ITmfProjectModelElement
>();
293 for (ITmfProjectModelElement element
: child
.getChildren()) {
294 String name
= element
.getResource().getName();
295 grandChildren
.put(name
, element
);
297 cleanupModel(child
, grandChildren
);
298 parent
.removeChild(child
);
303 // ------------------------------------------------------------------------
304 // IPipelinedTreeContentProvider
305 // ------------------------------------------------------------------------
308 @SuppressWarnings({ "rawtypes", "unchecked" })
309 public void getPipelinedChildren(Object parent
, Set currentChildren
) {
310 customizeTmfElements(getChildren(parent
), currentChildren
);
314 @SuppressWarnings({ "rawtypes", "unchecked" })
315 public void getPipelinedElements(Object input
, Set currentElements
) {
316 customizeTmfElements(getElements(input
), currentElements
);
320 * Add/replace the ITmfProjectElement to the list of children
323 * the list returned by getChildren()
325 * the current children
327 private void customizeTmfElements(Object
[] elements
, Set
<Object
> children
) {
328 if (elements
!= null && children
!= null) {
329 for (Object element
: elements
) {
330 if (element
instanceof ITmfProjectModelElement
) {
331 ITmfProjectModelElement tmfElement
= (ITmfProjectModelElement
) element
;
332 IResource resource
= tmfElement
.getResource();
333 if (resource
!= null) {
334 children
.remove(resource
);
336 children
.add(element
);
338 else if (element
!= null) {
339 children
.add(element
);
346 public Object
getPipelinedParent(Object anObject
, Object aSuggestedParent
) {
347 return aSuggestedParent
;
351 public PipelinedShapeModification
interceptAdd(PipelinedShapeModification anAddModification
) {
352 return anAddModification
;
356 public PipelinedShapeModification
interceptRemove(PipelinedShapeModification aRemoveModification
) {
361 public boolean interceptRefresh(PipelinedViewerUpdate aRefreshSynchronization
) {
366 public boolean interceptUpdate(PipelinedViewerUpdate anUpdateSynchronization
) {