1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.commands
;
15 import java
.util
.List
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.core
.runtime
.IStatus
;
19 import org
.eclipse
.core
.runtime
.Status
;
20 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
21 import org
.eclipse
.swt
.SWT
;
22 import org
.eclipse
.swt
.dnd
.Clipboard
;
23 import org
.eclipse
.swt
.dnd
.TextTransfer
;
24 import org
.eclipse
.swt
.dnd
.Transfer
;
25 import org
.eclipse
.swt
.widgets
.Display
;
26 import org
.eclipse
.swt
.widgets
.MessageBox
;
27 import org
.eclipse
.swt
.widgets
.Shell
;
28 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
29 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Messages
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.ITmfFilter
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
.ExecutionType
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.request
.TmfEventRequest
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
37 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.events
.columns
.TmfEventTableColumn
;
38 import org
.eclipse
.ui
.PlatformUI
;
41 * This operation copies the text of selected trace events to the clipboard.
43 public class CopyToClipboardOperation
implements IRunnableWithProgress
{
45 private static final String LINE_SEPARATOR
= System
.getProperty("line.separator"); //$NON-NLS-1$
46 private final ITmfTrace fTrace
;
47 private final ITmfFilter fFilter
;
48 private final List
<TmfEventTableColumn
> fColumns
;
49 private final long fStartRank
;
50 private final long fEndRank
;
56 * the trace to copy events from
58 * the filter to apply to trace events, or null
60 * the list of event table columns
62 * the start rank of the selection
64 * the end rank of the selection
66 public CopyToClipboardOperation(ITmfTrace trace
, ITmfFilter filter
, List
<TmfEventTableColumn
> columns
, long start
, long end
) {
75 public void run(IProgressMonitor monitor
) {
76 final StringBuilder sb
= new StringBuilder();
77 monitor
.beginTask(Messages
.CopyToClipboardOperation_TaskName
, (int) (fEndRank
- fStartRank
+ 1));
79 boolean needTab
= false;
80 for (TmfEventTableColumn column
: fColumns
) {
84 sb
.append(column
.getHeaderName());
87 sb
.append(LINE_SEPARATOR
);
91 Display
.getDefault().syncExec(new Runnable() {
94 if (sb
.length() == 0) {
98 Clipboard clipboard
= new Clipboard(Display
.getDefault());
99 clipboard
.setContents(new Object
[] { sb
.toString() },
100 new Transfer
[] { TextTransfer
.getInstance() });
101 } catch (OutOfMemoryError e
) {
112 private IStatus
copy(final StringBuilder sb
, final IProgressMonitor monitor
) {
113 ITmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class, TmfTimeRange
.ETERNITY
, fStartRank
, (int) (fEndRank
- fStartRank
+ 1), ExecutionType
.FOREGROUND
) {
115 public void handleData(ITmfEvent event
) {
116 super.handleData(event
);
117 if (monitor
.isCanceled()) {
122 if (fFilter
== null || fFilter
.matches(event
)) {
124 boolean needTab
= false;
125 for (TmfEventTableColumn column
: fColumns
) {
129 sb
.append(column
.getItemString(event
));
132 sb
.append(LINE_SEPARATOR
);
133 } catch (OutOfMemoryError e
) {
142 fTrace
.sendRequest(request
);
144 request
.waitForCompletion();
145 } catch (InterruptedException e
) {
146 Activator
.getDefault().logError("Wait for completion interrupted for copy to clipboard ", e
); //$NON-NLS-1$
148 return Status
.OK_STATUS
;
151 private static void showErrorDialog() {
152 Display
.getDefault().syncExec(new Runnable() {
155 Shell shell
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell();
156 MessageBox confirmOperation
= new MessageBox(shell
, SWT
.ICON_ERROR
| SWT
.OK
);
157 confirmOperation
.setText(Messages
.CopyToClipboardOperation_OutOfMemoryErrorTitle
);
158 confirmOperation
.setMessage(Messages
.CopyToClipboardOperation_OutOfMemoryErrorMessage
);
159 confirmOperation
.open();