Fix performance tests by changing derby dependency to derby.core
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / statesystem / TmfStateSystemViewer.java
CommitLineData
17c73d10
GB
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
7c246810 15 * Patrick Tasse - Refactoring
17c73d10
GB
16 *******************************************************************************/
17
2bdf0193 18package org.eclipse.tracecompass.tmf.ui.views.statesystem;
17c73d10
GB
19
20import java.util.ArrayList;
21import java.util.List;
22
23import org.eclipse.jdt.annotation.NonNull;
24import org.eclipse.jface.viewers.AbstractTreeViewer;
25import org.eclipse.jface.viewers.Viewer;
26import org.eclipse.jface.viewers.ViewerComparator;
17c73d10
GB
27import org.eclipse.swt.SWT;
28import org.eclipse.swt.graphics.Color;
29import org.eclipse.swt.widgets.Composite;
30import org.eclipse.swt.widgets.Display;
e894a508
AM
31import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
32import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
33import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
34import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
35import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
36import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
2bdf0193
AM
37import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
38import org.eclipse.tracecompass.tmf.core.signal.TmfTimestampFormatUpdateSignal;
39import org.eclipse.tracecompass.tmf.core.statesystem.ITmfAnalysisModuleWithStateSystems;
40import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
41import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
42import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
43import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
44import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
b8585c7c 45import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
2bdf0193
AM
46import org.eclipse.tracecompass.tmf.ui.viewers.tree.AbstractTmfTreeViewer;
47import org.eclipse.tracecompass.tmf.ui.viewers.tree.ITmfTreeColumnDataProvider;
48import org.eclipse.tracecompass.tmf.ui.viewers.tree.ITmfTreeViewerEntry;
49import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeColumnData;
50import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeViewerEntry;
17c73d10
GB
51
52/**
53 * Displays the content of the state systems at the current time
54 *
55 * @author Florian Wininger
56 * @author Alexandre Montplaisir
57 * @author Geneviève Bastien
17c73d10
GB
58 */
59public class TmfStateSystemViewer extends AbstractTmfTreeViewer {
60
61 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
17c73d10 62 private static final int DEFAULT_AUTOEXPAND = 2;
7c246810
PT
63 private boolean fFilterStatus = false;
64 private long fSelection = 0;
17c73d10
GB
65
66 /* Order of columns */
67 private static final int ATTRIBUTE_NAME_COL = 0;
68 private static final int QUARK_COL = 1;
69 private static final int VALUE_COL = 2;
70 private static final int TYPE_COL = 3;
71 private static final int START_TIME_COL = 4;
72 private static final int END_TIME_COL = 5;
73 private static final int ATTRIBUTE_FULLPATH_COL = 6;
74
75 /**
76 * Base class to provide the labels for the tree viewer. Views extending
77 * this class typically need to override the getColumnText method if they
78 * have more than one column to display
79 */
80 protected static class StateSystemTreeLabelProvider extends TreeLabelProvider {
81
82 @Override
83 public String getColumnText(Object element, int columnIndex) {
7c246810
PT
84 if (element instanceof StateEntry) {
85 StateEntry entry = (StateEntry) element;
17c73d10
GB
86 switch (columnIndex) {
87 case ATTRIBUTE_NAME_COL:
88 return entry.getName();
89 case QUARK_COL:
90 return String.valueOf(entry.getQuark());
91 case VALUE_COL:
92 return entry.getValue();
93 case TYPE_COL:
94 return entry.getType();
95 case START_TIME_COL:
96 return entry.getStartTime();
97 case END_TIME_COL:
98 return entry.getEndTime();
99 case ATTRIBUTE_FULLPATH_COL:
100 return entry.getFullPath();
101 default:
102 return EMPTY_STRING;
103 }
104 }
105 return super.getColumnText(element, columnIndex);
106 }
107
108 @Override
109 public Color getBackground(Object element, int columnIndex) {
7c246810
PT
110 if (element instanceof StateEntry) {
111 if (((StateEntry) element).isModified()) {
17c73d10
GB
112 return Display.getCurrent().getSystemColor(SWT.COLOR_YELLOW);
113 }
114 }
115 return super.getBackground(element, columnIndex);
116 }
117 }
118
119 /**
120 * Constructor
121 *
122 * @param parent
123 * The parent containing this viewer
124 */
125 public TmfStateSystemViewer(Composite parent) {
126 super(parent, false);
127 this.setLabelProvider(new StateSystemTreeLabelProvider());
128 getTreeViewer().setAutoExpandLevel(DEFAULT_AUTOEXPAND);
129 }
130
131 @Override
132 protected ITmfTreeColumnDataProvider getColumnDataProvider() {
133 return new ITmfTreeColumnDataProvider() {
134
135 @Override
136 public List<TmfTreeColumnData> getColumnData() {
137 List<TmfTreeColumnData> columns = new ArrayList<>();
138 TmfTreeColumnData column = new TmfTreeColumnData(Messages.TreeNodeColumnLabel);
139 columns.add(column);
140 column.setComparator(new ViewerComparator() {
141 @Override
142 public int compare(Viewer viewer, Object e1, Object e2) {
143 TmfTreeViewerEntry n1 = (TmfTreeViewerEntry) e1;
144 TmfTreeViewerEntry n2 = (TmfTreeViewerEntry) e2;
145
146 return n1.getName().compareTo(n2.getName());
147 }
148 });
149 columns.add(new TmfTreeColumnData(Messages.QuarkColumnLabel));
150 columns.add(new TmfTreeColumnData(Messages.ValueColumnLabel));
151 columns.add(new TmfTreeColumnData(Messages.TypeColumnLabel));
152 columns.add(new TmfTreeColumnData(Messages.StartTimeColumLabel));
153 columns.add(new TmfTreeColumnData(Messages.EndTimeColumLabel));
154 columns.add(new TmfTreeColumnData(Messages.AttributePathColumnLabel));
155 return columns;
156 }
157
158 };
159 }
160
161 // ------------------------------------------------------------------------
162 // Operations
163 // ------------------------------------------------------------------------
164
165 @Override
59c8ff34 166 protected ITmfTreeViewerEntry updateElements(long start, long end, boolean selection) {
7c246810
PT
167
168 if (selection) {
169 fSelection = start;
170 } else {
171 fSelection = TmfTraceManager.getInstance().getSelectionBeginTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
172 }
173
17c73d10
GB
174 if (getTrace() == null) {
175 return null;
176 }
177
59c8ff34 178 ITmfTreeViewerEntry root = getInput();
17c73d10 179
7c246810
PT
180 if (root == null) {
181 root = createRoot();
182 } else if (fFilterStatus) {
183 clearStateSystemEntries(root);
17c73d10
GB
184 }
185
186 /*
7c246810
PT
187 * Update the values of the elements of the state systems at the
188 * selection start time
17c73d10 189 */
7c246810 190 boolean changed = updateStateSystemEntries(root, fSelection);
17c73d10 191
7c246810 192 return selection || changed ? root : null;
17c73d10
GB
193 }
194
7c246810 195 private ITmfTreeViewerEntry createRoot() {
59c8ff34 196 // 'Fake' root node
7c246810 197 TmfTreeViewerEntry rootEntry = new TmfTreeViewerEntry("root"); //$NON-NLS-1$
59c8ff34 198
0f8687b4 199 for (final ITmfTrace trace : TmfTraceManager.getTraceSetWithExperiment(getTrace())) {
7c246810
PT
200 if (trace != null) {
201 rootEntry.addChild(createTraceEntry(trace));
17c73d10
GB
202 }
203 }
7c246810 204 return rootEntry;
17c73d10
GB
205 }
206
7c246810 207 private static TmfTreeViewerEntry createTraceEntry(ITmfTrace trace) {
17c73d10 208 TmfTreeViewerEntry traceEntry = new TmfTreeViewerEntry(trace.getName());
b8585c7c 209 Iterable<ITmfAnalysisModuleWithStateSystems> modules = TmfTraceUtils.getAnalysisModulesOfClass(trace, ITmfAnalysisModuleWithStateSystems.class);
17c73d10
GB
210 for (ITmfAnalysisModuleWithStateSystems module : modules) {
211 /* Just schedule the module, the data will be filled when available */
212 module.schedule();
7c246810
PT
213 if (module instanceof TmfStateSystemAnalysisModule) {
214 // TODO: add this method to ITmfAnalysisModuleWithStateSystems
215 ((TmfStateSystemAnalysisModule) module).waitForInitialization();
216 }
17c73d10 217 for (ITmfStateSystem ss : module.getStateSystems()) {
7c246810
PT
218 if (ss != null) {
219 traceEntry.addChild(new StateSystemEntry(ss));
17c73d10 220 }
17c73d10
GB
221 }
222 }
223 return traceEntry;
224 }
225
7c246810
PT
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 }
17c73d10
GB
232 }
233
7c246810
PT
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 }
17c73d10 250 }
7c246810 251 return changed;
17c73d10
GB
252 }
253
7c246810
PT
254 private boolean updateStateEntries(ITmfStateSystem ss, List<ITmfStateInterval> fullState, TmfTreeViewerEntry parent, int parentQuark, long timestamp) {
255 boolean changed = false;
17c73d10 256 try {
7c246810
PT
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 }
17c73d10 262 ITmfStateInterval interval = fullState.get(quark);
7c246810
PT
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));
17c73d10 290
7c246810
PT
291 // update children recursively
292 updateStateEntries(ss, fullState, stateEntry, quark, timestamp);
17c73d10 293 }
17c73d10 294
7c246810 295 }
17c73d10
GB
296 } catch (AttributeNotFoundException e) {
297 /* Should not happen, we're iterating on known attributes */
17c73d10 298 }
7c246810 299 return changed;
17c73d10
GB
300 }
301
7c246810
PT
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;
17c73d10
GB
307 }
308 }
7c246810 309 return null;
17c73d10 310 }
17c73d10
GB
311 /**
312 * Set the entries as out of range
313 */
7c246810
PT
314 private static void markOutOfRange(ITmfTreeViewerEntry parent) {
315 for (ITmfTreeViewerEntry entry : parent.getChildren()) {
316 if (entry instanceof StateEntry) {
317 ((StateEntry) entry).setOutOfRange();
17c73d10
GB
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
7c246810
PT
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 {
17c73d10
GB
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;
7c246810 373 private boolean fModified;
17c73d10
GB
374 private boolean fOutOfRange = false;
375
7c246810 376 public StateEntry(String name, int quark, String fullPath, ITmfStateValue value, @NonNull TmfTimestamp start, @NonNull TmfTimestamp end, boolean modified) {
17c73d10
GB
377 super(name);
378 fQuark = quark;
379 fFullPath = fullPath;
380 fStart = start;
381 fEnd = end;
382 fValue = value;
7c246810 383 fModified = modified;
17c73d10
GB
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.070983 seconds and 5 git commands to generate.