1 /*******************************************************************************
2 * Copyright (c) 2013 Kalray
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 * Xavier Raynaud - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.commands
;
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
;
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
;
32 * This job exports traces to text files.
33 * @author Xavier Raynaud <xavier.raynaud@kalray.eu>
35 public class ExportToTextJob
extends Job
{
37 private static final int TOTAL_WORK
= 100;
38 private static final int SLEEPING_INTERVAL
= 100;
40 /** the ExportToCSV job family */
41 public static final Object ExportToCSVJobFamily
= new Object();
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
;
55 * the filter to apply when exporting the trace. may be null.
57 * the {@link TmfEventsTable} requesting the export (may be <code>null</code>)
59 * the header to put at top of the exported file (may be <code>null</code>)
61 * the path of the file where the data is exported.
63 public ExportToTextJob(ITmfTrace trace
, ITmfFilter filter
, TmfEventsTable table
, String header
, String destination
) {
64 super(MessageFormat
.format(Messages
.ExportToTextJob_Export_to
, destination
));
66 this.fFilter
= filter
;
68 this.fHeader
= header
;
69 this.destination
= destination
;
73 public IStatus
run(IProgressMonitor monitor
) {
74 monitor
.beginTask(Messages
.ExportToTextJob_Export_trace_to
+ destination
, TOTAL_WORK
);
75 IStatus ret
= saveImpl(monitor
);
80 private IStatus
saveImpl(IProgressMonitor monitor
) {
81 final BufferedWriter bw
;
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
),
91 if (fHeader
!= null) {
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
),
104 } catch (IOException e
) {
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()) {
116 return Status
.CANCEL_STATUS
;
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
;
125 Thread
.sleep(SLEEPING_INTERVAL
);
126 } catch (InterruptedException e
) {
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());
135 return Status
.OK_STATUS
;
139 public boolean belongsTo(Object family
) {
140 return ExportToCSVJobFamily
.equals(family
);