-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathTasksCopyHandler.java
52 lines (37 loc) · 1.79 KB
/
TasksCopyHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.vogella.tasks.ui.handlers;
import java.util.List;
import java.util.stream.Collectors;
import jakarta.inject.Named;
import org.eclipse.e4.core.di.annotations.CanExecute;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.jface.dialogs.PlainMessageDialog;
import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.RTFTransfer;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import com.vogella.tasks.model.Task;
public class TasksCopyHandler {
@Execute
public void execute(Shell shell, @Optional @Named(IServiceConstants.ACTIVE_SELECTION) List<Task> tasks) {
Clipboard clipboard = new Clipboard(shell.getDisplay());
String transferString = tasks.stream().map(task -> task.toString()).collect(Collectors.joining(", "));
TextTransfer textTransfer = TextTransfer.getInstance();
RTFTransfer rtfTransfer = RTFTransfer.getInstance();
LocalSelectionTransfer localSelectionTransfer = LocalSelectionTransfer.getTransfer();
Object[] transferData = new Object[] { transferString, transferString, tasks };
Transfer[] transfers = new Transfer[] { textTransfer, rtfTransfer, localSelectionTransfer };
clipboard.setContents(transferData, transfers);
clipboard.dispose();
PlainMessageDialog.getBuilder(shell, "Copied to clipboard").message("Selected task copied to clipboard. Paste in any editor")
.buttonLabels(List.of("Close")).build().open();
}
@CanExecute
public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) List<Task> tasks) {
return tasks != null && !tasks.isEmpty();
}
}