Add missing TMF constants file
[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
0f0d17bb 19import org.eclipse.core.resources.IFile;
5e4bf87d 20import org.eclipse.core.resources.IFolder;
12c155f5
FC
21import org.eclipse.core.resources.IResource;
22import org.eclipse.core.runtime.CoreException;
23import org.eclipse.core.runtime.IConfigurationElement;
5e4bf87d
BH
24import org.eclipse.core.runtime.IStatus;
25import org.eclipse.core.runtime.NullProgressMonitor;
12c155f5 26import org.eclipse.core.runtime.Platform;
0f0d17bb 27import org.eclipse.core.runtime.QualifiedName;
5e4bf87d
BH
28import org.eclipse.core.runtime.Status;
29import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin;
d34665f9
FC
30import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtEvent;
31import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTrace;
32import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
33import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlEvent;
34import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTrace;
35import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition;
ce2388e0 36import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
6c13869b 37import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
4bf17f4a 38import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
12c155f5
FC
39import org.eclipse.ui.IActionFilter;
40import org.eclipse.ui.views.properties.IPropertyDescriptor;
41import org.eclipse.ui.views.properties.IPropertySource2;
42import org.eclipse.ui.views.properties.TextPropertyDescriptor;
43
44/**
45 * <b><u>TmfTraceElement</u></b>
46 * <p>
47 */
48public class TmfTraceElement extends TmfProjectModelElement implements IActionFilter, IPropertySource2 {
49
50 // ------------------------------------------------------------------------
51 // Constants
52 // ------------------------------------------------------------------------
53
0f0d17bb
AM
54 // Property keys
55 public static final QualifiedName TRACEBUNDLE = new QualifiedName("org.eclipse.linuxtools.tmf", "tracetype.bundle"); //$NON-NLS-1$//$NON-NLS-2$
56 public static final QualifiedName TRACETYPE = new QualifiedName("org.eclipse.linuxtools.tmf", "tracetype.id"); //$NON-NLS-1$//$NON-NLS-2$
57 public static final QualifiedName TRACEICON = new QualifiedName("org.eclipse.linuxtools.tmf", "tracetype.icon"); //$NON-NLS-1$//$NON-NLS-2$
58
12c155f5
FC
59 // Other attributes
60 public static final String BUNDLE = "bundle"; //$NON-NLS-1$
61 public static final String IS_LINKED = "isLinked"; //$NON-NLS-1$
62
63 // Property View stuff
64 private static final String sfInfoCategory = "Info"; //$NON-NLS-1$
65 private static final String sfName = "name"; //$NON-NLS-1$
66 private static final String sfPath = "path"; //$NON-NLS-1$
67 private static final String sfLocation = "location"; //$NON-NLS-1$
68 private static final String sfEventType = "type"; //$NON-NLS-1$
69 private static final String sfIsLinked = "linked"; //$NON-NLS-1$
70
71 private static final TextPropertyDescriptor sfNameDescriptor = new TextPropertyDescriptor(sfName, sfName);
72 private static final TextPropertyDescriptor sfPathDescriptor = new TextPropertyDescriptor(sfPath, sfPath);
73 private static final TextPropertyDescriptor sfLocationDescriptor = new TextPropertyDescriptor(sfLocation, sfLocation);
74 private static final TextPropertyDescriptor sfTypeDescriptor = new TextPropertyDescriptor(sfEventType, sfEventType);
75 private static final TextPropertyDescriptor sfIsLinkedDescriptor = new TextPropertyDescriptor(sfIsLinked, sfIsLinked);
76
77 private static final IPropertyDescriptor[] sfDescriptors = { sfNameDescriptor, sfPathDescriptor, sfLocationDescriptor,
78 sfTypeDescriptor, sfIsLinkedDescriptor };
79
80 static {
81 sfNameDescriptor.setCategory(sfInfoCategory);
82 sfPathDescriptor.setCategory(sfInfoCategory);
83 sfLocationDescriptor.setCategory(sfInfoCategory);
84 sfTypeDescriptor.setCategory(sfInfoCategory);
85 sfIsLinkedDescriptor.setCategory(sfInfoCategory);
86 }
0f0d17bb
AM
87
88 private static final String[] SUPPLEMENATARY_FILES_EXTENSIONS = { ".ht" }; //$NON-NLS-1$
89 public static final String SUPPLEMENATARY_FILES_PREFIX = "."; //$NON-NLS-1$
5e4bf87d 90
12c155f5
FC
91 // ------------------------------------------------------------------------
92 // Attributes
93 // ------------------------------------------------------------------------
94
95 // This trace type ID as defined in plugin.xml
96 private String fTraceTypeId = null;
97
98 // ------------------------------------------------------------------------
99 // Static initialization
100 // ------------------------------------------------------------------------
101
102 // The mapping of available trace type IDs to their corresponding configuration element
103 private static final Map<String, IConfigurationElement> sfTraceTypeAttributes = new HashMap<String, IConfigurationElement>();
104 private static final Map<String, IConfigurationElement> sfTraceCategories = new HashMap<String, IConfigurationElement>();
105
106 // Initialize statically at startup
107 public static void init() {
4bf17f4a 108 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
12c155f5 109 for (IConfigurationElement ce : config) {
4bf17f4a 110 String elementName = ce.getName();
111 if (elementName.equals(TmfTraceType.TYPE_ELEM)) {
112 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
12c155f5 113 sfTraceTypeAttributes.put(traceTypeId, ce);
4bf17f4a 114 } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
115 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
12c155f5
FC
116 sfTraceCategories.put(categoryId, ce);
117 }
118 }
119 }
120
121 // ------------------------------------------------------------------------
122 // Constructors
123 // ------------------------------------------------------------------------
124
125 public TmfTraceElement(String name, IResource trace, TmfTraceFolder parent) {
126 this(name, trace, (TmfProjectModelElement) parent);
127 }
128
129 public TmfTraceElement(String name, IResource trace, TmfExperimentElement parent) {
130 this(name, trace, (TmfProjectModelElement) parent);
131 }
132
133 private TmfTraceElement(String name, IResource trace, TmfProjectModelElement parent) {
134 super(name, trace, parent);
135 parent.addChild(this);
136 refreshTraceType();
137 }
138
139 // ------------------------------------------------------------------------
140 // Operations
141 // ------------------------------------------------------------------------
142
143 public String getTraceType() {
144 return fTraceTypeId;
145 }
146
147 public void refreshTraceType() {
148 try {
0f0d17bb 149 fTraceTypeId = getResource().getPersistentProperty(TRACETYPE);
12c155f5
FC
150 } catch (CoreException e) {
151 e.printStackTrace();
152 }
153 }
154
155 public ITmfTrace<?> instantiateTrace() {
156 try {
157 if (fTraceTypeId != null) {
4bf17f4a 158 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
159 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
160 if (fTraceTypeId.equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
161 return new CustomTxtTrace(def);
162 }
163 }
164 }
165 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
166 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
167 if (fTraceTypeId.equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
168 return new CustomXmlTrace(def);
169 }
170 }
171 }
12c155f5 172 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 173 ITmfTrace<?> trace = (ITmfTrace<?>) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
12c155f5
FC
174 return trace;
175 }
176 } catch (CoreException e) {
177 e.printStackTrace();
178 }
179 return null;
180 }
181
ce2388e0 182 public ITmfEvent instantiateEvent() {
12c155f5
FC
183 try {
184 if (fTraceTypeId != null) {
4bf17f4a 185 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
186 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
187 if (fTraceTypeId.equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
188 return new CustomTxtEvent(def);
189 }
190 }
191 }
192 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
193 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
194 if (fTraceTypeId.equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
195 return new CustomXmlEvent(def);
196 }
197 }
198 }
12c155f5 199 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
ce2388e0 200 ITmfEvent event = (ITmfEvent) ce.createExecutableExtension(TmfTraceType.EVENT_TYPE_ATTR);
12c155f5
FC
201 return event;
202 }
203 } catch (CoreException e) {
204 e.printStackTrace();
205 }
206 return null;
207 }
208
209 public String getEditorId() {
210 if (fTraceTypeId != null) {
4bf17f4a 211 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
212 return TmfEventsEditor.ID;
213 }
214 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
215 return TmfEventsEditor.ID;
216 }
12c155f5 217 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 218 IConfigurationElement[] defaultEditorCE = ce.getChildren(TmfTraceType.DEFAULT_EDITOR_ELEM);
12c155f5 219 if (defaultEditorCE.length == 1) {
4bf17f4a 220 return defaultEditorCE[0].getAttribute(TmfTraceType.ID_ATTR);
12c155f5
FC
221 }
222 }
223 return null;
224 }
0f0d17bb 225
5e4bf87d 226 public TmfTraceElement getElementUnderTraceFolder() {
0f0d17bb 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 }
5e4bf87d 238
0f0d17bb
AM
239 public boolean hasSupplementaryFiles() {
240 // Check for all supplementary files
241 for (int i = 0; i < SUPPLEMENATARY_FILES_EXTENSIONS.length; i++) {
242 IFile supplFile = createSupplemenatryFile(fResource.getName(), SUPPLEMENATARY_FILES_EXTENSIONS[i]);
b885a58c 243
0f0d17bb
AM
244 if (supplFile.exists()) {
245 return true;
5e4bf87d
BH
246 }
247 }
0f0d17bb 248 return false;
5e4bf87d
BH
249 }
250
0f0d17bb
AM
251 public void deleteSupplementaryFiles() {
252 // Delete all supplementary files
253 for (int i = 0; i < SUPPLEMENATARY_FILES_EXTENSIONS.length; i++) {
254 IFile supplFile = createSupplemenatryFile(fResource.getName(), SUPPLEMENATARY_FILES_EXTENSIONS[i]);
255 if (supplFile.exists()) {
256 try {
257 supplFile.delete(true, new NullProgressMonitor());
258 } catch (CoreException e) {
259 TmfUiPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, "Error deleting resource supplementary file " + supplFile, e)); //$NON-NLS-1$
260 }
b885a58c
BH
261 }
262 }
b885a58c
BH
263 }
264
0f0d17bb
AM
265 public void renameSupplementaryFiles(String newTraceName) {
266 // Rename all supplementary files
267 for (int i = 0; i < SUPPLEMENATARY_FILES_EXTENSIONS.length; i++) {
268 IFile oldSupplFile = createSupplemenatryFile(fResource.getName(), SUPPLEMENATARY_FILES_EXTENSIONS[i]);
269 IFile newSupplFile = createSupplemenatryFile(newTraceName, SUPPLEMENATARY_FILES_EXTENSIONS[i]);
270
271 if (oldSupplFile.exists()) {
272 try {
273 oldSupplFile.move(newSupplFile.getFullPath(), true, new NullProgressMonitor());
274 } catch (CoreException e) {
275 TmfUiPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, "Error renaming resource supplementary file " + oldSupplFile, e)); //$NON-NLS-1$
276 }
b885a58c
BH
277 }
278 }
279 }
0f0d17bb
AM
280
281 public void copySupplementaryFiles(String newTraceName) {
282 // Copy all supplementary files
283 for (int i = 0; i < SUPPLEMENATARY_FILES_EXTENSIONS.length; i++) {
284 IFile oldSupplFile = createSupplemenatryFile(fResource.getName(), SUPPLEMENATARY_FILES_EXTENSIONS[i]);
285 IFile newSupplFile = createSupplemenatryFile(newTraceName, SUPPLEMENATARY_FILES_EXTENSIONS[i]);
286
287 if (oldSupplFile.exists()) {
288 try {
289 oldSupplFile.copy(newSupplFile.getFullPath(), true, new NullProgressMonitor());
290 } catch (CoreException e) {
291 TmfUiPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, "Error copying resource supplementary file " + oldSupplFile, e)); //$NON-NLS-1$
292 }
b885a58c
BH
293 }
294 }
b885a58c 295 }
0f0d17bb
AM
296
297 private IFile createSupplemenatryFile(String fileName, String extension) {
298 String name = SUPPLEMENATARY_FILES_PREFIX + fileName + extension;
299 IFolder folder = (IFolder)fResource.getParent();
300 return folder.getFile(name);
301 }
302
12c155f5
FC
303 // ------------------------------------------------------------------------
304 // IActionFilter
305 // ------------------------------------------------------------------------
306
307 @Override
308 public boolean testAttribute(Object target, String name, String value) {
309 if (name.equals(IS_LINKED)) {
310 boolean isLinked = getResource().isLinked();
311 return Boolean.toString(isLinked).equals(value);
312 }
313 return false;
314 }
315
316 // ------------------------------------------------------------------------
317 // TmfTraceElement
318 // ------------------------------------------------------------------------
319
320 @Override
321 public TmfProjectElement getProject() {
322 if (getParent() instanceof TmfTraceFolder) {
323 TmfTraceFolder folder = (TmfTraceFolder) getParent();
324 TmfProjectElement project = (TmfProjectElement) folder.getParent();
325 return project;
326 }
327 if (getParent() instanceof TmfExperimentElement) {
328 TmfExperimentElement experiment = (TmfExperimentElement) getParent();
329 TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent();
330 TmfProjectElement project = (TmfProjectElement) folder.getParent();
331 return project;
332 }
333 return null;
334 }
335
336 // ------------------------------------------------------------------------
337 // IPropertySource2
338 // ------------------------------------------------------------------------
339
340 @Override
341 public Object getEditableValue() {
342 return null;
343 }
344
345 @Override
346 public IPropertyDescriptor[] getPropertyDescriptors() {
5a5c2fc7 347 return (sfDescriptors != null) ? Arrays.copyOf(sfDescriptors, sfDescriptors.length) : null;
12c155f5
FC
348 }
349
350 @Override
351 public Object getPropertyValue(Object id) {
352
ce2388e0 353 if (sfName.equals(id)) {
12c155f5 354 return getName();
ce2388e0 355 }
12c155f5 356
ce2388e0 357 if (sfPath.equals(id)) {
12c155f5 358 return getPath().toString();
ce2388e0 359 }
12c155f5 360
ce2388e0 361 if (sfLocation.equals(id)) {
12c155f5 362 return getLocation().toString();
ce2388e0 363 }
12c155f5 364
ce2388e0 365 if (sfIsLinked.equals(id)) {
12c155f5 366 return Boolean.valueOf(getResource().isLinked()).toString();
ce2388e0 367 }
12c155f5
FC
368
369 if (sfEventType.equals(id)) {
370 if (fTraceTypeId != null) {
371 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 372 return (ce != null) ? (getCategory(ce) + " : " + ce.getAttribute(TmfTraceType.NAME_ATTR)) : ""; //$NON-NLS-1$ //$NON-NLS-2$
12c155f5
FC
373 }
374 }
375
376 return null;
377 }
378
379 private String getCategory(IConfigurationElement ce) {
4bf17f4a 380 String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
12c155f5
FC
381 if (categoryId != null) {
382 IConfigurationElement category = sfTraceCategories.get(categoryId);
4bf17f4a 383 if (category != null) {
384 return category.getAttribute(TmfTraceType.NAME_ATTR);
12c155f5
FC
385 }
386 }
387 return "[no category]"; //$NON-NLS-1$
388 }
389
390 @Override
391 public void resetPropertyValue(Object id) {
392 }
393
394 @Override
395 public void setPropertyValue(Object id, Object value) {
396 }
397
398 @Override
399 public boolean isPropertyResettable(Object id) {
400 return false;
401 }
402
403 @Override
404 public boolean isPropertySet(Object id) {
405 return false;
406 }
407
408}
This page took 0.047486 seconds and 5 git commands to generate.