1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 * Bernd Hufmann - Added call site and model URI properties
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.events
;
16 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.nullToEmptyString
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
20 import java
.util
.Optional
;
21 import java
.util
.stream
.Stream
;
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
;
38 * Property source for events
40 public class TmfEventPropertySource
implements IPropertySource
{
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$
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$
52 private static final String EMPTY_STRING
= ""; //$NON-NLS-1$
54 private final @NonNull ITmfEvent fEvent
;
56 private static class ContentPropertySource
implements IPropertySource
{
57 private ITmfEventField fContent
;
59 public ContentPropertySource(ITmfEventField content
) {
64 public Object
getEditableValue() {
65 return fContent
.toString();
69 public IPropertyDescriptor
[] getPropertyDescriptors() {
70 List
<IPropertyDescriptor
> descriptors
= new ArrayList
<>(fContent
.getFields().size());
71 for (ITmfEventField field
: fContent
.getFields()) {
73 descriptors
.add(new ReadOnlyTextPropertyDescriptor(field
, field
.getName()));
76 return descriptors
.toArray(new IPropertyDescriptor
[0]);
80 public Object
getPropertyValue(Object id
) {
81 ITmfEventField field
= (ITmfEventField
) id
;
82 if (!field
.getFields().isEmpty()) {
83 return new ContentPropertySource(field
);
85 return field
.getFormattedValue();
89 public boolean isPropertySet(Object id
) {
94 public void resetPropertyValue(Object id
) {
98 public void setPropertyValue(Object id
, Object value
) {
102 private static class SourceLookupPropertySource
implements IPropertySource
{
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$
107 private static final String NAME_FILE_NAME
= "File"; //$NON-NLS-1$
108 private static final String NAME_LINE_NUMBER
= "Line"; //$NON-NLS-1$
110 private final ITmfSourceLookup fSourceLookup
;
112 public SourceLookupPropertySource(ITmfSourceLookup lookup
) {
113 fSourceLookup
= lookup
;
117 public Object
getEditableValue() {
118 ITmfCallsite cs
= fSourceLookup
.getCallsite();
120 return cs
.toString();
126 public IPropertyDescriptor
[] getPropertyDescriptors() {
127 List
<IPropertyDescriptor
> descriptors
= new ArrayList
<>();
128 ITmfCallsite cs
= fSourceLookup
.getCallsite();
130 descriptors
.add(new ReadOnlyTextPropertyDescriptor(ID_FILE_NAME
, NAME_FILE_NAME
));
131 descriptors
.add(new ReadOnlyTextPropertyDescriptor(ID_LINE_NUMBER
, NAME_LINE_NUMBER
));
133 return descriptors
.toArray(new IPropertyDescriptor
[0]);
137 public String
getPropertyValue(Object id
) {
138 ITmfCallsite cs
= fSourceLookup
.getCallsite();
141 * The callsite should not be null here, we would not have
142 * created the descriptors otherwise
144 throw new IllegalStateException();
147 if (!(id
instanceof String
)) {
151 switch ((String
) id
) {
153 return cs
.getFileName();
155 return nullToEmptyString(cs
.getLineNo());
162 public boolean isPropertySet(Object id
) {
167 public void resetPropertyValue(Object id
) {
172 public void setPropertyValue(Object id
, Object value
) {
176 private class CustomAttributePropertySource
implements IPropertySource
{
178 private final ITmfCustomAttributes event
;
180 public CustomAttributePropertySource(ITmfCustomAttributes event
) {
185 public Object
getEditableValue() {
190 public IPropertyDescriptor
[] getPropertyDescriptors() {
191 List
<IPropertyDescriptor
> descriptors
= new ArrayList
<>();
193 for (String customAttribute
: event
.listCustomAttributes()) {
194 descriptors
.add(new ReadOnlyTextPropertyDescriptor(customAttribute
, customAttribute
));
197 return descriptors
.toArray(new IPropertyDescriptor
[0]);
201 public Object
getPropertyValue(Object id
) {
202 if (!(id
instanceof String
)) {
205 return event
.getCustomAttribute((String
) id
);
209 public boolean isPropertySet(Object id
) {
214 public void resetPropertyValue(Object id
) {
218 public void setPropertyValue(Object id
, Object value
) {
223 * Default constructor
225 * @param event the event
227 public TmfEventPropertySource(@NonNull ITmfEvent event
) {
233 public Object
getEditableValue() {
238 public IPropertyDescriptor
[] getPropertyDescriptors() {
239 List
<IPropertyDescriptor
> descriptors
= new ArrayList
<>();
241 /* Display properties for event aspects */
242 getTraceAspects().forEach(aspect
-> {
244 * Contents has its special property source, which puts the fields
247 if (aspect
== TmfBaseAspects
.getContentsAspect()) {
248 descriptors
.add(new ReadOnlyTextPropertyDescriptor(ID_CONTENT
, NAME_CONTENT
));
250 String name
= aspect
.getName();
251 descriptors
.add(new ReadOnlyTextPropertyDescriptor(name
, name
));
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
));
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
));
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
));
273 return descriptors
.toArray(new IPropertyDescriptor
[0]);
277 public Object
getPropertyValue(Object objectId
) {
278 if (!(objectId
instanceof String
)) {
282 String id
= (String
) objectId
;
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
);
294 /* Look for the ID in the aspect names */
295 Optional
<ITmfEventAspect
<?
>> potentialAspect
= getTraceAspects()
296 .filter(aspect
-> aspect
.getName().equals(id
))
299 if (potentialAspect
.isPresent()) {
300 Object res
= potentialAspect
.get().resolve(fEvent
);
301 return (res
== null ?
"" : res
.toString()); //$NON-NLS-1$
307 private Stream
<ITmfEventAspect
<?
>> getTraceAspects() {
308 return StreamUtils
.getStream(fEvent
.getTrace().getEventAspects());
312 public boolean isPropertySet(Object id
) {
317 public void resetPropertyValue(Object id
) {
321 public void setPropertyValue(Object id
, Object value
) {