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