Fix another batch of FindBugs warnings
[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
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
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
19import org.eclipse.core.resources.IResource;
20import org.eclipse.core.runtime.CoreException;
21import org.eclipse.core.runtime.IConfigurationElement;
22import org.eclipse.core.runtime.Platform;
23import org.eclipse.core.runtime.QualifiedName;
6c13869b
FC
24import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
25import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
26import org.eclipse.linuxtools.tmf.core.util.TmfTraceType;
4bf17f4a 27import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
28import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomTxtEvent;
29import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomTxtTrace;
30import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
31import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlEvent;
32import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlTrace;
33import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlTraceDefinition;
12c155f5
FC
34import org.eclipse.ui.IActionFilter;
35import org.eclipse.ui.views.properties.IPropertyDescriptor;
36import org.eclipse.ui.views.properties.IPropertySource2;
37import org.eclipse.ui.views.properties.TextPropertyDescriptor;
38
39/**
40 * <b><u>TmfTraceElement</u></b>
41 * <p>
42 */
43public class TmfTraceElement extends TmfProjectModelElement implements IActionFilter, IPropertySource2 {
44
45 // ------------------------------------------------------------------------
46 // Constants
47 // ------------------------------------------------------------------------
48
49 // Property keys
50 public static final QualifiedName TRACEBUNDLE = new QualifiedName("org.eclipse.linuxtools.tmf", "tracetype.bundle"); //$NON-NLS-1$//$NON-NLS-2$
51 public static final QualifiedName TRACETYPE = new QualifiedName("org.eclipse.linuxtools.tmf", "tracetype.id"); //$NON-NLS-1$//$NON-NLS-2$
52 public static final QualifiedName TRACEICON = new QualifiedName("org.eclipse.linuxtools.tmf", "tracetype.icon"); //$NON-NLS-1$//$NON-NLS-2$
53
12c155f5
FC
54 // Other attributes
55 public static final String BUNDLE = "bundle"; //$NON-NLS-1$
56 public static final String IS_LINKED = "isLinked"; //$NON-NLS-1$
57
58 // Property View stuff
59 private static final String sfInfoCategory = "Info"; //$NON-NLS-1$
60 private static final String sfName = "name"; //$NON-NLS-1$
61 private static final String sfPath = "path"; //$NON-NLS-1$
62 private static final String sfLocation = "location"; //$NON-NLS-1$
63 private static final String sfEventType = "type"; //$NON-NLS-1$
64 private static final String sfIsLinked = "linked"; //$NON-NLS-1$
65
66 private static final TextPropertyDescriptor sfNameDescriptor = new TextPropertyDescriptor(sfName, sfName);
67 private static final TextPropertyDescriptor sfPathDescriptor = new TextPropertyDescriptor(sfPath, sfPath);
68 private static final TextPropertyDescriptor sfLocationDescriptor = new TextPropertyDescriptor(sfLocation, sfLocation);
69 private static final TextPropertyDescriptor sfTypeDescriptor = new TextPropertyDescriptor(sfEventType, sfEventType);
70 private static final TextPropertyDescriptor sfIsLinkedDescriptor = new TextPropertyDescriptor(sfIsLinked, sfIsLinked);
71
72 private static final IPropertyDescriptor[] sfDescriptors = { sfNameDescriptor, sfPathDescriptor, sfLocationDescriptor,
73 sfTypeDescriptor, sfIsLinkedDescriptor };
74
75 static {
76 sfNameDescriptor.setCategory(sfInfoCategory);
77 sfPathDescriptor.setCategory(sfInfoCategory);
78 sfLocationDescriptor.setCategory(sfInfoCategory);
79 sfTypeDescriptor.setCategory(sfInfoCategory);
80 sfIsLinkedDescriptor.setCategory(sfInfoCategory);
81 }
82
83 // ------------------------------------------------------------------------
84 // Attributes
85 // ------------------------------------------------------------------------
86
87 // This trace type ID as defined in plugin.xml
88 private String fTraceTypeId = null;
89
90 // ------------------------------------------------------------------------
91 // Static initialization
92 // ------------------------------------------------------------------------
93
94 // The mapping of available trace type IDs to their corresponding configuration element
95 private static final Map<String, IConfigurationElement> sfTraceTypeAttributes = new HashMap<String, IConfigurationElement>();
96 private static final Map<String, IConfigurationElement> sfTraceCategories = new HashMap<String, IConfigurationElement>();
97
98 // Initialize statically at startup
99 public static void init() {
4bf17f4a 100 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
12c155f5 101 for (IConfigurationElement ce : config) {
4bf17f4a 102 String elementName = ce.getName();
103 if (elementName.equals(TmfTraceType.TYPE_ELEM)) {
104 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
12c155f5 105 sfTraceTypeAttributes.put(traceTypeId, ce);
4bf17f4a 106 } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
107 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
12c155f5
FC
108 sfTraceCategories.put(categoryId, ce);
109 }
110 }
111 }
112
113 // ------------------------------------------------------------------------
114 // Constructors
115 // ------------------------------------------------------------------------
116
117 public TmfTraceElement(String name, IResource trace, TmfTraceFolder parent) {
118 this(name, trace, (TmfProjectModelElement) parent);
119 }
120
121 public TmfTraceElement(String name, IResource trace, TmfExperimentElement parent) {
122 this(name, trace, (TmfProjectModelElement) parent);
123 }
124
125 private TmfTraceElement(String name, IResource trace, TmfProjectModelElement parent) {
126 super(name, trace, parent);
127 parent.addChild(this);
128 refreshTraceType();
129 }
130
131 // ------------------------------------------------------------------------
132 // Operations
133 // ------------------------------------------------------------------------
134
135 public String getTraceType() {
136 return fTraceTypeId;
137 }
138
139 public void refreshTraceType() {
140 try {
141 fTraceTypeId = getResource().getPersistentProperty(TRACETYPE);
142 } catch (CoreException e) {
143 e.printStackTrace();
144 }
145 }
146
147 public ITmfTrace<?> instantiateTrace() {
148 try {
149 if (fTraceTypeId != null) {
4bf17f4a 150 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
151 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
152 if (fTraceTypeId.equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
153 return new CustomTxtTrace(def);
154 }
155 }
156 }
157 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
158 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
159 if (fTraceTypeId.equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
160 return new CustomXmlTrace(def);
161 }
162 }
163 }
12c155f5 164 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 165 ITmfTrace<?> trace = (ITmfTrace<?>) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
12c155f5
FC
166 return trace;
167 }
168 } catch (CoreException e) {
169 e.printStackTrace();
170 }
171 return null;
172 }
173
174 public TmfEvent instantiateEvent() {
175 try {
176 if (fTraceTypeId != null) {
4bf17f4a 177 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
178 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
179 if (fTraceTypeId.equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
180 return new CustomTxtEvent(def);
181 }
182 }
183 }
184 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
185 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
186 if (fTraceTypeId.equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
187 return new CustomXmlEvent(def);
188 }
189 }
190 }
12c155f5 191 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 192 TmfEvent event = (TmfEvent) ce.createExecutableExtension(TmfTraceType.EVENT_TYPE_ATTR);
12c155f5
FC
193 return event;
194 }
195 } catch (CoreException e) {
196 e.printStackTrace();
197 }
198 return null;
199 }
200
201 public String getEditorId() {
202 if (fTraceTypeId != null) {
4bf17f4a 203 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
204 return TmfEventsEditor.ID;
205 }
206 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
207 return TmfEventsEditor.ID;
208 }
12c155f5 209 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 210 IConfigurationElement[] defaultEditorCE = ce.getChildren(TmfTraceType.DEFAULT_EDITOR_ELEM);
12c155f5 211 if (defaultEditorCE.length == 1) {
4bf17f4a 212 return defaultEditorCE[0].getAttribute(TmfTraceType.ID_ATTR);
12c155f5
FC
213 }
214 }
215 return null;
216 }
217
218 // ------------------------------------------------------------------------
219 // IActionFilter
220 // ------------------------------------------------------------------------
221
222 @Override
223 public boolean testAttribute(Object target, String name, String value) {
224 if (name.equals(IS_LINKED)) {
225 boolean isLinked = getResource().isLinked();
226 return Boolean.toString(isLinked).equals(value);
227 }
228 return false;
229 }
230
231 // ------------------------------------------------------------------------
232 // TmfTraceElement
233 // ------------------------------------------------------------------------
234
235 @Override
236 public TmfProjectElement getProject() {
237 if (getParent() instanceof TmfTraceFolder) {
238 TmfTraceFolder folder = (TmfTraceFolder) getParent();
239 TmfProjectElement project = (TmfProjectElement) folder.getParent();
240 return project;
241 }
242 if (getParent() instanceof TmfExperimentElement) {
243 TmfExperimentElement experiment = (TmfExperimentElement) getParent();
244 TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent();
245 TmfProjectElement project = (TmfProjectElement) folder.getParent();
246 return project;
247 }
248 return null;
249 }
250
251 // ------------------------------------------------------------------------
252 // IPropertySource2
253 // ------------------------------------------------------------------------
254
255 @Override
256 public Object getEditableValue() {
257 return null;
258 }
259
260 @Override
261 public IPropertyDescriptor[] getPropertyDescriptors() {
5a5c2fc7 262 return (sfDescriptors != null) ? Arrays.copyOf(sfDescriptors, sfDescriptors.length) : null;
12c155f5
FC
263 }
264
265 @Override
266 public Object getPropertyValue(Object id) {
267
268 if (sfName.equals(id))
269 return getName();
270
271 if (sfPath.equals(id))
272 return getPath().toString();
273
274 if (sfLocation.equals(id))
275 return getLocation().toString();
276
277 if (sfIsLinked.equals(id))
278 return Boolean.valueOf(getResource().isLinked()).toString();
279
280 if (sfEventType.equals(id)) {
281 if (fTraceTypeId != null) {
282 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 283 return (ce != null) ? (getCategory(ce) + " : " + ce.getAttribute(TmfTraceType.NAME_ATTR)) : ""; //$NON-NLS-1$ //$NON-NLS-2$
12c155f5
FC
284 }
285 }
286
287 return null;
288 }
289
290 private String getCategory(IConfigurationElement ce) {
4bf17f4a 291 String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
12c155f5
FC
292 if (categoryId != null) {
293 IConfigurationElement category = sfTraceCategories.get(categoryId);
4bf17f4a 294 if (category != null) {
295 return category.getAttribute(TmfTraceType.NAME_ATTR);
12c155f5
FC
296 }
297 }
298 return "[no category]"; //$NON-NLS-1$
299 }
300
301 @Override
302 public void resetPropertyValue(Object id) {
303 }
304
305 @Override
306 public void setPropertyValue(Object id, Object value) {
307 }
308
309 @Override
310 public boolean isPropertyResettable(Object id) {
311 return false;
312 }
313
314 @Override
315 public boolean isPropertySet(Object id) {
316 return false;
317 }
318
319}
This page took 0.037933 seconds and 5 git commands to generate.