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
CommitLineData
93bfd50a 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2012, 2014 Ericsson
93bfd50a
PT
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:
080600d9
MAL
10 * Patrick Tasse - Initial API and implementation
11 * Bernd Hufmann - Added call site and model URI properties
93bfd50a
PT
12 *******************************************************************************/
13
2bdf0193 14package org.eclipse.tracecompass.tmf.ui.viewers.events;
93bfd50a 15
0c65e461
AM
16import static org.eclipse.tracecompass.common.core.NonNullUtils.nullToEmptyString;
17
93bfd50a
PT
18import java.util.ArrayList;
19import java.util.List;
459a0736
AM
20import java.util.Optional;
21import java.util.stream.Stream;
93bfd50a 22
459a0736
AM
23import org.eclipse.jdt.annotation.NonNull;
24import org.eclipse.tracecompass.common.core.StreamUtils;
2bdf0193
AM
25import org.eclipse.tracecompass.tmf.core.event.ITmfCustomAttributes;
26import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
27import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
459a0736
AM
28import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
29import org.eclipse.tracecompass.tmf.core.event.aspect.TmfBaseAspects;
04ca66e5 30import org.eclipse.tracecompass.tmf.core.event.lookup.ITmfCallsite;
2bdf0193
AM
31import org.eclipse.tracecompass.tmf.core.event.lookup.ITmfModelLookup;
32import org.eclipse.tracecompass.tmf.core.event.lookup.ITmfSourceLookup;
2bdf0193 33import org.eclipse.tracecompass.tmf.ui.properties.ReadOnlyTextPropertyDescriptor;
93bfd50a
PT
34import org.eclipse.ui.views.properties.IPropertyDescriptor;
35import org.eclipse.ui.views.properties.IPropertySource;
93bfd50a
PT
36
37/**
38 * Property source for events
93bfd50a
PT
39 */
40public class TmfEventPropertySource implements IPropertySource {
41
93bfd50a 42 private static final String ID_CONTENT = "event_content"; //$NON-NLS-1$
f47ed727
BH
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$
860b76d4 45 private static final String ID_CUSTOM_ATTRIBUTE = "custom_attribute"; //$NON-NLS-1$
f47ed727 46
93bfd50a 47 private static final String NAME_CONTENT = "Content"; //$NON-NLS-1$
860b76d4 48 private static final String NAME_SOURCE_LOOKUP = "Source Lookup"; //$NON-NLS-1$
f47ed727 49 private static final String NAME_MODEL_URI = "Model URI"; //$NON-NLS-1$
860b76d4 50 private static final String NAME_CUSTOM_ATTRIBUTES = "Custom Attributes"; //$NON-NLS-1$
f47ed727 51
860b76d4 52 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
93bfd50a 53
459a0736 54 private final @NonNull ITmfEvent fEvent;
93bfd50a 55
658e0268 56 private static class ContentPropertySource implements IPropertySource {
93bfd50a
PT
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() {
b742c196 70 List<IPropertyDescriptor> descriptors = new ArrayList<>(fContent.getFields().size());
93bfd50a
PT
71 for (ITmfEventField field : fContent.getFields()) {
72 if (field != null) {
0505c6fc 73 descriptors.add(new ReadOnlyTextPropertyDescriptor(field, field.getName()));
93bfd50a
PT
74 }
75 }
76 return descriptors.toArray(new IPropertyDescriptor[0]);
77 }
78
79 @Override
80 public Object getPropertyValue(Object id) {
81 ITmfEventField field = (ITmfEventField) id;
fafdd006 82 if (!field.getFields().isEmpty()) {
93bfd50a
PT
83 return new ContentPropertySource(field);
84 }
8f86c552 85 return field.getFormattedValue();
93bfd50a
PT
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
658e0268 102 private static class SourceLookupPropertySource implements IPropertySource {
f47ed727
BH
103
104 private static final String ID_FILE_NAME = "callsite_file"; //$NON-NLS-1$
f47ed727
BH
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$
f47ed727
BH
108 private static final String NAME_LINE_NUMBER = "Line"; //$NON-NLS-1$
109
574f43ad 110 private final ITmfSourceLookup fSourceLookup;
f47ed727
BH
111
112 public SourceLookupPropertySource(ITmfSourceLookup lookup) {
113 fSourceLookup = lookup;
114 }
115
116 @Override
117 public Object getEditableValue() {
04ca66e5
AM
118 ITmfCallsite cs = fSourceLookup.getCallsite();
119 if (cs != null) {
120 return cs.toString();
f47ed727
BH
121 }
122 return null;
123 }
124
125 @Override
126 public IPropertyDescriptor[] getPropertyDescriptors() {
507b1336 127 List<IPropertyDescriptor> descriptors= new ArrayList<>();
04ca66e5
AM
128 ITmfCallsite cs = fSourceLookup.getCallsite();
129 if (cs != null) {
0505c6fc
BH
130 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_FILE_NAME, NAME_FILE_NAME));
131 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_LINE_NUMBER, NAME_LINE_NUMBER));
f47ed727
BH
132 }
133 return descriptors.toArray(new IPropertyDescriptor[0]);
134 }
135
136 @Override
1685d123 137 public String getPropertyValue(Object id) {
04ca66e5
AM
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();
04ca66e5 154 case ID_LINE_NUMBER:
0c65e461 155 return nullToEmptyString(cs.getLineNo());
04ca66e5
AM
156 default:
157 return null;
f47ed727 158 }
f47ed727
BH
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
860b76d4
SD
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() {
507b1336 191 List<IPropertyDescriptor> descriptors = new ArrayList<>();
860b76d4
SD
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) {
aa353506
AM
202 if (!(id instanceof String)) {
203 return null;
204 }
860b76d4
SD
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 }
f47ed727 221
93bfd50a
PT
222 /**
223 * Default constructor
224 *
225 * @param event the event
226 */
459a0736 227 public TmfEventPropertySource(@NonNull ITmfEvent event) {
93bfd50a
PT
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() {
507b1336 239 List<IPropertyDescriptor> descriptors= new ArrayList<>();
860b76d4 240
459a0736
AM
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 });
860b76d4
SD
254
255 /* Display source lookup information, if the event supplies it */
f47ed727 256 if ((fEvent instanceof ITmfSourceLookup) && (((ITmfSourceLookup)fEvent).getCallsite() != null)) {
0505c6fc 257 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_SOURCE_LOOKUP, NAME_SOURCE_LOOKUP));
f47ed727 258 }
860b76d4
SD
259
260 /* Display Model URI information, if the event supplies it */
261 if ((fEvent instanceof ITmfModelLookup) && (((ITmfModelLookup) fEvent).getModelUri() != null)) {
0505c6fc 262 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_MODEL_URI, NAME_MODEL_URI));
f47ed727 263 }
860b76d4
SD
264
265 /* Display custom attributes, if available */
266 if (fEvent instanceof ITmfCustomAttributes) {
267 ITmfCustomAttributes event = (ITmfCustomAttributes) fEvent;
4c4e2816 268 if (!event.listCustomAttributes().isEmpty()) {
860b76d4
SD
269 descriptors.add(new ReadOnlyTextPropertyDescriptor(ID_CUSTOM_ATTRIBUTE, NAME_CUSTOM_ATTRIBUTES));
270 }
271 }
272
f47ed727 273 return descriptors.toArray(new IPropertyDescriptor[0]);
93bfd50a
PT
274 }
275
276 @Override
459a0736
AM
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)) {
f47ed727
BH
285 return ((ITmfModelLookup)fEvent).getModelUri();
286 } else if (id.equals(ID_SOURCE_LOOKUP)) {
287 return new SourceLookupPropertySource(((ITmfSourceLookup)fEvent));
93bfd50a
PT
288 } else if (id.equals(ID_CONTENT) && fEvent.getContent() != null) {
289 return new ContentPropertySource(fEvent.getContent());
860b76d4
SD
290 } else if (id.equals(ID_CUSTOM_ATTRIBUTE)) {
291 return new CustomAttributePropertySource((ITmfCustomAttributes) fEvent);
93bfd50a 292 }
459a0736
AM
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
93bfd50a
PT
304 return null;
305 }
306
459a0736
AM
307 private Stream<ITmfEventAspect<?>> getTraceAspects() {
308 return StreamUtils.getStream(fEvent.getTrace().getEventAspects());
309 }
310
93bfd50a
PT
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.106181 seconds and 5 git commands to generate.