7b944e9016a480845897fb7d70fa939d6d5757be
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / statesystem / TmfStateSystemViewer.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2016 École Polytechnique de Montréal and others.
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 * Florian Wininger - Initial API and implementation
11 * Alexandre Montplaisir - Refactoring, performance tweaks
12 * Bernd Hufmann - Updated signal handling
13 * Marc-Andre Laperle - Add time zone preference
14 * Geneviève Bastien - Moved state system explorer to use the abstract tree viewer
15 * Patrick Tasse - Refactoring
16 *******************************************************************************/
17
18 package org.eclipse.tracecompass.tmf.ui.views.statesystem;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.jface.viewers.AbstractTreeViewer;
25 import org.eclipse.jface.viewers.Viewer;
26 import org.eclipse.jface.viewers.ViewerComparator;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.graphics.Color;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
32 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
33 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
34 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
35 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
36 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
37 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
38 import org.eclipse.tracecompass.tmf.core.signal.TmfTimestampFormatUpdateSignal;
39 import org.eclipse.tracecompass.tmf.core.statesystem.ITmfAnalysisModuleWithStateSystems;
40 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
41 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
42 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
43 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceContext;
44 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
45 import org.eclipse.tracecompass.tmf.ui.viewers.tree.AbstractTmfTreeViewer;
46 import org.eclipse.tracecompass.tmf.ui.viewers.tree.ITmfTreeColumnDataProvider;
47 import org.eclipse.tracecompass.tmf.ui.viewers.tree.ITmfTreeViewerEntry;
48 import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeColumnData;
49 import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeViewerEntry;
50
51 /**
52 * Displays the content of the state systems at the current time
53 *
54 * @author Florian Wininger
55 * @author Alexandre Montplaisir
56 * @author Geneviève Bastien
57 */
58 public class TmfStateSystemViewer extends AbstractTmfTreeViewer {
59
60 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
61 private static final int DEFAULT_AUTOEXPAND = 2;
62 private boolean fFilterStatus = false;
63 private long fSelection = 0;
64
65 /* Order of columns */
66 private static final int ATTRIBUTE_NAME_COL = 0;
67 private static final int QUARK_COL = 1;
68 private static final int VALUE_COL = 2;
69 private static final int TYPE_COL = 3;
70 private static final int START_TIME_COL = 4;
71 private static final int END_TIME_COL = 5;
72 private static final int ATTRIBUTE_FULLPATH_COL = 6;
73
74 /**
75 * Base class to provide the labels for the tree viewer. Views extending
76 * this class typically need to override the getColumnText method if they
77 * have more than one column to display
78 */
79 protected static class StateSystemTreeLabelProvider extends TreeLabelProvider {
80
81 @Override
82 public String getColumnText(Object element, int columnIndex) {
83 if (element instanceof StateEntry) {
84 StateEntry entry = (StateEntry) element;
85 switch (columnIndex) {
86 case ATTRIBUTE_NAME_COL:
87 return entry.getName();
88 case QUARK_COL:
89 return String.valueOf(entry.getQuark());
90 case VALUE_COL:
91 return entry.getValue();
92 case TYPE_COL:
93 return entry.getType();
94 case START_TIME_COL:
95 return entry.getStartTime();
96 case END_TIME_COL:
97 return entry.getEndTime();
98 case ATTRIBUTE_FULLPATH_COL:
99 return entry.getFullPath();
100 default:
101 return EMPTY_STRING;
102 }
103 }
104 return super.getColumnText(element, columnIndex);
105 }
106
107 @Override
108 public Color getBackground(Object element, int columnIndex) {
109 if (element instanceof StateEntry) {
110 if (((StateEntry) element).isModified()) {
111 return Display.getCurrent().getSystemColor(SWT.COLOR_YELLOW);
112 }
113 }
114 return super.getBackground(element, columnIndex);
115 }
116 }
117
118 /**
119 * Constructor
120 *
121 * @param parent
122 * The parent containing this viewer
123 */
124 public TmfStateSystemViewer(Composite parent) {
125 super(parent, false);
126 this.setLabelProvider(new StateSystemTreeLabelProvider());
127 getTreeViewer().setAutoExpandLevel(DEFAULT_AUTOEXPAND);
128 }
129
130 @Override
131 protected ITmfTreeColumnDataProvider getColumnDataProvider() {
132 return new ITmfTreeColumnDataProvider() {
133
134 @Override
135 public List<TmfTreeColumnData> getColumnData() {
136 List<TmfTreeColumnData> columns = new ArrayList<>();
137 TmfTreeColumnData column = new TmfTreeColumnData(Messages.TreeNodeColumnLabel);
138 columns.add(column);
139 column.setComparator(new ViewerComparator() {
140 @Override
141 public int compare(Viewer viewer, Object e1, Object e2) {
142 TmfTreeViewerEntry n1 = (TmfTreeViewerEntry) e1;
143 TmfTreeViewerEntry n2 = (TmfTreeViewerEntry) e2;
144
145 return n1.getName().compareTo(n2.getName());
146 }
147 });
148 columns.add(new TmfTreeColumnData(Messages.QuarkColumnLabel));
149 columns.add(new TmfTreeColumnData(Messages.ValueColumnLabel));
150 columns.add(new TmfTreeColumnData(Messages.TypeColumnLabel));
151 columns.add(new TmfTreeColumnData(Messages.StartTimeColumLabel));
152 columns.add(new TmfTreeColumnData(Messages.EndTimeColumLabel));
153 columns.add(new TmfTreeColumnData(Messages.AttributePathColumnLabel));
154 return columns;
155 }
156
157 };
158 }
159
160 // ------------------------------------------------------------------------
161 // Operations
162 // ------------------------------------------------------------------------
163
164 @Override
165 protected ITmfTreeViewerEntry updateElements(long start, long end, boolean selection) {
166
167 if (selection) {
168 fSelection = start;
169 } else {
170 TmfTraceContext ctx = TmfTraceManager.getInstance().getCurrentTraceContext();
171 fSelection = ctx.getSelectionRange().getStartTime().toNanos();
172 }
173
174 if (getTrace() == null) {
175 return null;
176 }
177
178 ITmfTreeViewerEntry root = getInput();
179
180 if (root == null) {
181 root = createRoot();
182 } else if (fFilterStatus) {
183 clearStateSystemEntries(root);
184 }
185
186 /*
187 * Update the values of the elements of the state systems at the
188 * selection start time
189 */
190 boolean changed = updateStateSystemEntries(root, fSelection);
191
192 return selection || changed ? root : null;
193 }
194
195 private ITmfTreeViewerEntry createRoot() {
196 // 'Fake' root node
197 TmfTreeViewerEntry rootEntry = new TmfTreeViewerEntry("root"); //$NON-NLS-1$
198
199 for (final ITmfTrace trace : TmfTraceManager.getTraceSetWithExperiment(getTrace())) {
200 if (trace != null) {
201 rootEntry.addChild(createTraceEntry(trace));
202 }
203 }
204 return rootEntry;
205 }
206
207 private static TmfTreeViewerEntry createTraceEntry(ITmfTrace trace) {
208 TmfTreeViewerEntry traceEntry = new TmfTreeViewerEntry(trace.getName());
209 Iterable<IAnalysisModule> modules = trace.getAnalysisModules();
210 for (IAnalysisModule module : modules) {
211 if (module instanceof ITmfAnalysisModuleWithStateSystems) {
212 ITmfAnalysisModuleWithStateSystems moduleWithStateSystem = (ITmfAnalysisModuleWithStateSystems) module;
213 /*
214 * Just schedule the module, the data will be filled when
215 * available
216 */
217 moduleWithStateSystem.schedule();
218 if (!moduleWithStateSystem.waitForInitialization()) {
219 continue;
220 }
221 for (ITmfStateSystem ss : moduleWithStateSystem.getStateSystems()) {
222 traceEntry.addChild(new StateSystemEntry(ss));
223 }
224 }
225 }
226 return traceEntry;
227 }
228
229 private static void clearStateSystemEntries(ITmfTreeViewerEntry root) {
230 for (ITmfTreeViewerEntry traceEntry : root.getChildren()) {
231 for (ITmfTreeViewerEntry ssEntry : traceEntry.getChildren()) {
232 ssEntry.getChildren().clear();
233 }
234 }
235 }
236
237 private boolean updateStateSystemEntries(ITmfTreeViewerEntry root, long timestamp) {
238 boolean changed = false;
239 for (ITmfTreeViewerEntry traceEntry : root.getChildren()) {
240 for (ITmfTreeViewerEntry ssEntry : traceEntry.getChildren()) {
241 StateSystemEntry stateSystemEntry = (StateSystemEntry) ssEntry;
242 ITmfStateSystem ss = stateSystemEntry.getSS();
243 try {
244 List<ITmfStateInterval> fullState = ss.queryFullState(timestamp);
245 changed |= updateStateEntries(ss, fullState, stateSystemEntry, -1, timestamp);
246 } catch (TimeRangeException e) {
247 markOutOfRange(stateSystemEntry);
248 changed = true;
249 } catch (StateSystemDisposedException e) {
250 /* Ignored */
251 }
252 }
253 }
254 return changed;
255 }
256
257 private boolean updateStateEntries(ITmfStateSystem ss, List<ITmfStateInterval> fullState, TmfTreeViewerEntry parent, int parentQuark, long timestamp) {
258 boolean changed = false;
259 for (int quark : ss.getSubAttributes(parentQuark, false)) {
260 if (quark >= fullState.size()) {
261 // attribute was created after the full state query
262 continue;
263 }
264 ITmfStateInterval interval = fullState.get(quark);
265 StateEntry stateEntry = findStateEntry(parent, quark);
266 if (stateEntry == null) {
267 boolean modified = fFilterStatus ?
268 interval.getStartTime() == timestamp :
269 !interval.getStateValue().isNull();
270 stateEntry = new StateEntry(ss.getAttributeName(quark), quark, ss.getFullAttributePath(quark),
271 interval.getStateValue(),
272 TmfTimestamp.fromNanos(interval.getStartTime()),
273 TmfTimestamp.fromNanos(interval.getEndTime()),
274 modified);
275
276 // update children first to know if parent is really needed
277 updateStateEntries(ss, fullState, stateEntry, quark, timestamp);
278
279 /*
280 * Add this entry to parent if filtering is off, or
281 * if the entry has children to display, or
282 * if there is a state change at the current timestamp
283 */
284 if (!fFilterStatus || stateEntry.hasChildren() || interval.getStartTime() == timestamp) {
285 parent.addChild(stateEntry);
286 changed = true;
287 }
288 } else {
289 stateEntry.update(interval.getStateValue(),
290 TmfTimestamp.fromNanos(interval.getStartTime()),
291 TmfTimestamp.fromNanos(interval.getEndTime()));
292
293 // update children recursively
294 updateStateEntries(ss, fullState, stateEntry, quark, timestamp);
295 }
296
297 }
298 return changed;
299 }
300
301 private static StateEntry findStateEntry(TmfTreeViewerEntry parent, int quark) {
302 for (ITmfTreeViewerEntry child : parent.getChildren()) {
303 StateEntry stateEntry = (StateEntry) child;
304 if (stateEntry.getQuark() == quark) {
305 return stateEntry;
306 }
307 }
308 return null;
309 }
310 /**
311 * Set the entries as out of range
312 */
313 private static void markOutOfRange(ITmfTreeViewerEntry parent) {
314 for (ITmfTreeViewerEntry entry : parent.getChildren()) {
315 if (entry instanceof StateEntry) {
316 ((StateEntry) entry).setOutOfRange();
317
318 /* Update this node's children recursively */
319 markOutOfRange(entry);
320 }
321 }
322 }
323
324 /**
325 * Set the filter status of the viewer. By default, all entries of all state
326 * system are present, and the values that changed since last refresh are
327 * shown in yellow. When the filter status is true, only the entries with
328 * values modified at current time are displayed.
329 */
330 public void changeFilterStatus() {
331 fFilterStatus = !fFilterStatus;
332 if (fFilterStatus) {
333 getTreeViewer().setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS);
334 } else {
335 getTreeViewer().setAutoExpandLevel(DEFAULT_AUTOEXPAND);
336 clearContent();
337 }
338 updateContent(getSelectionBeginTime(), getSelectionEndTime(), true);
339 }
340
341 /**
342 * Update the display to use the updated timestamp format
343 *
344 * @param signal
345 * the incoming signal
346 */
347 @TmfSignalHandler
348 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal) {
349 updateContent(getSelectionBeginTime(), getSelectionEndTime(), true);
350 }
351
352 private static class StateSystemEntry extends TmfTreeViewerEntry {
353 private final @NonNull ITmfStateSystem fSS;
354
355 public StateSystemEntry(@NonNull ITmfStateSystem ss) {
356 super(ss.getSSID());
357 fSS = ss;
358 }
359
360 public @NonNull ITmfStateSystem getSS() {
361 return fSS;
362 }
363 }
364
365 private class StateEntry extends TmfTreeViewerEntry {
366
367 private final int fQuark;
368 private final String fFullPath;
369 private @NonNull ITmfTimestamp fStart;
370 private @NonNull ITmfTimestamp fEnd;
371 private ITmfStateValue fValue;
372 private boolean fModified;
373 private boolean fOutOfRange = false;
374
375 public StateEntry(String name, int quark, String fullPath, ITmfStateValue value, @NonNull ITmfTimestamp start, @NonNull ITmfTimestamp end, boolean modified) {
376 super(name);
377 fQuark = quark;
378 fFullPath = fullPath;
379 fStart = start;
380 fEnd = end;
381 fValue = value;
382 fModified = modified;
383 }
384
385 public int getQuark() {
386 return fQuark;
387 }
388
389 public String getFullPath() {
390 return fFullPath;
391 }
392
393 public String getStartTime() {
394 if (fOutOfRange) {
395 return EMPTY_STRING;
396 }
397 return fStart.toString();
398 }
399
400 public String getEndTime() {
401 if (fOutOfRange) {
402 return EMPTY_STRING;
403 }
404 return fEnd.toString();
405 }
406
407 public String getValue() {
408 if (fOutOfRange) {
409 return Messages.OutOfRangeMsg;
410 }
411 switch (fValue.getType()) {
412 case INTEGER:
413 case LONG:
414 case DOUBLE:
415 case STRING:
416 case CUSTOM:
417 return fValue.toString();
418 case NULL:
419 default:
420 return EMPTY_STRING;
421 }
422 }
423
424 public String getType() {
425 if (fOutOfRange) {
426 return EMPTY_STRING;
427 }
428 switch (fValue.getType()) {
429 case INTEGER:
430 return Messages.TypeInteger;
431 case LONG:
432 return Messages.TypeLong;
433 case DOUBLE:
434 return Messages.TypeDouble;
435 case STRING:
436 return Messages.TypeString;
437 case CUSTOM:
438 return Messages.TypeCustom;
439 case NULL:
440 default:
441 return EMPTY_STRING;
442 }
443 }
444
445 public boolean isModified() {
446 return fModified;
447 }
448
449 public void update(ITmfStateValue value, @NonNull ITmfTimestamp start, @NonNull ITmfTimestamp end) {
450 fModified = false;
451 fOutOfRange = false;
452 if (!start.equals(fStart)) {
453 fModified = true;
454 fStart = start;
455 fEnd = end;
456 fValue = value;
457 }
458 }
459
460 public void setOutOfRange() {
461 fModified = false;
462 fOutOfRange = true;
463 }
464 }
465 }
This page took 0.041783 seconds and 4 git commands to generate.