From 53bae90232306b95b79de78708df8652e6d09418 Mon Sep 17 00:00:00 2001 From: Bernd Hufmann Date: Wed, 27 Aug 2014 14:50:12 -0400 Subject: [PATCH] tmf: Adapted ExportToText feature to new events table column API Change-Id: I11d9e500fb19c368233047c153205b2a9fba807d Signed-off-by: Bernd Hufmann Reviewed-on: https://git.eclipse.org/r/32496 Tested-by: Hudson CI Reviewed-by: Alexandre Montplaisir Tested-by: Alexandre Montplaisir --- .../commands/ExportToTextCommandHandler.java | 38 +++++++------------ .../tmf/ui/commands/ExportToTextJob.java | 31 ++++++++------- .../tmf/ui/commands/ExportToTextRequest.java | 34 ++++++++--------- .../tmf/ui/viewers/events/TmfEventsTable.java | 12 +----- 4 files changed, 48 insertions(+), 67 deletions(-) diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/commands/ExportToTextCommandHandler.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/commands/ExportToTextCommandHandler.java index a63d81900b..f8f4a82300 100644 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/commands/ExportToTextCommandHandler.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/commands/ExportToTextCommandHandler.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Kalray + * Copyright (c) 2013, 2014 Kalray, Ericsson * * All rights reserved. This program and the accompanying materials are * made available under the terms of the Eclipse Public License v1.0 which @@ -8,10 +8,13 @@ * * Contributors: * Xavier Raynaud - Initial API and implementation + * Bernd Hufmann - Adapted to new events table column API *******************************************************************************/ package org.eclipse.linuxtools.internal.tmf.ui.commands; +import java.util.List; + import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; @@ -20,7 +23,7 @@ import org.eclipse.core.runtime.jobs.Job; import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter; import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager; -import org.eclipse.linuxtools.tmf.ui.viewers.events.TmfEventsTable; +import org.eclipse.linuxtools.tmf.ui.viewers.events.columns.TmfEventTableColumn; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.ui.PlatformUI; @@ -33,16 +36,11 @@ 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$ + public static final String TMF_EVENT_TABLE_COLUMNS_ID = "org.eclipse.linuxtools.tmf.ui.exportToText.columns"; //$NON-NLS-1$ /** * Constructor @@ -52,8 +50,7 @@ public class ExportToTextCommandHandler extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { - String header = getHeader(event.getApplicationContext()); - TmfEventsTable table = getTable(event.getApplicationContext()); + List columns = getColumns(event.getApplicationContext()); ITmfTrace trace = TmfTraceManager.getInstance().getActiveTrace(); ITmfFilter filter = TmfTraceManager.getInstance().getCurrentFilter(); if (trace != null) { @@ -62,7 +59,7 @@ public class ExportToTextCommandHandler extends AbstractHandler { fd.setOverwrite(true); final String s = fd.open(); if (s != null) { - Job j = new ExportToTextJob(trace, filter, table, header, s); + Job j = new ExportToTextJob(trace, filter, columns, s); j.setUser(true); j.schedule(); } @@ -70,21 +67,12 @@ public class ExportToTextCommandHandler extends AbstractHandler { 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) { + @SuppressWarnings("unchecked") + private static List getColumns(Object evaluationContext) { if (evaluationContext instanceof IEvaluationContext) { - Object o = ((IEvaluationContext) evaluationContext).getVariable(TMF_EVENT_TABLE_PARAMETER_ID); - if (o instanceof TmfEventsTable) { - return (TmfEventsTable) o; + Object s = ((IEvaluationContext) evaluationContext).getVariable(TMF_EVENT_TABLE_COLUMNS_ID); + if (s instanceof List) { + return (List) s; } } 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 index 6a4979d3a4..4ffef85d23 100644 --- 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Kalray + * Copyright (c) 2013, 2014 Kalray, Ericsson * * All rights reserved. This program and the accompanying materials are * made available under the terms of the Eclipse Public License v1.0 which @@ -8,6 +8,7 @@ * * Contributors: * Xavier Raynaud - Initial API and implementation + * Bernd Hufmann - Adapted to new events table column API *******************************************************************************/ package org.eclipse.linuxtools.internal.tmf.ui.commands; @@ -17,6 +18,7 @@ import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.text.MessageFormat; +import java.util.List; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; @@ -26,8 +28,8 @@ 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; import org.eclipse.osgi.util.NLS; +import org.eclipse.linuxtools.tmf.ui.viewers.events.columns.TmfEventTableColumn; /** * This job exports traces to text files. @@ -43,8 +45,7 @@ public class ExportToTextJob extends Job { private final ITmfTrace fTrace; private final ITmfFilter fFilter; - private final TmfEventsTable fTable; - private final String fHeader; + private final List fColumns; private final String destination; /** @@ -54,19 +55,16 @@ public class ExportToTextJob extends Job { * 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 + * @param columns * 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) { + public ExportToTextJob(ITmfTrace trace, ITmfFilter filter, List columns, String destination) { super(MessageFormat.format(Messages.ExportToTextJob_Export_to, destination)); this.fTrace = trace; this.fFilter = filter; - this.fTable = table; - this.fHeader = header; + this.fColumns = columns; this.destination = destination; } @@ -80,8 +78,15 @@ public class ExportToTextJob extends Job { private IStatus saveImpl(IProgressMonitor monitor) { try (final BufferedWriter bw = new BufferedWriter(new FileWriter(destination));) { - if (fHeader != null) { - bw.write(fHeader); + if (fColumns != null) { + boolean needTab = false; + for (TmfEventTableColumn column : fColumns) { + if (needTab) { + bw.write('\t'); + } + bw.write(column.getHeaderName()); + needTab = true; + } bw.append('\n'); } return saveImpl(bw, monitor); @@ -94,7 +99,7 @@ public class ExportToTextJob extends Job { } private IStatus saveImpl(Writer bw, IProgressMonitor monitor) { - ExportToTextRequest request = new ExportToTextRequest(bw, fFilter, fTable); + ExportToTextRequest request = new ExportToTextRequest(bw, fFilter, fColumns); fTrace.sendRequest(request); int currentIndex = 0; while (!request.isCompleted()) { 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 index b95450e8b0..950d9739a9 100644 --- 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Kalray + * Copyright (c) 2013, 2014 Kalray, Ericsson * * All rights reserved. This program and the accompanying materials are * made available under the terms of the Eclipse Public License v1.0 which @@ -8,12 +8,14 @@ * * Contributors: * Xavier Raynaud - Initial API and implementation + * Bernd Hufmann - Adapted to new events table column API *******************************************************************************/ package org.eclipse.linuxtools.internal.tmf.ui.commands; import java.io.IOException; import java.io.Writer; +import java.util.List; import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; @@ -21,7 +23,7 @@ import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter; import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest; import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; -import org.eclipse.linuxtools.tmf.ui.viewers.events.TmfEventsTable; +import org.eclipse.linuxtools.tmf.ui.viewers.events.columns.TmfEventTableColumn; /** * This TMF Requests exports traces to text files. @@ -31,7 +33,7 @@ public class ExportToTextRequest extends TmfEventRequest { private final Writer fWriter; private final ITmfFilter fFilter; - private final TmfEventsTable fTable; + private final List fColumns; private IOException fIOException; /** @@ -40,14 +42,14 @@ public class ExportToTextRequest extends TmfEventRequest { * 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) + * @param columns + * the {@link TmfEventTableColumn} requesting the export (may be null) */ - public ExportToTextRequest(Writer w, ITmfFilter filter, TmfEventsTable table) { + public ExportToTextRequest(Writer w, ITmfFilter filter, List columns) { super(ITmfEvent.class, TmfTimeRange.ETERNITY, 0, ITmfEventRequest.ALL_DATA, ExecutionType.FOREGROUND); this.fWriter = w; this.fFilter = filter; - this.fTable = table; + this.fColumns = columns; } /** @@ -66,14 +68,13 @@ public class ExportToTextRequest extends TmfEventRequest { } try { if (fFilter == null || fFilter.matches(event)) { - if (fTable != null) { - String[] entries = fTable.getItemStrings(event); + if (fColumns != null) { boolean needTab = false; - for (String entry : entries) { + for (TmfEventTableColumn column : fColumns) { if (needTab) { fWriter.write('\t'); } - printValue(entry); + fWriter.write(column.getItemString(event)); needTab = true; } } else { // fallback to default formatting @@ -86,7 +87,10 @@ public class ExportToTextRequest extends TmfEventRequest { fWriter.write(event.getReference()); fWriter.write('\t'); ITmfEventField content = event.getContent(); - printValue(content.getValue()); + Object value = content.getValue(); + if (value != null) { + fWriter.write(value.toString()); + } } fWriter.write('\n'); } @@ -96,10 +100,4 @@ public class ExportToTextRequest extends TmfEventRequest { } } - 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/tmf/ui/viewers/events/TmfEventsTable.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/events/TmfEventsTable.java index bbb40033c0..8a950c16e6 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 @@ -818,20 +818,10 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS 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); + context.addVariable(ExportToTextCommandHandler.TMF_EVENT_TABLE_COLUMNS_ID, fColumns); handlerService.executeCommandInContext(cmd, null, context); } catch (ExecutionException e) { displayException(e); -- 2.34.1