Commit | Line | Data |
---|---|---|
12c155f5 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2010, 2011 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.project.model; | |
14 | ||
15 | import java.util.ArrayList; | |
16 | import java.util.HashMap; | |
17 | import java.util.List; | |
18 | import java.util.Map; | |
19 | import java.util.Set; | |
20 | ||
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; | |
32 | ||
33 | /** | |
34 | * <b><u>TmfNavigatorContentProvider</u></b> | |
35 | * <p> | |
36 | */ | |
37 | public class TmfNavigatorContentProvider implements ICommonContentProvider, IPipelinedTreeContentProvider { | |
38 | ||
39 | // ------------------------------------------------------------------------ | |
40 | // ICommonContentProvider | |
41 | // ------------------------------------------------------------------------ | |
42 | ||
43 | @Override | |
44 | public Object[] getElements(Object inputElement) { | |
45 | return null; | |
46 | } | |
47 | ||
48 | @Override | |
49 | public Object getParent(Object element) { | |
50 | return null; | |
51 | } | |
52 | ||
53 | @Override | |
54 | public boolean hasChildren(Object element) { | |
55 | if (element instanceof IProject) { | |
56 | IProject project = (IProject) element; | |
57 | return project.isAccessible(); | |
58 | } | |
59 | if (element instanceof TmfTraceFolder) { | |
60 | TmfTraceFolder folder = (TmfTraceFolder) element; | |
61 | return folder.hasChildren(); | |
62 | } | |
63 | if (element instanceof TmfExperimentFolder) { | |
64 | TmfExperimentFolder folder = (TmfExperimentFolder) element; | |
65 | return folder.hasChildren(); | |
66 | } | |
67 | if (element instanceof TmfExperimentElement) { | |
68 | TmfExperimentElement folder = (TmfExperimentElement) element; | |
69 | return folder.hasChildren(); | |
70 | } | |
71 | return false; | |
72 | } | |
73 | ||
74 | @Override | |
75 | public void dispose() { | |
76 | } | |
77 | ||
78 | @Override | |
79 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { | |
80 | } | |
81 | ||
82 | @Override | |
83 | public void restoreState(IMemento aMemento) { | |
84 | } | |
85 | ||
86 | @Override | |
87 | public void saveState(IMemento aMemento) { | |
88 | } | |
89 | ||
90 | @Override | |
91 | public void init(ICommonContentExtensionSite aConfig) { | |
92 | } | |
93 | ||
94 | // ------------------------------------------------------------------------ | |
95 | // ICommonContentProvider - getChildren() | |
96 | // ------------------------------------------------------------------------ | |
97 | ||
98 | @Override | |
99 | public synchronized Object[] getChildren(Object parentElement) { | |
100 | ||
101 | // Tracing project level | |
102 | if (parentElement instanceof IProject) { | |
103 | return getProjectChildren((IProject) parentElement); | |
104 | } | |
105 | ||
106 | // Traces "folder" level | |
107 | if (parentElement instanceof TmfTraceFolder) { | |
108 | return getTraceFolderChildren((TmfTraceFolder) parentElement); | |
109 | } | |
110 | ||
111 | // Experiments "folder" level | |
112 | if (parentElement instanceof TmfExperimentFolder) { | |
113 | return getExperimentFolderChildren((TmfExperimentFolder) parentElement); | |
114 | } | |
115 | ||
116 | // Experiment | |
117 | if (parentElement instanceof TmfExperimentElement) { | |
118 | return getExperimentChildren((TmfExperimentElement) parentElement); | |
119 | } | |
120 | ||
121 | return new Object[0]; | |
122 | } | |
123 | ||
124 | private Object[] getProjectChildren(IProject project) { | |
125 | // The children structure | |
126 | List<Object> children = new ArrayList<Object>(); | |
127 | ||
128 | // Get the children from the model | |
129 | Map<String, ITmfProjectModelElement> childrenMap = new HashMap<String, ITmfProjectModelElement>(); | |
828e5592 | 130 | TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, true); |
12c155f5 FC |
131 | for (ITmfProjectModelElement element : tmfProject.getChildren()) { |
132 | if (element instanceof TmfTraceFolder) { | |
133 | TmfTraceFolder child = (TmfTraceFolder) element; | |
134 | childrenMap.put(child.getResource().getName(), child); | |
135 | } | |
136 | if (element instanceof TmfExperimentFolder) { | |
137 | TmfExperimentFolder child = (TmfExperimentFolder) element; | |
138 | childrenMap.put(child.getResource().getName(), child); | |
139 | } | |
140 | } | |
141 | ||
142 | // Add the model folder if the corresponding resource exists and is not | |
143 | // accounted for | |
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); | |
150 | } | |
151 | children.add(element); | |
152 | childrenMap.remove(name); | |
153 | getTraceFolderChildren((TmfTraceFolder) element); | |
154 | } | |
155 | ||
156 | // Add the model folder if the corresponding resource exists and is not | |
157 | // accounted for | |
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); | |
164 | } | |
165 | children.add(element); | |
166 | childrenMap.remove(name); | |
167 | getExperimentFolderChildren((TmfExperimentFolder) element); | |
168 | } | |
169 | ||
170 | // Remove the leftovers (what was in the model but removed from the | |
171 | // project) | |
172 | cleanupModel(tmfProject, childrenMap); | |
173 | ||
174 | return children.toArray(); | |
175 | } | |
176 | ||
177 | private Object[] getTraceFolderChildren(TmfTraceFolder tmfTraceFolder) { | |
178 | // The children structure | |
179 | List<Object> children = new ArrayList<Object>(); | |
180 | ||
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); | |
187 | } | |
188 | } | |
189 | ||
190 | IFolder folder = tmfTraceFolder.getResource(); | |
191 | try { | |
192 | IResource[] members = folder.members(); | |
193 | for (IResource resource : members) { | |
194 | String name = resource.getName(); | |
195 | ITmfProjectModelElement trace = childrenMap.get(name); | |
e12ecd30 BH |
196 | if (trace == null) { |
197 | trace = new TmfTraceElement(name, resource, tmfTraceFolder); | |
12c155f5 | 198 | } |
e12ecd30 | 199 | children.add(trace); |
12c155f5 FC |
200 | childrenMap.remove(name); |
201 | } | |
202 | } catch (CoreException e) { | |
203 | } | |
204 | ||
205 | // Remove the leftovers (what was in the model but removed from the | |
206 | // project) | |
207 | cleanupModel(tmfTraceFolder, childrenMap); | |
208 | ||
209 | return children.toArray(); | |
210 | } | |
211 | ||
212 | private Object[] getExperimentFolderChildren(TmfExperimentFolder tmfExperimentFolder) { | |
213 | // The children structure | |
214 | List<Object> children = new ArrayList<Object>(); | |
215 | ||
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); | |
222 | } | |
223 | } | |
224 | ||
225 | IFolder folder = tmfExperimentFolder.getResource(); | |
226 | try { | |
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); | |
235 | } | |
236 | children.add(experiment); | |
237 | childrenMap.remove(name); | |
238 | getExperimentChildren((TmfExperimentElement) experiment); | |
239 | } | |
240 | } | |
241 | } catch (CoreException e) { | |
242 | } | |
243 | ||
244 | // Remove the leftovers (what was in the model but removed from the | |
245 | // project) | |
246 | cleanupModel(tmfExperimentFolder, childrenMap); | |
247 | ||
248 | return children.toArray(); | |
249 | } | |
250 | ||
251 | private Object[] getExperimentChildren(TmfExperimentElement tmfExperiment) { | |
252 | // The children structure | |
253 | List<Object> children = new ArrayList<Object>(); | |
254 | ||
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); | |
261 | } | |
262 | } | |
263 | ||
264 | IFolder folder = tmfExperiment.getResource(); | |
265 | try { | |
266 | IResource[] members = folder.members(); | |
267 | for (IResource resource : members) { | |
268 | String name = resource.getName(); | |
269 | ITmfProjectModelElement trace = childrenMap.get(name); | |
828e5592 PT |
270 | if (trace == null && !resource.isHidden()) { |
271 | // exclude hidden resources (e.g. bookmarks file) | |
12c155f5 FC |
272 | trace = new TmfTraceElement(name, resource, tmfExperiment); |
273 | } | |
274 | children.add(trace); | |
275 | childrenMap.remove(name); | |
276 | } | |
277 | } catch (CoreException e) { | |
278 | } | |
279 | ||
280 | // Remove the leftovers (what was in the model but removed from the | |
281 | // project) | |
282 | cleanupModel(tmfExperiment, childrenMap); | |
283 | ||
284 | return children.toArray(); | |
285 | } | |
286 | ||
287 | private void cleanupModel(ITmfProjectModelElement parent, Map<String, ITmfProjectModelElement> danglingChildren) { | |
288 | if (parent != null) { | |
289 | for (ITmfProjectModelElement child : danglingChildren.values()) { | |
290 | Map<String, ITmfProjectModelElement> grandChildren = new HashMap<String, ITmfProjectModelElement>(); | |
291 | for (ITmfProjectModelElement element : child.getChildren()) { | |
292 | String name = element.getResource().getName(); | |
293 | grandChildren.put(name, element); | |
294 | } | |
295 | cleanupModel(child, grandChildren); | |
296 | parent.removeChild(child); | |
297 | } | |
298 | } | |
299 | } | |
300 | ||
301 | // ------------------------------------------------------------------------ | |
302 | // IPipelinedTreeContentProvider | |
303 | // ------------------------------------------------------------------------ | |
304 | ||
305 | @Override | |
306 | @SuppressWarnings({ "rawtypes", "unchecked" }) | |
307 | public void getPipelinedChildren(Object parent, Set currentChildren) { | |
308 | customizeTmfElements(getChildren(parent), currentChildren); | |
309 | } | |
310 | ||
311 | @Override | |
312 | @SuppressWarnings({ "rawtypes", "unchecked" }) | |
313 | public void getPipelinedElements(Object input, Set currentElements) { | |
314 | customizeTmfElements(getElements(input), currentElements); | |
315 | } | |
316 | ||
317 | /** | |
318 | * Add/replace the ITmfProjectElement to the list of children | |
319 | * | |
320 | * @param elements | |
321 | * the list returned by getChildren() | |
322 | * @param children | |
323 | * the current children | |
324 | */ | |
325 | private void customizeTmfElements(Object[] elements, Set<Object> children) { | |
326 | if (elements != null && children != null) { | |
327 | for (Object element : elements) { | |
328 | if (element instanceof ITmfProjectModelElement) { | |
329 | ITmfProjectModelElement tmfElement = (ITmfProjectModelElement) element; | |
330 | IResource resource = tmfElement.getResource(); | |
331 | if (resource != null) { | |
332 | children.remove(resource); | |
333 | } | |
334 | children.add(element); | |
335 | } | |
336 | else if (element != null) { | |
337 | children.add(element); | |
338 | } | |
339 | } | |
340 | } | |
341 | } | |
342 | ||
343 | @Override | |
344 | public Object getPipelinedParent(Object anObject, Object aSuggestedParent) { | |
bfa12ff0 | 345 | return aSuggestedParent; |
12c155f5 FC |
346 | } |
347 | ||
348 | @Override | |
349 | public PipelinedShapeModification interceptAdd(PipelinedShapeModification anAddModification) { | |
bfa12ff0 | 350 | return anAddModification; |
12c155f5 FC |
351 | } |
352 | ||
353 | @Override | |
354 | public PipelinedShapeModification interceptRemove(PipelinedShapeModification aRemoveModification) { | |
355 | return null; | |
356 | } | |
357 | ||
358 | @Override | |
359 | public boolean interceptRefresh(PipelinedViewerUpdate aRefreshSynchronization) { | |
360 | return false; | |
361 | } | |
362 | ||
363 | @Override | |
364 | public boolean interceptUpdate(PipelinedViewerUpdate anUpdateSynchronization) { | |
365 | return false; | |
366 | } | |
367 | ||
368 | } |