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