ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / commands / ExportToTextRequest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Kalray, Ericsson
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 * Bernd Hufmann - Adapted to new events table column API
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.internal.tmf.ui.commands;
15
16 import java.io.IOException;
17 import java.io.Writer;
18 import java.util.List;
19
20 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
21 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
22 import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter;
23 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
24 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
25 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
26 import org.eclipse.linuxtools.tmf.ui.viewers.events.columns.TmfEventTableColumn;
27
28 /**
29 * This TMF Requests exports traces to text files.
30 * @author Xavier Raynaud <xavier.raynaud@kalray.eu>
31 */
32 public class ExportToTextRequest extends TmfEventRequest {
33
34 private final Writer fWriter;
35 private final ITmfFilter fFilter;
36 private final List<TmfEventTableColumn> fColumns;
37 private IOException fIOException;
38
39 /**
40 * Constructor
41 * @param w
42 * a Writer, typically a FileWriter.
43 * @param filter
44 * a TmfFilter, if we want to filter some events. May be <code>null</code>.
45 * @param columns
46 * the {@link TmfEventTableColumn} requesting the export (may be <code>null</code>)
47 */
48 public ExportToTextRequest(Writer w, ITmfFilter filter, List<TmfEventTableColumn> columns) {
49 super(ITmfEvent.class, TmfTimeRange.ETERNITY, 0, ITmfEventRequest.ALL_DATA, ExecutionType.FOREGROUND);
50 this.fWriter = w;
51 this.fFilter = filter;
52 this.fColumns = columns;
53 }
54
55 /**
56 * Gets the IOException thrown by this export request, if any.
57 * @return the fIoException
58 */
59 public IOException getIOException() {
60 return fIOException;
61 }
62
63 @Override
64 public void handleData(final ITmfEvent event) {
65 super.handleData(event);
66 if (isCancelled()) {
67 return;
68 }
69 try {
70 if (fFilter == null || fFilter.matches(event)) {
71 if (fColumns != null) {
72 boolean needTab = false;
73 for (TmfEventTableColumn column : fColumns) {
74 if (needTab) {
75 fWriter.write('\t');
76 }
77 fWriter.write(column.getItemString(event));
78 needTab = true;
79 }
80 } else { // fallback to default formatting
81 fWriter.write(event.getTimestamp().toString());
82 fWriter.write('\t');
83 fWriter.write(event.getSource());
84 fWriter.write('\t');
85 fWriter.write(event.getType().getName());
86 fWriter.write('\t');
87 fWriter.write(event.getReference());
88 fWriter.write('\t');
89 ITmfEventField content = event.getContent();
90 Object value = content.getValue();
91 if (value != null) {
92 fWriter.write(value.toString());
93 }
94 }
95 fWriter.write('\n');
96 }
97 } catch (IOException ex) {
98 fIOException = ex;
99 fail();
100 }
101 }
102
103 }
This page took 0.032381 seconds and 5 git commands to generate.