Merge branch 'master' into TmfTrace-new
[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.NullProgressMonitor;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin;
26 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtEvent;
27 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTrace;
28 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
29 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlEvent;
30 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTrace;
31 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition;
32 import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
33 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
34 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
35 import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
36 import org.eclipse.ui.IActionFilter;
37 import org.eclipse.ui.views.properties.IPropertyDescriptor;
38 import org.eclipse.ui.views.properties.IPropertySource2;
39 import org.eclipse.ui.views.properties.TextPropertyDescriptor;
40
41 /**
42 * <b><u>TmfTraceElement</u></b>
43 * <p>
44 */
45 public class TmfTraceElement extends TmfProjectModelElement implements IActionFilter, IPropertySource2 {
46
47 // ------------------------------------------------------------------------
48 // Constants
49 // ------------------------------------------------------------------------
50
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 }
79
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() {
97 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
98 for (IConfigurationElement ce : config) {
99 String elementName = ce.getName();
100 if (elementName.equals(TmfTraceType.TYPE_ELEM)) {
101 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
102 sfTraceTypeAttributes.put(traceTypeId, ce);
103 } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
104 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
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 {
138 fTraceTypeId = getResource().getPersistentProperty(TmfCommonConstants.TRACETYPE);
139 } catch (CoreException e) {
140 TmfUiPlugin.getDefault().logError("Error refreshing trace type pesistent property for trace " + getName(), e); //$NON-NLS-1$
141 }
142 }
143
144 public ITmfTrace<?> instantiateTrace() {
145 try {
146
147 // make sure that supplementary folder exists
148 refreshSupplementaryFolder();
149
150 if (fTraceTypeId != null) {
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 }
165 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
166 ITmfTrace<?> trace = (ITmfTrace<?>) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
167 return trace;
168 }
169 } catch (CoreException e) {
170 TmfUiPlugin.getDefault().logError("Error instantiating ITmfTrace object for trace " + getName(), e); //$NON-NLS-1$
171 }
172 return null;
173 }
174
175 public ITmfEvent instantiateEvent() {
176 try {
177 if (fTraceTypeId != null) {
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 }
192 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
193 ITmfEvent event = (ITmfEvent) ce.createExecutableExtension(TmfTraceType.EVENT_TYPE_ATTR);
194 return event;
195 }
196 } catch (CoreException e) {
197 TmfUiPlugin.getDefault().logError("Error instantiating ITmfEvent object for trace " + getName(), e); //$NON-NLS-1$
198 }
199 return null;
200 }
201
202 public String getEditorId() {
203 if (fTraceTypeId != null) {
204 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
205 return TmfEventsEditor.ID;
206 }
207 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
208 return TmfEventsEditor.ID;
209 }
210 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
211 IConfigurationElement[] defaultEditorCE = ce.getChildren(TmfTraceType.DEFAULT_EDITOR_ELEM);
212 if (defaultEditorCE.length == 1) {
213 return defaultEditorCE[0].getAttribute(TmfTraceType.ID_ATTR);
214 }
215 }
216 return null;
217 }
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 */
226 public TmfTraceElement getElementUnderTraceFolder() {
227
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 }
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) {
248 TmfUiPlugin.getDefault().logError("Error deleting supplementary folder " + supplFolder, e); //$NON-NLS-1$
249 }
250 }
251 }
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) {
267 TmfUiPlugin.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder, e); //$NON-NLS-1$
268 }
269 }
270 }
271
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) {
286 TmfUiPlugin.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder, e); //$NON-NLS-1$
287 }
288 }
289 }
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) {
304 TmfUiPlugin.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder, e); //$NON-NLS-1$
305 }
306 }
307 }
308
309
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);
326 }
327
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) {
339 TmfUiPlugin.getDefault().logError("Error deleting supplementary folder " + supplFolder, e); //$NON-NLS-1$
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) {
356 TmfUiPlugin.getDefault().logError("Error deleting supplementary resource " + resources[i], e); //$NON-NLS-1$
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) {
367 TmfUiPlugin.getDefault().logError("Error creating resource supplementary file " + supplFolder, e); //$NON-NLS-1$
368 }
369 }
370
371 try {
372 fResource.setPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER, supplFolder.getLocationURI().getPath());
373 } catch (CoreException e) {
374 TmfUiPlugin.getDefault().logError("Error setting persistant property " + TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER, e); //$NON-NLS-1$
375 }
376
377 }
378
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() {
423 return (sfDescriptors != null) ? Arrays.copyOf(sfDescriptors, sfDescriptors.length) : null;
424 }
425
426 @Override
427 public Object getPropertyValue(Object id) {
428
429 if (sfName.equals(id)) {
430 return getName();
431 }
432
433 if (sfPath.equals(id)) {
434 return getPath().toString();
435 }
436
437 if (sfLocation.equals(id)) {
438 return getLocation().toString();
439 }
440
441 if (sfIsLinked.equals(id)) {
442 return Boolean.valueOf(getResource().isLinked()).toString();
443 }
444
445 if (sfEventType.equals(id)) {
446 if (fTraceTypeId != null) {
447 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
448 return (ce != null) ? (getCategory(ce) + " : " + ce.getAttribute(TmfTraceType.NAME_ATTR)) : ""; //$NON-NLS-1$ //$NON-NLS-2$
449 }
450 }
451
452 return null;
453 }
454
455 private String getCategory(IConfigurationElement ce) {
456 String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
457 if (categoryId != null) {
458 IConfigurationElement category = sfTraceCategories.get(categoryId);
459 if (category != null) {
460 return category.getAttribute(TmfTraceType.NAME_ATTR);
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.057469 seconds and 6 git commands to generate.