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