tmf: Switch tmf.ui to Java 7 + fix warnings
[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 try (final BufferedWriter bw = new BufferedWriter(new FileWriter(destination));) {
82 if (fHeader != null) {
83 bw.write(fHeader);
84 bw.append('\n');
85 }
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),
90 ex);
91 return status;
92 }
93 }
94
95 private IStatus saveImpl(Writer bw, IProgressMonitor monitor) {
96 ExportToTextRequest request = new ExportToTextRequest(bw, fFilter, fTable);
97 fTrace.sendRequest(request);
98 int currentIndex = 0;
99 while (!request.isCompleted()) {
100 if (monitor.isCanceled()) {
101 request.cancel();
102 return Status.CANCEL_STATUS;
103 }
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;
109 }
110 try {
111 Thread.sleep(SLEEPING_INTERVAL);
112 } catch (InterruptedException e) {
113 }
114 }
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());
119 return status;
120 }
121 return Status.OK_STATUS;
122 }
123
124 @Override
125 public boolean belongsTo(Object family) {
126 return ExportToCSVJobFamily.equals(family);
127 }
128
129 }
This page took 0.040624 seconds and 5 git commands to generate.