29c251d1d0c9464348da20fd51b09da05346ee7e
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / commands / ExportToTextJob.java
1 /*******************************************************************************
2 * Copyright (c) 2013 Kalray
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
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.tmf.ui.commands;
14
15 import java.io.BufferedWriter;
16 import java.io.FileWriter;
17 import java.io.IOException;
18 import java.io.Writer;
19 import java.text.MessageFormat;
20
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.core.runtime.jobs.Job;
25 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
26 import org.eclipse.linuxtools.internal.tmf.ui.Messages;
27 import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter;
28 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
29 import org.eclipse.linuxtools.tmf.ui.viewers.events.TmfEventsTable;
30
31 /**
32 * This job exports traces to text files.
33 * @author Xavier Raynaud <xavier.raynaud@kalray.eu>
34 */
35 public class ExportToTextJob extends Job {
36
37 private static final int TOTAL_WORK = 100;
38 private static final int SLEEPING_INTERVAL = 100;
39
40 /** the ExportToCSV job family */
41 public static final Object ExportToCSVJobFamily = new Object();
42
43 private final ITmfTrace fTrace;
44 private final ITmfFilter fFilter;
45 private final TmfEventsTable fTable;
46 private final String fHeader;
47 private final String destination;
48
49 /**
50 * Job constructor.
51 *
52 * @param trace
53 * the trace to export
54 * @param filter
55 * the filter to apply when exporting the trace. may be null.
56 * @param table
57 * the {@link TmfEventsTable} requesting the export (may be <code>null</code>)
58 * @param header
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 */
63 public ExportToTextJob(ITmfTrace trace, ITmfFilter filter, TmfEventsTable table, String header, String destination) {
64 super(MessageFormat.format(Messages.ExportToTextJob_Export_to, destination));
65 this.fTrace = trace;
66 this.fFilter = filter;
67 this.fTable = table;
68 this.fHeader = header;
69 this.destination = destination;
70 }
71
72 @Override
73 public IStatus run(IProgressMonitor monitor) {
74 monitor.beginTask(Messages.ExportToTextJob_Export_trace_to + destination, TOTAL_WORK);
75 IStatus ret = saveImpl(monitor);
76 monitor.done();
77 return ret;
78 }
79
80 private IStatus saveImpl(IProgressMonitor monitor) {
81 final BufferedWriter bw;
82 try {
83 bw = new BufferedWriter(new FileWriter(destination));
84 } catch (IOException ex) {
85 Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID,
86 MessageFormat.format(Messages.ExportToTextJob_Unable_to_export_trace, destination),
87 ex);
88 return status;
89 }
90 try {
91 if (fHeader != null) {
92 bw.write(fHeader);
93 bw.append('\n');
94 }
95 return saveImpl(bw, monitor);
96 } catch (IOException ex) {
97 Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID,
98 MessageFormat.format(Messages.ExportToTextJob_Unable_to_export_trace, destination),
99 ex);
100 return status;
101 } finally {
102 try {
103 bw.close();
104 } catch (IOException e) {
105 }
106 }
107 }
108
109 private IStatus saveImpl(Writer bw, IProgressMonitor monitor) {
110 ExportToTextRequest request = new ExportToTextRequest(bw, fFilter, fTable);
111 fTrace.sendRequest(request);
112 int currentIndex = 0;
113 while (!request.isCompleted()) {
114 if (monitor.isCanceled()) {
115 request.cancel();
116 return Status.CANCEL_STATUS;
117 }
118 int index = (int) (request.getNbRead() * TOTAL_WORK / fTrace.getNbEvents());
119 if (index > currentIndex) {
120 int progress = index - currentIndex;
121 monitor.worked(progress);
122 currentIndex = index;
123 }
124 try {
125 Thread.sleep(SLEEPING_INTERVAL);
126 } catch (InterruptedException e) {
127 }
128 }
129 if (request.isFailed()) {
130 Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID,
131 MessageFormat.format(Messages.ExportToTextJob_Unable_to_export_trace, destination),
132 request.getIOException());
133 return status;
134 }
135 return Status.OK_STATUS;
136 }
137
138 @Override
139 public boolean belongsTo(Object family) {
140 return ExportToCSVJobFamily.equals(family);
141 }
142
143 }
This page took 0.03362 seconds and 4 git commands to generate.