timing.ui: add Export to TSV to tables and 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
38 *
39 */
3507ca88 40public abstract class AbstractSegmentStoreStatisticsView extends TmfView {
ce8319b6 41
37b7faba
MK
42 private final Action fExportAction = new ExportToTsvAction() {
43 @Override
44 protected void exportToTsv(@Nullable OutputStream stream) {
45 AbstractSegmentStoreStatisticsView.this.exportToTsv(stream);
46 }
47
48 @Override
49 protected @Nullable Shell getShell() {
50 return getViewSite().getShell();
51 }
52
53 };
54
55 private @Nullable AbstractSegmentStoreStatisticsViewer fStatsViewer = null;
ce8319b6
BH
56
57 /**
58 * Constructor
59 */
3507ca88 60 public AbstractSegmentStoreStatisticsView() {
ce8319b6
BH
61 super("StatisticsView"); //$NON-NLS-1$
62 }
63
64 @Override
65 public void createPartControl(@Nullable Composite parent) {
66 super.createPartControl(parent);
3507ca88 67 AbstractSegmentStoreStatisticsViewer statsViewer = createSegmentStoreStatisticsViewer(NonNullUtils.checkNotNull(parent));
ce8319b6
BH
68 ITmfTrace trace = TmfTraceManager.getInstance().getActiveTrace();
69 if (trace != null) {
70 statsViewer.loadTrace(trace);
71 }
72 fStatsViewer = statsViewer;
37b7faba 73 getViewSite().getActionBars().getMenuManager().add(fExportAction);
ce8319b6
BH
74 }
75
76 @Override
77 public void setFocus() {
3507ca88 78 AbstractSegmentStoreStatisticsViewer statsViewer = fStatsViewer;
ce8319b6
BH
79 if (statsViewer != null) {
80 statsViewer.getControl().setFocus();
81 }
82 }
83
84 @Override
85 public void dispose() {
86 super.dispose();
3507ca88 87 AbstractSegmentStoreStatisticsViewer statsViewer = fStatsViewer;
ce8319b6
BH
88 if (statsViewer != null) {
89 statsViewer.dispose();
90 }
91 }
92
93 /**
3507ca88 94 * Creates a segment store statistics viewer instance.
ce8319b6
BH
95 *
96 * @param parent
97 * the parent composite to create the viewer in.
98 * @return the latency statistics viewer implementation
99 */
3507ca88 100 protected abstract AbstractSegmentStoreStatisticsViewer createSegmentStoreStatisticsViewer(Composite parent);
ce8319b6 101
37b7faba
MK
102 /**
103 * Export a given items's TSV
104 *
105 * @param stream
106 * an output stream to write the TSV to
107 * @since 1.2
108 */
109 @VisibleForTesting
110 protected void exportToTsv(@Nullable OutputStream stream) {
111 try (PrintWriter pw = new PrintWriter(stream)) {
112 AbstractSegmentStoreStatisticsViewer statsViewer = fStatsViewer;
113 if (statsViewer == null) {
114 return;
115 }
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
123 // with gtk2
124 break;
125 }
126 columns.add(valueOf);
127 }
128 String join = Joiner.on('\t').skipNulls().join(columns);
129 pw.println(join);
130 for (int i = 0; i < size; i++) {
131 TreeItem item = tree.getItem(i);
132 printItem(pw, columns, item);
133 }
134 }
135 }
136
137 private void printItem(PrintWriter pw, List<String> columns, @Nullable TreeItem item) {
138 if (item == null) {
139 return;
140 }
141 List<String> data = new ArrayList<>();
142 for (int col = 0; col < columns.size(); col++) {
143 data.add(String.valueOf(item.getText(col)));
144 }
145 pw.println(Joiner.on('\t').join(data));
146 for (TreeItem child : item.getItems()) {
147 printItem(pw, columns, child);
148 }
149 }
150
ce8319b6 151}
This page took 0.044225 seconds and 5 git commands to generate.