timing: ss statistics use the common statistics
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.ui / src / org / eclipse / tracecompass / analysis / timing / ui / views / segmentstore / statistics / AbstractSegmentStoreStatisticsView.java
CommitLineData
ce8319b6 1/*******************************************************************************
658401c8 2 * Copyright (c) 2015, 2016 Ericsson
ce8319b6
BH
3 *
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
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
5b901f94 12package org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.statistics;
ce8319b6 13
37b7faba
MK
14import java.io.OutputStream;
15import java.io.PrintWriter;
16import java.util.ArrayList;
17import java.util.List;
18
ce8319b6 19import org.eclipse.jdt.annotation.Nullable;
37b7faba 20import org.eclipse.jface.action.Action;
ce8319b6 21import org.eclipse.swt.widgets.Composite;
37b7faba
MK
22import org.eclipse.swt.widgets.Shell;
23import org.eclipse.swt.widgets.Tree;
24import org.eclipse.swt.widgets.TreeItem;
ce8319b6 25import org.eclipse.tracecompass.common.core.NonNullUtils;
37b7faba 26import org.eclipse.tracecompass.internal.analysis.timing.ui.views.segmentstore.ExportToTsvAction;
ce8319b6
BH
27import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
28import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
29import org.eclipse.tracecompass.tmf.ui.views.TmfView;
30
37b7faba
MK
31import com.google.common.annotations.VisibleForTesting;
32import com.google.common.base.Joiner;
33
ce8319b6 34/**
3507ca88 35 * Abstract view to to be extended to display segment store statistics.
ce8319b6
BH
36 *
37 * @author Bernd Hufmann
fb042960 38 * @deprecated use {@link AbstractSegmentsStatisticsView} instead
ce8319b6 39 */
fb042960 40@Deprecated
3507ca88 41public abstract class AbstractSegmentStoreStatisticsView extends TmfView {
ce8319b6 42
37b7faba
MK
43 private final Action fExportAction = new ExportToTsvAction() {
44 @Override
45 protected void exportToTsv(@Nullable OutputStream stream) {
46 AbstractSegmentStoreStatisticsView.this.exportToTsv(stream);
47 }
48
49 @Override
50 protected @Nullable Shell getShell() {
51 return getViewSite().getShell();
52 }
53
54 };
55
56 private @Nullable AbstractSegmentStoreStatisticsViewer fStatsViewer = null;
ce8319b6
BH
57
58 /**
59 * Constructor
60 */
3507ca88 61 public AbstractSegmentStoreStatisticsView() {
ce8319b6
BH
62 super("StatisticsView"); //$NON-NLS-1$
63 }
64
65 @Override
66 public void createPartControl(@Nullable Composite parent) {
67 super.createPartControl(parent);
3507ca88 68 AbstractSegmentStoreStatisticsViewer statsViewer = createSegmentStoreStatisticsViewer(NonNullUtils.checkNotNull(parent));
ce8319b6
BH
69 ITmfTrace trace = TmfTraceManager.getInstance().getActiveTrace();
70 if (trace != null) {
71 statsViewer.loadTrace(trace);
72 }
73 fStatsViewer = statsViewer;
37b7faba 74 getViewSite().getActionBars().getMenuManager().add(fExportAction);
ce8319b6
BH
75 }
76
77 @Override
78 public void setFocus() {
3507ca88 79 AbstractSegmentStoreStatisticsViewer statsViewer = fStatsViewer;
ce8319b6
BH
80 if (statsViewer != null) {
81 statsViewer.getControl().setFocus();
82 }
83 }
84
85 @Override
86 public void dispose() {
87 super.dispose();
3507ca88 88 AbstractSegmentStoreStatisticsViewer statsViewer = fStatsViewer;
ce8319b6
BH
89 if (statsViewer != null) {
90 statsViewer.dispose();
91 }
92 }
93
94 /**
3507ca88 95 * Creates a segment store statistics viewer instance.
ce8319b6
BH
96 *
97 * @param parent
98 * the parent composite to create the viewer in.
99 * @return the latency statistics viewer implementation
100 */
3507ca88 101 protected abstract AbstractSegmentStoreStatisticsViewer createSegmentStoreStatisticsViewer(Composite parent);
ce8319b6 102
37b7faba
MK
103 /**
104 * Export a given items's TSV
105 *
106 * @param stream
107 * an output stream to write the TSV to
108 * @since 1.2
109 */
110 @VisibleForTesting
111 protected void exportToTsv(@Nullable OutputStream stream) {
112 try (PrintWriter pw = new PrintWriter(stream)) {
113 AbstractSegmentStoreStatisticsViewer statsViewer = fStatsViewer;
114 if (statsViewer == null) {
115 return;
116 }
117 Tree tree = statsViewer.getTreeViewer().getTree();
118 int size = tree.getItemCount();
119 List<String> columns = new ArrayList<>();
120 for (int i = 0; i < tree.getColumnCount(); i++) {
121 String valueOf = String.valueOf(tree.getColumn(i).getText());
122 if (valueOf.isEmpty() && i == tree.getColumnCount() - 1) {
123 // Linux "feature", an invisible column is added at the end
124 // with gtk2
125 break;
126 }
127 columns.add(valueOf);
128 }
129 String join = Joiner.on('\t').skipNulls().join(columns);
130 pw.println(join);
131 for (int i = 0; i < size; i++) {
132 TreeItem item = tree.getItem(i);
133 printItem(pw, columns, item);
134 }
135 }
136 }
137
138 private void printItem(PrintWriter pw, List<String> columns, @Nullable TreeItem item) {
139 if (item == null) {
140 return;
141 }
142 List<String> data = new ArrayList<>();
143 for (int col = 0; col < columns.size(); col++) {
144 data.add(String.valueOf(item.getText(col)));
145 }
146 pw.println(Joiner.on('\t').join(data));
147 for (TreeItem child : item.getItems()) {
148 printItem(pw, columns, child);
149 }
150 }
151
ce8319b6 152}
This page took 0.046815 seconds and 5 git commands to generate.