tmf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / commands / CopyToClipboardOperation.java
1 /*******************************************************************************
2 * Copyright (c) 2015 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 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.tmf.ui.commands;
14
15 import java.util.List;
16
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;
39
40 /**
41 * This operation copies the text of selected trace events to the clipboard.
42 */
43 public class CopyToClipboardOperation implements IRunnableWithProgress {
44
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;
51
52 /**
53 * Constructor.
54 *
55 * @param trace
56 * the trace to copy events from
57 * @param filter
58 * the filter to apply to trace events, or null
59 * @param columns
60 * the list of event table columns
61 * @param start
62 * the start rank of the selection
63 * @param end
64 * the end rank of the selection
65 */
66 public CopyToClipboardOperation(ITmfTrace trace, ITmfFilter filter, List<TmfEventTableColumn> columns, long start, long end) {
67 fTrace = trace;
68 fFilter = filter;
69 fColumns = columns;
70 fStartRank = start;
71 fEndRank = end;
72 }
73
74 @Override
75 public void run(IProgressMonitor monitor) {
76 final StringBuilder sb = new StringBuilder();
77 monitor.beginTask(Messages.CopyToClipboardOperation_TaskName, (int) (fEndRank - fStartRank + 1));
78
79 boolean needTab = false;
80 for (TmfEventTableColumn column : fColumns) {
81 if (needTab) {
82 sb.append('\t');
83 }
84 sb.append(column.getHeaderName());
85 needTab = true;
86 }
87 sb.append(LINE_SEPARATOR);
88
89 copy(sb, monitor);
90
91 Display.getDefault().syncExec(new Runnable() {
92 @Override
93 public void run() {
94 if (sb.length() == 0) {
95 return;
96 }
97 try {
98 Clipboard clipboard = new Clipboard(Display.getDefault());
99 clipboard.setContents(new Object[] { sb.toString() },
100 new Transfer[] { TextTransfer.getInstance() });
101 } catch (OutOfMemoryError e) {
102 sb.setLength(0);
103 sb.trimToSize();
104 showErrorDialog();
105 }
106 }
107 });
108
109 monitor.done();
110 }
111
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) {
114 @Override
115 public void handleData(ITmfEvent event) {
116 super.handleData(event);
117 if (monitor.isCanceled()) {
118 cancel();
119 return;
120 }
121 monitor.worked(1);
122 if (fFilter == null || fFilter.matches(event)) {
123 try {
124 boolean needTab = false;
125 for (TmfEventTableColumn column : fColumns) {
126 if (needTab) {
127 sb.append('\t');
128 }
129 sb.append(column.getItemString(event));
130 needTab = true;
131 }
132 sb.append(LINE_SEPARATOR);
133 } catch (OutOfMemoryError e) {
134 sb.setLength(0);
135 sb.trimToSize();
136 showErrorDialog();
137 cancel();
138 }
139 }
140 }
141 };
142 fTrace.sendRequest(request);
143 try {
144 request.waitForCompletion();
145 } catch (InterruptedException e) {
146 Activator.getDefault().logError("Wait for completion interrupted for copy to clipboard ", e); //$NON-NLS-1$
147 }
148 return Status.OK_STATUS;
149 }
150
151 private static void showErrorDialog() {
152 Display.getDefault().syncExec(new Runnable() {
153 @Override
154 public void run() {
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();
160 }
161 });
162 }
163 }
This page took 0.038749 seconds and 5 git commands to generate.