Move alltests plugin to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / commands / ExportToTextJob.java
CommitLineData
d3de0920 1/*******************************************************************************
53bae902 2 * Copyright (c) 2013, 2014 Kalray, Ericsson
d3de0920
XR
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 * Xavier Raynaud - Initial API and implementation
53bae902 11 * Bernd Hufmann - Adapted to new events table column API
d3de0920
XR
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.internal.tmf.ui.commands;
15
16import java.io.BufferedWriter;
17import java.io.FileWriter;
18import java.io.IOException;
19import java.io.Writer;
20import java.text.MessageFormat;
53bae902 21import java.util.List;
d3de0920
XR
22
23import org.eclipse.core.runtime.IProgressMonitor;
24import org.eclipse.core.runtime.IStatus;
25import org.eclipse.core.runtime.Status;
26import org.eclipse.core.runtime.jobs.Job;
27import org.eclipse.linuxtools.internal.tmf.ui.Activator;
28import org.eclipse.linuxtools.internal.tmf.ui.Messages;
29import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter;
30import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
89151d6a 31import org.eclipse.osgi.util.NLS;
53bae902 32import org.eclipse.linuxtools.tmf.ui.viewers.events.columns.TmfEventTableColumn;
d3de0920
XR
33
34/**
35 * This job exports traces to text files.
36 * @author Xavier Raynaud <xavier.raynaud@kalray.eu>
37 */
38public class ExportToTextJob extends Job {
39
40 private static final int TOTAL_WORK = 100;
41 private static final int SLEEPING_INTERVAL = 100;
42
43 /** the ExportToCSV job family */
44 public static final Object ExportToCSVJobFamily = new Object();
45
46 private final ITmfTrace fTrace;
47 private final ITmfFilter fFilter;
53bae902 48 private final List<TmfEventTableColumn> fColumns;
d3de0920
XR
49 private final String destination;
50
51 /**
52 * Job constructor.
53 *
54 * @param trace
55 * the trace to export
56 * @param filter
57 * the filter to apply when exporting the trace. may be null.
53bae902 58 * @param columns
d3de0920
XR
59 * the header to put at top of the exported file (may be <code>null</code>)
60 * @param destination
61 * the path of the file where the data is exported.
62 */
53bae902 63 public ExportToTextJob(ITmfTrace trace, ITmfFilter filter, List<TmfEventTableColumn> columns, String destination) {
d3de0920
XR
64 super(MessageFormat.format(Messages.ExportToTextJob_Export_to, destination));
65 this.fTrace = trace;
66 this.fFilter = filter;
53bae902 67 this.fColumns = columns;
d3de0920
XR
68 this.destination = destination;
69 }
70
71 @Override
72 public IStatus run(IProgressMonitor monitor) {
89151d6a 73 monitor.beginTask(NLS.bind(Messages.ExportToTextJob_Export_trace_to, destination), TOTAL_WORK);
d3de0920
XR
74 IStatus ret = saveImpl(monitor);
75 monitor.done();
76 return ret;
77 }
78
79 private IStatus saveImpl(IProgressMonitor monitor) {
507b1336 80 try (final BufferedWriter bw = new BufferedWriter(new FileWriter(destination));) {
53bae902
BH
81 if (fColumns != null) {
82 boolean needTab = false;
83 for (TmfEventTableColumn column : fColumns) {
84 if (needTab) {
85 bw.write('\t');
86 }
87 bw.write(column.getHeaderName());
88 needTab = true;
89 }
d3de0920
XR
90 bw.append('\n');
91 }
92 return saveImpl(bw, monitor);
93 } catch (IOException ex) {
94 Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID,
95 MessageFormat.format(Messages.ExportToTextJob_Unable_to_export_trace, destination),
96 ex);
97 return status;
d3de0920
XR
98 }
99 }
100
101 private IStatus saveImpl(Writer bw, IProgressMonitor monitor) {
53bae902 102 ExportToTextRequest request = new ExportToTextRequest(bw, fFilter, fColumns);
d3de0920
XR
103 fTrace.sendRequest(request);
104 int currentIndex = 0;
105 while (!request.isCompleted()) {
106 if (monitor.isCanceled()) {
107 request.cancel();
108 return Status.CANCEL_STATUS;
109 }
110 int index = (int) (request.getNbRead() * TOTAL_WORK / fTrace.getNbEvents());
111 if (index > currentIndex) {
112 int progress = index - currentIndex;
113 monitor.worked(progress);
114 currentIndex = index;
115 }
116 try {
117 Thread.sleep(SLEEPING_INTERVAL);
118 } catch (InterruptedException e) {
119 }
120 }
121 if (request.isFailed()) {
122 Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID,
123 MessageFormat.format(Messages.ExportToTextJob_Unable_to_export_trace, destination),
124 request.getIOException());
125 return status;
126 }
127 return Status.OK_STATUS;
128 }
129
130 @Override
131 public boolean belongsTo(Object family) {
132 return ExportToCSVJobFamily.equals(family);
133 }
134
135}
This page took 0.047561 seconds and 5 git commands to generate.