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