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>(); | |
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); | |
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); | |
196 | if (trace == null) { | |
197 | trace = new TmfTraceElement(name, resource, tmfTraceFolder); | |
198 | } | |
199 | children.add(trace); | |
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); | |
270 | if (trace == null) { | |
271 | trace = new TmfTraceElement(name, resource, tmfExperiment); | |
272 | } | |
273 | children.add(trace); | |
274 | childrenMap.remove(name); | |
275 | } | |
276 | } catch (CoreException e) { | |
277 | } | |
278 | ||
279 | // Remove the leftovers (what was in the model but removed from the | |
280 | // project) | |
281 | cleanupModel(tmfExperiment, childrenMap); | |
282 | ||
283 | return children.toArray(); | |
284 | } | |
285 | ||
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); | |
293 | } | |
294 | cleanupModel(child, grandChildren); | |
295 | parent.removeChild(child); | |
296 | } | |
297 | } | |
298 | } | |
299 | ||
300 | // ------------------------------------------------------------------------ | |
301 | // IPipelinedTreeContentProvider | |
302 | // ------------------------------------------------------------------------ | |
303 | ||
304 | @Override | |
305 | @SuppressWarnings({ "rawtypes", "unchecked" }) | |
306 | public void getPipelinedChildren(Object parent, Set currentChildren) { | |
307 | customizeTmfElements(getChildren(parent), currentChildren); | |
308 | } | |
309 | ||
310 | @Override | |
311 | @SuppressWarnings({ "rawtypes", "unchecked" }) | |
312 | public void getPipelinedElements(Object input, Set currentElements) { | |
313 | customizeTmfElements(getElements(input), currentElements); | |
314 | } | |
315 | ||
316 | /** | |
317 | * Add/replace the ITmfProjectElement to the list of children | |
318 | * | |
319 | * @param elements | |
320 | * the list returned by getChildren() | |
321 | * @param children | |
322 | * the current children | |
323 | */ | |
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); | |
332 | } | |
333 | children.add(element); | |
334 | } | |
335 | else if (element != null) { | |
336 | children.add(element); | |
337 | } | |
338 | } | |
339 | } | |
340 | } | |
341 | ||
342 | @Override | |
343 | public Object getPipelinedParent(Object anObject, Object aSuggestedParent) { | |
344 | return null; | |
345 | } | |
346 | ||
347 | @Override | |
348 | public PipelinedShapeModification interceptAdd(PipelinedShapeModification anAddModification) { | |
349 | return null; | |
350 | } | |
351 | ||
352 | @Override | |
353 | public PipelinedShapeModification interceptRemove(PipelinedShapeModification aRemoveModification) { | |
354 | return null; | |
355 | } | |
356 | ||
357 | @Override | |
358 | public boolean interceptRefresh(PipelinedViewerUpdate aRefreshSynchronization) { | |
359 | return false; | |
360 | } | |
361 | ||
362 | @Override | |
363 | public boolean interceptUpdate(PipelinedViewerUpdate anUpdateSynchronization) { | |
364 | return false; | |
365 | } | |
366 | ||
367 | } |