1 package eu
.etaxonomy
.taxeditor
.molecular
.handler
;
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
;
10 import java
.util
.ArrayList
;
11 import java
.util
.List
;
13 import java
.util
.Scanner
;
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
;
22 import eu
.etaxonomy
.taxeditor
.molecular
.editor
.AlignmentEditor
;
27 * Handler that pastes the current contents of the clipboard into an active instance of {@link AlignmentEditor}.
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());
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()));
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
);
52 while (scanner
.hasNext()) {
53 lines
.add(scanner
.nextLine());
55 if (lines
.get(lines
.size() - 1).equals("")) {
56 lines
.remove(lines
.size() - 1);
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));
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"},
78 //TODO Does the dialog have to be disposed in some way?
80 switch (dialog
.open()) {
81 case 0: // Paste all lines in one sequence.
82 pasteString(focusedArea
, sequenceID
, clipboardText
);
84 case 1: // Paste only first line.
85 pasteString(focusedArea
, sequenceID
, lines
.get(0));
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.
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.");
113 public boolean isEnabled() {
114 AlignmentEditor editor
= getActiveAlignmentEditor();
115 if (editor
!= null) {
116 AlignmentArea focusedArea
= editor
.getFocusedArea();
117 return (focusedArea
!= null);
126 public void updateElement(UIElement element
, @SuppressWarnings("rawtypes") Map parameters
) {
127 setBaseEnabled(isEnabled());