tmf: Annotate methods in ITmfEventField
[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 return event.getCustomAttribute((String) id);
259 }
260
261 @Override
262 public boolean isPropertySet(Object id) {
263 return false;
264 }
265
266 @Override
267 public void resetPropertyValue(Object id) {
268 }
269
270 @Override
271 public void setPropertyValue(Object id, Object value) {
272 }
273 }
274
275 /**
276 * Default constructor
277 *
278 * @param event the event
279 */
280 public TmfEventPropertySource(ITmfEvent event) {
281 super();
282 this.fEvent = event;
283 }
284
285 @Override
286 public Object getEditableValue() {
287 return null;
288 }
289
290 @Override
291 public IPropertyDescriptor[] getPropertyDescriptors() {
292 List<IPropertyDescriptor> descriptors= new ArrayList<>();
293
294 /* Display basic event information */
295 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_TIMESTAMP, NAME_TIMESTAMP));
296 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_TYPE, NAME_TYPE));
297 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_TRACE, NAME_TRACE));
298
299 /* Display event fields */
300 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_CONTENT, NAME_CONTENT));
301
302 /* Display source lookup information, if the event supplies it */
303 if ((fEvent instanceof ITmfSourceLookup) && (((ITmfSourceLookup)fEvent).getCallsite() != null)) {
304 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_SOURCE_LOOKUP, NAME_SOURCE_LOOKUP));
305 }
306
307 /* Display Model URI information, if the event supplies it */
308 if ((fEvent instanceof ITmfModelLookup) && (((ITmfModelLookup) fEvent).getModelUri() != null)) {
309 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_MODEL_URI, NAME_MODEL_URI));
310 }
311
312 /* Display custom attributes, if available */
313 if (fEvent instanceof ITmfCustomAttributes) {
314 ITmfCustomAttributes event = (ITmfCustomAttributes) fEvent;
315 if (event.listCustomAttributes() != null && !event.listCustomAttributes().isEmpty()) {
316 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_CUSTOM_ATTRIBUTE, NAME_CUSTOM_ATTRIBUTES));
317 }
318 }
319
320 return descriptors.toArray(new IPropertyDescriptor[0]);
321 }
322
323 @Override
324 public Object getPropertyValue(Object id) {
325 if (id.equals(ID_TIMESTAMP)) {
326 return new TimestampPropertySource(fEvent.getTimestamp());
327 } else if (id.equals(ID_TYPE) && fEvent.getType() != null) {
328 return fEvent.getType().toString();
329 } else if (id.equals(ID_TRACE)) {
330 return fEvent.getTrace().getName();
331 } else if (id.equals(ID_MODEL_URI)) {
332 return ((ITmfModelLookup)fEvent).getModelUri();
333 } else if (id.equals(ID_SOURCE_LOOKUP)) {
334 return new SourceLookupPropertySource(((ITmfSourceLookup)fEvent));
335 } else if (id.equals(ID_CONTENT) && fEvent.getContent() != null) {
336 return new ContentPropertySource(fEvent.getContent());
337 } else if (id.equals(ID_CUSTOM_ATTRIBUTE)) {
338 return new CustomAttributePropertySource((ITmfCustomAttributes) fEvent);
339 }
340 return null;
341 }
342
343 @Override
344 public boolean isPropertySet(Object id) {
345 return false;
346 }
347
348 @Override
349 public void resetPropertyValue(Object id) {
350 }
351
352 @Override
353 public void setPropertyValue(Object id, Object value) {
354 }
355
356 }
This page took 0.049633 seconds and 5 git commands to generate.