Plugin molecular.lib refactored to use direct project dependencies. This should not...
[taxeditor.git] / eu.etaxonomy.taxeditor.molecular / src / main / java / eu / etaxonomy / taxeditor / molecular / handler / AlignmentEditorPasteHandler.java
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, int sequenceID, String content) {
34 area.getActionProvider().deleteSelection(); // Overwrite selected tokens.
35 area.getActionProvider().elongateSequence(sequenceID, area.getSelection().getCursorColumn());
36
37 AlignmentModel<Object> alignmentModel = (AlignmentModel<Object>)area.getAlignmentModel();
38 alignmentModel.insertTokensAt(sequenceID, area.getSelection().getCursorColumn(),
39 AlignmentModelUtils.charSequenceToTokenList(content, alignmentModel.getTokenSet(),
40 true, alignmentModel.getTokenSet().getGapToken()));
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 }