1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 Ericsson
4 * All rights reserved. This program and the accompanying materials are made
5 * 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
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12 package org
.eclipse
.tracecompass
.analysis
.timing
.ui
.views
.segmentstore
.statistics
;
14 import java
.io
.OutputStream
;
15 import java
.io
.PrintWriter
;
16 import java
.util
.ArrayList
;
17 import java
.util
.List
;
19 import org
.eclipse
.jdt
.annotation
.Nullable
;
20 import org
.eclipse
.jface
.action
.Action
;
21 import org
.eclipse
.swt
.widgets
.Composite
;
22 import org
.eclipse
.swt
.widgets
.Shell
;
23 import org
.eclipse
.swt
.widgets
.Tree
;
24 import org
.eclipse
.swt
.widgets
.TreeItem
;
25 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
26 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.ui
.views
.segmentstore
.ExportToTsvAction
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
29 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.TmfView
;
31 import com
.google
.common
.annotations
.VisibleForTesting
;
32 import com
.google
.common
.base
.Joiner
;
35 * Abstract view to to be extended to display segment store statistics.
37 * @author Bernd Hufmann
40 public abstract class AbstractSegmentStoreStatisticsView
extends TmfView
{
42 private final Action fExportAction
= new ExportToTsvAction() {
44 protected void exportToTsv(@Nullable OutputStream stream
) {
45 AbstractSegmentStoreStatisticsView
.this.exportToTsv(stream
);
49 protected @Nullable Shell
getShell() {
50 return getViewSite().getShell();
55 private @Nullable AbstractSegmentStoreStatisticsViewer fStatsViewer
= null;
60 public AbstractSegmentStoreStatisticsView() {
61 super("StatisticsView"); //$NON-NLS-1$
65 public void createPartControl(@Nullable Composite parent
) {
66 super.createPartControl(parent
);
67 AbstractSegmentStoreStatisticsViewer statsViewer
= createSegmentStoreStatisticsViewer(NonNullUtils
.checkNotNull(parent
));
68 ITmfTrace trace
= TmfTraceManager
.getInstance().getActiveTrace();
70 statsViewer
.loadTrace(trace
);
72 fStatsViewer
= statsViewer
;
73 getViewSite().getActionBars().getMenuManager().add(fExportAction
);
77 public void setFocus() {
78 AbstractSegmentStoreStatisticsViewer statsViewer
= fStatsViewer
;
79 if (statsViewer
!= null) {
80 statsViewer
.getControl().setFocus();
85 public void dispose() {
87 AbstractSegmentStoreStatisticsViewer statsViewer
= fStatsViewer
;
88 if (statsViewer
!= null) {
89 statsViewer
.dispose();
94 * Creates a segment store statistics viewer instance.
97 * the parent composite to create the viewer in.
98 * @return the latency statistics viewer implementation
100 protected abstract AbstractSegmentStoreStatisticsViewer
createSegmentStoreStatisticsViewer(Composite parent
);
103 * Export a given items's TSV
106 * an output stream to write the TSV to
110 protected void exportToTsv(@Nullable OutputStream stream
) {
111 try (PrintWriter pw
= new PrintWriter(stream
)) {
112 AbstractSegmentStoreStatisticsViewer statsViewer
= fStatsViewer
;
113 if (statsViewer
== null) {
116 Tree tree
= statsViewer
.getTreeViewer().getTree();
117 int size
= tree
.getItemCount();
118 List
<String
> columns
= new ArrayList
<>();
119 for (int i
= 0; i
< tree
.getColumnCount(); i
++) {
120 String valueOf
= String
.valueOf(tree
.getColumn(i
).getText());
121 if (valueOf
.isEmpty() && i
== tree
.getColumnCount() - 1) {
122 // Linux "feature", an invisible column is added at the end
126 columns
.add(valueOf
);
128 String join
= Joiner
.on('\t').skipNulls().join(columns
);
130 for (int i
= 0; i
< size
; i
++) {
131 TreeItem item
= tree
.getItem(i
);
132 printItem(pw
, columns
, item
);
137 private void printItem(PrintWriter pw
, List
<String
> columns
, @Nullable TreeItem item
) {
141 List
<String
> data
= new ArrayList
<>();
142 for (int col
= 0; col
< columns
.size(); col
++) {
143 data
.add(String
.valueOf(item
.getText(col
)));
145 pw
.println(Joiner
.on('\t').join(data
));
146 for (TreeItem child
: item
.getItems()) {
147 printItem(pw
, columns
, child
);