Commit | Line | Data |
---|---|---|
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 | 18 | package org.eclipse.tracecompass.tmf.ui.views.statesystem; |
17c73d10 GB |
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; | |
17c73d10 GB |
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; | |
e894a508 AM |
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; | |
2bdf0193 AM |
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; | |
2bdf0193 AM |
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; | |
21852dfa | 43 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceContext; |
2bdf0193 | 44 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
b8585c7c | 45 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils; |
2bdf0193 AM |
46 | import org.eclipse.tracecompass.tmf.ui.viewers.tree.AbstractTmfTreeViewer; |
47 | import org.eclipse.tracecompass.tmf.ui.viewers.tree.ITmfTreeColumnDataProvider; | |
48 | import org.eclipse.tracecompass.tmf.ui.viewers.tree.ITmfTreeViewerEntry; | |
49 | import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeColumnData; | |
50 | import 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 | */ |
59 | public 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 { | |
21852dfa | 171 | TmfTraceContext ctx = TmfTraceManager.getInstance().getCurrentTraceContext(); |
16801c72 | 172 | fSelection = ctx.getSelectionRange().getStartTime().toNanos(); |
7c246810 PT |
173 | } |
174 | ||
17c73d10 GB |
175 | if (getTrace() == null) { |
176 | return null; | |
177 | } | |
178 | ||
59c8ff34 | 179 | ITmfTreeViewerEntry root = getInput(); |
17c73d10 | 180 | |
7c246810 PT |
181 | if (root == null) { |
182 | root = createRoot(); | |
183 | } else if (fFilterStatus) { | |
184 | clearStateSystemEntries(root); | |
17c73d10 GB |
185 | } |
186 | ||
187 | /* | |
7c246810 PT |
188 | * Update the values of the elements of the state systems at the |
189 | * selection start time | |
17c73d10 | 190 | */ |
7c246810 | 191 | boolean changed = updateStateSystemEntries(root, fSelection); |
17c73d10 | 192 | |
7c246810 | 193 | return selection || changed ? root : null; |
17c73d10 GB |
194 | } |
195 | ||
7c246810 | 196 | private ITmfTreeViewerEntry createRoot() { |
59c8ff34 | 197 | // 'Fake' root node |
7c246810 | 198 | TmfTreeViewerEntry rootEntry = new TmfTreeViewerEntry("root"); //$NON-NLS-1$ |
59c8ff34 | 199 | |
0f8687b4 | 200 | for (final ITmfTrace trace : TmfTraceManager.getTraceSetWithExperiment(getTrace())) { |
7c246810 PT |
201 | if (trace != null) { |
202 | rootEntry.addChild(createTraceEntry(trace)); | |
17c73d10 GB |
203 | } |
204 | } | |
7c246810 | 205 | return rootEntry; |
17c73d10 GB |
206 | } |
207 | ||
7c246810 | 208 | private static TmfTreeViewerEntry createTraceEntry(ITmfTrace trace) { |
17c73d10 | 209 | TmfTreeViewerEntry traceEntry = new TmfTreeViewerEntry(trace.getName()); |
b8585c7c | 210 | Iterable<ITmfAnalysisModuleWithStateSystems> modules = TmfTraceUtils.getAnalysisModulesOfClass(trace, ITmfAnalysisModuleWithStateSystems.class); |
17c73d10 GB |
211 | for (ITmfAnalysisModuleWithStateSystems module : modules) { |
212 | /* Just schedule the module, the data will be filled when available */ | |
213 | module.schedule(); | |
c81ffdf2 JCK |
214 | if (!module.waitForInitialization()) { |
215 | continue; | |
216 | } | |
17c73d10 | 217 | for (ITmfStateSystem ss : module.getStateSystems()) { |
4c4e2816 | 218 | traceEntry.addChild(new StateSystemEntry(ss)); |
17c73d10 GB |
219 | } |
220 | } | |
221 | return traceEntry; | |
222 | } | |
223 | ||
7c246810 PT |
224 | private static void clearStateSystemEntries(ITmfTreeViewerEntry root) { |
225 | for (ITmfTreeViewerEntry traceEntry : root.getChildren()) { | |
226 | for (ITmfTreeViewerEntry ssEntry : traceEntry.getChildren()) { | |
227 | ssEntry.getChildren().clear(); | |
228 | } | |
229 | } | |
17c73d10 GB |
230 | } |
231 | ||
7c246810 PT |
232 | private boolean updateStateSystemEntries(ITmfTreeViewerEntry root, long timestamp) { |
233 | boolean changed = false; | |
234 | for (ITmfTreeViewerEntry traceEntry : root.getChildren()) { | |
235 | for (ITmfTreeViewerEntry ssEntry : traceEntry.getChildren()) { | |
236 | StateSystemEntry stateSystemEntry = (StateSystemEntry) ssEntry; | |
237 | ITmfStateSystem ss = stateSystemEntry.getSS(); | |
238 | try { | |
239 | List<ITmfStateInterval> fullState = ss.queryFullState(timestamp); | |
240 | changed |= updateStateEntries(ss, fullState, stateSystemEntry, -1, timestamp); | |
241 | } catch (TimeRangeException e) { | |
242 | markOutOfRange(stateSystemEntry); | |
243 | changed = true; | |
244 | } catch (StateSystemDisposedException e) { | |
245 | /* Ignored */ | |
246 | } | |
247 | } | |
17c73d10 | 248 | } |
7c246810 | 249 | return changed; |
17c73d10 GB |
250 | } |
251 | ||
7c246810 PT |
252 | private boolean updateStateEntries(ITmfStateSystem ss, List<ITmfStateInterval> fullState, TmfTreeViewerEntry parent, int parentQuark, long timestamp) { |
253 | boolean changed = false; | |
17c73d10 | 254 | try { |
7c246810 PT |
255 | for (int quark : ss.getSubAttributes(parentQuark, false)) { |
256 | if (quark >= fullState.size()) { | |
257 | // attribute was created after the full state query | |
258 | continue; | |
259 | } | |
17c73d10 | 260 | ITmfStateInterval interval = fullState.get(quark); |
7c246810 PT |
261 | StateEntry stateEntry = findStateEntry(parent, quark); |
262 | if (stateEntry == null) { | |
263 | boolean modified = fFilterStatus ? | |
264 | interval.getStartTime() == timestamp : | |
265 | !interval.getStateValue().isNull(); | |
266 | stateEntry = new StateEntry(ss.getAttributeName(quark), quark, ss.getFullAttributePath(quark), | |
267 | interval.getStateValue(), | |
b2c971ec MK |
268 | TmfTimestamp.fromNanos(interval.getStartTime()), |
269 | TmfTimestamp.fromNanos(interval.getEndTime()), | |
7c246810 PT |
270 | modified); |
271 | ||
272 | // update children first to know if parent is really needed | |
273 | updateStateEntries(ss, fullState, stateEntry, quark, timestamp); | |
274 | ||
275 | /* | |
276 | * Add this entry to parent if filtering is off, or | |
277 | * if the entry has children to display, or | |
278 | * if there is a state change at the current timestamp | |
279 | */ | |
280 | if (!fFilterStatus || stateEntry.hasChildren() || interval.getStartTime() == timestamp) { | |
281 | parent.addChild(stateEntry); | |
282 | changed = true; | |
283 | } | |
284 | } else { | |
285 | stateEntry.update(interval.getStateValue(), | |
b2c971ec MK |
286 | TmfTimestamp.fromNanos(interval.getStartTime()), |
287 | TmfTimestamp.fromNanos(interval.getEndTime())); | |
17c73d10 | 288 | |
7c246810 PT |
289 | // update children recursively |
290 | updateStateEntries(ss, fullState, stateEntry, quark, timestamp); | |
17c73d10 | 291 | } |
17c73d10 | 292 | |
7c246810 | 293 | } |
17c73d10 GB |
294 | } catch (AttributeNotFoundException e) { |
295 | /* Should not happen, we're iterating on known attributes */ | |
17c73d10 | 296 | } |
7c246810 | 297 | return changed; |
17c73d10 GB |
298 | } |
299 | ||
7c246810 PT |
300 | private static StateEntry findStateEntry(TmfTreeViewerEntry parent, int quark) { |
301 | for (ITmfTreeViewerEntry child : parent.getChildren()) { | |
302 | StateEntry stateEntry = (StateEntry) child; | |
303 | if (stateEntry.getQuark() == quark) { | |
304 | return stateEntry; | |
17c73d10 GB |
305 | } |
306 | } | |
7c246810 | 307 | return null; |
17c73d10 | 308 | } |
17c73d10 GB |
309 | /** |
310 | * Set the entries as out of range | |
311 | */ | |
7c246810 PT |
312 | private static void markOutOfRange(ITmfTreeViewerEntry parent) { |
313 | for (ITmfTreeViewerEntry entry : parent.getChildren()) { | |
314 | if (entry instanceof StateEntry) { | |
315 | ((StateEntry) entry).setOutOfRange(); | |
17c73d10 GB |
316 | |
317 | /* Update this node's children recursively */ | |
318 | markOutOfRange(entry); | |
319 | } | |
320 | } | |
321 | } | |
322 | ||
323 | /** | |
324 | * Set the filter status of the viewer. By default, all entries of all state | |
325 | * system are present, and the values that changed since last refresh are | |
326 | * shown in yellow. When the filter status is true, only the entries with | |
327 | * values modified at current time are displayed. | |
328 | */ | |
329 | public void changeFilterStatus() { | |
330 | fFilterStatus = !fFilterStatus; | |
331 | if (fFilterStatus) { | |
332 | getTreeViewer().setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS); | |
333 | } else { | |
334 | getTreeViewer().setAutoExpandLevel(DEFAULT_AUTOEXPAND); | |
335 | clearContent(); | |
336 | } | |
337 | updateContent(getSelectionBeginTime(), getSelectionEndTime(), true); | |
338 | } | |
339 | ||
340 | /** | |
341 | * Update the display to use the updated timestamp format | |
342 | * | |
343 | * @param signal | |
344 | * the incoming signal | |
345 | */ | |
346 | @TmfSignalHandler | |
347 | public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal) { | |
348 | updateContent(getSelectionBeginTime(), getSelectionEndTime(), true); | |
349 | } | |
350 | ||
7c246810 PT |
351 | private static class StateSystemEntry extends TmfTreeViewerEntry { |
352 | private final @NonNull ITmfStateSystem fSS; | |
353 | ||
354 | public StateSystemEntry(@NonNull ITmfStateSystem ss) { | |
355 | super(ss.getSSID()); | |
356 | fSS = ss; | |
357 | } | |
358 | ||
359 | public @NonNull ITmfStateSystem getSS() { | |
360 | return fSS; | |
361 | } | |
362 | } | |
363 | ||
364 | private class StateEntry extends TmfTreeViewerEntry { | |
17c73d10 GB |
365 | |
366 | private final int fQuark; | |
367 | private final String fFullPath; | |
b2c971ec MK |
368 | private @NonNull ITmfTimestamp fStart; |
369 | private @NonNull ITmfTimestamp fEnd; | |
17c73d10 | 370 | private ITmfStateValue fValue; |
7c246810 | 371 | private boolean fModified; |
17c73d10 GB |
372 | private boolean fOutOfRange = false; |
373 | ||
b2c971ec | 374 | public StateEntry(String name, int quark, String fullPath, ITmfStateValue value, @NonNull ITmfTimestamp start, @NonNull ITmfTimestamp end, boolean modified) { |
17c73d10 GB |
375 | super(name); |
376 | fQuark = quark; | |
377 | fFullPath = fullPath; | |
378 | fStart = start; | |
379 | fEnd = end; | |
380 | fValue = value; | |
7c246810 | 381 | fModified = modified; |
17c73d10 GB |
382 | } |
383 | ||
384 | public int getQuark() { | |
385 | return fQuark; | |
386 | } | |
387 | ||
388 | public String getFullPath() { | |
389 | return fFullPath; | |
390 | } | |
391 | ||
392 | public String getStartTime() { | |
393 | if (fOutOfRange) { | |
394 | return EMPTY_STRING; | |
395 | } | |
396 | return fStart.toString(); | |
397 | } | |
398 | ||
399 | public String getEndTime() { | |
400 | if (fOutOfRange) { | |
401 | return EMPTY_STRING; | |
402 | } | |
403 | return fEnd.toString(); | |
404 | } | |
405 | ||
406 | public String getValue() { | |
407 | if (fOutOfRange) { | |
408 | return Messages.OutOfRangeMsg; | |
409 | } | |
410 | switch (fValue.getType()) { | |
411 | case INTEGER: | |
412 | case LONG: | |
413 | case DOUBLE: | |
414 | case STRING: | |
415 | return fValue.toString(); | |
416 | case NULL: | |
417 | default: | |
418 | return EMPTY_STRING; | |
419 | } | |
420 | } | |
421 | ||
422 | public String getType() { | |
423 | if (fOutOfRange) { | |
424 | return EMPTY_STRING; | |
425 | } | |
426 | switch (fValue.getType()) { | |
427 | case INTEGER: | |
428 | return Messages.TypeInteger; | |
429 | case LONG: | |
430 | return Messages.TypeLong; | |
431 | case DOUBLE: | |
432 | return Messages.TypeDouble; | |
433 | case STRING: | |
434 | return Messages.TypeString; | |
435 | case NULL: | |
436 | default: | |
437 | return EMPTY_STRING; | |
438 | } | |
439 | } | |
440 | ||
441 | public boolean isModified() { | |
442 | return fModified; | |
443 | } | |
444 | ||
b2c971ec | 445 | public void update(ITmfStateValue value, @NonNull ITmfTimestamp start, @NonNull ITmfTimestamp end) { |
17c73d10 GB |
446 | fModified = false; |
447 | fOutOfRange = false; | |
448 | if (!start.equals(fStart)) { | |
449 | fModified = true; | |
450 | fStart = start; | |
451 | fEnd = end; | |
452 | fValue = value; | |
453 | } | |
454 | } | |
455 | ||
456 | public void setOutOfRange() { | |
457 | fModified = false; | |
458 | fOutOfRange = true; | |
459 | } | |
460 | } | |
461 | } |