TMF: Add an interface for strings regarding a session configuration
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfExperimentElement.java
CommitLineData
12c155f5 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2010, 2014 Ericsson, École Polytechnique de Montréal
c4c81d91 3 *
12c155f5
FC
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
c4c81d91 8 *
12c155f5
FC
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
beb19106 11 * Geneviève Bastien - Copied code to add/remove traces in this class
a72a6830 12 * Patrick Tasse - Close editors to release resources
8f5221c2 13 * Geneviève Bastien - Experiment instantiated with trace type
12c155f5
FC
14 *******************************************************************************/
15
16package org.eclipse.linuxtools.tmf.ui.project.model;
17
18import java.util.ArrayList;
5a5c2fc7 19import java.util.Arrays;
f537c959 20import java.util.HashMap;
12c155f5 21import java.util.List;
beb19106 22import java.util.Map;
12c155f5 23
c4c81d91 24import org.eclipse.core.resources.IFile;
12c155f5 25import org.eclipse.core.resources.IFolder;
c4c81d91 26import org.eclipse.core.resources.IResource;
beb19106
GB
27import org.eclipse.core.resources.IWorkspace;
28import org.eclipse.core.resources.ResourcesPlugin;
c4c81d91 29import org.eclipse.core.runtime.CoreException;
8f5221c2 30import org.eclipse.core.runtime.IConfigurationElement;
beb19106 31import org.eclipse.core.runtime.IPath;
8f5221c2
GB
32import org.eclipse.core.runtime.InvalidRegistryObjectException;
33import org.eclipse.core.runtime.Platform;
beb19106
GB
34import org.eclipse.core.runtime.QualifiedName;
35import org.eclipse.linuxtools.internal.tmf.ui.Activator;
c4c81d91 36import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
8f5221c2 37import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType;
c4c81d91 38import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
8f5221c2 39import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
080600d9 40import org.eclipse.linuxtools.tmf.ui.properties.ReadOnlyTextPropertyDescriptor;
8f5221c2 41import org.eclipse.osgi.util.NLS;
12c155f5
FC
42import org.eclipse.ui.views.properties.IPropertyDescriptor;
43import org.eclipse.ui.views.properties.IPropertySource2;
12c155f5
FC
44
45/**
b544077e 46 * Implementation of TMF Experiment Model Element.
12c155f5 47 * <p>
b544077e
BH
48 * @version 1.0
49 * @author Francois Chouinard
c4c81d91 50 *
12c155f5 51 */
16036bc2 52public class TmfExperimentElement extends TmfCommonProjectElement implements IPropertySource2 {
12c155f5
FC
53
54 // ------------------------------------------------------------------------
55 // Constants
56 // ------------------------------------------------------------------------
57
58 // Property View stuff
59 private static final String sfInfoCategory = "Info"; //$NON-NLS-1$
60 private static final String sfName = "name"; //$NON-NLS-1$
61 private static final String sfPath = "path"; //$NON-NLS-1$
62 private static final String sfLocation = "location"; //$NON-NLS-1$
99504bb8 63 private static final String sfFolderSuffix = "_exp"; //$NON-NLS-1$
8f5221c2 64 private static final String sfExperimentType = "type"; //$NON-NLS-1$
12c155f5 65
253d5be1
BH
66 private static final ReadOnlyTextPropertyDescriptor sfNameDescriptor = new ReadOnlyTextPropertyDescriptor(sfName, sfName);
67 private static final ReadOnlyTextPropertyDescriptor sfPathDescriptor = new ReadOnlyTextPropertyDescriptor(sfPath, sfPath);
68 private static final ReadOnlyTextPropertyDescriptor sfLocationDescriptor = new ReadOnlyTextPropertyDescriptor(sfLocation,
12c155f5 69 sfLocation);
8f5221c2 70 private static final ReadOnlyTextPropertyDescriptor sfTypeDescriptor = new ReadOnlyTextPropertyDescriptor(sfExperimentType, sfExperimentType);
12c155f5
FC
71
72 private static final IPropertyDescriptor[] sfDescriptors = { sfNameDescriptor, sfPathDescriptor,
8f5221c2 73 sfLocationDescriptor, sfTypeDescriptor };
12c155f5
FC
74
75 static {
76 sfNameDescriptor.setCategory(sfInfoCategory);
77 sfPathDescriptor.setCategory(sfInfoCategory);
78 sfLocationDescriptor.setCategory(sfInfoCategory);
8f5221c2 79 sfTypeDescriptor.setCategory(sfInfoCategory);
12c155f5
FC
80 }
81
8f5221c2
GB
82 // The mapping of available trace type IDs to their corresponding
83 // configuration element
84 private static final Map<String, IConfigurationElement> sfTraceTypeAttributes = new HashMap<>();
85 private static final Map<String, IConfigurationElement> sfTraceTypeUIAttributes = new HashMap<>();
86 private static final Map<String, IConfigurationElement> sfTraceCategories = new HashMap<>();
87
88 // ------------------------------------------------------------------------
89 // Static initialization
90 // ------------------------------------------------------------------------
91
92 /**
93 * Initialize statically at startup by getting extensions from the platform
94 * extension registry.
95 * @since 3.0
96 */
97 public static void init() {
98 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
99 for (IConfigurationElement ce : config) {
100 String elementName = ce.getName();
101 if (elementName.equals(TmfTraceType.EXPERIMENT_ELEM)) {
102 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
103 sfTraceTypeAttributes.put(traceTypeId, ce);
104 } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
105 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
106 sfTraceCategories.put(categoryId, ce);
107 }
108 }
109
110 /*
111 * Read the corresponding tmf.ui "tracetypeui" extension point for this
112 * trace type, if it exists.
113 */
114 config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceTypeUIUtils.TMF_TRACE_TYPE_UI_ID);
115 for (IConfigurationElement ce : config) {
116 String elemName = ce.getName();
117 if (TmfTraceTypeUIUtils.EXPERIMENT_ELEM.equals(elemName)) {
118 String traceType = ce.getAttribute(TmfTraceTypeUIUtils.TRACETYPE_ATTR);
119 sfTraceTypeUIAttributes.put(traceType, ce);
120 }
121 }
122 }
c4c81d91 123
12c155f5
FC
124 // ------------------------------------------------------------------------
125 // Constructors
126 // ------------------------------------------------------------------------
b544077e 127 /**
c4c81d91 128 * Constructor
b544077e
BH
129 * @param name The name of the experiment
130 * @param folder The folder reference
131 * @param parent The experiment folder reference.
132 */
12c155f5
FC
133 public TmfExperimentElement(String name, IFolder folder, TmfExperimentFolder parent) {
134 super(name, folder, parent);
12c155f5
FC
135 }
136
137 // ------------------------------------------------------------------------
138 // TmfProjectModelElement
139 // ------------------------------------------------------------------------
140
141 @Override
142 public IFolder getResource() {
143 return (IFolder) fResource;
144 }
145
146 @Override
147 public TmfProjectElement getProject() {
148 return (TmfProjectElement) getParent().getParent();
149 }
150
f537c959
PT
151 @Override
152 void refreshChildren() {
153 IFolder folder = getResource();
154
8f5221c2 155 /* Update the trace children of this experiment */
f537c959
PT
156 // Get the children from the model
157 Map<String, ITmfProjectModelElement> childrenMap = new HashMap<>();
8f5221c2 158 for (ITmfProjectModelElement element : getTraces()) {
f537c959
PT
159 childrenMap.put(element.getResource().getName(), element);
160 }
161
162 try {
163 IResource[] members = folder.members();
164 for (IResource resource : members) {
165 String name = resource.getName();
166 ITmfProjectModelElement element = childrenMap.get(name);
167 if (element instanceof TmfTraceElement) {
168 childrenMap.remove(name);
169 } else if (!resource.isHidden()) {
170 // exclude hidden resources (e.g. bookmarks file)
171 element = new TmfTraceElement(name, resource, this);
172 }
173 }
174 } catch (CoreException e) {
175 }
176
177 // Cleanup dangling children from the model
178 for (ITmfProjectModelElement danglingChild : childrenMap.values()) {
179 removeChild(danglingChild);
180 }
8f5221c2 181
8f5221c2 182 /* Update the analysis under this experiment */
9928ddeb 183 super.refreshChildren();
f537c959
PT
184 }
185
12c155f5
FC
186 // ------------------------------------------------------------------------
187 // Operations
188 // ------------------------------------------------------------------------
11252342 189
8f5221c2
GB
190 /**
191 * Refreshes the trace type filed by reading the trace type persistent
192 * property of the resource reference.
193 *
194 * If trace type is null after refresh, set it to the generic trace type
195 * (for seamless upgrade)
196 */
197 @Override
198 public void refreshTraceType() {
199 super.refreshTraceType();
200 if (getTraceType() == null) {
201 IConfigurationElement ce = TmfTraceType.getInstance().getTraceAttributes(TmfTraceType.DEFAULT_EXPERIMENT_TYPE);
202 if (ce != null) {
203 try {
204 IFolder experimentFolder = getResource();
205 experimentFolder.setPersistentProperty(TmfCommonConstants.TRACETYPE, ce.getAttribute(TmfTraceType.ID_ATTR));
206 super.refreshTraceType();
207 } catch (InvalidRegistryObjectException | CoreException e) {
208 }
209 }
210 }
211 }
212
b544077e
BH
213 /**
214 * Returns a list of TmfTraceElements contained in this experiment.
215 * @return a list of TmfTraceElements
216 */
8f5221c2 217 @Override
12c155f5
FC
218 public List<TmfTraceElement> getTraces() {
219 List<ITmfProjectModelElement> children = getChildren();
507b1336 220 List<TmfTraceElement> traces = new ArrayList<>();
12c155f5
FC
221 for (ITmfProjectModelElement child : children) {
222 if (child instanceof TmfTraceElement) {
223 traces.add((TmfTraceElement) child);
224 }
225 }
226 return traces;
227 }
228
beb19106
GB
229 /**
230 * Adds a trace to the experiment
231 *
232 * @param trace The trace element to add
233 * @since 2.0
234 */
235 public void addTrace(TmfTraceElement trace) {
236 /**
237 * Create a link to the actual trace and set the trace type
238 */
239 IFolder experiment = getResource();
240 IResource resource = trace.getResource();
241 IPath location = resource.getLocation();
242 IWorkspace workspace = ResourcesPlugin.getWorkspace();
243 try {
244 Map<QualifiedName, String> properties = trace.getResource().getPersistentProperties();
beb19106 245 String traceType = properties.get(TmfCommonConstants.TRACETYPE);
beb19106
GB
246
247 if (resource instanceof IFolder) {
248 IFolder folder = experiment.getFolder(trace.getName());
249 if (workspace.validateLinkLocation(folder, location).isOK()) {
250 folder.createLink(location, IResource.REPLACE, null);
2d64a788 251 setProperties(folder, traceType);
beb19106
GB
252
253 } else {
254 Activator.getDefault().logError("Error creating link. Invalid trace location " + location); //$NON-NLS-1$
255 }
256 } else {
257 IFile file = experiment.getFile(trace.getName());
258 if (workspace.validateLinkLocation(file, location).isOK()) {
259 file.createLink(location, IResource.REPLACE, null);
2d64a788 260 setProperties(file, traceType);
beb19106
GB
261 } else {
262 Activator.getDefault().logError("Error creating link. Invalid trace location " + location); //$NON-NLS-1$
263 }
264 }
265 } catch (CoreException e) {
266 Activator.getDefault().logError("Error creating link to location " + location, e); //$NON-NLS-1$
267 }
268
269 }
270
271 /**
272 * Removes a trace from an experiment
273 *
274 * @param trace The trace to remove
275 * @throws CoreException exception
276 * @since 2.0
277 */
278 public void removeTrace(TmfTraceElement trace) throws CoreException {
279
280 // Close the experiment if open
a72a6830 281 closeEditors();
beb19106
GB
282
283 /* Finally, remove the trace from experiment*/
284 removeChild(trace);
285 trace.getResource().delete(true, null);
d9030b38 286 deleteSupplementaryResources();
beb19106
GB
287 }
288
2d64a788 289 private static void setProperties(IResource resource, String traceType) throws CoreException {
beb19106 290 resource.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceType);
beb19106
GB
291 }
292
8f5221c2 293 @Override
81fe3479 294 public IFile createBookmarksFile() throws CoreException {
8f5221c2
GB
295 return createBookmarksFile(getProject().getExperimentsFolder().getResource(), TmfExperiment.class.getCanonicalName());
296 }
297
298 @Override
299 public String getEditorId() {
300 /* See if a default editor was set for this experiment type */
301 if (getTraceType() != null) {
302 IConfigurationElement ce = sfTraceTypeUIAttributes.get(getTraceType());
c1c0dfd0
GB
303 if (ce != null) {
304 IConfigurationElement[] defaultEditorCE = ce.getChildren(TmfTraceTypeUIUtils.DEFAULT_EDITOR_ELEM);
305 if (defaultEditorCE.length == 1) {
306 return defaultEditorCE[0].getAttribute(TmfTraceType.ID_ATTR);
307 }
81fe3479 308 }
c4c81d91 309 }
8f5221c2
GB
310
311 /* No default editor, try to find a common editor for all traces */
312 final List<TmfTraceElement> traceEntries = getTraces();
313 String commonEditorId = null;
314
315 for (TmfTraceElement element : traceEntries) {
316 // If all traces use the same editorId, use it, otherwise use the
317 // default
318 final String editorId = element.getEditorId();
319 if (commonEditorId == null) {
320 commonEditorId = (editorId != null) ? editorId : TmfEventsEditor.ID;
321 } else if (!commonEditorId.equals(editorId)) {
322 commonEditorId = TmfEventsEditor.ID;
323 }
324 }
325 return null;
81fe3479
PT
326 }
327
328 /**
8f5221c2
GB
329 * Instantiate a {@link TmfExperiment} object based on the experiment type
330 * and the corresponding extension.
331 *
332 * @return the {@link TmfExperiment} or <code>null</code> for an error
333 * @since 3.0
81fe3479 334 */
8f5221c2
GB
335 @Override
336 public TmfExperiment instantiateTrace() {
337 try {
338
339 // make sure that supplementary folder exists
340 refreshSupplementaryFolder();
341
342 if (getTraceType() != null) {
343
344 IConfigurationElement ce = sfTraceTypeAttributes.get(getTraceType());
345 if (ce == null) {
346 return null;
347 }
348 TmfExperiment experiment = (TmfExperiment) ce.createExecutableExtension(TmfTraceType.EXPERIMENT_TYPE_ATTR);
349 return experiment;
350 }
351 } catch (CoreException e) {
352 Activator.getDefault().logError(NLS.bind(Messages.TmfExperimentElement_ErrorInstantiatingTrace, getName()), e);
353 }
354 return null;
355 }
356
357 @Override
358 public String getTypeName() {
359 return Messages.TmfExperimentElement_TypeName;
c4c81d91
PT
360 }
361
12c155f5
FC
362 // ------------------------------------------------------------------------
363 // IPropertySource2
364 // ------------------------------------------------------------------------
365
366 @Override
367 public Object getEditableValue() {
368 return null;
369 }
370
371 @Override
372 public IPropertyDescriptor[] getPropertyDescriptors() {
77fdc5df 373 return Arrays.copyOf(sfDescriptors, sfDescriptors.length);
12c155f5
FC
374 }
375
376 @Override
377 public Object getPropertyValue(Object id) {
378
c4c81d91 379 if (sfName.equals(id)) {
12c155f5 380 return getName();
c4c81d91 381 }
12c155f5 382
c4c81d91 383 if (sfPath.equals(id)) {
12c155f5 384 return getPath().toString();
c4c81d91 385 }
12c155f5 386
c4c81d91 387 if (sfLocation.equals(id)) {
12c155f5 388 return getLocation().toString();
c4c81d91 389 }
12c155f5 390
8f5221c2
GB
391 if (sfExperimentType.equals(id)) {
392 if (getTraceType() != null) {
393 IConfigurationElement ce = sfTraceTypeAttributes.get(getTraceType());
394 if (ce == null) {
395 return ""; //$NON-NLS-1$
396 }
397 String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
398 if (categoryId != null) {
399 IConfigurationElement category = sfTraceCategories.get(categoryId);
400 if (category != null) {
401 return category.getAttribute(TmfTraceType.NAME_ATTR) + ':' + ce.getAttribute(TmfTraceType.NAME_ATTR);
402 }
403 }
404 return ce.getAttribute(TmfTraceType.NAME_ATTR);
405 }
406 }
407
12c155f5
FC
408 return null;
409 }
c4c81d91 410
12c155f5
FC
411 @Override
412 public void resetPropertyValue(Object id) {
413 }
414
415 @Override
416 public void setPropertyValue(Object id, Object value) {
417 }
418
419 @Override
420 public boolean isPropertyResettable(Object id) {
421 return false;
422 }
423
424 @Override
425 public boolean isPropertySet(Object id) {
426 return false;
427 }
428
99504bb8
GB
429 /**
430 * Return the suffix for resource names
431 * @return The folder suffix
432 */
433 @Override
434 public String getSuffix() {
435 return sfFolderSuffix;
436 }
437
12c155f5 438}
This page took 0.071746 seconds and 5 git commands to generate.