1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 Ericsson
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
10 * France Lapointe Nguyen - Initial API and implementation
11 * Bernd Hufmann - Move abstract class to TMF
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.analysis
.timing
.ui
.views
.segmentstore
.table
;
16 import java
.io
.OutputStream
;
17 import java
.io
.PrintWriter
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
21 import org
.eclipse
.jdt
.annotation
.Nullable
;
22 import org
.eclipse
.jface
.action
.Action
;
23 import org
.eclipse
.jface
.viewers
.TableViewer
;
24 import org
.eclipse
.swt
.SWT
;
25 import org
.eclipse
.swt
.custom
.SashForm
;
26 import org
.eclipse
.swt
.widgets
.Composite
;
27 import org
.eclipse
.swt
.widgets
.Shell
;
28 import org
.eclipse
.swt
.widgets
.Table
;
29 import org
.eclipse
.swt
.widgets
.TableColumn
;
30 import org
.eclipse
.swt
.widgets
.TableItem
;
31 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.ui
.views
.segmentstore
.ExportToTsvAction
;
32 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.TmfView
;
34 import com
.google
.common
.annotations
.VisibleForTesting
;
35 import com
.google
.common
.base
.Joiner
;
38 * View for displaying a segment store analysis in a table.
40 * @author France Lapointe Nguyen
43 public abstract class AbstractSegmentStoreTableView
extends TmfView
{
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
49 private final Action fExportAction
= new ExportToTsvAction() {
51 protected void exportToTsv(@Nullable OutputStream stream
) {
52 AbstractSegmentStoreTableView
.this.exportToTsv(stream
);
57 protected @Nullable Shell
getShell() {
58 return getViewSite().getShell();
62 private @Nullable AbstractSegmentStoreTableViewer fSegmentStoreViewer
;
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
71 public AbstractSegmentStoreTableView() {
72 super(""); //$NON-NLS-1$
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
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
);
83 fSegmentStoreViewer
= createSegmentStoreViewer(tableViewer
);
84 getViewSite().getActionBars().getMenuManager().add(fExportAction
);
88 // ------------------------------------------------------------------------
90 // ------------------------------------------------------------------------
93 public void setFocus() {
94 if (fSegmentStoreViewer
!= null) {
95 fSegmentStoreViewer
.getTableViewer().getControl().setFocus();
100 public void dispose() {
102 if (fSegmentStoreViewer
!= null) {
103 fSegmentStoreViewer
.dispose();
108 * Returns the latency analysis table viewer instance
111 * the table viewer to use
112 * @return the latency analysis table viewer instance
114 protected abstract AbstractSegmentStoreTableViewer
createSegmentStoreViewer(TableViewer tableViewer
);
117 * Get the table viewer
119 * @return the table viewer, useful for testing
122 public AbstractSegmentStoreTableViewer
getSegmentStoreViewer() {
123 return fSegmentStoreViewer
;
127 * Set initial data into the viewer
129 private void setInitialData() {
130 if (fSegmentStoreViewer
!= null) {
131 fSegmentStoreViewer
.setData(fSegmentStoreViewer
.getSegmentProvider());
136 * Export a given items's TSV
139 * an output stream to write the TSV to
143 protected void exportToTsv(@Nullable OutputStream stream
) {
144 try (PrintWriter pw
= new PrintWriter(stream
)) {
145 AbstractSegmentStoreTableViewer segmentStoreViewer
= getSegmentStoreViewer();
146 if (segmentStoreViewer
== null) {
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) {
157 String columnName
= String
.valueOf(column
.getText());
158 if (columnName
.isEmpty() && i
== table
.getColumnCount() - 1) {
159 // Linux GTK2 undocumented feature
162 columns
.add(columnName
);
164 pw
.println(Joiner
.on('\t').join(columns
));
165 for (int i
= 0; i
< size
; i
++) {
166 TableItem item
= table
.getItem(i
);
170 List
<String
> data
= new ArrayList
<>();
171 for (int col
= 0; col
< columns
.size(); col
++) {
172 data
.add(String
.valueOf(item
.getText(col
)));
174 pw
.println(Joiner
.on('\t').join(data
));