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
, String sequenceID
, String content
) {
34 area
.getActionProvider().deleteSelection(); // Overwrite selected tokens.
35 area
.getActionProvider().elongateSequence(sequenceID
, area
.getSelection().getCursorColumn());
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()));
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
);
53 while (scanner
.hasNext()) {
54 lines
.add(scanner
.nextLine());
56 if (lines
.get(lines
.size() - 1).equals("")) {
57 lines
.remove(lines
.size() - 1);
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));
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"},
79 //TODO Does the dialog have to be disposed in some way?
81 switch (dialog
.open()) {
82 case 0: // Paste all lines in one sequence.
83 pasteString(focusedArea
, sequenceID
, clipboardText
);
85 case 1: // Paste only first line.
86 pasteString(focusedArea
, sequenceID
, lines
.get(0));
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.
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.");
114 public boolean isEnabled() {
115 AlignmentEditor editor
= getActiveAlignmentEditor();
116 if (editor
!= null) {
117 AlignmentArea focusedArea
= editor
.getFocusedArea();
118 return (focusedArea
!= null);
127 public void updateElement(UIElement element
, @SuppressWarnings("rawtypes") Map parameters
) {
128 setBaseEnabled(isEnabled());