693b660d20af65d8d60359daf8fc4f23ef6408fd
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / viewers / events / TmfEventPropertySource.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 * Patrick Tasse - Initial API and implementation
11 * Bernd Hufmann - Added call site and model URI properties
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.ui.viewers.events;
15
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import org.eclipse.tracecompass.tmf.core.event.ITmfCustomAttributes;
20 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
21 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
22 import org.eclipse.tracecompass.tmf.core.event.lookup.ITmfCallsite;
23 import org.eclipse.tracecompass.tmf.core.event.lookup.ITmfModelLookup;
24 import org.eclipse.tracecompass.tmf.core.event.lookup.ITmfSourceLookup;
25 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
26 import org.eclipse.tracecompass.tmf.ui.properties.ReadOnlyTextPropertyDescriptor;
27 import org.eclipse.ui.views.properties.IPropertyDescriptor;
28 import org.eclipse.ui.views.properties.IPropertySource;
29
30 /**
31 * Property source for events
32 */
33 public class TmfEventPropertySource implements IPropertySource {
34
35 private static final String ID_TIMESTAMP = "event_timestamp"; //$NON-NLS-1$
36 private static final String ID_TYPE = "event_type"; //$NON-NLS-1$
37 private static final String ID_TRACE = "trace_attribute"; //$NON-NLS-1$
38 private static final String ID_CONTENT = "event_content"; //$NON-NLS-1$
39 private static final String ID_SOURCE_LOOKUP = "event_lookup"; //$NON-NLS-1$
40 private static final String ID_MODEL_URI = "model_uri"; //$NON-NLS-1$
41 private static final String ID_CUSTOM_ATTRIBUTE = "custom_attribute"; //$NON-NLS-1$
42
43 private static final String NAME_TIMESTAMP = "Timestamp"; //$NON-NLS-1$
44 private static final String NAME_TYPE = "Type"; //$NON-NLS-1$
45 private static final String NAME_TRACE = "Trace"; //$NON-NLS-1$
46 private static final String NAME_CONTENT = "Content"; //$NON-NLS-1$
47 private static final String NAME_SOURCE_LOOKUP = "Source Lookup"; //$NON-NLS-1$
48 private static final String NAME_MODEL_URI = "Model URI"; //$NON-NLS-1$
49 private static final String NAME_CUSTOM_ATTRIBUTES = "Custom Attributes"; //$NON-NLS-1$
50
51 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
52
53 private ITmfEvent fEvent;
54
55 private class TimestampPropertySource implements IPropertySource {
56 private static final String ID_TIMESTAMP_VALUE = "timestamp_value"; //$NON-NLS-1$
57 private static final String ID_TIMESTAMP_SCALE = "timestamp_scale"; //$NON-NLS-1$
58 private static final String NAME_TIMESTAMP_VALUE = "value"; //$NON-NLS-1$
59 private static final String NAME_TIMESTAMP_SCALE = "scale"; //$NON-NLS-1$
60
61 private ITmfTimestamp fTimestamp;
62
63 public TimestampPropertySource(ITmfTimestamp timestamp) {
64 fTimestamp = timestamp;
65 }
66
67 @Override
68 public Object getEditableValue() {
69 return fTimestamp.toString();
70 }
71
72 @Override
73 public IPropertyDescriptor[] getPropertyDescriptors() {
74 IPropertyDescriptor[] descriptors = new IPropertyDescriptor[2];
75 descriptors[0] = new ReadOnlyTextPropertyDescriptor(ID_TIMESTAMP_VALUE, NAME_TIMESTAMP_VALUE);
76 descriptors[1] = new ReadOnlyTextPropertyDescriptor(ID_TIMESTAMP_SCALE, NAME_TIMESTAMP_SCALE);
77 return descriptors;
78 }
79
80 @Override
81 public Object getPropertyValue(Object id) {
82 if (id.equals(ID_TIMESTAMP_VALUE)) {
83 return Long.toString(fTimestamp.getValue());
84 } else if (id.equals(ID_TIMESTAMP_SCALE)) {
85 return Integer.toString(fTimestamp.getScale());
86 }
87 return null;
88 }
89
90 @Override
91 public boolean isPropertySet(Object id) {
92 return false;
93 }
94
95 @Override
96 public void resetPropertyValue(Object id) {
97 }
98
99 @Override
100 public void setPropertyValue(Object id, Object value) {
101 }
102 }
103
104 private class ContentPropertySource implements IPropertySource {
105 private ITmfEventField fContent;
106
107 public ContentPropertySource(ITmfEventField content) {
108 fContent = content;
109 }
110
111 @Override
112 public Object getEditableValue() {
113 return fContent.toString();
114 }
115
116 @Override
117 public IPropertyDescriptor[] getPropertyDescriptors() {
118 List<IPropertyDescriptor> descriptors = new ArrayList<>(fContent.getFields().size());
119 for (ITmfEventField field : fContent.getFields()) {
120 if (field != null) {
121 descriptors.add(new ReadOnlyTextPropertyDescriptor(field, field.getName()));
122 }
123 }
124 return descriptors.toArray(new IPropertyDescriptor[0]);
125 }
126
127 @Override
128 public Object getPropertyValue(Object id) {
129 ITmfEventField field = (ITmfEventField) id;
130 if (!field.getFields().isEmpty()) {
131 return new ContentPropertySource(field);
132 }
133 return field.getFormattedValue();
134 }
135
136 @Override
137 public boolean isPropertySet(Object id) {
138 return false;
139 }
140
141 @Override
142 public void resetPropertyValue(Object id) {
143 }
144
145 @Override
146 public void setPropertyValue(Object id, Object value) {
147 }
148 }
149
150 private class SourceLookupPropertySource implements IPropertySource {
151
152 private static final String ID_FILE_NAME = "callsite_file"; //$NON-NLS-1$
153 private static final String ID_FUNCTION_NAME = "callsite_function"; //$NON-NLS-1$
154 private static final String ID_LINE_NUMBER = "callsite_line"; //$NON-NLS-1$
155
156 private static final String NAME_FILE_NAME = "File"; //$NON-NLS-1$
157 private static final String NAME_FUNCTION_NAME = "Function"; //$NON-NLS-1$
158 private static final String NAME_LINE_NUMBER = "Line"; //$NON-NLS-1$
159
160 private final ITmfSourceLookup fSourceLookup;
161
162 public SourceLookupPropertySource(ITmfSourceLookup lookup) {
163 fSourceLookup = lookup;
164 }
165
166 @Override
167 public Object getEditableValue() {
168 ITmfCallsite cs = fSourceLookup.getCallsite();
169 if (cs != null) {
170 return cs.toString();
171 }
172 return null;
173 }
174
175 @Override
176 public IPropertyDescriptor[] getPropertyDescriptors() {
177 List<IPropertyDescriptor> descriptors= new ArrayList<>();
178 ITmfCallsite cs = fSourceLookup.getCallsite();
179 if (cs != null) {
180 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_FILE_NAME, NAME_FILE_NAME));
181 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_LINE_NUMBER, NAME_LINE_NUMBER));
182 // only display function if available
183 if (cs.getFunctionName() != null) {
184 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_FUNCTION_NAME, NAME_FUNCTION_NAME));
185 }
186 }
187 return descriptors.toArray(new IPropertyDescriptor[0]);
188 }
189
190 @Override
191 public Object getPropertyValue(Object id) {
192 ITmfCallsite cs = fSourceLookup.getCallsite();
193 if (cs == null) {
194 /*
195 * The callsite should not be null here, we would not have
196 * created the descriptors otherwise
197 */
198 throw new IllegalStateException();
199 }
200
201 if (!(id instanceof String)) {
202 return null;
203 }
204
205 switch ((String) id) {
206 case ID_FILE_NAME:
207 return cs.getFileName();
208 case ID_FUNCTION_NAME:
209 return cs.getFunctionName();
210 case ID_LINE_NUMBER:
211 return Long.valueOf(cs.getLineNumber());
212 default:
213 return null;
214 }
215 }
216
217 @Override
218 public boolean isPropertySet(Object id) {
219 return false;
220 }
221
222 @Override
223 public void resetPropertyValue(Object id) {
224
225 }
226
227 @Override
228 public void setPropertyValue(Object id, Object value) {
229 }
230 }
231
232 private class CustomAttributePropertySource implements IPropertySource {
233
234 private final ITmfCustomAttributes event;
235
236 public CustomAttributePropertySource(ITmfCustomAttributes event) {
237 this.event = event;
238 }
239
240 @Override
241 public Object getEditableValue() {
242 return EMPTY_STRING;
243 }
244
245 @Override
246 public IPropertyDescriptor[] getPropertyDescriptors() {
247 List<IPropertyDescriptor> descriptors = new ArrayList<>();
248
249 for (String customAttribute : event.listCustomAttributes()) {
250 descriptors.add(new ReadOnlyTextPropertyDescriptor(customAttribute, customAttribute));
251 }
252
253 return descriptors.toArray(new IPropertyDescriptor[0]);
254 }
255
256 @Override
257 public Object getPropertyValue(Object id) {
258 if (!(id instanceof String)) {
259 return null;
260 }
261 return event.getCustomAttribute((String) id);
262 }
263
264 @Override
265 public boolean isPropertySet(Object id) {
266 return false;
267 }
268
269 @Override
270 public void resetPropertyValue(Object id) {
271 }
272
273 @Override
274 public void setPropertyValue(Object id, Object value) {
275 }
276 }
277
278 /**
279 * Default constructor
280 *
281 * @param event the event
282 */
283 public TmfEventPropertySource(ITmfEvent event) {
284 super();
285 this.fEvent = event;
286 }
287
288 @Override
289 public Object getEditableValue() {
290 return null;
291 }
292
293 @Override
294 public IPropertyDescriptor[] getPropertyDescriptors() {
295 List<IPropertyDescriptor> descriptors= new ArrayList<>();
296
297 /* Display basic event information */
298 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_TIMESTAMP, NAME_TIMESTAMP));
299 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_TYPE, NAME_TYPE));
300 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_TRACE, NAME_TRACE));
301
302 /* Display event fields */
303 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_CONTENT, NAME_CONTENT));
304
305 /* Display source lookup information, if the event supplies it */
306 if ((fEvent instanceof ITmfSourceLookup) && (((ITmfSourceLookup)fEvent).getCallsite() != null)) {
307 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_SOURCE_LOOKUP, NAME_SOURCE_LOOKUP));
308 }
309
310 /* Display Model URI information, if the event supplies it */
311 if ((fEvent instanceof ITmfModelLookup) && (((ITmfModelLookup) fEvent).getModelUri() != null)) {
312 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_MODEL_URI, NAME_MODEL_URI));
313 }
314
315 /* Display custom attributes, if available */
316 if (fEvent instanceof ITmfCustomAttributes) {
317 ITmfCustomAttributes event = (ITmfCustomAttributes) fEvent;
318 if (!event.listCustomAttributes().isEmpty()) {
319 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_CUSTOM_ATTRIBUTE, NAME_CUSTOM_ATTRIBUTES));
320 }
321 }
322
323 return descriptors.toArray(new IPropertyDescriptor[0]);
324 }
325
326 @Override
327 public Object getPropertyValue(Object id) {
328 if (id.equals(ID_TIMESTAMP)) {
329 return new TimestampPropertySource(fEvent.getTimestamp());
330 } else if (id.equals(ID_TYPE) && fEvent.getType() != null) {
331 return fEvent.getType().toString();
332 } else if (id.equals(ID_TRACE)) {
333 return fEvent.getTrace().getName();
334 } else if (id.equals(ID_MODEL_URI)) {
335 return ((ITmfModelLookup)fEvent).getModelUri();
336 } else if (id.equals(ID_SOURCE_LOOKUP)) {
337 return new SourceLookupPropertySource(((ITmfSourceLookup)fEvent));
338 } else if (id.equals(ID_CONTENT) && fEvent.getContent() != null) {
339 return new ContentPropertySource(fEvent.getContent());
340 } else if (id.equals(ID_CUSTOM_ATTRIBUTE)) {
341 return new CustomAttributePropertySource((ITmfCustomAttributes) fEvent);
342 }
343 return null;
344 }
345
346 @Override
347 public boolean isPropertySet(Object id) {
348 return false;
349 }
350
351 @Override
352 public void resetPropertyValue(Object id) {
353 }
354
355 @Override
356 public void setPropertyValue(Object id, Object value) {
357 }
358
359 }
This page took 0.039944 seconds and 4 git commands to generate.