tmf: Ellipse function names that don't fit in CallStackView
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / callstack / CallStackPresentationProvider.java
CommitLineData
e8251298 1/*******************************************************************************
ed48dc75 2 * Copyright (c) 2013, 2016 Ericsson
e8251298
PT
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made 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 *******************************************************************************/
12
2bdf0193 13package org.eclipse.tracecompass.tmf.ui.views.callstack;
e8251298 14
6f8332c4
AM
15import java.util.Optional;
16
e8251298
PT
17import org.eclipse.swt.SWT;
18import org.eclipse.swt.graphics.GC;
19import org.eclipse.swt.graphics.RGB;
20import org.eclipse.swt.graphics.Rectangle;
2bdf0193
AM
21import org.eclipse.tracecompass.internal.tmf.ui.Activator;
22import org.eclipse.tracecompass.internal.tmf.ui.Messages;
e894a508 23import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
e894a508
AM
24import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
25import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
26import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
2bdf0193
AM
27import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.StateItem;
28import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphPresentationProvider;
29import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
30import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
31import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent;
32import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils;
e8251298 33
6f8332c4
AM
34import com.google.common.cache.CacheBuilder;
35import com.google.common.cache.CacheLoader;
36import com.google.common.cache.LoadingCache;
37
e8251298
PT
38/**
39 * Presentation provider for the Call Stack view, based on the generic TMF
40 * presentation provider.
41 *
42 * @author Patrick Tasse
e8251298
PT
43 */
44public class CallStackPresentationProvider extends TimeGraphPresentationProvider {
45
46 /** Number of colors used for call stack events */
47 public static final int NUM_COLORS = 360;
48
ade0a3c5 49 private CallStackView fView;
5da83da5 50
2984a466
AM
51 /**
52 * Minimum width of a displayed state below which we will not print any text
53 * into it. It corresponds to the average width of 1 char, plus the width of
54 * the ellipsis characters.
55 */
56 private Integer fMinimumBarWidth;
0a563f93 57
6f8332c4
AM
58 private final LoadingCache<CallStackEvent, Optional<String>> fTimeEventNames = CacheBuilder.newBuilder()
59 .maximumSize(1000)
60 .build(new CacheLoader<CallStackEvent, Optional<String>>() {
61 @Override
62 public Optional<String> load(CallStackEvent event) {
63 CallStackEntry entry = event.getEntry();
64 ITmfStateSystem ss = entry.getStateSystem();
65 try {
66 ITmfStateValue value = ss.querySingleState(event.getTime(), entry.getQuark()).getStateValue();
67 if (!value.isNull()) {
68 String name = fView.getFunctionName(entry.getTrace(), entry.getProcessId(), event.getTime(), value);
69 return Optional.ofNullable(name);
70 }
71 } catch (TimeRangeException e) {
72 Activator.getDefault().logError("Error querying state system", e); //$NON-NLS-1$
73 } catch (StateSystemDisposedException e) {
74 /* Ignored */
75 }
76 return Optional.empty();
77 }
78 });
79
e8251298
PT
80 private enum State {
81 MULTIPLE (new RGB(100, 100, 100)),
82 EXEC (new RGB(0, 200, 0));
83
52974e38 84 private final RGB rgb;
e8251298
PT
85
86 private State (RGB rgb) {
87 this.rgb = rgb;
88 }
89 }
90
5da83da5
AM
91 /**
92 * Constructor
93 *
066b02aa 94 * @since 1.2
ade0a3c5
PT
95 */
96 public CallStackPresentationProvider() {
97 }
98
99 /**
100 * Sets the call stack view
101 *
5da83da5 102 * @param view
ade0a3c5 103 * The call stack view that will contain the time events
066b02aa 104 * @since 1.2
5da83da5 105 */
ade0a3c5 106 public void setCallStackView(CallStackView view) {
5da83da5
AM
107 fView = view;
108 }
109
e8251298
PT
110 @Override
111 public String getStateTypeName(ITimeGraphEntry entry) {
60b4d44c 112 return Messages.CallStackPresentationProvider_Thread;
e8251298
PT
113 }
114
115 @Override
116 public StateItem[] getStateTable() {
52974e38
PT
117 final float saturation = 0.6f;
118 final float brightness = 0.6f;
e8251298
PT
119 StateItem[] stateTable = new StateItem[NUM_COLORS + 1];
120 stateTable[0] = new StateItem(State.MULTIPLE.rgb, State.MULTIPLE.toString());
121 for (int i = 0; i < NUM_COLORS; i++) {
52974e38 122 RGB rgb = new RGB(i, saturation, brightness);
e8251298
PT
123 stateTable[i + 1] = new StateItem(rgb, State.EXEC.toString());
124 }
125 return stateTable;
126 }
127
128 @Override
129 public int getStateTableIndex(ITimeEvent event) {
130 if (event instanceof CallStackEvent) {
131 CallStackEvent callStackEvent = (CallStackEvent) event;
132 return callStackEvent.getValue() + 1;
133 } else if (event instanceof NullTimeEvent) {
134 return INVISIBLE;
135 }
136 return State.MULTIPLE.ordinal();
137 }
138
139 @Override
140 public String getEventName(ITimeEvent event) {
141 if (event instanceof CallStackEvent) {
6f8332c4 142 return fTimeEventNames.getUnchecked((CallStackEvent) event).orElse(null);
e8251298
PT
143 }
144 return State.MULTIPLE.toString();
145 }
146
147 @Override
148 public void postDrawEvent(ITimeEvent event, Rectangle bounds, GC gc) {
2984a466
AM
149 if (!(event instanceof CallStackEvent)) {
150 return;
0a563f93 151 }
2984a466
AM
152
153 if (fMinimumBarWidth == null) {
154 fMinimumBarWidth = gc.getFontMetrics().getAverageCharWidth() + gc.stringExtent(Utils.ELLIPSIS).x;
155 }
156 if (bounds.width <= fMinimumBarWidth) {
157 /*
158 * Don't print anything if we cannot at least show one character and
159 * ellipses.
160 */
e8251298
PT
161 return;
162 }
2984a466
AM
163
164 String name = fTimeEventNames.getUnchecked((CallStackEvent) event).orElse(""); //$NON-NLS-1$
165 if (name.isEmpty()) {
166 /* No text to print */
e8251298
PT
167 return;
168 }
2984a466 169
6f8332c4
AM
170 gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE));
171 Utils.drawText(gc, name, bounds.x, bounds.y, bounds.width, bounds.height, true, true);
172 }
173
174 /**
175 * Indicate that the provider of function names has changed, so any cached
176 * values must be reset.
177 */
178 void resetFunctionNames() {
179 fTimeEventNames.invalidateAll();
e8251298
PT
180 }
181
182}
This page took 0.095651 seconds and 5 git commands to generate.