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