tmf: Fix NLS bundle for the time offset dialog
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.analysis.xml.ui / src / org / eclipse / tracecompass / tmf / analysis / xml / ui / views / timegraph / XmlTimeGraphView.java
CommitLineData
1a23419e
FW
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 * Geneviève Bastien - Review of the initial implementation
12 *******************************************************************************/
13
2bdf0193 14package org.eclipse.tracecompass.tmf.analysis.xml.ui.views.timegraph;
1a23419e
FW
15
16import java.util.ArrayList;
17import java.util.Collections;
18import java.util.Comparator;
19import java.util.HashMap;
20import java.util.LinkedList;
21import java.util.List;
22import java.util.Map;
23import java.util.Set;
24import java.util.TreeSet;
25
26import org.eclipse.core.runtime.IProgressMonitor;
e1c415b3 27import org.eclipse.core.runtime.IStatus;
1a23419e 28import org.eclipse.jdt.annotation.NonNull;
1a23419e
FW
29import org.eclipse.jface.util.IPropertyChangeListener;
30import org.eclipse.jface.util.PropertyChangeEvent;
1a23419e 31import org.eclipse.swt.widgets.Display;
2bdf0193
AM
32import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.Activator;
33import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.TmfXmlUiStrings;
34import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views.XmlViewInfo;
e894a508
AM
35import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
36import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
37import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
38import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
39import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
40import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
2bdf0193
AM
41import org.eclipse.tracecompass.tmf.analysis.xml.core.model.ITmfXmlModelFactory;
42import org.eclipse.tracecompass.tmf.analysis.xml.core.model.ITmfXmlStateAttribute;
43import org.eclipse.tracecompass.tmf.analysis.xml.core.model.readonly.TmfXmlReadOnlyModelFactory;
44import org.eclipse.tracecompass.tmf.analysis.xml.core.module.IXmlStateSystemContainer;
45import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
46import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
47import org.eclipse.tracecompass.tmf.analysis.xml.ui.views.timegraph.XmlEntry.EntryDisplayType;
48import org.eclipse.tracecompass.tmf.core.statesystem.ITmfAnalysisModuleWithStateSystems;
49import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
50import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
51import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
52import org.eclipse.tracecompass.tmf.ui.views.timegraph.AbstractTimeGraphView;
53import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider2;
54import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ILinkEvent;
55import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
56import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
57import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent;
58import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent;
59import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
1a23419e
FW
60import org.w3c.dom.Element;
61
62/**
63 * This view displays state system data in a time graph view. It uses an XML
64 * {@link TmfXmlUiStrings#TIME_GRAPH_VIEW} element from an XML file. This
65 * element defines which entries from the state system will be shown and also
66 * gives additional information on the presentation of the view (states, colors,
67 * etc)
68 *
69 * @author Florian Wininger
70 */
71public class XmlTimeGraphView extends AbstractTimeGraphView {
72
73 /** View ID. */
35423ee0 74 public static final @NonNull String ID = "org.eclipse.linuxtools.tmf.analysis.xml.ui.views.timegraph"; //$NON-NLS-1$
1a23419e
FW
75
76 private static final String[] DEFAULT_COLUMN_NAMES = new String[] {
77 Messages.XmlTimeGraphView_ColumnName,
78 Messages.XmlTimeGraphView_ColumnId,
79 Messages.XmlTimeGraphView_ColumnParentId,
80 };
81
82 private static final String[] DEFAULT_FILTER_COLUMN_NAMES = new String[] {
83 Messages.XmlTimeGraphView_ColumnName,
84 Messages.XmlTimeGraphView_ColumnId
85 };
86
87 /** The relative weight of the sash */
88 private static final int[] fWeight = { 1, 2 };
89
90 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
91 private static final String SPLIT_STRING = "/"; //$NON-NLS-1$
92
35423ee0 93 private final @NonNull XmlViewInfo fViewInfo = new XmlViewInfo(ID);
1a23419e
FW
94 private final ITmfXmlModelFactory fFactory;
95
96 // ------------------------------------------------------------------------
97 // Constructors
98 // ------------------------------------------------------------------------
99
100 /**
101 * Default constructor
102 */
103 public XmlTimeGraphView() {
104 super(ID, new XmlPresentationProvider());
105 setWeight(fWeight);
106 setTreeColumns(DEFAULT_COLUMN_NAMES);
107 setTreeLabelProvider(new XmlTreeLabelProvider());
108 setFilterColumns(DEFAULT_FILTER_COLUMN_NAMES);
109 setFilterLabelProvider(new XmlTreeLabelProvider());
110 setEntryComparator(new XmlEntryComparator());
111 this.addPartPropertyListener(new IPropertyChangeListener() {
112 @Override
113 public void propertyChange(PropertyChangeEvent event) {
114 if (event.getProperty().equals(TmfXmlUiStrings.XML_OUTPUT_DATA)) {
35423ee0
GB
115 Object newValue = event.getNewValue();
116 if (newValue instanceof String) {
117 String data = (String) newValue;
118 fViewInfo.setViewData(data);
119 loadNewXmlView();
120 }
1a23419e
FW
121 }
122 }
123 });
1a23419e 124
1a23419e
FW
125 fFactory = TmfXmlReadOnlyModelFactory.getInstance();
126 }
127
1a23419e
FW
128 private void loadNewXmlView() {
129 rebuild();
130 }
131
132 private void setViewTitle(final String title) {
133 Display.getDefault().asyncExec(new Runnable() {
134 @Override
135 public void run() {
136 setPartName(title);
137 }
138 });
139
140 }
141
142 @Override
143 protected String getNextText() {
144 return Messages.XmlTimeGraphView_NextText;
145 }
146
147 @Override
148 protected String getNextTooltip() {
149 return Messages.XmlTimeGraphView_NextTooltip;
150 }
151
152 @Override
153 protected String getPrevText() {
154 return Messages.XmlTimeGraphView_PreviousText;
155 }
156
157 @Override
158 protected String getPrevTooltip() {
159 return Messages.XmlTimeGraphView_PreviousInterval;
160 }
161
162 /**
163 * Default label provider, it shows name, id and parent columns
164 *
165 * TODO: There should be a way to define columns in the XML
166 * */
167 private static class XmlTreeLabelProvider extends TreeLabelProvider {
168
169 @Override
170 public String getColumnText(Object element, int columnIndex) {
171 XmlEntry entry = (XmlEntry) element;
172
173 if (DEFAULT_COLUMN_NAMES[columnIndex].equals(Messages.XmlTimeGraphView_ColumnName)) {
174 return entry.getName();
175 } else if (DEFAULT_COLUMN_NAMES[columnIndex].equals(Messages.XmlTimeGraphView_ColumnId)) {
176 return entry.getId();
177 } else if (DEFAULT_COLUMN_NAMES[columnIndex].equals(Messages.XmlTimeGraphView_ColumnParentId)) {
178 return entry.getParentId();
179 }
180 return EMPTY_STRING;
181 }
182
183 }
184
185 private static class XmlEntryComparator implements Comparator<ITimeGraphEntry> {
186
187 @Override
188 public int compare(ITimeGraphEntry o1, ITimeGraphEntry o2) {
189
190 int result = 0;
191
192 if ((o1 instanceof XmlEntry) && (o2 instanceof XmlEntry)) {
193 XmlEntry entry1 = (XmlEntry) o1;
194 XmlEntry entry2 = (XmlEntry) o2;
195 result = entry1.getTrace().getStartTime().compareTo(entry2.getTrace().getStartTime());
196 if (result == 0) {
197 result = entry1.getTrace().getName().compareTo(entry2.getTrace().getName());
198 }
199 if (result == 0) {
200 result = entry1.getName().compareTo(entry2.getName());
201 }
202 }
203
204 if (result == 0) {
205 result = o1.getStartTime() < o2.getStartTime() ? -1 : o1.getStartTime() > o2.getStartTime() ? 1 : 0;
206 }
207
208 return result;
209 }
210 }
211
212 // ------------------------------------------------------------------------
213 // Internal
214 // ------------------------------------------------------------------------
215
216 @Override
217 protected void buildEventList(ITmfTrace trace, ITmfTrace parentTrace, IProgressMonitor monitor) {
218
219 /*
220 * Get the view element from the XML file. If the element can't be
221 * found, return.
222 */
35423ee0 223 Element viewElement = fViewInfo.getViewElement(TmfXmlUiStrings.TIME_GRAPH_VIEW);
1a23419e
FW
224 if (viewElement == null) {
225 return;
226 }
227 ITimeGraphPresentationProvider2 pres = this.getPresentationProvider();
228 if (pres instanceof XmlPresentationProvider) {
229 /*
230 * TODO: Each entry of a line could have their own states/color.
231 * That will require an update to the presentation provider
232 */
233 ((XmlPresentationProvider) pres).loadNewStates(viewElement);
234 }
235
35423ee0
GB
236 String title = fViewInfo.getViewTitle(viewElement);
237 if (title == null) {
238 title = Messages.XmlTimeGraphView_DefaultTitle;
1a23419e 239 }
35423ee0
GB
240 setViewTitle(title);
241 Set<String> analysisIds = fViewInfo.getViewAnalysisIds(viewElement);
242
1a23419e
FW
243 List<Element> entries = XmlUtils.getChildElements(viewElement, TmfXmlUiStrings.ENTRY_ELEMENT);
244 Set<XmlEntry> entryList = new TreeSet<>(getEntryComparator());
245 for (ITmfTrace aTrace : TmfTraceManager.getTraceSet(trace)) {
246 if (monitor.isCanceled()) {
247 return;
248 }
249
250 List<ITmfAnalysisModuleWithStateSystems> stateSystemModules = new LinkedList<>();
251 if (analysisIds.isEmpty()) {
252 /*
253 * No analysis specified, take all state system analysis modules
254 */
255 for (ITmfAnalysisModuleWithStateSystems module : aTrace.getAnalysisModulesOfClass(ITmfAnalysisModuleWithStateSystems.class)) {
256 stateSystemModules.add(module);
257 }
258 } else {
259 for (String moduleId : analysisIds) {
260 ITmfAnalysisModuleWithStateSystems module = aTrace.getAnalysisModuleOfClass(ITmfAnalysisModuleWithStateSystems.class, moduleId);
261 if (module != null) {
262 stateSystemModules.add(module);
263 }
264 }
265 }
266
267 for (ITmfAnalysisModuleWithStateSystems module : stateSystemModules) {
e1c415b3
BH
268 IStatus status = module.schedule();
269 if (!status.isOK()) {
270 return;
271 }
1a23419e
FW
272 if (module instanceof TmfStateSystemAnalysisModule) {
273 ((TmfStateSystemAnalysisModule) module).waitForInitialization();
274 }
275 for (ITmfStateSystem ssq : module.getStateSystems()) {
276 if (ssq == null) {
277 return;
278 }
279 ssq.waitUntilBuilt();
280
281 long startTime = ssq.getStartTime();
282 long endTime = ssq.getCurrentEndTime();
283 XmlEntry groupEntry = new XmlEntry(-1, aTrace, aTrace.getName(), ssq);
284 entryList.add(groupEntry);
285 setStartTime(Math.min(getStartTime(), startTime));
286 setEndTime(Math.max(getEndTime(), endTime));
287
288 /* Add children entry of this entry for each line */
289 for (Element entry : entries) {
290 buildEntry(entry, groupEntry, -1);
291 }
292 }
293 }
294 }
295 putEntryList(trace, new ArrayList<TimeGraphEntry>(entryList));
296
297 if (trace.equals(getTrace())) {
298 refresh();
299 }
300 for (XmlEntry traceEntry : entryList) {
301 if (monitor.isCanceled()) {
302 return;
303 }
304 long startTime = traceEntry.getStateSystem().getStartTime();
305 long endTime = traceEntry.getStateSystem().getCurrentEndTime() + 1;
306 buildStatusEvent(traceEntry, monitor, startTime, endTime);
307 }
308 }
309
310 private void buildEntry(Element entryElement, XmlEntry parentEntry, int baseQuark) {
311 /* Get the attribute string to display */
312 String path = entryElement.getAttribute(TmfXmlUiStrings.PATH);
313 if (path.isEmpty()) {
314 path = TmfXmlStrings.WILDCARD;
315 }
316
317 /*
318 * Make sure the XML element has either a display attribute or entries,
319 * otherwise issue a warning
320 */
321
322 List<Element> displayElements = XmlUtils.getChildElements(entryElement, TmfXmlUiStrings.DISPLAY_ELEMENT);
323 List<Element> entryElements = XmlUtils.getChildElements(entryElement, TmfXmlUiStrings.ENTRY_ELEMENT);
324
325 if (displayElements.isEmpty() && entryElements.isEmpty()) {
326 Activator.logWarning(String.format("XML view: entry for %s should have either a display element or entry elements", path)); //$NON-NLS-1$
327 return;
328 }
329
330 ITmfStateSystem ss = parentEntry.getStateSystem();
331
332 /* Get the list of quarks to process with this path */
333 String[] paths = path.split(SPLIT_STRING);
334 int i = 0;
335 List<Integer> quarks = Collections.singletonList(baseQuark);
336
337 try {
338 while (i < paths.length) {
339 List<Integer> subQuarks = new LinkedList<>();
340 /* Replace * by .* to have a regex string */
341 String name = paths[i].replaceAll("\\*", ".*"); //$NON-NLS-1$ //$NON-NLS-2$
342 for (int relativeQuark : quarks) {
343 for (int quark : ss.getSubAttributes(relativeQuark, false, name)) {
344 subQuarks.add(quark);
345 }
346 }
347 quarks = subQuarks;
348 i++;
349 }
350
351 /* Process each quark */
352 XmlEntry currentEntry = parentEntry;
353 Element displayElement = null;
354 Map<String, XmlEntry> entryMap = new HashMap<>();
355 if (!displayElements.isEmpty()) {
356 displayElement = displayElements.get(0);
357 }
358 for (int quark : quarks) {
359 currentEntry = parentEntry;
360 /* Process the current entry, if specified */
361 if (displayElement != null) {
362 currentEntry = processEntry(entryElement, displayElement, parentEntry, quark, ss);
363 entryMap.put(currentEntry.getId(), currentEntry);
364 }
365 /* Process the children entry of this entry */
366 for (Element subEntryEl : entryElements) {
367 buildEntry(subEntryEl, currentEntry, quark);
368 }
369 }
370 if (!entryMap.isEmpty()) {
371 buildTree(entryMap, parentEntry);
372 }
373 } catch (AttributeNotFoundException e) {
374 }
375 }
376
377 private XmlEntry processEntry(@NonNull Element entryElement, @NonNull Element displayEl,
378 XmlEntry parentEntry, int quark, ITmfStateSystem ss) {
379 /*
380 * Get the start time and end time of this entry from the display
381 * attribute
382 */
383 ITmfXmlStateAttribute display = fFactory.createStateAttribute(displayEl, parentEntry);
384 int displayQuark = display.getAttributeQuark(quark);
385 if (displayQuark == IXmlStateSystemContainer.ERROR_QUARK) {
386 return new XmlEntry(quark, parentEntry.getTrace(),
387 String.format("Unknown display quark for %s", ss.getAttributeName(quark)), ss); //$NON-NLS-1$
388 }
389
390 long entryStart = ss.getStartTime();
391 long entryEnd = ss.getCurrentEndTime();
fadf52eb 392
1a23419e 393 try {
1a23419e 394
fadf52eb 395 ITmfStateInterval oneInterval = ss.querySingleState(entryStart, displayQuark);
1a23419e 396
fadf52eb
FR
397 /* The entry start is the first non-null interval */
398 while (oneInterval.getStateValue().isNull()) {
399 long ts = oneInterval.getEndTime() + 1;
400 if (ts > ss.getCurrentEndTime()) {
401 break;
1a23419e 402 }
fadf52eb 403 oneInterval = ss.querySingleState(ts, displayQuark);
1a23419e 404 }
fadf52eb
FR
405 entryStart = oneInterval.getStartTime();
406
407 /* The entry end is the last non-null interval */
408 oneInterval = ss.querySingleState(entryEnd, displayQuark);
409 while (oneInterval.getStateValue().isNull()) {
410 long ts = oneInterval.getStartTime() - 1;
411 if (ts < ss.getStartTime()) {
412 break;
413 }
414 oneInterval = ss.querySingleState(ts, displayQuark);
415 }
416 entryEnd = oneInterval.getEndTime();
417
1a23419e
FW
418 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
419 }
420
421 return new XmlEntry(quark, displayQuark, parentEntry.getTrace(), ss.getAttributeName(quark),
422 entryStart, entryEnd, EntryDisplayType.DISPLAY, ss, entryElement);
423 }
424
425 private void buildStatusEvent(XmlEntry traceEntry, IProgressMonitor monitor, long start, long end) {
426 long resolution = (end - start) / getDisplayWidth();
427 long startTime = Math.max(start, traceEntry.getStartTime());
428 long endTime = Math.min(end + 1, traceEntry.getEndTime());
429 List<ITimeEvent> eventList = getEventList(traceEntry, startTime, endTime, resolution, monitor);
430 if (monitor.isCanceled()) {
431 return;
432 }
433 traceEntry.setEventList(eventList);
434 redraw();
435
a3188982 436 for (ITimeGraphEntry entry : traceEntry.getChildren()) {
1a23419e
FW
437 if (monitor.isCanceled()) {
438 return;
439 }
440 XmlEntry xmlEntry = (XmlEntry) entry;
441 buildStatusEvent(xmlEntry, monitor, start, end);
442 }
443 }
444
445 /** Build a tree using getParentId() and getId() */
446 private static void buildTree(Map<String, XmlEntry> entryMap, XmlEntry rootEntry) {
447 for (XmlEntry entry : entryMap.values()) {
448 boolean root = true;
449 if (!entry.getParentId().isEmpty()) {
450 XmlEntry parent = entryMap.get(entry.getParentId());
451 if (parent != null &&
452 entry.getStartTime() >= parent.getStartTime() &&
453 entry.getStartTime() <= parent.getEndTime()) {
454 parent.addChild(entry);
455 root = false;
456 }
457 }
458 if (root) {
459 rootEntry.addChild(entry);
460 }
461 }
462 }
463
464 @Override
465 protected List<ITimeEvent> getEventList(TimeGraphEntry entry, long startTime, long endTime, long resolution, IProgressMonitor monitor) {
466 if (!(entry instanceof XmlEntry)) {
467 return Collections.EMPTY_LIST;
468 }
469 XmlEntry xmlEntry = (XmlEntry) entry;
470 ITmfStateSystem ssq = xmlEntry.getStateSystem();
471 final long realStart = Math.max(startTime, entry.getStartTime());
472 final long realEnd = Math.min(endTime, entry.getEndTime());
473 if (realEnd <= realStart) {
474 return null;
475 }
476 List<ITimeEvent> eventList = null;
477 int quark = xmlEntry.getDisplayQuark();
478
479 try {
480 if (xmlEntry.getType() == EntryDisplayType.DISPLAY) {
481
482 List<ITmfStateInterval> statusIntervals = ssq.queryHistoryRange(quark, realStart, realEnd - 1, resolution, monitor);
483 eventList = new ArrayList<>(statusIntervals.size());
484 long lastEndTime = -1;
485 for (ITmfStateInterval statusInterval : statusIntervals) {
486 if (monitor.isCanceled()) {
487 return null;
488 }
489 int status = statusInterval.getStateValue().unboxInt();
490 long time = statusInterval.getStartTime();
491 long duration = statusInterval.getEndTime() - time + 1;
492 if (!statusInterval.getStateValue().isNull()) {
493 if (lastEndTime != time && lastEndTime != -1) {
494 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
495 }
496 eventList.add(new TimeEvent(entry, time, duration, status));
497 } else if (lastEndTime == -1 || time + duration >= endTime) {
498 // add null event if it intersects the start or end time
499 eventList.add(new NullTimeEvent(entry, time, duration));
500 }
501 lastEndTime = time + duration;
502 }
503 }
504 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException | StateSystemDisposedException e) {
505 /* Ignored */
506 }
507 return eventList;
508 }
509
510 @Override
511 protected List<ILinkEvent> getLinkList(long startTime, long endTime, long resolution, IProgressMonitor monitor) {
512 /* TODO: not implemented yet, need XML to go along */
513 return Collections.EMPTY_LIST;
514 }
515
516}
This page took 0.075256 seconds and 5 git commands to generate.