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