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
;
30 import org
.eclipse
.osgi
.util
.NLS
;
33 * This job exports traces to text files.
34 * @author Xavier Raynaud <xavier.raynaud@kalray.eu>
36 public class ExportToTextJob
extends Job
{
38 private static final int TOTAL_WORK
= 100;
39 private static final int SLEEPING_INTERVAL
= 100;
41 /** the ExportToCSV job family */
42 public static final Object ExportToCSVJobFamily
= new Object();
44 private final ITmfTrace fTrace
;
45 private final ITmfFilter fFilter
;
46 private final TmfEventsTable fTable
;
47 private final String fHeader
;
48 private final String destination
;
56 * the filter to apply when exporting the trace. may be null.
58 * the {@link TmfEventsTable} requesting the export (may be <code>null</code>)
60 * the header to put at top of the exported file (may be <code>null</code>)
62 * the path of the file where the data is exported.
64 public ExportToTextJob(ITmfTrace trace
, ITmfFilter filter
, TmfEventsTable table
, String header
, String destination
) {
65 super(MessageFormat
.format(Messages
.ExportToTextJob_Export_to
, destination
));
67 this.fFilter
= filter
;
69 this.fHeader
= header
;
70 this.destination
= destination
;
74 public IStatus
run(IProgressMonitor monitor
) {
75 monitor
.beginTask(NLS
.bind(Messages
.ExportToTextJob_Export_trace_to
, destination
), TOTAL_WORK
);
76 IStatus ret
= saveImpl(monitor
);
81 private IStatus
saveImpl(IProgressMonitor monitor
) {
82 try (final BufferedWriter bw
= new BufferedWriter(new FileWriter(destination
));) {
83 if (fHeader
!= null) {
87 return saveImpl(bw
, monitor
);
88 } catch (IOException ex
) {
89 Status status
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
,
90 MessageFormat
.format(Messages
.ExportToTextJob_Unable_to_export_trace
, destination
),
96 private IStatus
saveImpl(Writer bw
, IProgressMonitor monitor
) {
97 ExportToTextRequest request
= new ExportToTextRequest(bw
, fFilter
, fTable
);
98 fTrace
.sendRequest(request
);
100 while (!request
.isCompleted()) {
101 if (monitor
.isCanceled()) {
103 return Status
.CANCEL_STATUS
;
105 int index
= (int) (request
.getNbRead() * TOTAL_WORK
/ fTrace
.getNbEvents());
106 if (index
> currentIndex
) {
107 int progress
= index
- currentIndex
;
108 monitor
.worked(progress
);
109 currentIndex
= index
;
112 Thread
.sleep(SLEEPING_INTERVAL
);
113 } catch (InterruptedException e
) {
116 if (request
.isFailed()) {
117 Status status
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
,
118 MessageFormat
.format(Messages
.ExportToTextJob_Unable_to_export_trace
, destination
),
119 request
.getIOException());
122 return Status
.OK_STATUS
;
126 public boolean belongsTo(Object family
) {
127 return ExportToCSVJobFamily
.equals(family
);