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
index 069c67442f6e4409eaec089777dea4e56a784ef9..0a18e3977a095ec291126b2530e7a1b1be4afc5f 100644 (file)
  *******************************************************************************/
 package org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.statistics;
 
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.List;
+
 import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.jface.action.Action;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Tree;
+import org.eclipse.swt.widgets.TreeItem;
 import org.eclipse.tracecompass.common.core.NonNullUtils;
+import org.eclipse.tracecompass.internal.analysis.timing.ui.views.segmentstore.ExportToTsvAction;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
 import org.eclipse.tracecompass.tmf.ui.views.TmfView;
 
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Joiner;
+
 /**
  * Abstract view to to be extended to display segment store statistics.
  *
@@ -26,7 +39,20 @@ import org.eclipse.tracecompass.tmf.ui.views.TmfView;
  */
 public abstract class AbstractSegmentStoreStatisticsView extends TmfView {
 
-    @Nullable private AbstractSegmentStoreStatisticsViewer fStatsViewer = null;
+    private final Action fExportAction = new ExportToTsvAction() {
+        @Override
+        protected void exportToTsv(@Nullable OutputStream stream) {
+            AbstractSegmentStoreStatisticsView.this.exportToTsv(stream);
+        }
+
+        @Override
+        protected @Nullable Shell getShell() {
+            return getViewSite().getShell();
+        }
+
+    };
+
+    private @Nullable AbstractSegmentStoreStatisticsViewer fStatsViewer = null;
 
     /**
      * Constructor
@@ -44,6 +70,7 @@ public abstract class AbstractSegmentStoreStatisticsView extends TmfView {
             statsViewer.loadTrace(trace);
         }
         fStatsViewer = statsViewer;
+        getViewSite().getActionBars().getMenuManager().add(fExportAction);
     }
 
     @Override
@@ -72,4 +99,53 @@ public abstract class AbstractSegmentStoreStatisticsView extends TmfView {
      */
     protected abstract AbstractSegmentStoreStatisticsViewer createSegmentStoreStatisticsViewer(Composite parent);
 
+    /**
+     * Export a given items's TSV
+     *
+     * @param stream
+     *            an output stream to write the TSV to
+     * @since 1.2
+     */
+    @VisibleForTesting
+    protected void exportToTsv(@Nullable OutputStream stream) {
+        try (PrintWriter pw = new PrintWriter(stream)) {
+            AbstractSegmentStoreStatisticsViewer statsViewer = fStatsViewer;
+            if (statsViewer == null) {
+                return;
+            }
+            Tree tree = statsViewer.getTreeViewer().getTree();
+            int size = tree.getItemCount();
+            List<String> columns = new ArrayList<>();
+            for (int i = 0; i < tree.getColumnCount(); i++) {
+                String valueOf = String.valueOf(tree.getColumn(i).getText());
+                if (valueOf.isEmpty() && i == tree.getColumnCount() - 1) {
+                    // Linux "feature", an invisible column is added at the end
+                    // with gtk2
+                    break;
+                }
+                columns.add(valueOf);
+            }
+            String join = Joiner.on('\t').skipNulls().join(columns);
+            pw.println(join);
+            for (int i = 0; i < size; i++) {
+                TreeItem item = tree.getItem(i);
+                printItem(pw, columns, item);
+            }
+        }
+    }
+
+    private void printItem(PrintWriter pw, List<String> columns, @Nullable TreeItem item) {
+        if (item == null) {
+            return;
+        }
+        List<String> data = new ArrayList<>();
+        for (int col = 0; col < columns.size(); col++) {
+            data.add(String.valueOf(item.getText(col)));
+        }
+        pw.println(Joiner.on('\t').join(data));
+        for (TreeItem child : item.getItems()) {
+            printItem(pw, columns, child);
+        }
+    }
+
 }
This page took 0.02603 seconds and 5 git commands to generate.