From d3de0920ea03d1097b6fa29e99296bd0a6ecf3a9 Mon Sep 17 00:00:00 2001 From: Xavier Raynaud Date: Thu, 14 Nov 2013 12:58:46 +0100 Subject: [PATCH] TMF: Export trace to text Add a command to export traces to text. If a filter is applied, only filtered elements are exported. Change-Id: Ie213eda715f68104572704cb3c08226aa5c52a85 Signed-off-by: Xavier Raynaud Reviewed-on: https://git.eclipse.org/r/17813 Tested-by: Hudson CI Reviewed-by: Bernd Hufmann IP-Clean: Bernd Hufmann Tested-by: Bernd Hufmann --- .../ui/views/events/GdbEventsTable.java | 2 +- .../ui/viewers/events/LTTng2EventsTable.java | 2 +- .../tmf/core/trace/TmfTraceContext.java | 27 ++++ .../tmf/core/trace/TmfTraceManager.java | 34 ++++- .../plugin.properties | 5 +- org.eclipse.linuxtools.tmf.ui/plugin.xml | 10 ++ .../linuxtools/internal/tmf/ui/Messages.java | 6 + .../commands/ExportToTextCommandHandler.java | 93 ++++++++++++ .../tmf/ui/commands/ExportToTextJob.java | 143 ++++++++++++++++++ .../tmf/ui/commands/ExportToTextRequest.java | 104 +++++++++++++ .../internal/tmf/ui/messages.properties | 4 + .../tmf/ui/viewers/events/TmfEventsTable.java | 65 +++++++- 12 files changed, 490 insertions(+), 5 deletions(-) create mode 100644 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/commands/ExportToTextCommandHandler.java create mode 100644 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/commands/ExportToTextJob.java create mode 100644 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/commands/ExportToTextRequest.java diff --git a/org.eclipse.linuxtools.gdbtrace.ui/src/org/eclipse/linuxtools/internal/gdbtrace/ui/views/events/GdbEventsTable.java b/org.eclipse.linuxtools.gdbtrace.ui/src/org/eclipse/linuxtools/internal/gdbtrace/ui/views/events/GdbEventsTable.java index f4e06c18e1..98f8ada9bf 100644 --- a/org.eclipse.linuxtools.gdbtrace.ui/src/org/eclipse/linuxtools/internal/gdbtrace/ui/views/events/GdbEventsTable.java +++ b/org.eclipse.linuxtools.gdbtrace.ui/src/org/eclipse/linuxtools/internal/gdbtrace/ui/views/events/GdbEventsTable.java @@ -111,7 +111,7 @@ public class GdbEventsTable extends TmfEventsTable { @Override protected ITmfEventField[] extractItemFields(ITmfEvent event) { - ITmfEventField[] fields = new TmfEventField[0]; + ITmfEventField[] fields = EMPTY_FIELD_ARRAY; if (event != null) { GdbTraceEventContent content = (GdbTraceEventContent) event.getContent(); fields = new TmfEventField[] { diff --git a/org.eclipse.linuxtools.lttng2.kernel.ui/src/org/eclipse/linuxtools/internal/lttng2/kernel/ui/viewers/events/LTTng2EventsTable.java b/org.eclipse.linuxtools.lttng2.kernel.ui/src/org/eclipse/linuxtools/internal/lttng2/kernel/ui/viewers/events/LTTng2EventsTable.java index fa2ca56eee..265ebebf0d 100644 --- a/org.eclipse.linuxtools.lttng2.kernel.ui/src/org/eclipse/linuxtools/internal/lttng2/kernel/ui/viewers/events/LTTng2EventsTable.java +++ b/org.eclipse.linuxtools.lttng2.kernel.ui/src/org/eclipse/linuxtools/internal/lttng2/kernel/ui/viewers/events/LTTng2EventsTable.java @@ -70,7 +70,7 @@ public class LTTng2EventsTable extends TmfEventsTable { @Override protected ITmfEventField[] extractItemFields(ITmfEvent event) { - ITmfEventField[] fields = new TmfEventField[0]; + ITmfEventField[] fields = EMPTY_FIELD_ARRAY; if (event != null) { fields = new TmfEventField[] { new TmfEventField(ITmfEvent.EVENT_FIELD_TIMESTAMP, event.getTimestamp().toString(), null), diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTraceContext.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTraceContext.java index 66370834b1..58f99b0abc 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTraceContext.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTraceContext.java @@ -9,10 +9,12 @@ * Contributors: * Alexandre Montplaisir - Initial API and implementation * Patrick Tasse - Support selection range + * Xavier Raynaud - Support filters tracking *******************************************************************************/ package org.eclipse.linuxtools.tmf.core.trace; +import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter; import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; @@ -33,20 +35,37 @@ final class TmfTraceContext { private final TmfTimeRange fSelection; private final TmfTimeRange fWindowRange; + private final ITmfFilter fFilter; public TmfTraceContext(ITmfTimestamp beginTs, ITmfTimestamp endTs, TmfTimeRange tr) { fSelection = new TmfTimeRange(beginTs, endTs); fWindowRange = tr; + fFilter = null; } public TmfTraceContext(TmfTraceContext prevCtx, ITmfTimestamp beginTs, ITmfTimestamp endTs) { fSelection = new TmfTimeRange(beginTs, endTs); fWindowRange = prevCtx.fWindowRange; + fFilter = prevCtx.fFilter; } public TmfTraceContext(TmfTraceContext prevCtx, TmfTimeRange tr) { fSelection = prevCtx.fSelection; fWindowRange = tr; + fFilter = prevCtx.fFilter; + } + + /** + * @param prevCtx + * The previous context + * @param filter + * The applied filter + * @since 2.2 + */ + public TmfTraceContext(TmfTraceContext prevCtx, ITmfFilter filter) { + fSelection = prevCtx.fSelection; + fWindowRange = prevCtx.fWindowRange; + fFilter = filter; } public ITmfTimestamp getSelectionBegin() { @@ -61,6 +80,14 @@ final class TmfTraceContext { return fWindowRange; } + /** + * @return the current filter applied to the trace + * @since 2.2 + */ + public ITmfFilter getFilter() { + return fFilter; + } + public boolean isValid() { if (fSelection.getStartTime().compareTo(TmfTimestamp.ZERO) <= 0 || fSelection.getEndTime().compareTo(TmfTimestamp.ZERO) <= 0 || diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTraceManager.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTraceManager.java index a2a5d15d0c..770de0a93a 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTraceManager.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTraceManager.java @@ -9,6 +9,7 @@ * Contributors: * Alexandre Montplaisir - Initial API and implementation * Patrick Tasse - Support selection range + * Xavier Raynaud - Support filters tracking *******************************************************************************/ package org.eclipse.linuxtools.tmf.core.trace; @@ -22,6 +23,8 @@ import java.util.Set; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; +import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter; +import org.eclipse.linuxtools.tmf.core.signal.TmfEventFilterAppliedSignal; import org.eclipse.linuxtools.tmf.core.signal.TmfRangeSynchSignal; import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler; import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager; @@ -36,7 +39,8 @@ import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; /** * Central trace manager for TMF. It tracks the currently opened traces and * experiment, as well as the currently-selected time or time range and the - * current window time range for each one of those. + * current window time range for each one of those. It also tracks filters + * applied for each trace. * * It's a singleton class, so only one instance should exist (available via * {@link #getInstance()}). @@ -119,6 +123,17 @@ public final class TmfTraceManager { return getCurrentTraceContext().getWindowRange(); } + /** + * Gets the filter applied to the current trace + * + * @return + * a filter, or null + * @since 2.2 + */ + public synchronized ITmfFilter getCurrentFilter() { + return getCurrentTraceContext().getFilter(); + } + /** * Get the currently selected trace (normally, the focused editor). * @@ -252,6 +267,23 @@ public final class TmfTraceManager { fCurrentTrace = newTrace; } + /** + * Signal handler for the filterApplied signal. + * + * @param signal + * The incoming signal + * @since 2.2 + */ + @TmfSignalHandler + public synchronized void filterApplied(TmfEventFilterAppliedSignal signal) { + final ITmfTrace newTrace = signal.getTrace(); + TmfTraceContext context = fTraces.get(newTrace); + if (context == null) { + throw new RuntimeException(); + } + fTraces.put(newTrace, new TmfTraceContext(context, signal.getEventFilter())); + } + /** * Signal handler for the traceClosed signal. * diff --git a/org.eclipse.linuxtools.tmf.ui/plugin.properties b/org.eclipse.linuxtools.tmf.ui/plugin.properties index 9f43e208f7..ab241fca3b 100644 --- a/org.eclipse.linuxtools.tmf.ui/plugin.properties +++ b/org.eclipse.linuxtools.tmf.ui/plugin.properties @@ -89,7 +89,10 @@ command.select_trace_type.icon = Icon command.batch_import = Batch Import... command.batch_import.mnemonic = B -command.batch_import.descriptio = Batch Import +command.batch_import.description = Batch Import + +command.export_to_text = Export To Text... +command.export_to_text.description = Export trace to text... ## Trace menu # Open, Copy, Rename, Delete, Delete Supplementary Files, Select Trace Type diff --git a/org.eclipse.linuxtools.tmf.ui/plugin.xml b/org.eclipse.linuxtools.tmf.ui/plugin.xml index c0a01acfa6..229929ce81 100644 --- a/org.eclipse.linuxtools.tmf.ui/plugin.xml +++ b/org.eclipse.linuxtools.tmf.ui/plugin.xml @@ -822,6 +822,12 @@ id="org.eclipse.linuxtools.tmf.ui.batch_import" name="%command.batch_import"> + + @@ -1124,6 +1130,10 @@ + + + */ +public class ExportToTextCommandHandler extends AbstractHandler { + + /** Id of the export-to-text command */ + public static final String COMMAND_ID = "org.eclipse.linuxtools.tmf.ui.exportToText"; //$NON-NLS-1$ + /** + * Id used to retrieve the TmfEventsTable that launched this command. + * This TmfEventsTable is taken from the application context of this handler. + */ + public static final String TMF_EVENT_TABLE_PARAMETER_ID = "org.eclipse.linuxtools.tmf.ui.exportToText.table"; //$NON-NLS-1$ + /** + * Id used to retrieve the header (as a String) of the trace to export. + * This header is from the application context of this handler. + */ + public static final String TMF_EVENT_TABLE_HEADER_ID = "org.eclipse.linuxtools.tmf.ui.exportToText.header"; //$NON-NLS-1$ + + /** + * Constructor + */ + public ExportToTextCommandHandler() { + } + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + String header = getHeader(event.getApplicationContext()); + TmfEventsTable table = getTable(event.getApplicationContext()); + ITmfTrace trace = TmfTraceManager.getInstance().getActiveTrace(); + ITmfFilter filter = TmfTraceManager.getInstance().getCurrentFilter(); + if (trace != null) { + FileDialog fd = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.SAVE); + fd.setFilterExtensions(new String[] { "*.csv", "*.*", "*" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + fd.setOverwrite(true); + final String s = fd.open(); + if (s != null) { + Job j = new ExportToTextJob(trace, filter, table, header, s); + j.setUser(true); + j.schedule(); + } + } + return null; + } + + private static String getHeader(Object evaluationContext) { + if (evaluationContext instanceof IEvaluationContext) { + Object s = ((IEvaluationContext) evaluationContext).getVariable(TMF_EVENT_TABLE_HEADER_ID); + if (s instanceof String) { + return s.toString(); + } + } + return null; + } + + private static TmfEventsTable getTable(Object evaluationContext) { + if (evaluationContext instanceof IEvaluationContext) { + Object o = ((IEvaluationContext) evaluationContext).getVariable(TMF_EVENT_TABLE_PARAMETER_ID); + if (o instanceof TmfEventsTable) { + return (TmfEventsTable) o; + } + } + return null; + } + +} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/commands/ExportToTextJob.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/commands/ExportToTextJob.java new file mode 100644 index 0000000000..29c251d1d0 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/commands/ExportToTextJob.java @@ -0,0 +1,143 @@ +/******************************************************************************* + * Copyright (c) 2013 Kalray + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Xavier Raynaud - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.internal.tmf.ui.commands; + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; +import java.text.MessageFormat; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.linuxtools.internal.tmf.ui.Activator; +import org.eclipse.linuxtools.internal.tmf.ui.Messages; +import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter; +import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; +import org.eclipse.linuxtools.tmf.ui.viewers.events.TmfEventsTable; + +/** + * This job exports traces to text files. + * @author Xavier Raynaud + */ +public class ExportToTextJob extends Job { + + private static final int TOTAL_WORK = 100; + private static final int SLEEPING_INTERVAL = 100; + + /** the ExportToCSV job family */ + public static final Object ExportToCSVJobFamily = new Object(); + + private final ITmfTrace fTrace; + private final ITmfFilter fFilter; + private final TmfEventsTable fTable; + private final String fHeader; + private final String destination; + + /** + * Job constructor. + * + * @param trace + * the trace to export + * @param filter + * the filter to apply when exporting the trace. may be null. + * @param table + * the {@link TmfEventsTable} requesting the export (may be null) + * @param header + * the header to put at top of the exported file (may be null) + * @param destination + * the path of the file where the data is exported. + */ + public ExportToTextJob(ITmfTrace trace, ITmfFilter filter, TmfEventsTable table, String header, String destination) { + super(MessageFormat.format(Messages.ExportToTextJob_Export_to, destination)); + this.fTrace = trace; + this.fFilter = filter; + this.fTable = table; + this.fHeader = header; + this.destination = destination; + } + + @Override + public IStatus run(IProgressMonitor monitor) { + monitor.beginTask(Messages.ExportToTextJob_Export_trace_to + destination, TOTAL_WORK); + IStatus ret = saveImpl(monitor); + monitor.done(); + return ret; + } + + private IStatus saveImpl(IProgressMonitor monitor) { + final BufferedWriter bw; + try { + bw = new BufferedWriter(new FileWriter(destination)); + } catch (IOException ex) { + Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, + MessageFormat.format(Messages.ExportToTextJob_Unable_to_export_trace, destination), + ex); + return status; + } + try { + if (fHeader != null) { + bw.write(fHeader); + bw.append('\n'); + } + return saveImpl(bw, monitor); + } catch (IOException ex) { + Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, + MessageFormat.format(Messages.ExportToTextJob_Unable_to_export_trace, destination), + ex); + return status; + } finally { + try { + bw.close(); + } catch (IOException e) { + } + } + } + + private IStatus saveImpl(Writer bw, IProgressMonitor monitor) { + ExportToTextRequest request = new ExportToTextRequest(bw, fFilter, fTable); + fTrace.sendRequest(request); + int currentIndex = 0; + while (!request.isCompleted()) { + if (monitor.isCanceled()) { + request.cancel(); + return Status.CANCEL_STATUS; + } + int index = (int) (request.getNbRead() * TOTAL_WORK / fTrace.getNbEvents()); + if (index > currentIndex) { + int progress = index - currentIndex; + monitor.worked(progress); + currentIndex = index; + } + try { + Thread.sleep(SLEEPING_INTERVAL); + } catch (InterruptedException e) { + } + } + if (request.isFailed()) { + Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, + MessageFormat.format(Messages.ExportToTextJob_Unable_to_export_trace, destination), + request.getIOException()); + return status; + } + return Status.OK_STATUS; + } + + @Override + public boolean belongsTo(Object family) { + return ExportToCSVJobFamily.equals(family); + } + +} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/commands/ExportToTextRequest.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/commands/ExportToTextRequest.java new file mode 100644 index 0000000000..53268a8554 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/commands/ExportToTextRequest.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * Copyright (c) 2013 Kalray + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Xavier Raynaud - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.internal.tmf.ui.commands; + +import java.io.IOException; +import java.io.Writer; + +import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; +import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; +import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter; +import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest; +import org.eclipse.linuxtools.tmf.ui.viewers.events.TmfEventsTable; + +/** + * This TMF Requests exports traces to text files. + * @author Xavier Raynaud + */ +public class ExportToTextRequest extends TmfDataRequest { + + private final Writer fWriter; + private final ITmfFilter fFilter; + private final TmfEventsTable fTable; + private IOException fIOException; + + /** + * Constructor + * @param w + * a Writer, typically a FileWriter. + * @param filter + * a TmfFilter, if we want to filter some events. May be null. + * @param table + * the {@link TmfEventsTable} requesting the export (may be null) + */ + public ExportToTextRequest(Writer w, ITmfFilter filter, TmfEventsTable table) { + super(ITmfEvent.class); + this.fWriter = w; + this.fFilter = filter; + this.fTable = table; + } + + /** + * Gets the IOException thrown by this export request, if any. + * @return the fIoException + */ + public IOException getIOException() { + return fIOException; + } + + @Override + public void handleData(final ITmfEvent event) { + super.handleData(event); + if (isCancelled()) { + return; + } + try { + if (fFilter == null || fFilter.matches(event)) { + if (fTable != null) { + ITmfEventField[] fields = fTable.getItemFields(event); + boolean needTab = false; + for (ITmfEventField field: fields) { + if (needTab) { + fWriter.write('\t'); + } + Object value = field.getValue(); + printValue(value); + needTab = true; + } + } else { // fallback to default formatting + fWriter.write(event.getTimestamp().toString()); + fWriter.write('\t'); + fWriter.write(event.getSource()); + fWriter.write('\t'); + fWriter.write(event.getType().getName()); + fWriter.write('\t'); + fWriter.write(event.getReference()); + fWriter.write('\t'); + ITmfEventField content = event.getContent(); + printValue(content.getValue()); + } + fWriter.write('\n'); + } + } catch (IOException ex) { + fIOException = ex; + fail(); + } + } + + private void printValue(Object value) throws IOException { + if (value != null) { + fWriter.write(value.toString()); + } + } + +} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/messages.properties b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/messages.properties index 34865bf936..512ef67cbe 100644 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/messages.properties +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/messages.properties @@ -63,6 +63,7 @@ TmfEventsTable_AddBookmarkDialogTitle=Add Bookmark TmfEventsTable_ApplyPresetFilterMenuName=Apply Preset Filter... TmfEventsTable_ClearFiltersActionText=Clear Filters TmfEventsTable_ContentColumnHeader=Content +TmfEventsTable_Export_to_text=Export To Text... TmfEventsTable_FilterHint= TmfEventsTable_HideRawActionText=Hide Raw TmfEventsTable_HideTableActionText=Hide Table @@ -296,3 +297,6 @@ CallStackView_EntryTimeColumn=Entry time CallStackView_ExitTimeColumn=Exit time CallStackView_DurationColumn=Duration CallStackView_StackInfoNotAvailable=Stack info not available +ExportToTextJob_Export_to=Export to {0}... +ExportToTextJob_Export_trace_to=Export trace to +ExportToTextJob_Unable_to_export_trace=Unable to export trace to {0} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/events/TmfEventsTable.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/events/TmfEventsTable.java index 93f59158dc..dffa8f99a2 100644 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/events/TmfEventsTable.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/events/TmfEventsTable.java @@ -27,6 +27,13 @@ import java.util.Map.Entry; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; +import org.eclipse.core.commands.Command; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.commands.NotEnabledException; +import org.eclipse.core.commands.NotHandledException; +import org.eclipse.core.commands.ParameterizedCommand; +import org.eclipse.core.commands.common.NotDefinedException; +import org.eclipse.core.expressions.IEvaluationContext; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IResource; @@ -66,6 +73,7 @@ import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.window.Window; import org.eclipse.linuxtools.internal.tmf.ui.Activator; import org.eclipse.linuxtools.internal.tmf.ui.Messages; +import org.eclipse.linuxtools.internal.tmf.ui.commands.ExportToTextCommandHandler; import org.eclipse.linuxtools.internal.tmf.ui.dialogs.MultiLineInputDialog; import org.eclipse.linuxtools.tmf.core.component.ITmfDataProvider; import org.eclipse.linuxtools.tmf.core.component.TmfComponent; @@ -132,7 +140,9 @@ import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.commands.ICommandService; import org.eclipse.ui.dialogs.ListDialog; +import org.eclipse.ui.handlers.IHandlerService; import org.eclipse.ui.ide.IDE; import org.eclipse.ui.ide.IGotoMarker; import org.eclipse.ui.themes.ColorUtil; @@ -162,6 +172,11 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS private static final String SEARCH_HINT = Messages.TmfEventsTable_SearchHint; private static final String FILTER_HINT = Messages.TmfEventsTable_FilterHint; private static final int MAX_CACHE_SIZE = 1000; + /** + * Empty ITmfEventField array, used by {@link #extractItemFields(ITmfEvent)} + * @since 2.2 + */ + public static final ITmfEventField[] EMPTY_FIELD_ARRAY = new TmfEventField[0]; /** * The events table search/filter keys @@ -730,6 +745,41 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS } }; + final IAction exportToTextAction = new Action(Messages.TmfEventsTable_Export_to_text) { + @Override + public void run() { + IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + IHandlerService handlerService = (IHandlerService) activePage.getActiveEditor().getSite().getService(IHandlerService.class); + ICommandService cmdService = (ICommandService) activePage.getActiveEditor().getSite().getService(ICommandService.class); + try { + HashMap parameters = new HashMap(); + StringBuilder header = new StringBuilder(); + boolean needTab = false; + for (TableColumn tc: fTable.getColumns()) { + if (needTab) { + header.append('\t'); + } + header.append(tc.getText()); + needTab = true; + } + Command command = cmdService.getCommand(ExportToTextCommandHandler.COMMAND_ID); + ParameterizedCommand cmd = ParameterizedCommand.generateCommand(command,parameters); + IEvaluationContext context = handlerService.getCurrentState(); + context.addVariable(ExportToTextCommandHandler.TMF_EVENT_TABLE_HEADER_ID, header.toString()); + context.addVariable(ExportToTextCommandHandler.TMF_EVENT_TABLE_PARAMETER_ID, TmfEventsTable.this); + handlerService.executeCommandInContext(cmd, null, context); + } catch (ExecutionException e) { + displayException(e); + } catch (NotDefinedException e) { + displayException(e); + } catch (NotEnabledException e) { + displayException(e); + } catch (NotHandledException e) { + displayException(e); + } + } + }; + final IAction showSearchBarAction = new Action(Messages.TmfEventsTable_ShowSearchBarActionText) { @Override public void run() { @@ -811,6 +861,7 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS } else if (!fRawViewer.isVisible()) { tablePopupMenu.add(showRawAction); } + tablePopupMenu.add(exportToTextAction); tablePopupMenu.add(new Separator()); if (item != null) { @@ -1794,6 +1845,18 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS fPackDone = true; } + /** + * Extract the fields of an event (item in the table). + * + * @param event + * The event to extract from + * @return The array of fields + * @since 2.2 + */ + public final ITmfEventField[] getItemFields(final ITmfEvent event) { + return extractItemFields(event); + } + /** * Extract the fields of an event (item in the table). * @@ -1804,7 +1867,7 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS * FIXME: Add support for column selection */ protected ITmfEventField[] extractItemFields(final ITmfEvent event) { - ITmfEventField[] fields = new TmfEventField[0]; + ITmfEventField[] fields = EMPTY_FIELD_ARRAY; if (event != null) { final String timestamp = event.getTimestamp().toString(); final String source = event.getSource(); -- 2.34.1