09509f4cef6b721d2ec033162ecae828a4c2367d
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / project / model / TmfExperimentElement.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 Ericsson, École Polytechnique de Montréal
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 * Geneviève Bastien - Copied code to add/remove traces in this class
12 * Patrick Tasse - Close editors to release resources
13 * Geneviève Bastien - Experiment instantiated with trace type
14 *******************************************************************************/
15
16 package org.eclipse.tracecompass.tmf.ui.project.model;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.eclipse.core.resources.IContainer;
27 import org.eclipse.core.resources.IFile;
28 import org.eclipse.core.resources.IFolder;
29 import org.eclipse.core.resources.IResource;
30 import org.eclipse.core.resources.IResourceProxy;
31 import org.eclipse.core.resources.IResourceProxyVisitor;
32 import org.eclipse.core.resources.IWorkspace;
33 import org.eclipse.core.resources.ResourcesPlugin;
34 import org.eclipse.core.runtime.CoreException;
35 import org.eclipse.core.runtime.IConfigurationElement;
36 import org.eclipse.core.runtime.IPath;
37 import org.eclipse.core.runtime.IStatus;
38 import org.eclipse.core.runtime.InvalidRegistryObjectException;
39 import org.eclipse.core.runtime.NullProgressMonitor;
40 import org.eclipse.core.runtime.Platform;
41 import org.eclipse.jdt.annotation.NonNull;
42 import org.eclipse.osgi.util.NLS;
43 import org.eclipse.swt.graphics.Image;
44 import org.eclipse.swt.widgets.Display;
45 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
46 import org.eclipse.tracecompass.internal.tmf.ui.editors.ITmfEventsEditorConstants;
47 import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
48 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleHelper;
49 import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager;
50 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
51 import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
52 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
53 import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
54 import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
55 import org.eclipse.tracecompass.tmf.ui.properties.ReadOnlyTextPropertyDescriptor;
56 import org.eclipse.ui.views.properties.IPropertyDescriptor;
57 import org.eclipse.ui.views.properties.IPropertySource2;
58
59 /**
60 * Implementation of TMF Experiment Model Element.
61 * <p>
62 *
63 * @version 1.0
64 * @author Francois Chouinard
65 *
66 */
67 public class TmfExperimentElement extends TmfCommonProjectElement implements IPropertySource2 {
68
69 // ------------------------------------------------------------------------
70 // Constants
71 // ------------------------------------------------------------------------
72
73 // Property View stuff
74 private static final String INFO_CATEGORY = "Info"; //$NON-NLS-1$
75 private static final String NAME = "name"; //$NON-NLS-1$
76 private static final String PATH = "path"; //$NON-NLS-1$
77 private static final String LOCATION = "location"; //$NON-NLS-1$
78 private static final String FOLDER_SUFFIX = "_exp"; //$NON-NLS-1$
79 private static final String EXPERIMENT_TYPE = "type"; //$NON-NLS-1$
80
81 private static final ReadOnlyTextPropertyDescriptor NAME_DESCRIPTOR = new ReadOnlyTextPropertyDescriptor(NAME, NAME);
82 private static final ReadOnlyTextPropertyDescriptor PATH_DESCRIPTOR = new ReadOnlyTextPropertyDescriptor(PATH, PATH);
83 private static final ReadOnlyTextPropertyDescriptor LOCATION_DESCRIPTOR = new ReadOnlyTextPropertyDescriptor(LOCATION,
84 LOCATION);
85 private static final ReadOnlyTextPropertyDescriptor TYPE_DESCRIPTOR = new ReadOnlyTextPropertyDescriptor(EXPERIMENT_TYPE, EXPERIMENT_TYPE);
86
87 private static final IPropertyDescriptor[] DESCRIPTORS = { NAME_DESCRIPTOR, PATH_DESCRIPTOR,
88 LOCATION_DESCRIPTOR, TYPE_DESCRIPTOR };
89
90 static {
91 NAME_DESCRIPTOR.setCategory(INFO_CATEGORY);
92 PATH_DESCRIPTOR.setCategory(INFO_CATEGORY);
93 LOCATION_DESCRIPTOR.setCategory(INFO_CATEGORY);
94 TYPE_DESCRIPTOR.setCategory(INFO_CATEGORY);
95 }
96
97 // The mapping of available trace type IDs to their corresponding
98 // configuration element
99 private static final Map<String, IConfigurationElement> TRACE_TYPE_ATTRIBUTES = new HashMap<>();
100 private static final Map<String, IConfigurationElement> TRACE_TYPE_UI_ATTRIBUTES = new HashMap<>();
101 private static final Map<String, IConfigurationElement> TRACE_CATEGORIES = new HashMap<>();
102
103 // ------------------------------------------------------------------------
104 // Static initialization
105 // ------------------------------------------------------------------------
106
107 /**
108 * Initialize statically at startup by getting extensions from the platform
109 * extension registry.
110 */
111 public static void init() {
112 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
113 for (IConfigurationElement ce : config) {
114 String elementName = ce.getName();
115 if (elementName.equals(TmfTraceType.EXPERIMENT_ELEM)) {
116 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
117 TRACE_TYPE_ATTRIBUTES.put(traceTypeId, ce);
118 } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
119 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
120 TRACE_CATEGORIES.put(categoryId, ce);
121 }
122 }
123
124 /*
125 * Read the corresponding tmf.ui "tracetypeui" extension point for this
126 * trace type, if it exists.
127 */
128 config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceTypeUIUtils.TMF_TRACE_TYPE_UI_ID);
129 for (IConfigurationElement ce : config) {
130 String elemName = ce.getName();
131 if (TmfTraceTypeUIUtils.EXPERIMENT_ELEM.equals(elemName)) {
132 String traceType = ce.getAttribute(TmfTraceTypeUIUtils.TRACETYPE_ATTR);
133 TRACE_TYPE_UI_ATTRIBUTES.put(traceType, ce);
134 }
135 }
136 }
137
138 // ------------------------------------------------------------------------
139 // Constructors
140 // ------------------------------------------------------------------------
141 /**
142 * Constructor
143 *
144 * @param name
145 * The name of the experiment
146 * @param folder
147 * The folder reference
148 * @param parent
149 * The experiment folder reference.
150 */
151 public TmfExperimentElement(String name, IFolder folder, TmfExperimentFolder parent) {
152 super(name, folder, parent);
153 }
154
155 // ------------------------------------------------------------------------
156 // TmfProjectModelElement
157 // ------------------------------------------------------------------------
158
159 @Override
160 public IFolder getResource() {
161 return (IFolder) super.getResource();
162 }
163
164 /**
165 * @since 2.0
166 */
167 @Override
168 protected void refreshChildren() {
169 IFolder folder = getResource();
170
171 /* Update the trace children of this experiment */
172 // Get the children from the model
173 Map<String, ITmfProjectModelElement> childrenMap = new HashMap<>();
174 for (TmfTraceElement trace : getTraces()) {
175 childrenMap.put(trace.getElementPath(), trace);
176 }
177
178 List<IResource> members = getTraceResources();
179 for (IResource resource : members) {
180 String name = resource.getName();
181 String elementPath = resource.getFullPath().makeRelativeTo(folder.getFullPath()).toString();
182 ITmfProjectModelElement element = childrenMap.get(elementPath);
183 if (element instanceof TmfTraceElement) {
184 childrenMap.remove(elementPath);
185 } else {
186 element = new TmfTraceElement(name, resource, this);
187 addChild(element);
188 }
189 }
190
191 // Cleanup dangling children from the model
192 for (ITmfProjectModelElement danglingChild : childrenMap.values()) {
193 removeChild(danglingChild);
194 }
195
196 /* Update the analysis under this experiment */
197 super.refreshChildren();
198
199 /*
200 * If the experiment is opened, add any analysis that was not added by
201 * the parent if it is available with the experiment
202 */
203 ITmfTrace experiment = getTrace();
204 if (experiment == null) {
205 return;
206 }
207 Map<String, TmfAnalysisElement> analysisMap = new HashMap<>();
208 for (TmfAnalysisElement analysis : getAvailableAnalysis()) {
209 analysisMap.put(analysis.getAnalysisId(), analysis);
210 }
211 for (IAnalysisModuleHelper module : TmfAnalysisManager.getAnalysisModules().values()) {
212 if (!analysisMap.containsKey(module.getId()) && module.appliesToExperiment() && (experiment.getAnalysisModule(module.getId()) != null)) {
213 IFolder newresource = ResourcesPlugin.getWorkspace().getRoot().getFolder(getResource().getFullPath().append(module.getId()));
214 TmfAnalysisElement analysis = new TmfAnalysisElement(module.getName(), newresource, this, module);
215 addChild(analysis);
216 analysis.refreshChildren();
217 analysisMap.put(module.getId(), analysis);
218 }
219 }
220 }
221
222 private List<IResource> getTraceResources() {
223 IFolder folder = getResource();
224 final List<IResource> list = new ArrayList<>();
225 try {
226 folder.accept(new IResourceProxyVisitor() {
227 @Override
228 public boolean visit(IResourceProxy resource) throws CoreException {
229 if (resource.isLinked()) {
230 list.add(resource.requestResource());
231 }
232 return true;
233 }
234 }, IResource.NONE);
235 } catch (CoreException e) {
236 }
237 Comparator<IResource> comparator = new Comparator<IResource>() {
238 @Override
239 public int compare(IResource o1, IResource o2) {
240 return o1.getFullPath().toString().compareTo(o2.getFullPath().toString());
241 }
242 };
243 Collections.sort(list, comparator);
244 return list;
245 }
246
247 /**
248 * @since 2.0
249 */
250 @Override
251 public @NonNull Image getIcon() {
252 Image icon = super.getIcon();
253 return (icon == null ? TmfProjectModelIcons.DEFAULT_EXPERIMENT_ICON : icon);
254 }
255
256 /**
257 * @since 2.0
258 */
259 @Override
260 public String getLabelText() {
261 return getName() + " [" + getTraces().size() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
262 }
263
264 // ------------------------------------------------------------------------
265 // Operations
266 // ------------------------------------------------------------------------
267
268 /**
269 * Refreshes the trace type filed by reading the trace type persistent
270 * property of the resource reference.
271 *
272 * If trace type is null after refresh, set it to the generic trace type
273 * (for seamless upgrade)
274 */
275 @Override
276 public void refreshTraceType() {
277 super.refreshTraceType();
278 if (getTraceType() == null) {
279 IConfigurationElement ce = TmfTraceType.getTraceAttributes(TmfTraceType.DEFAULT_EXPERIMENT_TYPE);
280 if (ce != null) {
281 try {
282 IFolder experimentFolder = getResource();
283 experimentFolder.setPersistentProperty(TmfCommonConstants.TRACETYPE, ce.getAttribute(TmfTraceType.ID_ATTR));
284 super.refreshTraceType();
285 } catch (InvalidRegistryObjectException | CoreException e) {
286 }
287 }
288 }
289 }
290
291 /**
292 * Returns a list of TmfTraceElements contained in this experiment.
293 *
294 * @return a list of TmfTraceElements
295 */
296 @Override
297 public List<TmfTraceElement> getTraces() {
298 List<ITmfProjectModelElement> children = getChildren();
299 List<TmfTraceElement> traces = new ArrayList<>();
300 for (ITmfProjectModelElement child : children) {
301 if (child instanceof TmfTraceElement) {
302 traces.add((TmfTraceElement) child);
303 }
304 }
305 return traces;
306 }
307
308 /**
309 * Adds a trace to the experiment
310 *
311 * @param trace
312 * The trace element to add
313 */
314 public void addTrace(TmfTraceElement trace) {
315 addTrace(trace, true);
316 }
317
318 /**
319 * Adds a trace to the experiment
320 *
321 * @param trace
322 * The trace element to add
323 * @param refresh
324 * Flag for refreshing the project
325 */
326 public void addTrace(TmfTraceElement trace, boolean refresh) {
327 /**
328 * Create a link to the actual trace and set the trace type
329 */
330 IFolder experiment = getResource();
331 IResource resource = trace.getResource();
332 IPath location = resource.getLocation();
333 IWorkspace workspace = ResourcesPlugin.getWorkspace();
334 try {
335 String traceTypeId = TmfTraceType.getTraceTypeId(trace.getResource());
336 TraceTypeHelper traceType = TmfTraceType.getTraceType(traceTypeId);
337
338 if (resource instanceof IFolder) {
339 IFolder folder = experiment.getFolder(trace.getElementPath());
340 TraceUtils.createFolder((IFolder) folder.getParent(), new NullProgressMonitor());
341 IStatus result = workspace.validateLinkLocation(folder, location);
342 if (result.isOK() || result.matches(IStatus.INFO | IStatus.WARNING)) {
343 folder.createLink(location, IResource.REPLACE, null);
344 if (traceType != null) {
345 TmfTraceTypeUIUtils.setTraceType(folder, traceType, refresh);
346 }
347
348 } else {
349 Activator.getDefault().logError("Error creating link. Invalid trace location " + location); //$NON-NLS-1$
350 }
351 } else {
352 IFile file = experiment.getFile(trace.getElementPath());
353 TraceUtils.createFolder((IFolder) file.getParent(), new NullProgressMonitor());
354 IStatus result = workspace.validateLinkLocation(file, location);
355 if (result.isOK() || result.matches(IStatus.INFO | IStatus.WARNING)) {
356 file.createLink(location, IResource.REPLACE, null);
357 if (traceType != null) {
358 TmfTraceTypeUIUtils.setTraceType(file, traceType, refresh);
359 }
360 } else {
361 Activator.getDefault().logError("Error creating link. Invalid trace location " + location); //$NON-NLS-1$
362 }
363 }
364 } catch (CoreException e) {
365 Activator.getDefault().logError("Error creating link to location " + location, e); //$NON-NLS-1$
366 }
367
368 }
369
370 /**
371 * Removes a trace from an experiment
372 *
373 * @param trace
374 * The trace to remove
375 * @throws CoreException
376 * exception
377 */
378 public void removeTrace(TmfTraceElement trace) throws CoreException {
379
380 // Close editors in UI Thread
381 Display.getDefault().syncExec(new Runnable() {
382 @Override
383 public void run() {
384 closeEditors();
385 }
386 });
387
388 /* Finally, remove the trace from experiment */
389 removeChild(trace);
390 deleteTraceResource(trace.getResource());
391 deleteSupplementaryResources();
392 }
393
394 private void deleteTraceResource(IResource resource) throws CoreException {
395 resource.delete(true, null);
396 IContainer parent = resource.getParent();
397 // delete empty folders up to the parent experiment folder
398 if (!parent.equals(getResource()) && parent.members().length == 0) {
399 deleteTraceResource(parent);
400 }
401 }
402
403 @Override
404 public IFile createBookmarksFile() throws CoreException {
405 return createBookmarksFile(getProject().getExperimentsFolder().getResource(), ITmfEventsEditorConstants.EXPERIMENT_EDITOR_INPUT_TYPE);
406 }
407
408 @Override
409 public String getEditorId() {
410 /* See if a default editor was set for this experiment type */
411 if (getTraceType() != null) {
412 IConfigurationElement ce = TRACE_TYPE_UI_ATTRIBUTES.get(getTraceType());
413 if (ce != null) {
414 IConfigurationElement[] defaultEditorCE = ce.getChildren(TmfTraceTypeUIUtils.DEFAULT_EDITOR_ELEM);
415 if (defaultEditorCE.length == 1) {
416 return defaultEditorCE[0].getAttribute(TmfTraceType.ID_ATTR);
417 }
418 }
419 }
420
421 /* No default editor, try to find a common editor for all traces */
422 final List<TmfTraceElement> traceEntries = getTraces();
423 String commonEditorId = null;
424
425 for (TmfTraceElement element : traceEntries) {
426 // If all traces use the same editorId, use it, otherwise use the
427 // default
428 final String editorId = element.getEditorId();
429 if (commonEditorId == null) {
430 commonEditorId = (editorId != null) ? editorId : TmfEventsEditor.ID;
431 } else if (!commonEditorId.equals(editorId)) {
432 commonEditorId = TmfEventsEditor.ID;
433 }
434 }
435 return null;
436 }
437
438 /**
439 * Instantiate a {@link TmfExperiment} object based on the experiment type
440 * and the corresponding extension.
441 *
442 * @return the {@link TmfExperiment} or <code>null</code> for an error
443 */
444 @Override
445 public TmfExperiment instantiateTrace() {
446 try {
447
448 // make sure that supplementary folder exists
449 refreshSupplementaryFolder();
450
451 if (getTraceType() != null) {
452
453 IConfigurationElement ce = TRACE_TYPE_ATTRIBUTES.get(getTraceType());
454 if (ce == null) {
455 return null;
456 }
457 TmfExperiment experiment = (TmfExperiment) ce.createExecutableExtension(TmfTraceType.EXPERIMENT_TYPE_ATTR);
458 return experiment;
459 }
460 } catch (CoreException e) {
461 Activator.getDefault().logError(NLS.bind(Messages.TmfExperimentElement_ErrorInstantiatingTrace, getName()), e);
462 }
463 return null;
464 }
465
466 @Override
467 public String getTypeName() {
468 return Messages.TmfExperimentElement_TypeName;
469 }
470
471 // ------------------------------------------------------------------------
472 // IPropertySource2
473 // ------------------------------------------------------------------------
474
475 @Override
476 public Object getEditableValue() {
477 return null;
478 }
479
480 @Override
481 public IPropertyDescriptor[] getPropertyDescriptors() {
482 return Arrays.copyOf(DESCRIPTORS, DESCRIPTORS.length);
483 }
484
485 @Override
486 public Object getPropertyValue(Object id) {
487
488 if (NAME.equals(id)) {
489 return getName();
490 }
491
492 if (PATH.equals(id)) {
493 return getPath().toString();
494 }
495
496 if (LOCATION.equals(id)) {
497 return getLocation().toString();
498 }
499
500 if (EXPERIMENT_TYPE.equals(id)) {
501 if (getTraceType() != null) {
502 IConfigurationElement ce = TRACE_TYPE_ATTRIBUTES.get(getTraceType());
503 if (ce == null) {
504 return ""; //$NON-NLS-1$
505 }
506 String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
507 if (categoryId != null) {
508 IConfigurationElement category = TRACE_CATEGORIES.get(categoryId);
509 if (category != null) {
510 return category.getAttribute(TmfTraceType.NAME_ATTR) + ':' + ce.getAttribute(TmfTraceType.NAME_ATTR);
511 }
512 }
513 return ce.getAttribute(TmfTraceType.NAME_ATTR);
514 }
515 }
516
517 return null;
518 }
519
520 @Override
521 public void resetPropertyValue(Object id) {
522 }
523
524 @Override
525 public void setPropertyValue(Object id, Object value) {
526 }
527
528 @Override
529 public boolean isPropertyResettable(Object id) {
530 return false;
531 }
532
533 @Override
534 public boolean isPropertySet(Object id) {
535 return false;
536 }
537
538 /**
539 * Return the suffix for resource names
540 *
541 * @return The folder suffix
542 */
543 @Override
544 public String getSuffix() {
545 return FOLDER_SUFFIX;
546 }
547
548 }
This page took 0.044733 seconds and 4 git commands to generate.