Replace printStackTrace() with proper logging in TMF and LTTng
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfTraceElement.java
CommitLineData
12c155f5
FC
1/*******************************************************************************
2 * Copyright (c) 2010, 2011 Ericsson
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
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.ui.project.model;
14
5a5c2fc7 15import java.util.Arrays;
12c155f5
FC
16import java.util.HashMap;
17import java.util.Map;
18
5e4bf87d 19import org.eclipse.core.resources.IFolder;
12c155f5
FC
20import org.eclipse.core.resources.IResource;
21import org.eclipse.core.runtime.CoreException;
22import org.eclipse.core.runtime.IConfigurationElement;
5e4bf87d 23import org.eclipse.core.runtime.NullProgressMonitor;
12c155f5 24import org.eclipse.core.runtime.Platform;
5e4bf87d 25import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin;
d34665f9
FC
26import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtEvent;
27import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTrace;
28import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
29import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlEvent;
30import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTrace;
31import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition;
e12ecd30 32import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
ce2388e0 33import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
6c13869b 34import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
4bf17f4a 35import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
12c155f5
FC
36import org.eclipse.ui.IActionFilter;
37import org.eclipse.ui.views.properties.IPropertyDescriptor;
38import org.eclipse.ui.views.properties.IPropertySource2;
39import org.eclipse.ui.views.properties.TextPropertyDescriptor;
40
41/**
42 * <b><u>TmfTraceElement</u></b>
43 * <p>
44 */
45public class TmfTraceElement extends TmfProjectModelElement implements IActionFilter, IPropertySource2 {
46
47 // ------------------------------------------------------------------------
48 // Constants
49 // ------------------------------------------------------------------------
50
12c155f5
FC
51 // Other attributes
52 public static final String BUNDLE = "bundle"; //$NON-NLS-1$
53 public static final String IS_LINKED = "isLinked"; //$NON-NLS-1$
54
55 // Property View stuff
56 private static final String sfInfoCategory = "Info"; //$NON-NLS-1$
57 private static final String sfName = "name"; //$NON-NLS-1$
58 private static final String sfPath = "path"; //$NON-NLS-1$
59 private static final String sfLocation = "location"; //$NON-NLS-1$
60 private static final String sfEventType = "type"; //$NON-NLS-1$
61 private static final String sfIsLinked = "linked"; //$NON-NLS-1$
62
63 private static final TextPropertyDescriptor sfNameDescriptor = new TextPropertyDescriptor(sfName, sfName);
64 private static final TextPropertyDescriptor sfPathDescriptor = new TextPropertyDescriptor(sfPath, sfPath);
65 private static final TextPropertyDescriptor sfLocationDescriptor = new TextPropertyDescriptor(sfLocation, sfLocation);
66 private static final TextPropertyDescriptor sfTypeDescriptor = new TextPropertyDescriptor(sfEventType, sfEventType);
67 private static final TextPropertyDescriptor sfIsLinkedDescriptor = new TextPropertyDescriptor(sfIsLinked, sfIsLinked);
68
69 private static final IPropertyDescriptor[] sfDescriptors = { sfNameDescriptor, sfPathDescriptor, sfLocationDescriptor,
70 sfTypeDescriptor, sfIsLinkedDescriptor };
71
72 static {
73 sfNameDescriptor.setCategory(sfInfoCategory);
74 sfPathDescriptor.setCategory(sfInfoCategory);
75 sfLocationDescriptor.setCategory(sfInfoCategory);
76 sfTypeDescriptor.setCategory(sfInfoCategory);
77 sfIsLinkedDescriptor.setCategory(sfInfoCategory);
78 }
5e4bf87d 79
12c155f5
FC
80 // ------------------------------------------------------------------------
81 // Attributes
82 // ------------------------------------------------------------------------
83
84 // This trace type ID as defined in plugin.xml
85 private String fTraceTypeId = null;
86
87 // ------------------------------------------------------------------------
88 // Static initialization
89 // ------------------------------------------------------------------------
90
91 // The mapping of available trace type IDs to their corresponding configuration element
92 private static final Map<String, IConfigurationElement> sfTraceTypeAttributes = new HashMap<String, IConfigurationElement>();
93 private static final Map<String, IConfigurationElement> sfTraceCategories = new HashMap<String, IConfigurationElement>();
94
95 // Initialize statically at startup
96 public static void init() {
4bf17f4a 97 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
12c155f5 98 for (IConfigurationElement ce : config) {
4bf17f4a 99 String elementName = ce.getName();
100 if (elementName.equals(TmfTraceType.TYPE_ELEM)) {
101 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
12c155f5 102 sfTraceTypeAttributes.put(traceTypeId, ce);
4bf17f4a 103 } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
104 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
12c155f5
FC
105 sfTraceCategories.put(categoryId, ce);
106 }
107 }
108 }
109
110 // ------------------------------------------------------------------------
111 // Constructors
112 // ------------------------------------------------------------------------
113
114 public TmfTraceElement(String name, IResource trace, TmfTraceFolder parent) {
115 this(name, trace, (TmfProjectModelElement) parent);
116 }
117
118 public TmfTraceElement(String name, IResource trace, TmfExperimentElement parent) {
119 this(name, trace, (TmfProjectModelElement) parent);
120 }
121
122 private TmfTraceElement(String name, IResource trace, TmfProjectModelElement parent) {
123 super(name, trace, parent);
124 parent.addChild(this);
125 refreshTraceType();
126 }
127
128 // ------------------------------------------------------------------------
129 // Operations
130 // ------------------------------------------------------------------------
131
132 public String getTraceType() {
133 return fTraceTypeId;
134 }
135
136 public void refreshTraceType() {
137 try {
e12ecd30 138 fTraceTypeId = getResource().getPersistentProperty(TmfCommonConstants.TRACETYPE);
12c155f5 139 } catch (CoreException e) {
9fa32496 140 TmfUiPlugin.getDefault().logError("Error refreshing trace type pesistent property for trace " + getName(), e); //$NON-NLS-1$
12c155f5
FC
141 }
142 }
143
144 public ITmfTrace<?> instantiateTrace() {
145 try {
e12ecd30
BH
146
147 // make sure that supplementary folder exists
148 refreshSupplementaryFolder();
149
12c155f5 150 if (fTraceTypeId != null) {
4bf17f4a 151 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
152 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
153 if (fTraceTypeId.equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
154 return new CustomTxtTrace(def);
155 }
156 }
157 }
158 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
159 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
160 if (fTraceTypeId.equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
161 return new CustomXmlTrace(def);
162 }
163 }
164 }
12c155f5 165 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 166 ITmfTrace<?> trace = (ITmfTrace<?>) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
12c155f5
FC
167 return trace;
168 }
169 } catch (CoreException e) {
9fa32496 170 TmfUiPlugin.getDefault().logError("Error instantiating ITmfTrace object for trace " + getName(), e); //$NON-NLS-1$
12c155f5
FC
171 }
172 return null;
173 }
174
ce2388e0 175 public ITmfEvent instantiateEvent() {
12c155f5
FC
176 try {
177 if (fTraceTypeId != null) {
4bf17f4a 178 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
179 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
180 if (fTraceTypeId.equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
181 return new CustomTxtEvent(def);
182 }
183 }
184 }
185 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
186 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
187 if (fTraceTypeId.equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
188 return new CustomXmlEvent(def);
189 }
190 }
191 }
12c155f5 192 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
ce2388e0 193 ITmfEvent event = (ITmfEvent) ce.createExecutableExtension(TmfTraceType.EVENT_TYPE_ATTR);
12c155f5
FC
194 return event;
195 }
196 } catch (CoreException e) {
9fa32496 197 TmfUiPlugin.getDefault().logError("Error instantiating ITmfEvent object for trace " + getName(), e); //$NON-NLS-1$
12c155f5
FC
198 }
199 return null;
200 }
201
202 public String getEditorId() {
203 if (fTraceTypeId != null) {
4bf17f4a 204 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
205 return TmfEventsEditor.ID;
206 }
207 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
208 return TmfEventsEditor.ID;
209 }
12c155f5 210 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 211 IConfigurationElement[] defaultEditorCE = ce.getChildren(TmfTraceType.DEFAULT_EDITOR_ELEM);
12c155f5 212 if (defaultEditorCE.length == 1) {
4bf17f4a 213 return defaultEditorCE[0].getAttribute(TmfTraceType.ID_ATTR);
12c155f5
FC
214 }
215 }
216 return null;
217 }
e12ecd30
BH
218
219 /**
220 * Returns the <code>TmfTraceElement</code> located under the <code>TmfTracesFolder</code>.
221 *
222 * @return <code>this</code> if this element is under the <code>TmfTracesFolder</code>
223 * else the corresponding <code>TmfTraceElement</code> if this element is under
224 * <code>TmfExperimentElement</code>.
225 */
5e4bf87d 226 public TmfTraceElement getElementUnderTraceFolder() {
e12ecd30 227
5e4bf87d
BH
228 // If trace is under an experiment, return original trace from the traces folder
229 if (getParent() instanceof TmfExperimentElement) {
230 for (TmfTraceElement aTrace : getProject().getTracesFolder().getTraces()) {
231 if (aTrace.getName().equals(getName())) {
232 return aTrace;
233 }
234 }
235 }
236 return this;
237 }
e12ecd30
BH
238
239 /**
240 * Deletes the trace specific supplementary folder.
241 */
242 public void deleteSupplementaryFolder() {
243 IFolder supplFolder = getTraceSupplementaryFolder(fResource.getName());
244 if (supplFolder.exists()) {
245 try {
246 supplFolder.delete(true, new NullProgressMonitor());
247 } catch (CoreException e) {
9fa32496 248 TmfUiPlugin.getDefault().logError("Error deleting supplementary folder " + supplFolder, e); //$NON-NLS-1$
5e4bf87d
BH
249 }
250 }
251 }
e12ecd30
BH
252
253 /**
254 * Renames the trace specific supplementary folder according to the new trace name.
255 *
256 * @param newTraceName The new trace name
257 */
258 public void renameSupplementaryFolder(String newTraceName) {
259 IFolder oldSupplFolder = getTraceSupplementaryFolder(fResource.getName());
260 IFolder newSupplFolder = getTraceSupplementaryFolder(newTraceName);
261
262 // Rename supplementary folder
263 if (oldSupplFolder.exists()) {
264 try {
265 oldSupplFolder.move(newSupplFolder.getFullPath(), true, new NullProgressMonitor());
266 } catch (CoreException e) {
9fa32496 267 TmfUiPlugin.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder, e); //$NON-NLS-1$
b885a58c
BH
268 }
269 }
b885a58c
BH
270 }
271
e12ecd30
BH
272 /**
273 * Copies the trace specific supplementary folder to the new trace name.
274 *
275 * @param newTraceName The new trace name
276 */
277 public void copySupplementaryFolder(String newTraceName) {
278 IFolder oldSupplFolder = getTraceSupplementaryFolder(fResource.getName());
279 IFolder newSupplFolder = getTraceSupplementaryFolder(newTraceName);
280
281 // copy supplementary folder
282 if (oldSupplFolder.exists()) {
283 try {
284 oldSupplFolder.copy(newSupplFolder.getFullPath(), true, new NullProgressMonitor());
285 } catch (CoreException e) {
9fa32496 286 TmfUiPlugin.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder, e); //$NON-NLS-1$
b885a58c
BH
287 }
288 }
289 }
e12ecd30
BH
290
291 /**
292 * Copies the trace specific supplementary folder a new folder.
293 *
294 * @param destination The destination folder to copy to.
295 */
296 public void copySupplementaryFolder(IFolder destination) {
297 IFolder oldSupplFolder = getTraceSupplementaryFolder(fResource.getName());
298
299 // copy supplementary folder
300 if (oldSupplFolder.exists()) {
301 try {
302 oldSupplFolder.copy(destination.getFullPath(), true, new NullProgressMonitor());
303 } catch (CoreException e) {
9fa32496 304 TmfUiPlugin.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder, e); //$NON-NLS-1$
b885a58c
BH
305 }
306 }
b885a58c 307 }
e12ecd30 308
0f0d17bb 309
e12ecd30
BH
310 /**
311 * Refreshes the trace specific supplementary folder information. It creates the folder if not exists.
312 * It sets the persistence property of the trace resource
313 */
314 public void refreshSupplementaryFolder() {
315 createSupplementaryDirectory();
316 }
317
318 /**
319 * Checks if supplementary resource exist or not.
320 *
321 * @return <code>true</code> if one or more files are under the trace supplementary folder
322 */
323 public boolean hasSupplementaryResources() {
324 IResource[] resources = getSupplementaryResources();
325 return (resources.length > 0);
0f0d17bb
AM
326 }
327
e12ecd30
BH
328 /**
329 * Returns the supplementary resources under the trace supplementary folder.
330 *
331 * @return array of resources under the trace supplementary folder.
332 */
333 public IResource[] getSupplementaryResources() {
334 IFolder supplFolder = getTraceSupplementaryFolder(fResource.getName());
335 if (supplFolder.exists()) {
336 try {
337 return supplFolder.members();
338 } catch (CoreException e) {
9fa32496 339 TmfUiPlugin.getDefault().logError("Error deleting supplementary folder " + supplFolder, e); //$NON-NLS-1$
e12ecd30
BH
340 }
341 }
342 return new IResource[0];
343 }
344
345 /**
346 * Deletes the given resources.
347 *
348 * @param resources array of resources to delete.
349 */
350 public void deleteSupplementaryResources(IResource[] resources) {
351
352 for (int i = 0; i < resources.length; i++) {
353 try {
354 resources[i].delete(true, new NullProgressMonitor());
355 } catch (CoreException e) {
9fa32496 356 TmfUiPlugin.getDefault().logError("Error deleting supplementary resource " + resources[i], e); //$NON-NLS-1$
e12ecd30
BH
357 }
358 }
359 }
360
361 private void createSupplementaryDirectory() {
362 IFolder supplFolder = getTraceSupplementaryFolder(fResource.getName());
363 if (!supplFolder.exists()) {
364 try {
365 supplFolder.create(true, true, new NullProgressMonitor());
366 } catch (CoreException e) {
9fa32496 367 TmfUiPlugin.getDefault().logError("Error creating resource supplementary file " + supplFolder, e); //$NON-NLS-1$
e12ecd30
BH
368 }
369 }
370
371 try {
372 fResource.setPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER, supplFolder.getLocationURI().getPath());
373 } catch (CoreException e) {
9fa32496 374 TmfUiPlugin.getDefault().logError("Error setting persistant property " + TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER, e); //$NON-NLS-1$
e12ecd30
BH
375 }
376
377 }
378
12c155f5
FC
379 // ------------------------------------------------------------------------
380 // IActionFilter
381 // ------------------------------------------------------------------------
382
383 @Override
384 public boolean testAttribute(Object target, String name, String value) {
385 if (name.equals(IS_LINKED)) {
386 boolean isLinked = getResource().isLinked();
387 return Boolean.toString(isLinked).equals(value);
388 }
389 return false;
390 }
391
392 // ------------------------------------------------------------------------
393 // TmfTraceElement
394 // ------------------------------------------------------------------------
395
396 @Override
397 public TmfProjectElement getProject() {
398 if (getParent() instanceof TmfTraceFolder) {
399 TmfTraceFolder folder = (TmfTraceFolder) getParent();
400 TmfProjectElement project = (TmfProjectElement) folder.getParent();
401 return project;
402 }
403 if (getParent() instanceof TmfExperimentElement) {
404 TmfExperimentElement experiment = (TmfExperimentElement) getParent();
405 TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent();
406 TmfProjectElement project = (TmfProjectElement) folder.getParent();
407 return project;
408 }
409 return null;
410 }
411
412 // ------------------------------------------------------------------------
413 // IPropertySource2
414 // ------------------------------------------------------------------------
415
416 @Override
417 public Object getEditableValue() {
418 return null;
419 }
420
421 @Override
422 public IPropertyDescriptor[] getPropertyDescriptors() {
5a5c2fc7 423 return (sfDescriptors != null) ? Arrays.copyOf(sfDescriptors, sfDescriptors.length) : null;
12c155f5
FC
424 }
425
426 @Override
427 public Object getPropertyValue(Object id) {
428
ce2388e0 429 if (sfName.equals(id)) {
12c155f5 430 return getName();
ce2388e0 431 }
12c155f5 432
ce2388e0 433 if (sfPath.equals(id)) {
12c155f5 434 return getPath().toString();
ce2388e0 435 }
12c155f5 436
ce2388e0 437 if (sfLocation.equals(id)) {
12c155f5 438 return getLocation().toString();
ce2388e0 439 }
12c155f5 440
ce2388e0 441 if (sfIsLinked.equals(id)) {
12c155f5 442 return Boolean.valueOf(getResource().isLinked()).toString();
ce2388e0 443 }
12c155f5
FC
444
445 if (sfEventType.equals(id)) {
446 if (fTraceTypeId != null) {
447 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 448 return (ce != null) ? (getCategory(ce) + " : " + ce.getAttribute(TmfTraceType.NAME_ATTR)) : ""; //$NON-NLS-1$ //$NON-NLS-2$
12c155f5
FC
449 }
450 }
451
452 return null;
453 }
454
455 private String getCategory(IConfigurationElement ce) {
4bf17f4a 456 String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
12c155f5
FC
457 if (categoryId != null) {
458 IConfigurationElement category = sfTraceCategories.get(categoryId);
4bf17f4a 459 if (category != null) {
460 return category.getAttribute(TmfTraceType.NAME_ATTR);
12c155f5
FC
461 }
462 }
463 return "[no category]"; //$NON-NLS-1$
464 }
465
466 @Override
467 public void resetPropertyValue(Object id) {
468 }
469
470 @Override
471 public void setPropertyValue(Object id, Object value) {
472 }
473
474 @Override
475 public boolean isPropertyResettable(Object id) {
476 return false;
477 }
478
479 @Override
480 public boolean isPropertySet(Object id) {
481 return false;
482 }
483
484}
This page took 0.050957 seconds and 5 git commands to generate.