Moved functionality to be shared from AlignmentEditorCopyHandler to AlignmentEditor.
authorb.stoever <b.stoever@localhost>
Tue, 25 Aug 2015 15:28:42 +0000 (15:28 +0000)
committerb.stoever <b.stoever@localhost>
Tue, 25 Aug 2015 15:28:42 +0000 (15:28 +0000)
AlignmentEditorCopyHandler refactored.

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

index 3a0c06ebe7915ebf2e4adefd9330eae3c4dc40a8..b97df81c6e24e0788f370b0fadb929b531ba6872 100644 (file)
@@ -54,6 +54,8 @@ import org.biojava.bio.chromatogram.UnsupportedChromatogramFormatException;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.IActionBars;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorPart;
@@ -227,8 +229,51 @@ public class AlignmentEditor extends EditorPart {
     public AlignmentArea getEditableConsensusArea() {
        return getAlignmentsContainer().getAlignmentAreas().get(EDITABLE_CONSENSUS_AREA_INDEX);
     }
-
     
+    
+    /**
+     * Checks whether {@link #getReadsArea()} or {@link #getEditableConsensusArea()} currently
+     * have the user focus and returns the according component. 
+     * 
+     * @return either the reads or the consensus alignment area or {@code null} if none of these
+     *         components is currently focused
+     */
+    public AlignmentArea getFocusedArea() {
+       AlignmentArea result = getReadsArea();
+       if (hasFocus(result)) {
+               return result;
+       }
+       else {
+               result = getEditableConsensusArea();
+               if (hasFocus(result)) {
+                       return result;
+               }
+               else {
+                       return null;
+               }
+       }
+    }
+    
+    
+    /**
+     * Checks whether the specified alignment area or one of its subcomponents currently has the
+     * focus.
+     * 
+     * @param area the alignment area to be checked (Can only be {@link #getReadsArea()} or 
+     *        {@link #getEditableConsensusArea()}.)
+     * @return {@code true} if the specified component is focused and is either equal to
+     *         {@link #getReadsArea()} or {@link #getEditableConsensusArea()}or {@code false} otherwise
+     */
+    public boolean hasFocus(AlignmentArea area) {
+       Composite areaComponent = (Composite)area.getToolkitComponent();
+        Control control = Display.getCurrent().getFocusControl();
+        while ((control != areaComponent) && (control != null)) {
+            control = control.getParent();
+        }
+        return (control == areaComponent); 
+    }
+    
+
     public boolean hasPherogram(int sequenceID) {
         return getReadsArea().getDataAreas().getSequenceAreas(sequenceID).size() > PHEROGRAM_AREA_INDEX;
     }
@@ -454,7 +499,7 @@ public class AlignmentEditor extends EditorPart {
     
     @Override
     public void setFocus() {
-        if(conversationHolder!=null){
+        if(conversationHolder != null){
             conversationHolder.bind();
         }
     }
index 92ed0feb64ad2436e6680e6693c8fe383c42c7be..f2eae019e9669ede84aa4847cc963669fad1269c 100644 (file)
@@ -12,8 +12,6 @@ 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.Composite;
-import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.handlers.HandlerUtil;
@@ -37,21 +35,18 @@ import eu.etaxonomy.taxeditor.molecular.editor.AlignmentEditor;
  */
 public class AlignmentEditorCopyHandler extends AbstractHandler {
     @Override
+       @SuppressWarnings("unchecked")
     public Object execute(ExecutionEvent event) throws ExecutionException {
-        Clipboard clipboard = new Clipboard(Display.getCurrent());
-        TextTransfer textTransfer = TextTransfer.getInstance();
-        Transfer[] transfers = new Transfer[]{textTransfer};
-
         IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
         if(activeEditor instanceof AlignmentEditor){
             AlignmentEditor alignmentEditor = (AlignmentEditor)activeEditor;
             
             StringBuilder selectedCharacters = new StringBuilder();
-            AlignmentArea focusedArea = getFocusedArea(alignmentEditor);
+            AlignmentArea focusedArea = alignmentEditor.getFocusedArea();
             if (focusedArea != null) {
                SelectionModel selection = focusedArea.getSelection();
                if (!selection.isEmpty()) {
-                       AlignmentModel<Character> alignmentModel = (AlignmentModel<Character>)focusedArea.getAlignmentModel();
+                                       AlignmentModel<Character> alignmentModel = (AlignmentModel<Character>)focusedArea.getAlignmentModel();
                        for (int row = selection.getFirstRow(); row <= selection.getLastRow(); row++) {
                        int id = focusedArea.getSequenceOrder().idByIndex(row);
                        for (int column = selection.getFirstColumn(); column <= selection.getLastColumn(); column++) {
@@ -68,36 +63,9 @@ public class AlignmentEditorCopyHandler extends AbstractHandler {
                                        }
                }
             }
-            Object[] data = new Object[]{selectedCharacters.toString()};
-            clipboard.setContents(data, transfers);
+            new Clipboard(Display.getCurrent()).setContents(new Object[]{selectedCharacters.toString()}, 
+                       new Transfer[]{TextTransfer.getInstance()});
         }
         return null;
     }
-    
-    
-    private AlignmentArea getFocusedArea(AlignmentEditor editor) {
-       AlignmentArea result = editor.getReadsArea();
-       if (hasFocus(result)) {
-               return result;
-       }
-       else {
-               result = editor.getEditableConsensusArea();
-               if (hasFocus(result)) {
-                       return result;
-               }
-               else {
-                       return null;
-               }
-       }
-    }
-    
-    
-    private boolean hasFocus(AlignmentArea area) {
-       Composite areaComponent = (Composite)area.getToolkitComponent();
-        Control control = Display.getCurrent().getFocusControl();
-        while ((control != areaComponent) && (control != null)) {
-            control = control.getParent();
-        }
-        return (control == areaComponent); 
-    }
 }