Add Copy functionality to AlignmentEditor
authorPatrick Plitzner <p.plitzner@bgbm.org>
Tue, 25 Aug 2015 13:04:32 +0000 (15:04 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Tue, 25 Aug 2015 13:04:32 +0000 (15:04 +0200)
eu.etaxonomy.taxeditor.molecular/plugin.xml
eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/AlignmentEditor.java
eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AlignmentEditorCopyHandler.java [new file with mode: 0644]

index 20e8acae07575ffcc7db6bcccae830f87b360c9b..b24ffe91d54adb472113cfeea9cfd443a3534bf9 100644 (file)
                class="eu.etaxonomy.taxeditor.molecular.handler.ChangePherogramQualityOutputType"
                commandId="eu.etaxonomy.taxeditor.molecular.pherogramComponent.changeQualityOutput">
          </handler>
+         <handler
+               class="eu.etaxonomy.taxeditor.molecular.handler.AlignmentEditorCopyHandler"
+               commandId="org.eclipse.ui.edit.copy">
+            <activeWhen>
+               <with
+                     variable="activePartId">
+                  <equals
+                        value="eu.etaxonomy.taxeditor.molecular.AlignmentEditor">
+                  </equals>
+               </with>
+            </activeWhen>
+         </handler>
       </extension>
       <extension
             point="org.eclipse.core.expressions.definitions">
index 35b0a2df6a3f18915452ea3dcd99c7995a5e3029..6704bbc9342a7e53e6494173afcd7877a813a05d 100644 (file)
@@ -52,7 +52,6 @@ import java.util.TreeMap;
 import org.biojava.bio.chromatogram.ChromatogramFactory;
 import org.biojava.bio.chromatogram.UnsupportedChromatogramFormatException;
 import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.jface.action.Action;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.ui.IActionBars;
@@ -61,7 +60,6 @@ import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.IEditorSite;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.actions.ActionFactory;
 import org.eclipse.ui.commands.ICommandService;
 import org.eclipse.ui.part.EditorPart;
 
@@ -103,7 +101,6 @@ public class AlignmentEditor extends EditorPart {
        public static final String DEFAULT_READ_NAME_PREFIX = "Read ";
        public static final String CONSENSUS_NAME = "Consensus";
 
-
     private final ConversationHolder conversationHolder;
        private final AlignmentModelChangeListener DIRTY_LISTENER = new AlignmentModelChangeListener() {
                                @Override
@@ -133,14 +130,12 @@ public class AlignmentEditor extends EditorPart {
     private final Map<Integer, SingleReadAlignment> cdmMap = new TreeMap<Integer, SingleReadAlignment>();  //TODO Move this to ContigSequenceDataProvider
     private boolean dirty = false;
 
-
     public AlignmentEditor() {
        super();
        conversationHolder = CdmStore.createConversation();
        //conversationHolder = null;
     }
 
-
     private void refreshToolbarElement(String id) {
                ICommandService commandService =
                                (ICommandService)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(ICommandService.class);
@@ -149,24 +144,23 @@ public class AlignmentEditor extends EditorPart {
                }
     }
 
-
     private void registerEditSettingListener(MultipleAlignmentsContainer container) {
-       container.getEditSettings().addListener(new EditSettingsListener() {
-                                       @Override
-                                       public void workingModeChanged(EditSettingsChangeEvent e) {}  // Currently nothing to do
-
-                                       @Override
-                                       public void insertLeftInDataAreaChanged(EditSettingsChangeEvent e) {
-                                               updateStatusBar();
-                                       refreshToolbarElement(ToggleLeftRightInsertionHandler.COMMAND_ID);
-                                       }
-
-                                       @Override
-                                       public void insertChanged(EditSettingsChangeEvent e) {
-                                               updateStatusBar();
-                                       refreshToolbarElement(ToggleInsertOverwriteHandler.COMMAND_ID);
-                                       }
-                               });
+        container.getEditSettings().addListener(new EditSettingsListener() {
+            @Override
+            public void workingModeChanged(EditSettingsChangeEvent e) {}  // Currently nothing to do
+
+            @Override
+            public void insertLeftInDataAreaChanged(EditSettingsChangeEvent e) {
+                updateStatusBar();
+                refreshToolbarElement(ToggleLeftRightInsertionHandler.COMMAND_ID);
+            }
+
+            @Override
+            public void insertChanged(EditSettingsChangeEvent e) {
+                updateStatusBar();
+                refreshToolbarElement(ToggleInsertOverwriteHandler.COMMAND_ID);
+            }
+        });
     }
 
 
@@ -191,7 +185,6 @@ public class AlignmentEditor extends EditorPart {
                return result;
        }
 
-
     private AlignmentArea createConsensusHintArea(MultipleAlignmentsContainer container,
                AlignmentArea labeledArea) {
 
@@ -202,7 +195,6 @@ public class AlignmentEditor extends EditorPart {
                return result;
          }
 
-
     private MultipleAlignmentsContainer getAlignmentsContainer() {
        if (alignmentsContainer == null) {
                alignmentsContainer = new MultipleAlignmentsContainer();
@@ -219,22 +211,18 @@ public class AlignmentEditor extends EditorPart {
                return alignmentsContainer;
        }
 
-
     public AlignmentArea getReadsArea() {
        return getAlignmentsContainer().getAlignmentAreas().get(READS_AREA_INDEX);
     }
 
-
     private AlignmentArea getEditableConsensusArea() {
        return getAlignmentsContainer().getAlignmentAreas().get(EDITABLE_CONSENSUS_AREA_INDEX);
     }
 
-
     public boolean hasPherogram(int sequenceID) {
         return getReadsArea().getDataAreas().getSequenceAreas(sequenceID).size() > PHEROGRAM_AREA_INDEX;
     }
 
-
     public PherogramArea getPherogramArea(int sequenceID) {
         if (hasPherogram(sequenceID)) {
             return (PherogramArea)getReadsArea().getDataAreas().getSequenceAreas(sequenceID).get(PHEROGRAM_AREA_INDEX);
@@ -244,14 +232,12 @@ public class AlignmentEditor extends EditorPart {
         }
     }
 
-
     private ConsensusSequenceArea getConsensusHintDataArea() {
         return (ConsensusSequenceArea)getAlignmentsContainer().getAlignmentAreas().
                 get(CONSENSUS_HINT_AREA_INDEX).getDataAreas().getBottomAreas().
                 get(CONSENSUS_DATA_AREA_INDEX);
     }
 
-
     @Deprecated  //TODO Remove as soon as testing period is over
     private void createTestContents() {
                // Just for testing:
@@ -275,7 +261,6 @@ public class AlignmentEditor extends EditorPart {
                }
     }
 
-
     private void readCDMData(Sequence sequenceNode) {
        //TODO If called from somewhere else than createPartControl() the editorInput needs to be checked and previous contents need to be cleared (or updated).
 
@@ -311,10 +296,6 @@ public class AlignmentEditor extends EditorPart {
                //TODO Can the consensus sequence also be null? / Should it be created here, if nothing is in the DB?
     }
 
-
-       /* (non-Javadoc)
-     * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
-     */
     @Override
     public void createPartControl(Composite parent) {
                SWTComponentFactory.getInstance().getSWTComponent(getAlignmentsContainer(), parent, SWT.NONE);
@@ -339,7 +320,6 @@ public class AlignmentEditor extends EditorPart {
                }
        }
 
-
     private void updateStatusBar() {
         IActionBars bars = getEditorSite().getActionBars();
         bars.getStatusLineManager().setMessage("Edit mode: " +
@@ -348,7 +328,6 @@ public class AlignmentEditor extends EditorPart {
                        (getReadsArea().getEditSettings().isInsertLeftInDataArea() ? "Left" : "Right"));
     }
 
-
     private SingleReadAlignment.Shift[] convertToCDMShifts(PherogramAreaModel model) {
        Iterator<ShiftChange> iterator = model.shiftChangeIterator();
        List<Shift> shifts = new ArrayList<SingleReadAlignment.Shift>();
@@ -359,10 +338,6 @@ public class AlignmentEditor extends EditorPart {
        return shifts.toArray(new Shift[]{});
     }
 
-
-    /* (non-Javadoc)
-     * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
-     */
     @Override
     public void doSave(IProgressMonitor monitor) {
        if (getEditorInput() instanceof AlignmentEditorInput) {
@@ -429,62 +404,30 @@ public class AlignmentEditor extends EditorPart {
        }
     }
 
-
-    /* (non-Javadoc)
-     * @see org.eclipse.ui.part.EditorPart#doSaveAs()
-     */
     @Override
     public void doSaveAs() {}
 
-
-    /* (non-Javadoc)
-     * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
-     */
     @Override
     public void init(IEditorSite site, IEditorInput input) throws PartInitException {
         setSite(site);
         setInput(input);
-        System.out.println("AlignmentEditor.init(): " + ActionFactory.COPY.getId());
-        site.getActionBars().setGlobalActionHandler(ActionFactory.COPY.getId(), new Action(ActionFactory.COPY.getId()) {
-            @Override
-            public boolean isEnabled() {
-                System.out.println("isEnabled()");
-                return true;
-            }
-
-            @Override
-            public void run() {
-                System.out.println("run");
-                    super.run();
-                }
-            });
     }
 
-
-    /* (non-Javadoc)
-     * @see org.eclipse.ui.part.EditorPart#isDirty()
-     */
     @Override
     public boolean isDirty() {
         return dirty;
     }
 
-
     private void setDirty() {
        dirty = true;
        firePropertyChange(IEditorPart.PROP_DIRTY);
     }
 
-
-    /* (non-Javadoc)
-     * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
-     */
     @Override
     public boolean isSaveAsAllowed() {
         return false;  // "Save as" not allowed.
     }
 
-
     @Override
     public void setFocus() {
         if(conversationHolder!=null){
@@ -492,7 +435,6 @@ public class AlignmentEditor extends EditorPart {
         }
     }
 
-
     public boolean isInsertMode() {
         return getAlignmentsContainer().getEditSettings().isInsert();
     }
@@ -502,17 +444,14 @@ public class AlignmentEditor extends EditorPart {
         return getAlignmentsContainer().getEditSettings().isInsertLeftInDataArea();
     }
 
-
     public void toggleLeftRightInsertionInPherogram() {
        getAlignmentsContainer().getEditSettings().toggleInsertLeftInDataArea();
     }
 
-
     public void toggleInsertOverwrite() {
        getAlignmentsContainer().getEditSettings().toggleInsert();
     }
 
-
     private String cutPherogram(boolean left) {
         SelectionModel selection = getReadsArea().getSelection();
         if (selection.getCursorHeight() != 1) {
@@ -545,17 +484,14 @@ public class AlignmentEditor extends EditorPart {
         }
     }
 
-
     public String cutPherogramLeft() {
         return cutPherogram(true);
     }
 
-
     public String cutPherogramRight() {
         return cutPherogram(false);
     }
 
-
     public void reverseComplementSelectedSequences() {
        SelectionModel selection = getReadsArea().getSelection();
        AlignmentModel<?> model = getReadsArea().getAlignmentModel();
@@ -572,7 +508,6 @@ public class AlignmentEditor extends EditorPart {
                }
     }
 
-
     /**
      * Recreates the whole consensus sequence from all single read sequences. The previous consensus
      * sequence is overwritte.
@@ -593,7 +528,6 @@ public class AlignmentEditor extends EditorPart {
         model.insertTokensAt(sequenceID, 0, tokens);
     }
 
-
     /**
      * Updates the current consensus sequence by replacing gaps by the according consensus tokens
      * calculated from the single read sequences and extends the consensus sequence if necessary.
@@ -627,7 +561,6 @@ public class AlignmentEditor extends EditorPart {
         }
     }
 
-
        public static PherogramProvider readPherogram(URI uri) throws IOException, UnsupportedChromatogramFormatException {
            PherogramProvider result;
                InputStream stream = uri.toURL().openStream();
@@ -640,7 +573,6 @@ public class AlignmentEditor extends EditorPart {
                return result;
        }
 
-
        private String newReadName() {
                int index = 1;
                while (getReadsArea().getAlignmentModel().sequenceIDByName(DEFAULT_READ_NAME_PREFIX + index)
@@ -651,12 +583,10 @@ public class AlignmentEditor extends EditorPart {
                return DEFAULT_READ_NAME_PREFIX + index;
        }
 
-
     public void addRead(URI pherogramURI, boolean reverseComplemented) throws IOException, UnsupportedChromatogramFormatException {
        addRead(newReadName(), pherogramURI, reverseComplemented, null, null, null, null, null);
     }
 
-
     /**
      * Adds a new sequence with attached phergram data area to the reads alignment.
      * <p>
diff --git a/eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AlignmentEditorCopyHandler.java b/eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AlignmentEditorCopyHandler.java
new file mode 100644 (file)
index 0000000..f3caa13
--- /dev/null
@@ -0,0 +1,34 @@
+package eu.etaxonomy.taxeditor.molecular.handler;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.swt.dnd.Clipboard;
+import org.eclipse.swt.dnd.TextTransfer;
+import org.eclipse.swt.dnd.Transfer;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.handlers.HandlerUtil;
+
+import eu.etaxonomy.taxeditor.molecular.editor.AlignmentEditor;
+
+public class AlignmentEditorCopyHandler extends AbstractHandler {
+
+    @Override
+    public Object execute(ExecutionEvent event) throws ExecutionException {
+        Clipboard clipboard = new Clipboard(Display.getCurrent());
+        TextTransfer textTransfer = TextTransfer.getInstance();
+        Transfer[] transfers = new Transfer[]{textTransfer};
+
+        String selectedSequenceString = "Blabla";
+        IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
+        if(activeEditor instanceof AlignmentEditor){
+            AlignmentEditor alignmentEditor = (AlignmentEditor)activeEditor;
+            //TODO get selected sequence string
+        }
+        Object[] data = new Object[]{selectedSequenceString};
+        clipboard.setContents(data, transfers);
+        return null;
+    }
+
+}