Paste action for AlignmentEditor added.
authorb.stoever <b.stoever@localhost>
Wed, 2 Sep 2015 09:29:44 +0000 (09:29 +0000)
committerb.stoever <b.stoever@localhost>
Wed, 2 Sep 2015 09:29:44 +0000 (09:29 +0000)
.gitattributes
eu.etaxonomy.taxeditor.molecular/plugin.xml
eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/AlignmentEditorActionUpdater.java
eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AlignmentEditorPasteHandler.java [new file with mode: 0644]

index b160a1419f366b46bbeaf5503847a678a3de0d40..a9c346ed72e6baa6bcf6a91c670ee5023306f2bd 100644 (file)
@@ -936,6 +936,7 @@ eu.etaxonomy.taxeditor.help/tocnameparser.xml -text
 eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/AlignmentEditorActionUpdater.java -text
 eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AbstractFocusedAlignmentAreaHandler.java -text
 eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AlignmentEditorCutHandler.java -text
+eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AlignmentEditorPasteHandler.java -text
 eu.etaxonomy.taxeditor.navigation/.classpath -text
 eu.etaxonomy.taxeditor.navigation/.project -text
 eu.etaxonomy.taxeditor.navigation/META-INF/MANIFEST.MF -text
index 503d8e39d3e3baf7dd573c10ceaf7308416993aa..73690842934e25cdeea9f2377ef3960817ce6701 100644 (file)
                </with>
             </activeWhen>
          </handler>
+         <handler
+               class="eu.etaxonomy.taxeditor.molecular.handler.AlignmentEditorPasteHandler"
+               commandId="org.eclipse.ui.edit.paste">
+            <activeWhen>
+               <with
+                     variable="activePartId">
+                  <equals
+                        value="eu.etaxonomy.taxeditor.molecular.AlignmentEditor">
+                  </equals>
+               </with>
+            </activeWhen>
+         </handler>
       </extension>
       <extension
             point="org.eclipse.core.expressions.definitions">
index b1f8355818fa28ea8121a6cfe9c53f7c945b6cc1..7993a08c4a52422149214f8835968145b76ac4d0 100644 (file)
@@ -20,10 +20,14 @@ import info.bioinfweb.libralign.alignmentarea.selection.SelectionListener;
  * @date 25.08.2015\r
  */\r
 public class AlignmentEditorActionUpdater implements SelectionListener, Listener {\r
-       private void updateEvents(String[] ids) {\r
+       private static final String[] IDS = {ActionFactory.COPY.getCommandId(), ActionFactory.CUT.getCommandId(), \r
+               ActionFactory.PASTE.getCommandId()};\r
+       \r
+       \r
+       private void updateEvents() {\r
                ICommandService service = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);\r
-               for (int i = 0; i < ids.length; i++) {\r
-               service.refreshElements(ids[i], null);\r
+               for (int i = 0; i < IDS.length; i++) {\r
+               service.refreshElements(IDS[i], null);\r
                }\r
        }\r
        \r
@@ -32,8 +36,7 @@ public class AlignmentEditorActionUpdater implements SelectionListener, Listener
        public void handleEvent(Event event) {\r
                AlignmentEditor editor = AbstractAlignmentEditorHandler.getActiveAlignmentEditor();\r
                if (editor != null) {\r
-            updateEvents(new String[]{ActionFactory.COPY.getCommandId(), ActionFactory.CUT.getCommandId(), \r
-                       ActionFactory.PASTE.getCommandId()});\r
+            updateEvents();\r
                }\r
        }\r
 \r
@@ -45,7 +48,7 @@ public class AlignmentEditorActionUpdater implements SelectionListener, Listener
                        if ((e.getSource() == editor.getReadsArea().getSelection()) || \r
                                        (e.getSource() == editor.getEditableConsensusArea().getSelection())) {\r
                                \r
-                               updateEvents(new String[]{ActionFactory.COPY.getCommandId(),  ActionFactory.CUT.getCommandId()});\r
+                               updateEvents();\r
                        }\r
                }\r
        }\r
diff --git a/eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AlignmentEditorPasteHandler.java b/eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AlignmentEditorPasteHandler.java
new file mode 100644 (file)
index 0000000..4607b2f
--- /dev/null
@@ -0,0 +1,129 @@
+package eu.etaxonomy.taxeditor.molecular.handler;\r
+\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Scanner;\r
+\r
+import info.bioinfweb.libralign.alignmentarea.AlignmentArea;\r
+import info.bioinfweb.libralign.alignmentarea.order.SequenceOrder;\r
+import info.bioinfweb.libralign.alignmentarea.selection.SelectionModel;\r
+import info.bioinfweb.libralign.model.AlignmentModel;\r
+import info.bioinfweb.libralign.model.AlignmentModelUtils;\r
+\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.jface.dialogs.MessageDialog;\r
+import org.eclipse.swt.dnd.TextTransfer;\r
+import org.eclipse.ui.commands.IElementUpdater;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.eclipse.ui.menus.UIElement;\r
+\r
+import eu.etaxonomy.taxeditor.molecular.editor.AlignmentEditor;\r
+\r
+\r
+\r
+/**\r
+ * Handler that pastes the current contents of the clipboard into an active instance of {@link AlignmentEditor}.\r
+ * \r
+ * @author Ben Stöver\r
+ * @date 26.08.2015\r
+ */\r
+public class AlignmentEditorPasteHandler extends AbstractFocusedAlignmentAreaHandler implements IElementUpdater {\r
+       private void pasteString(AlignmentArea area, int sequenceID, String content) {\r
+               AlignmentModel<Object> alignmentModel = (AlignmentModel<Object>)area.getAlignmentModel();\r
+               List<Object> tokens = AlignmentModelUtils.charSequenceToTokenList(content, alignmentModel.getTokenSet(),\r
+                               true, alignmentModel.getTokenSet().getGapToken()); \r
+               \r
+               area.getActionProvider().deleteSelection();  // Overwrite selected tokens.\r
+               area.getActionProvider().elongateSequence(sequenceID, area.getSelection().getCursorColumn());\r
+               alignmentModel.insertTokensAt(sequenceID, area.getSelection().getCursorColumn(), tokens);\r
+       }\r
+       \r
+       \r
+       @Override\r
+       protected void doExecute2(ExecutionEvent event, AlignmentEditor editor, AlignmentArea focusedArea) {\r
+               SelectionModel selection = focusedArea.getSelection();\r
+               String clipboardText = (String)editor.CLIPBOARD.getContents(TextTransfer.getInstance());\r
+               if (clipboardText != null) {\r
+                       List<String> lines = new ArrayList<String>();\r
+                       Scanner scanner = new Scanner(clipboardText);\r
+                       try {\r
+                               while (scanner.hasNext()) {\r
+                                       lines.add(scanner.nextLine());\r
+                               }\r
+                               if (lines.get(lines.size() - 1).equals("")) {\r
+                                       lines.remove(lines.size() - 1);\r
+                               }\r
+                       }\r
+                       finally {\r
+                               scanner.close();\r
+                       }\r
+                       \r
+                       if (!lines.isEmpty()) { //TODO Can lines be empty? (Can an empty string "" be copied to the clipboard?)\r
+                               if (selection.getCursorHeight() == 1) {  // If the consensus sequence is focused, this is the only possible case.\r
+                                       int sequenceID = focusedArea.getSequenceOrder().idByIndex(selection.getCursorRow());\r
+                                       if (lines.size() == 1) {\r
+                                               pasteString(focusedArea, sequenceID, lines.get(0));\r
+                                       }\r
+                                       else {\r
+                                               MessageDialog dialog = new MessageDialog(HandlerUtil.getActiveWorkbenchWindow(event).getShell(),  //TODO Can the window be null? \r
+                                                               "Pasting multiple lines", null,\r
+                                                               "The text to be pasted contains mutlitple lines (" + lines.size() + \r
+                                                               ") although the current cursor height is one. What do you want to do?", \r
+                                                               MessageDialog.QUESTION, \r
+                                                               new String[]{"Ingnore line breaks and paste as one sequence", \r
+                                                                               "Only paste the first line from the clipboard", "Cancel"}, \r
+                                                               0);\r
+                                               //TODO Does the dialog have to be disposed in some way?\r
+                                               \r
+                                               switch (dialog.open()) {\r
+                                                       case 0:  // Paste all lines in one sequence.\r
+                                                               pasteString(focusedArea, sequenceID, clipboardText);\r
+                                                               break;\r
+                                                       case 1:  // Paste only first line.\r
+                                                               pasteString(focusedArea, sequenceID, lines.get(0));\r
+                                                               break;\r
+                                               }\r
+                                       }\r
+                               }\r
+                               else {\r
+                                       if (selection.getCursorHeight() == lines.size()) {\r
+                                               SequenceOrder order = focusedArea.getSequenceOrder();\r
+                                               for (int i = 0; i < selection.getCursorHeight(); i++) {\r
+                                                       pasteString(focusedArea, order.idByIndex(selection.getCursorRow() + i), lines.get(i));  // Multiple calls of deleteSelection() in here are unnecessary, but should have no effect.\r
+                                               }\r
+                                       }\r
+                                       else {\r
+                                               MessageDialog.openError(HandlerUtil.getActiveWorkbenchWindow(event).getShell(),  //TODO Can the window be null?\r
+                                                               "Unable to paste multiple lines", \r
+                                                               "The current cursor height (" + selection.getCursorHeight() + \r
+                                                               ") does not match the number of lines to be pasted (" + lines.size() + ")." + \r
+                                                               System.getProperty("line.separator") + System.getProperty("line.separator") + \r
+                                                               "You can either change the cursor height accordingly or set the cursor height to one "\r
+                                                               + "allowing you to paste all lines from the clipboad into one sequence.");\r
+                                       }\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+\r
+\r
+       @Override\r
+       public boolean isEnabled() {\r
+               AlignmentEditor editor = getActiveAlignmentEditor();\r
+               if (editor != null) {\r
+                       AlignmentArea focusedArea = editor.getFocusedArea();\r
+                       return (focusedArea != null);\r
+               }\r
+               else {\r
+                       return false;\r
+               }\r
+       }\r
+\r
+\r
+       @Override\r
+       public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {\r
+               setBaseEnabled(isEnabled());\r
+    }\r
+}\r