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 / table / AbstractSegmentStoreTableView.java
CommitLineData
b9fff7ad 1/*******************************************************************************
edded5c1 2 * Copyright (c) 2015, 2016 Ericsson
b9fff7ad
BH
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 * France Lapointe Nguyen - Initial API and implementation
11 * Bernd Hufmann - Move abstract class to TMF
12 *******************************************************************************/
13
edded5c1 14package org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.table;
b9fff7ad 15
37b7faba
MK
16import java.io.OutputStream;
17import java.io.PrintWriter;
18import java.util.ArrayList;
19import java.util.List;
20
b9fff7ad 21import org.eclipse.jdt.annotation.Nullable;
37b7faba 22import org.eclipse.jface.action.Action;
b9fff7ad
BH
23import org.eclipse.jface.viewers.TableViewer;
24import org.eclipse.swt.SWT;
25import org.eclipse.swt.custom.SashForm;
26import org.eclipse.swt.widgets.Composite;
37b7faba
MK
27import org.eclipse.swt.widgets.Shell;
28import org.eclipse.swt.widgets.Table;
29import org.eclipse.swt.widgets.TableColumn;
30import org.eclipse.swt.widgets.TableItem;
31import org.eclipse.tracecompass.internal.analysis.timing.ui.views.segmentstore.ExportToTsvAction;
b9fff7ad
BH
32import org.eclipse.tracecompass.tmf.ui.views.TmfView;
33
37b7faba
MK
34import com.google.common.annotations.VisibleForTesting;
35import com.google.common.base.Joiner;
36
b9fff7ad
BH
37/**
38 * View for displaying a segment store analysis in a table.
39 *
40 * @author France Lapointe Nguyen
41 * @since 2.0
42 */
43public abstract class AbstractSegmentStoreTableView extends TmfView {
44
45 // ------------------------------------------------------------------------
46 // Attributes
47 // ------------------------------------------------------------------------
48
37b7faba
MK
49 private final Action fExportAction = new ExportToTsvAction() {
50 @Override
51 protected void exportToTsv(@Nullable OutputStream stream) {
52 AbstractSegmentStoreTableView.this.exportToTsv(stream);
53
54 }
55
56 @Override
57 protected @Nullable Shell getShell() {
58 return getViewSite().getShell();
59 }
60 };
61
6fe3b6eb 62 private @Nullable AbstractSegmentStoreTableViewer fSegmentStoreViewer;
b9fff7ad
BH
63
64 // ------------------------------------------------------------------------
65 // Constructor
66 // ------------------------------------------------------------------------
67
68 /**
69 * Constructor
70 */
71 public AbstractSegmentStoreTableView() {
72 super(""); //$NON-NLS-1$
73 }
74
75 // ------------------------------------------------------------------------
76 // ViewPart
77 // ------------------------------------------------------------------------
78
79 @Override
80 public void createPartControl(@Nullable Composite parent) {
81 SashForm sf = new SashForm(parent, SWT.NONE);
82 TableViewer tableViewer = new TableViewer(sf, SWT.FULL_SELECTION | SWT.VIRTUAL);
6fe3b6eb 83 fSegmentStoreViewer = createSegmentStoreViewer(tableViewer);
37b7faba 84 getViewSite().getActionBars().getMenuManager().add(fExportAction);
b9fff7ad
BH
85 setInitialData();
86 }
87
88 // ------------------------------------------------------------------------
89 // Operations
90 // ------------------------------------------------------------------------
91
92 @Override
93 public void setFocus() {
6fe3b6eb
MK
94 if (fSegmentStoreViewer != null) {
95 fSegmentStoreViewer.getTableViewer().getControl().setFocus();
b9fff7ad
BH
96 }
97 }
98
99 @Override
100 public void dispose() {
101 super.dispose();
6fe3b6eb
MK
102 if (fSegmentStoreViewer != null) {
103 fSegmentStoreViewer.dispose();
b9fff7ad
BH
104 }
105 }
106
107 /**
108 * Returns the latency analysis table viewer instance
109 *
110 * @param tableViewer
111 * the table viewer to use
112 * @return the latency analysis table viewer instance
113 */
6fe3b6eb
MK
114 protected abstract AbstractSegmentStoreTableViewer createSegmentStoreViewer(TableViewer tableViewer);
115
116 /**
117 * Get the table viewer
118 *
119 * @return the table viewer, useful for testing
120 */
121 @Nullable
122 public AbstractSegmentStoreTableViewer getSegmentStoreViewer() {
123 return fSegmentStoreViewer;
124 }
b9fff7ad
BH
125
126 /**
127 * Set initial data into the viewer
128 */
129 private void setInitialData() {
6fe3b6eb 130 if (fSegmentStoreViewer != null) {
0f769d2b 131 fSegmentStoreViewer.setData(fSegmentStoreViewer.getSegmentProvider());
b9fff7ad
BH
132 }
133 }
37b7faba
MK
134
135 /**
136 * Export a given items's TSV
137 *
138 * @param stream
139 * an output stream to write the TSV to
140 * @since 1.2
141 */
142 @VisibleForTesting
143 protected void exportToTsv(@Nullable OutputStream stream) {
144 try (PrintWriter pw = new PrintWriter(stream)) {
145 AbstractSegmentStoreTableViewer segmentStoreViewer = getSegmentStoreViewer();
146 if (segmentStoreViewer == null) {
147 return;
148 }
149 Table table = segmentStoreViewer.getTableViewer().getTable();
150 int size = table.getItemCount();
151 List<String> columns = new ArrayList<>();
152 for (int i = 0; i < table.getColumnCount(); i++) {
153 TableColumn column = table.getColumn(i);
154 if (column == null) {
155 return;
156 }
157 String columnName = String.valueOf(column.getText());
158 if (columnName.isEmpty() && i == table.getColumnCount() - 1) {
159 // Linux GTK2 undocumented feature
160 break;
161 }
162 columns.add(columnName);
163 }
164 pw.println(Joiner.on('\t').join(columns));
165 for (int i = 0; i < size; i++) {
166 TableItem item = table.getItem(i);
167 if (item == null) {
168 continue;
169 }
170 List<String> data = new ArrayList<>();
171 for (int col = 0; col < columns.size(); col++) {
172 data.add(String.valueOf(item.getText(col)));
173 }
174 pw.println(Joiner.on('\t').join(data));
175 }
176 }
177 }
b9fff7ad 178}
This page took 0.043412 seconds and 5 git commands to generate.