tmf: Fix some NLS Message strings
[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 import org.eclipse.osgi.util.NLS;
31
32 /**
33 * This job exports traces to text files.
34 * @author Xavier Raynaud <xavier.raynaud@kalray.eu>
35 */
36 public class ExportToTextJob extends Job {
37
38 private static final int TOTAL_WORK = 100;
39 private static final int SLEEPING_INTERVAL = 100;
40
41 /** the ExportToCSV job family */
42 public static final Object ExportToCSVJobFamily = new Object();
43
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;
49
50 /**
51 * Job constructor.
52 *
53 * @param trace
54 * the trace to export
55 * @param filter
56 * the filter to apply when exporting the trace. may be null.
57 * @param table
58 * the {@link TmfEventsTable} requesting the export (may be <code>null</code>)
59 * @param header
60 * the header to put at top of the exported file (may be <code>null</code>)
61 * @param destination
62 * the path of the file where the data is exported.
63 */
64 public ExportToTextJob(ITmfTrace trace, ITmfFilter filter, TmfEventsTable table, String header, String destination) {
65 super(MessageFormat.format(Messages.ExportToTextJob_Export_to, destination));
66 this.fTrace = trace;
67 this.fFilter = filter;
68 this.fTable = table;
69 this.fHeader = header;
70 this.destination = destination;
71 }
72
73 @Override
74 public IStatus run(IProgressMonitor monitor) {
75 monitor.beginTask(NLS.bind(Messages.ExportToTextJob_Export_trace_to, destination), TOTAL_WORK);
76 IStatus ret = saveImpl(monitor);
77 monitor.done();
78 return ret;
79 }
80
81 private IStatus saveImpl(IProgressMonitor monitor) {
82 try (final BufferedWriter bw = new BufferedWriter(new FileWriter(destination));) {
83 if (fHeader != null) {
84 bw.write(fHeader);
85 bw.append('\n');
86 }
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),
91 ex);
92 return status;
93 }
94 }
95
96 private IStatus saveImpl(Writer bw, IProgressMonitor monitor) {
97 ExportToTextRequest request = new ExportToTextRequest(bw, fFilter, fTable);
98 fTrace.sendRequest(request);
99 int currentIndex = 0;
100 while (!request.isCompleted()) {
101 if (monitor.isCanceled()) {
102 request.cancel();
103 return Status.CANCEL_STATUS;
104 }
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;
110 }
111 try {
112 Thread.sleep(SLEEPING_INTERVAL);
113 } catch (InterruptedException e) {
114 }
115 }
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());
120 return status;
121 }
122 return Status.OK_STATUS;
123 }
124
125 @Override
126 public boolean belongsTo(Object family) {
127 return ExportToCSVJobFamily.equals(family);
128 }
129
130 }
This page took 0.035194 seconds and 5 git commands to generate.