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