ref #6902 migrate alignment editor and pherogram view
[taxeditor.git] / eu.etaxonomy.taxeditor.molecular / src / main / java / eu / etaxonomy / taxeditor / molecular / editor / e4 / handler / AlignmentEditorPasteHandlerE4.java
diff --git a/eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/e4/handler/AlignmentEditorPasteHandlerE4.java b/eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/e4/handler/AlignmentEditorPasteHandlerE4.java
new file mode 100644 (file)
index 0000000..da49e7f
--- /dev/null
@@ -0,0 +1,124 @@
+package eu.etaxonomy.taxeditor.molecular.editor.e4.handler;
+
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Scanner;
+
+import javax.inject.Named;
+
+import org.eclipse.e4.core.di.annotations.CanExecute;
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
+import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.swt.dnd.TextTransfer;
+import org.eclipse.swt.widgets.Shell;
+
+import eu.etaxonomy.taxeditor.molecular.editor.AlignmentEditor;
+import eu.etaxonomy.taxeditor.molecular.editor.e4.AlignmentEditorE4;
+import eu.etaxonomy.taxeditor.molecular.l10n.Messages;
+import info.bioinfweb.libralign.alignmentarea.AlignmentArea;
+import info.bioinfweb.libralign.alignmentarea.order.SequenceOrder;
+import info.bioinfweb.libralign.alignmentarea.selection.SelectionModel;
+import info.bioinfweb.libralign.model.AlignmentModel;
+import info.bioinfweb.libralign.model.utils.AlignmentModelUtils;
+
+
+
+/**
+ * Handler that pastes the current contents of the clipboard into an active instance of {@link AlignmentEditor}.
+ *
+ * @author Ben Stöver
+ * @date 26.08.2015
+ */
+public class AlignmentEditorPasteHandlerE4 {
+
+       private void pasteString(AlignmentArea area, String sequenceID, String content) {
+               area.getActionProvider().deleteSelection();  // Overwrite selected tokens.
+               area.getActionProvider().elongateSequence(sequenceID, area.getSelection().getCursorColumn());
+
+               @SuppressWarnings("unchecked")
+               AlignmentModel<Object> alignmentModel = (AlignmentModel<Object>)area.getAlignmentModel();
+               alignmentModel.insertTokensAt(sequenceID, area.getSelection().getCursorColumn(),
+                               AlignmentModelUtils.charSequenceToTokenList(content, alignmentModel.getTokenSet(),
+                                               true, alignmentModel.getTokenSet().getGapToken()));
+       }
+
+
+       @Execute
+       protected void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
+               @Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
+        AlignmentEditorE4 editor = (AlignmentEditorE4) activePart.getObject();
+        AlignmentArea focusedArea = editor.getFocusedArea();
+
+               SelectionModel selection = focusedArea.getSelection();
+               String clipboardText = (String)editor.CLIPBOARD.getContents(TextTransfer.getInstance());
+               if (clipboardText != null) {
+                       List<String> lines = new ArrayList<String>();
+                       Scanner scanner = new Scanner(clipboardText);
+                       try {
+                               while (scanner.hasNext()) {
+                                       lines.add(scanner.nextLine());
+                               }
+                               if (lines.get(lines.size() - 1).equals("")) { //$NON-NLS-1$
+                                       lines.remove(lines.size() - 1);
+                               }
+                       }
+                       finally {
+                               scanner.close();
+                       }
+
+                       if (!lines.isEmpty()) { //TODO Can lines be empty? (Can an empty string "" be copied to the clipboard?)
+                               if (selection.getCursorHeight() == 1) {  // If the consensus sequence is focused, this is the only possible case.
+                                       String sequenceID = focusedArea.getSequenceOrder().idByIndex(selection.getCursorRow());
+                                       if (lines.size() == 1) {
+                                               pasteString(focusedArea, sequenceID, lines.get(0));
+                                       }
+                                       else {
+                                               MessageDialog dialog = new MessageDialog(shell,
+                                                               Messages.AlignmentEditorPasteHandler_PASTING_LINES, null,
+                                                               String.format(Messages.AlignmentEditorPasteHandler_PASTING_LINES_QUESTION, lines.size()),
+                                                               MessageDialog.QUESTION,
+                                                               new String[]{Messages.AlignmentEditorPasteHandler_PASTING_LINES_IGNORE,
+                                                                               Messages.AlignmentEditorPasteHandler_PASTING_LINES_FIRST_LINE, Messages.AlignmentEditorPasteHandler_CANCEL},
+                                                               0);
+                                               //TODO Does the dialog have to be disposed in some way?
+
+                                               switch (dialog.open()) {
+                                                       case 0:  // Paste all lines in one sequence.
+                                                               pasteString(focusedArea, sequenceID, clipboardText);
+                                                               break;
+                                                       case 1:  // Paste only first line.
+                                                               pasteString(focusedArea, sequenceID, lines.get(0));
+                                                               break;
+                                               }
+                                       }
+                               }
+                               else {
+                                       if (selection.getCursorHeight() == lines.size()) {
+                                               SequenceOrder order = focusedArea.getSequenceOrder();
+                                               for (int i = 0; i < selection.getCursorHeight(); i++) {
+                                                       pasteString(focusedArea, order.idByIndex(selection.getCursorRow() + i), lines.get(i));  // Multiple calls of deleteSelection() in here are unnecessary, but should have no effect.
+                                               }
+                                       }
+                                       else {
+                                               MessageDialog.openError(shell,
+                                                               Messages.AlignmentEditorPasteHandler_PASTE_FAILURE,
+                                                               String.format(Messages.AlignmentEditorPasteHandler_PASTE_FAILURE_MESSAGE, selection.getCursorHeight(), lines.size(), System.getProperty("line.separator"))); //$NON-NLS-1$
+                                       }
+                               }
+                       }
+               }
+       }
+
+
+       @CanExecute
+       public boolean isEnabled(@Named(IServiceConstants.ACTIVE_PART)MPart activePart) {
+        AlignmentEditorE4 editor = (AlignmentEditorE4) activePart.getObject();
+        AlignmentArea focusedArea = editor.getFocusedArea();
+        focusedArea = editor.getFocusedArea();
+        return (focusedArea != null);
+       }
+
+}