tmf: Update event properties to use aspects
[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 import java.util.Optional;
21 import java.util.stream.Stream;
22
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.tracecompass.common.core.StreamUtils;
25 import org.eclipse.tracecompass.tmf.core.event.ITmfCustomAttributes;
26 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
27 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
28 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
29 import org.eclipse.tracecompass.tmf.core.event.aspect.TmfBaseAspects;
30 import org.eclipse.tracecompass.tmf.core.event.lookup.ITmfCallsite;
31 import org.eclipse.tracecompass.tmf.core.event.lookup.ITmfModelLookup;
32 import org.eclipse.tracecompass.tmf.core.event.lookup.ITmfSourceLookup;
33 import org.eclipse.tracecompass.tmf.ui.properties.ReadOnlyTextPropertyDescriptor;
34 import org.eclipse.ui.views.properties.IPropertyDescriptor;
35 import org.eclipse.ui.views.properties.IPropertySource;
36
37 /**
38 * Property source for events
39 */
40 public class TmfEventPropertySource implements IPropertySource {
41
42 private static final String ID_CONTENT = "event_content"; //$NON-NLS-1$
43 private static final String ID_SOURCE_LOOKUP = "event_lookup"; //$NON-NLS-1$
44 private static final String ID_MODEL_URI = "model_uri"; //$NON-NLS-1$
45 private static final String ID_CUSTOM_ATTRIBUTE = "custom_attribute"; //$NON-NLS-1$
46
47 private static final String NAME_CONTENT = "Content"; //$NON-NLS-1$
48 private static final String NAME_SOURCE_LOOKUP = "Source Lookup"; //$NON-NLS-1$
49 private static final String NAME_MODEL_URI = "Model URI"; //$NON-NLS-1$
50 private static final String NAME_CUSTOM_ATTRIBUTES = "Custom Attributes"; //$NON-NLS-1$
51
52 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
53
54 private final @NonNull ITmfEvent fEvent;
55
56 private static class ContentPropertySource implements IPropertySource {
57 private ITmfEventField fContent;
58
59 public ContentPropertySource(ITmfEventField content) {
60 fContent = content;
61 }
62
63 @Override
64 public Object getEditableValue() {
65 return fContent.toString();
66 }
67
68 @Override
69 public IPropertyDescriptor[] getPropertyDescriptors() {
70 List<IPropertyDescriptor> descriptors = new ArrayList<>(fContent.getFields().size());
71 for (ITmfEventField field : fContent.getFields()) {
72 if (field != null) {
73 descriptors.add(new ReadOnlyTextPropertyDescriptor(field, field.getName()));
74 }
75 }
76 return descriptors.toArray(new IPropertyDescriptor[0]);
77 }
78
79 @Override
80 public Object getPropertyValue(Object id) {
81 ITmfEventField field = (ITmfEventField) id;
82 if (!field.getFields().isEmpty()) {
83 return new ContentPropertySource(field);
84 }
85 return field.getFormattedValue();
86 }
87
88 @Override
89 public boolean isPropertySet(Object id) {
90 return false;
91 }
92
93 @Override
94 public void resetPropertyValue(Object id) {
95 }
96
97 @Override
98 public void setPropertyValue(Object id, Object value) {
99 }
100 }
101
102 private static class SourceLookupPropertySource implements IPropertySource {
103
104 private static final String ID_FILE_NAME = "callsite_file"; //$NON-NLS-1$
105 private static final String ID_LINE_NUMBER = "callsite_line"; //$NON-NLS-1$
106
107 private static final String NAME_FILE_NAME = "File"; //$NON-NLS-1$
108 private static final String NAME_LINE_NUMBER = "Line"; //$NON-NLS-1$
109
110 private final ITmfSourceLookup fSourceLookup;
111
112 public SourceLookupPropertySource(ITmfSourceLookup lookup) {
113 fSourceLookup = lookup;
114 }
115
116 @Override
117 public Object getEditableValue() {
118 ITmfCallsite cs = fSourceLookup.getCallsite();
119 if (cs != null) {
120 return cs.toString();
121 }
122 return null;
123 }
124
125 @Override
126 public IPropertyDescriptor[] getPropertyDescriptors() {
127 List<IPropertyDescriptor> descriptors= new ArrayList<>();
128 ITmfCallsite cs = fSourceLookup.getCallsite();
129 if (cs != null) {
130 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_FILE_NAME, NAME_FILE_NAME));
131 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_LINE_NUMBER, NAME_LINE_NUMBER));
132 }
133 return descriptors.toArray(new IPropertyDescriptor[0]);
134 }
135
136 @Override
137 public String getPropertyValue(Object id) {
138 ITmfCallsite cs = fSourceLookup.getCallsite();
139 if (cs == null) {
140 /*
141 * The callsite should not be null here, we would not have
142 * created the descriptors otherwise
143 */
144 throw new IllegalStateException();
145 }
146
147 if (!(id instanceof String)) {
148 return null;
149 }
150
151 switch ((String) id) {
152 case ID_FILE_NAME:
153 return cs.getFileName();
154 case ID_LINE_NUMBER:
155 return nullToEmptyString(cs.getLineNo());
156 default:
157 return null;
158 }
159 }
160
161 @Override
162 public boolean isPropertySet(Object id) {
163 return false;
164 }
165
166 @Override
167 public void resetPropertyValue(Object id) {
168
169 }
170
171 @Override
172 public void setPropertyValue(Object id, Object value) {
173 }
174 }
175
176 private class CustomAttributePropertySource implements IPropertySource {
177
178 private final ITmfCustomAttributes event;
179
180 public CustomAttributePropertySource(ITmfCustomAttributes event) {
181 this.event = event;
182 }
183
184 @Override
185 public Object getEditableValue() {
186 return EMPTY_STRING;
187 }
188
189 @Override
190 public IPropertyDescriptor[] getPropertyDescriptors() {
191 List<IPropertyDescriptor> descriptors = new ArrayList<>();
192
193 for (String customAttribute : event.listCustomAttributes()) {
194 descriptors.add(new ReadOnlyTextPropertyDescriptor(customAttribute, customAttribute));
195 }
196
197 return descriptors.toArray(new IPropertyDescriptor[0]);
198 }
199
200 @Override
201 public Object getPropertyValue(Object id) {
202 if (!(id instanceof String)) {
203 return null;
204 }
205 return event.getCustomAttribute((String) id);
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 @Override
218 public void setPropertyValue(Object id, Object value) {
219 }
220 }
221
222 /**
223 * Default constructor
224 *
225 * @param event the event
226 */
227 public TmfEventPropertySource(@NonNull ITmfEvent event) {
228 super();
229 this.fEvent = event;
230 }
231
232 @Override
233 public Object getEditableValue() {
234 return null;
235 }
236
237 @Override
238 public IPropertyDescriptor[] getPropertyDescriptors() {
239 List<IPropertyDescriptor> descriptors= new ArrayList<>();
240
241 /* Display properties for event aspects */
242 getTraceAspects().forEach(aspect -> {
243 /*
244 * Contents has its special property source, which puts the fields
245 * in a sub-tree.
246 */
247 if (aspect == TmfBaseAspects.getContentsAspect()) {
248 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_CONTENT, NAME_CONTENT));
249 } else {
250 String name = aspect.getName();
251 descriptors.add(new ReadOnlyTextPropertyDescriptor(name, name));
252 }
253 });
254
255 /* Display source lookup information, if the event supplies it */
256 if ((fEvent instanceof ITmfSourceLookup) && (((ITmfSourceLookup)fEvent).getCallsite() != null)) {
257 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_SOURCE_LOOKUP, NAME_SOURCE_LOOKUP));
258 }
259
260 /* Display Model URI information, if the event supplies it */
261 if ((fEvent instanceof ITmfModelLookup) && (((ITmfModelLookup) fEvent).getModelUri() != null)) {
262 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_MODEL_URI, NAME_MODEL_URI));
263 }
264
265 /* Display custom attributes, if available */
266 if (fEvent instanceof ITmfCustomAttributes) {
267 ITmfCustomAttributes event = (ITmfCustomAttributes) fEvent;
268 if (!event.listCustomAttributes().isEmpty()) {
269 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_CUSTOM_ATTRIBUTE, NAME_CUSTOM_ATTRIBUTES));
270 }
271 }
272
273 return descriptors.toArray(new IPropertyDescriptor[0]);
274 }
275
276 @Override
277 public Object getPropertyValue(Object objectId) {
278 if (!(objectId instanceof String)) {
279 return null;
280 }
281
282 String id = (String) objectId;
283
284 if (id.equals(ID_MODEL_URI)) {
285 return ((ITmfModelLookup)fEvent).getModelUri();
286 } else if (id.equals(ID_SOURCE_LOOKUP)) {
287 return new SourceLookupPropertySource(((ITmfSourceLookup)fEvent));
288 } else if (id.equals(ID_CONTENT) && fEvent.getContent() != null) {
289 return new ContentPropertySource(fEvent.getContent());
290 } else if (id.equals(ID_CUSTOM_ATTRIBUTE)) {
291 return new CustomAttributePropertySource((ITmfCustomAttributes) fEvent);
292 }
293
294 /* Look for the ID in the aspect names */
295 Optional<ITmfEventAspect<?>> potentialAspect = getTraceAspects()
296 .filter(aspect -> aspect.getName().equals(id))
297 .findFirst();
298
299 if (potentialAspect.isPresent()) {
300 Object res = potentialAspect.get().resolve(fEvent);
301 return (res == null ? "" : res.toString()); //$NON-NLS-1$
302 }
303
304 return null;
305 }
306
307 private Stream<ITmfEventAspect<?>> getTraceAspects() {
308 return StreamUtils.getStream(fEvent.getTrace().getEventAspects());
309 }
310
311 @Override
312 public boolean isPropertySet(Object id) {
313 return false;
314 }
315
316 @Override
317 public void resetPropertyValue(Object id) {
318 }
319
320 @Override
321 public void setPropertyValue(Object id, Object value) {
322 }
323
324 }
This page took 0.038727 seconds and 5 git commands to generate.