tmf/lttng: Remove unneeded (non-Javadoc) comments
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfTraceElement.java
CommitLineData
12c155f5 1/*******************************************************************************
beb19106 2 * Copyright (c) 2010, 2013 Ericsson, École Polytechnique de Montréal
ce2388e0 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
ce2388e0 8 *
12c155f5
FC
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
b544077e 11 * Bernd Hufmann - Added supplementary files handling
beb19106
GB
12 * Geneviève Bastien - Moved supplementary files handling to parent class, added
13 * code to copy trace
12c155f5
FC
14 *******************************************************************************/
15
16package org.eclipse.linuxtools.tmf.ui.project.model;
17
c4c81d91
PT
18import java.io.ByteArrayInputStream;
19import java.io.InputStream;
5a5c2fc7 20import java.util.Arrays;
12c155f5
FC
21import java.util.HashMap;
22import java.util.Map;
23
c4c81d91 24import org.eclipse.core.resources.IFile;
5e4bf87d 25import org.eclipse.core.resources.IFolder;
12c155f5
FC
26import org.eclipse.core.resources.IResource;
27import org.eclipse.core.runtime.CoreException;
28import org.eclipse.core.runtime.IConfigurationElement;
29import org.eclipse.core.runtime.Platform;
8fd82db5 30import org.eclipse.linuxtools.internal.tmf.ui.Activator;
d34665f9
FC
31import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtEvent;
32import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTrace;
33import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
34import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlEvent;
35import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTrace;
36import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition;
e12ecd30 37import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
ce2388e0 38import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
6c13869b 39import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
c4c81d91 40import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
253d5be1 41import org.eclipse.linuxtools.tmf.core.util.ReadOnlyTextPropertyDescriptor;
4bf17f4a 42import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
12c155f5
FC
43import org.eclipse.ui.IActionFilter;
44import org.eclipse.ui.views.properties.IPropertyDescriptor;
45import org.eclipse.ui.views.properties.IPropertySource2;
12c155f5
FC
46
47/**
b544077e
BH
48 * Implementation of trace model element representing a trace. It provides methods to instantiate
49 * <code>ITmfTrace</code> and <code>ITmfEvent</code> as well as editor ID from the trace type
50 * extension definition.
abbdd66a 51 *
b544077e
BH
52 * @version 1.0
53 * @author Francois Chouinard
12c155f5 54 */
99504bb8 55public class TmfTraceElement extends TmfWithFolderElement implements IActionFilter, IPropertySource2 {
12c155f5
FC
56
57 // ------------------------------------------------------------------------
58 // Constants
59 // ------------------------------------------------------------------------
60
12c155f5 61 // Other attributes
b544077e
BH
62 /**
63 * Bundle attribute name
64 */
12c155f5 65 public static final String BUNDLE = "bundle"; //$NON-NLS-1$
b544077e
BH
66 /**
67 * IsLinked attribute name.
68 */
12c155f5
FC
69 public static final String IS_LINKED = "isLinked"; //$NON-NLS-1$
70
71 // Property View stuff
72 private static final String sfInfoCategory = "Info"; //$NON-NLS-1$
73 private static final String sfName = "name"; //$NON-NLS-1$
74 private static final String sfPath = "path"; //$NON-NLS-1$
75 private static final String sfLocation = "location"; //$NON-NLS-1$
76 private static final String sfEventType = "type"; //$NON-NLS-1$
77 private static final String sfIsLinked = "linked"; //$NON-NLS-1$
78
253d5be1
BH
79 private static final ReadOnlyTextPropertyDescriptor sfNameDescriptor = new ReadOnlyTextPropertyDescriptor(sfName, sfName);
80 private static final ReadOnlyTextPropertyDescriptor sfPathDescriptor = new ReadOnlyTextPropertyDescriptor(sfPath, sfPath);
81 private static final ReadOnlyTextPropertyDescriptor sfLocationDescriptor = new ReadOnlyTextPropertyDescriptor(sfLocation, sfLocation);
82 private static final ReadOnlyTextPropertyDescriptor sfTypeDescriptor = new ReadOnlyTextPropertyDescriptor(sfEventType, sfEventType);
83 private static final ReadOnlyTextPropertyDescriptor sfIsLinkedDescriptor = new ReadOnlyTextPropertyDescriptor(sfIsLinked, sfIsLinked);
12c155f5
FC
84
85 private static final IPropertyDescriptor[] sfDescriptors = { sfNameDescriptor, sfPathDescriptor, sfLocationDescriptor,
86 sfTypeDescriptor, sfIsLinkedDescriptor };
87
88 static {
89 sfNameDescriptor.setCategory(sfInfoCategory);
90 sfPathDescriptor.setCategory(sfInfoCategory);
91 sfLocationDescriptor.setCategory(sfInfoCategory);
92 sfTypeDescriptor.setCategory(sfInfoCategory);
93 sfIsLinkedDescriptor.setCategory(sfInfoCategory);
94 }
6256d8ad 95
c4c81d91
PT
96 private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
97
12c155f5
FC
98 // ------------------------------------------------------------------------
99 // Attributes
100 // ------------------------------------------------------------------------
101
102 // This trace type ID as defined in plugin.xml
103 private String fTraceTypeId = null;
104
105 // ------------------------------------------------------------------------
106 // Static initialization
107 // ------------------------------------------------------------------------
108
109 // The mapping of available trace type IDs to their corresponding configuration element
110 private static final Map<String, IConfigurationElement> sfTraceTypeAttributes = new HashMap<String, IConfigurationElement>();
111 private static final Map<String, IConfigurationElement> sfTraceCategories = new HashMap<String, IConfigurationElement>();
112
b544077e
BH
113 /**
114 * Initialize statically at startup by getting extensions from the platform extension registry.
115 */
12c155f5 116 public static void init() {
4bf17f4a 117 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
12c155f5 118 for (IConfigurationElement ce : config) {
4bf17f4a 119 String elementName = ce.getName();
120 if (elementName.equals(TmfTraceType.TYPE_ELEM)) {
121 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
12c155f5 122 sfTraceTypeAttributes.put(traceTypeId, ce);
4bf17f4a 123 } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
124 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
12c155f5
FC
125 sfTraceCategories.put(categoryId, ce);
126 }
127 }
128 }
129
130 // ------------------------------------------------------------------------
131 // Constructors
132 // ------------------------------------------------------------------------
b544077e 133 /**
abbdd66a 134 * Constructor.
b544077e
BH
135 * Creates trace model element under the trace folder.
136 * @param name The name of trace
137 * @param trace The trace resource.
138 * @param parent The parent element (trace folder)
139 */
12c155f5
FC
140 public TmfTraceElement(String name, IResource trace, TmfTraceFolder parent) {
141 this(name, trace, (TmfProjectModelElement) parent);
142 }
b544077e 143 /**
abbdd66a 144 * Constructor.
b544077e
BH
145 * Creates trace model element under the experiment folder.
146 * @param name The name of trace
147 * @param trace The trace resource.
148 * @param parent The parent element (experiment folder)
149 */
12c155f5
FC
150 public TmfTraceElement(String name, IResource trace, TmfExperimentElement parent) {
151 this(name, trace, (TmfProjectModelElement) parent);
152 }
153
154 private TmfTraceElement(String name, IResource trace, TmfProjectModelElement parent) {
155 super(name, trace, parent);
156 parent.addChild(this);
157 refreshTraceType();
158 }
159
160 // ------------------------------------------------------------------------
161 // Operations
162 // ------------------------------------------------------------------------
b544077e
BH
163 /**
164 * Returns the trace type ID.
165 * @return trace type ID.
166 */
12c155f5
FC
167 public String getTraceType() {
168 return fTraceTypeId;
169 }
170
b544077e 171 /**
abbdd66a 172 * Refreshes the trace type filed by reading the trace type persistent property of the resource
b544077e
BH
173 * referenece.
174 */
12c155f5
FC
175 public void refreshTraceType() {
176 try {
e12ecd30 177 fTraceTypeId = getResource().getPersistentProperty(TmfCommonConstants.TRACETYPE);
12c155f5 178 } catch (CoreException e) {
8fd82db5 179 Activator.getDefault().logError("Error refreshing trace type pesistent property for trace " + getName(), e); //$NON-NLS-1$
12c155f5
FC
180 }
181 }
182
b544077e 183 /**
abbdd66a
AM
184 * Instantiate a <code>ITmfTrace</code> object based on the trace type and the corresponding extension.
185 *
b544077e
BH
186 * @return the <code>ITmfTrace</code> or <code>null</code> for an error
187 */
6256d8ad 188 public ITmfTrace instantiateTrace() {
12c155f5 189 try {
e12ecd30
BH
190
191 // make sure that supplementary folder exists
192 refreshSupplementaryFolder();
193
12c155f5 194 if (fTraceTypeId != null) {
4bf17f4a 195 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
196 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
197 if (fTraceTypeId.equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
198 return new CustomTxtTrace(def);
199 }
200 }
201 }
202 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
203 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
204 if (fTraceTypeId.equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
205 return new CustomXmlTrace(def);
206 }
207 }
208 }
12c155f5 209 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
6256d8ad 210 ITmfTrace trace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
12c155f5
FC
211 return trace;
212 }
213 } catch (CoreException e) {
8fd82db5 214 Activator.getDefault().logError("Error instantiating ITmfTrace object for trace " + getName(), e); //$NON-NLS-1$
12c155f5
FC
215 }
216 return null;
217 }
218
b544077e 219 /**
abbdd66a
AM
220 * Instantiate a <code>ITmfEvent</code> object based on the trace type and the corresponding extension.
221 *
b544077e
BH
222 * @return the <code>ITmfEvent</code> or <code>null</code> for an error
223 */
ce2388e0 224 public ITmfEvent instantiateEvent() {
12c155f5
FC
225 try {
226 if (fTraceTypeId != null) {
4bf17f4a 227 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
228 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
229 if (fTraceTypeId.equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
230 return new CustomTxtEvent(def);
231 }
232 }
233 }
234 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
235 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
236 if (fTraceTypeId.equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
237 return new CustomXmlEvent(def);
238 }
239 }
240 }
12c155f5 241 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
ce2388e0 242 ITmfEvent event = (ITmfEvent) ce.createExecutableExtension(TmfTraceType.EVENT_TYPE_ATTR);
12c155f5
FC
243 return event;
244 }
245 } catch (CoreException e) {
8fd82db5 246 Activator.getDefault().logError("Error instantiating ITmfEvent object for trace " + getName(), e); //$NON-NLS-1$
12c155f5
FC
247 }
248 return null;
249 }
250
b544077e
BH
251 /**
252 * Returns the optional editor ID from the trace type extension.
253 * @return the editor ID or <code>null</code> if not defined.
254 */
12c155f5
FC
255 public String getEditorId() {
256 if (fTraceTypeId != null) {
4bf17f4a 257 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
258 return TmfEventsEditor.ID;
259 }
260 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
261 return TmfEventsEditor.ID;
262 }
12c155f5 263 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 264 IConfigurationElement[] defaultEditorCE = ce.getChildren(TmfTraceType.DEFAULT_EDITOR_ELEM);
12c155f5 265 if (defaultEditorCE.length == 1) {
4bf17f4a 266 return defaultEditorCE[0].getAttribute(TmfTraceType.ID_ATTR);
12c155f5
FC
267 }
268 }
269 return null;
270 }
e12ecd30 271
c4c81d91 272 /**
81fe3479 273 * Returns the file resource used to store bookmarks after creating it if necessary.
c4c81d91
PT
274 * If the trace resource is a file, it is returned directly.
275 * If the trace resource is a folder, a linked file is returned.
81fe3479 276 * The file will be created if it does not exist.
c4c81d91
PT
277 * @return the bookmarks file
278 * @throws CoreException if the bookmarks file cannot be created
279 * @since 2.0
280 */
81fe3479
PT
281 public IFile createBookmarksFile() throws CoreException {
282 IFile file = getBookmarksFile();
283 if (fResource instanceof IFolder) {
284 if (!file.exists()) {
285 final IFile bookmarksFile = getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE);
286 if (!bookmarksFile.exists()) {
287 final InputStream source = new ByteArrayInputStream(new byte[0]);
288 bookmarksFile.create(source, true, null);
289 }
290 bookmarksFile.setHidden(true);
291 file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null);
292 file.setHidden(true);
293 file.setPersistentProperty(TmfCommonConstants.TRACETYPE, TmfTrace.class.getCanonicalName());
294 }
295 }
296 return file;
297 }
298
299 /**
300 * Returns the file resource used to store bookmarks.
301 * The file may not exist.
302 * @return the bookmarks file
303 * @since 2.0
304 */
305 public IFile getBookmarksFile() {
c4c81d91
PT
306 IFile file = null;
307 if (fResource instanceof IFile) {
308 file = (IFile) fResource;
309 } else if (fResource instanceof IFolder) {
c4c81d91
PT
310 final IFolder folder = (IFolder) fResource;
311 file = folder.getFile(getName() + '_');
c4c81d91
PT
312 }
313 return file;
314 }
315
e12ecd30
BH
316 /**
317 * Returns the <code>TmfTraceElement</code> located under the <code>TmfTracesFolder</code>.
6256d8ad
AM
318 *
319 * @return <code>this</code> if this element is under the <code>TmfTracesFolder</code>
320 * else the corresponding <code>TmfTraceElement</code> if this element is under
e12ecd30
BH
321 * <code>TmfExperimentElement</code>.
322 */
5e4bf87d 323 public TmfTraceElement getElementUnderTraceFolder() {
e12ecd30 324
5e4bf87d
BH
325 // If trace is under an experiment, return original trace from the traces folder
326 if (getParent() instanceof TmfExperimentElement) {
327 for (TmfTraceElement aTrace : getProject().getTracesFolder().getTraces()) {
328 if (aTrace.getName().equals(getName())) {
329 return aTrace;
330 }
331 }
332 }
333 return this;
334 }
6256d8ad 335
12c155f5
FC
336 // ------------------------------------------------------------------------
337 // IActionFilter
338 // ------------------------------------------------------------------------
339
340 @Override
341 public boolean testAttribute(Object target, String name, String value) {
342 if (name.equals(IS_LINKED)) {
343 boolean isLinked = getResource().isLinked();
344 return Boolean.toString(isLinked).equals(value);
345 }
346 return false;
347 }
348
349 // ------------------------------------------------------------------------
350 // TmfTraceElement
351 // ------------------------------------------------------------------------
11252342 352
12c155f5
FC
353 @Override
354 public TmfProjectElement getProject() {
355 if (getParent() instanceof TmfTraceFolder) {
356 TmfTraceFolder folder = (TmfTraceFolder) getParent();
357 TmfProjectElement project = (TmfProjectElement) folder.getParent();
358 return project;
359 }
360 if (getParent() instanceof TmfExperimentElement) {
361 TmfExperimentElement experiment = (TmfExperimentElement) getParent();
362 TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent();
363 TmfProjectElement project = (TmfProjectElement) folder.getParent();
364 return project;
365 }
366 return null;
367 }
368
369 // ------------------------------------------------------------------------
370 // IPropertySource2
371 // ------------------------------------------------------------------------
372
373 @Override
374 public Object getEditableValue() {
375 return null;
376 }
377
378 @Override
379 public IPropertyDescriptor[] getPropertyDescriptors() {
77fdc5df 380 return Arrays.copyOf(sfDescriptors, sfDescriptors.length);
12c155f5
FC
381 }
382
383 @Override
384 public Object getPropertyValue(Object id) {
385
ce2388e0 386 if (sfName.equals(id)) {
12c155f5 387 return getName();
ce2388e0 388 }
12c155f5 389
ce2388e0 390 if (sfPath.equals(id)) {
12c155f5 391 return getPath().toString();
ce2388e0 392 }
12c155f5 393
ce2388e0 394 if (sfLocation.equals(id)) {
12c155f5 395 return getLocation().toString();
ce2388e0 396 }
12c155f5 397
ce2388e0 398 if (sfIsLinked.equals(id)) {
12c155f5 399 return Boolean.valueOf(getResource().isLinked()).toString();
ce2388e0 400 }
12c155f5
FC
401
402 if (sfEventType.equals(id)) {
403 if (fTraceTypeId != null) {
404 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 405 return (ce != null) ? (getCategory(ce) + " : " + ce.getAttribute(TmfTraceType.NAME_ATTR)) : ""; //$NON-NLS-1$ //$NON-NLS-2$
12c155f5
FC
406 }
407 }
408
409 return null;
410 }
411
abbdd66a 412 private static String getCategory(IConfigurationElement ce) {
4bf17f4a 413 String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
12c155f5
FC
414 if (categoryId != null) {
415 IConfigurationElement category = sfTraceCategories.get(categoryId);
4bf17f4a 416 if (category != null) {
417 return category.getAttribute(TmfTraceType.NAME_ATTR);
12c155f5
FC
418 }
419 }
420 return "[no category]"; //$NON-NLS-1$
421 }
422
423 @Override
424 public void resetPropertyValue(Object id) {
425 }
426
427 @Override
428 public void setPropertyValue(Object id, Object value) {
429 }
430
431 @Override
432 public boolean isPropertyResettable(Object id) {
433 return false;
434 }
435
436 @Override
437 public boolean isPropertySet(Object id) {
438 return false;
439 }
440
beb19106
GB
441 /**
442 * Copy this trace in the trace folder. No other parameters are mentioned so
443 * the trace is copied in this element's project trace folder
444 *
445 * @param string
446 * The new trace name
447 * @return the new Resource object
448 * @since 2.0
449 */
450 public TmfTraceElement copy(String string) {
451 TmfTraceFolder folder = this.getProject().getTracesFolder();
452 IResource res = super.copy(string, false);
453 return new TmfTraceElement(string, res, folder);
454 }
455
12c155f5 456}
This page took 0.060531 seconds and 5 git commands to generate.