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 try (final BufferedWriter bw
= new BufferedWriter(new FileWriter(destination
));) {
82 if (fHeader
!= null) {
86 return saveImpl(bw
, monitor
);
87 } catch (IOException ex
) {
88 Status status
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
,
89 MessageFormat
.format(Messages
.ExportToTextJob_Unable_to_export_trace
, destination
),
95 private IStatus
saveImpl(Writer bw
, IProgressMonitor monitor
) {
96 ExportToTextRequest request
= new ExportToTextRequest(bw
, fFilter
, fTable
);
97 fTrace
.sendRequest(request
);
99 while (!request
.isCompleted()) {
100 if (monitor
.isCanceled()) {
102 return Status
.CANCEL_STATUS
;
104 int index
= (int) (request
.getNbRead() * TOTAL_WORK
/ fTrace
.getNbEvents());
105 if (index
> currentIndex
) {
106 int progress
= index
- currentIndex
;
107 monitor
.worked(progress
);
108 currentIndex
= index
;
111 Thread
.sleep(SLEEPING_INTERVAL
);
112 } catch (InterruptedException e
) {
115 if (request
.isFailed()) {
116 Status status
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
,
117 MessageFormat
.format(Messages
.ExportToTextJob_Unable_to_export_trace
, destination
),
118 request
.getIOException());
121 return Status
.OK_STATUS
;
125 public boolean belongsTo(Object family
) {
126 return ExportToCSVJobFamily
.equals(family
);