1
|
package eu.etaxonomy.taxeditor.molecular.handler;
|
2
|
|
3
|
|
4
|
import info.bioinfweb.libralign.alignmentarea.AlignmentArea;
|
5
|
import info.bioinfweb.libralign.alignmentarea.order.SequenceOrder;
|
6
|
import info.bioinfweb.libralign.alignmentarea.selection.SelectionModel;
|
7
|
import info.bioinfweb.libralign.model.AlignmentModel;
|
8
|
import info.bioinfweb.libralign.model.utils.AlignmentModelUtils;
|
9
|
|
10
|
import java.util.ArrayList;
|
11
|
import java.util.List;
|
12
|
import java.util.Map;
|
13
|
import java.util.Scanner;
|
14
|
|
15
|
import org.eclipse.core.commands.ExecutionEvent;
|
16
|
import org.eclipse.jface.dialogs.MessageDialog;
|
17
|
import org.eclipse.swt.dnd.TextTransfer;
|
18
|
import org.eclipse.ui.commands.IElementUpdater;
|
19
|
import org.eclipse.ui.handlers.HandlerUtil;
|
20
|
import org.eclipse.ui.menus.UIElement;
|
21
|
|
22
|
import eu.etaxonomy.taxeditor.molecular.editor.AlignmentEditor;
|
23
|
|
24
|
|
25
|
|
26
|
/**
|
27
|
* Handler that pastes the current contents of the clipboard into an active instance of {@link AlignmentEditor}.
|
28
|
*
|
29
|
* @author Ben Stöver
|
30
|
* @date 26.08.2015
|
31
|
*/
|
32
|
public class AlignmentEditorPasteHandler extends AbstractFocusedAlignmentAreaHandler implements IElementUpdater {
|
33
|
private void pasteString(AlignmentArea area, String sequenceID, String content) {
|
34
|
area.getActionProvider().deleteSelection(); // Overwrite selected tokens.
|
35
|
area.getActionProvider().elongateSequence(sequenceID, area.getSelection().getCursorColumn());
|
36
|
|
37
|
@SuppressWarnings("unchecked")
|
38
|
AlignmentModel<Object> alignmentModel = (AlignmentModel<Object>)area.getAlignmentModel();
|
39
|
alignmentModel.insertTokensAt(sequenceID, area.getSelection().getCursorColumn(),
|
40
|
AlignmentModelUtils.charSequenceToTokenList(content, alignmentModel.getTokenSet(),
|
41
|
true, alignmentModel.getTokenSet().getGapToken()));
|
42
|
}
|
43
|
|
44
|
|
45
|
@Override
|
46
|
protected void doExecute2(ExecutionEvent event, AlignmentEditor editor, AlignmentArea focusedArea) {
|
47
|
SelectionModel selection = focusedArea.getSelection();
|
48
|
String clipboardText = (String)editor.CLIPBOARD.getContents(TextTransfer.getInstance());
|
49
|
if (clipboardText != null) {
|
50
|
List<String> lines = new ArrayList<String>();
|
51
|
Scanner scanner = new Scanner(clipboardText);
|
52
|
try {
|
53
|
while (scanner.hasNext()) {
|
54
|
lines.add(scanner.nextLine());
|
55
|
}
|
56
|
if (lines.get(lines.size() - 1).equals("")) {
|
57
|
lines.remove(lines.size() - 1);
|
58
|
}
|
59
|
}
|
60
|
finally {
|
61
|
scanner.close();
|
62
|
}
|
63
|
|
64
|
if (!lines.isEmpty()) { //TODO Can lines be empty? (Can an empty string "" be copied to the clipboard?)
|
65
|
if (selection.getCursorHeight() == 1) { // If the consensus sequence is focused, this is the only possible case.
|
66
|
String sequenceID = focusedArea.getSequenceOrder().idByIndex(selection.getCursorRow());
|
67
|
if (lines.size() == 1) {
|
68
|
pasteString(focusedArea, sequenceID, lines.get(0));
|
69
|
}
|
70
|
else {
|
71
|
MessageDialog dialog = new MessageDialog(HandlerUtil.getActiveWorkbenchWindow(event).getShell(), //TODO Can the window be null?
|
72
|
"Pasting multiple lines", null,
|
73
|
"The text to be pasted contains mutlitple lines (" + lines.size() +
|
74
|
") although the current cursor height is one. What do you want to do?",
|
75
|
MessageDialog.QUESTION,
|
76
|
new String[]{"Ingnore line breaks and paste as one sequence",
|
77
|
"Only paste the first line from the clipboard", "Cancel"},
|
78
|
0);
|
79
|
//TODO Does the dialog have to be disposed in some way?
|
80
|
|
81
|
switch (dialog.open()) {
|
82
|
case 0: // Paste all lines in one sequence.
|
83
|
pasteString(focusedArea, sequenceID, clipboardText);
|
84
|
break;
|
85
|
case 1: // Paste only first line.
|
86
|
pasteString(focusedArea, sequenceID, lines.get(0));
|
87
|
break;
|
88
|
}
|
89
|
}
|
90
|
}
|
91
|
else {
|
92
|
if (selection.getCursorHeight() == lines.size()) {
|
93
|
SequenceOrder order = focusedArea.getSequenceOrder();
|
94
|
for (int i = 0; i < selection.getCursorHeight(); i++) {
|
95
|
pasteString(focusedArea, order.idByIndex(selection.getCursorRow() + i), lines.get(i)); // Multiple calls of deleteSelection() in here are unnecessary, but should have no effect.
|
96
|
}
|
97
|
}
|
98
|
else {
|
99
|
MessageDialog.openError(HandlerUtil.getActiveWorkbenchWindow(event).getShell(), //TODO Can the window be null?
|
100
|
"Unable to paste multiple lines",
|
101
|
"The current cursor height (" + selection.getCursorHeight() +
|
102
|
") does not match the number of lines to be pasted (" + lines.size() + ")." +
|
103
|
System.getProperty("line.separator") + System.getProperty("line.separator") +
|
104
|
"You can either change the cursor height accordingly or set the cursor height to one "
|
105
|
+ "allowing you to paste all lines from the clipboad into one sequence.");
|
106
|
}
|
107
|
}
|
108
|
}
|
109
|
}
|
110
|
}
|
111
|
|
112
|
|
113
|
@Override
|
114
|
public boolean isEnabled() {
|
115
|
AlignmentEditor editor = getActiveAlignmentEditor();
|
116
|
if (editor != null) {
|
117
|
AlignmentArea focusedArea = editor.getFocusedArea();
|
118
|
return (focusedArea != null);
|
119
|
}
|
120
|
else {
|
121
|
return false;
|
122
|
}
|
123
|
}
|
124
|
|
125
|
|
126
|
@Override
|
127
|
public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {
|
128
|
setBaseEnabled(isEnabled());
|
129
|
}
|
130
|
}
|