AlignmentEditorCopyHandler now sets current action status.
authorb.stoever <b.stoever@localhost>
Tue, 25 Aug 2015 18:13:05 +0000 (18:13 +0000)
committerb.stoever <b.stoever@localhost>
Tue, 25 Aug 2015 18:13:05 +0000 (18:13 +0000)
.gitattributes
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/editor/AlignmentEditorActionUpdater.java [new file with mode: 0644]
eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AbstractAlignmentEditorHandler.java
eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AlignmentEditorCopyHandler.java

index e6bb9df..52155c0 100644 (file)
@@ -933,6 +933,7 @@ eu.etaxonomy.taxeditor.help/src/eu/etaxonomy/taxeditor/help/Activator.java -text
 eu.etaxonomy.taxeditor.help/toc.xml -text
 eu.etaxonomy.taxeditor.help/tocgettingstarted.xml -text
 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.navigation/.classpath -text
 eu.etaxonomy.taxeditor.navigation/.project -text
 eu.etaxonomy.taxeditor.navigation/META-INF/MANIFEST.MF -text
index b97df81..9ed9208 100644 (file)
@@ -128,6 +128,7 @@ public class AlignmentEditor extends EditorPart {
                                        setDirty();
                                }
                        };
+       private final AlignmentEditorActionUpdater ACTION_UPDATER = new AlignmentEditorActionUpdater();
                        
 
     private MultipleAlignmentsContainer alignmentsContainer = null;
@@ -207,14 +208,17 @@ public class AlignmentEditor extends EditorPart {
     private MultipleAlignmentsContainer getAlignmentsContainer() {
        if (alignmentsContainer == null) {
                alignmentsContainer = new MultipleAlignmentsContainer();
-
+               
                AlignmentAreaList list = alignmentsContainer.getAlignmentAreas();
                AlignmentArea readsArea = createEditableAlignmentArea(alignmentsContainer, true);
+               readsArea.getSelection().addSelectionListener(ACTION_UPDATER);
            list.add(createIndexArea(alignmentsContainer, readsArea));
                list.add(readsArea);  // Make sure READS_AREA_INDEX is correct.
-               list.add(createEditableAlignmentArea(alignmentsContainer, false));  // Make sure COMSENSUS_AREA_INDEX is correct.
+               AlignmentArea editableConsensusArea = createEditableAlignmentArea(alignmentsContainer, false);
+               editableConsensusArea.getSelection().addSelectionListener(ACTION_UPDATER);
+               list.add(editableConsensusArea);  // Make sure COMSENSUS_AREA_INDEX is correct.
                list.add(createConsensusHintArea(alignmentsContainer, readsArea));
-
+               
                registerEditSettingListener(alignmentsContainer);
                }
                return alignmentsContainer;
@@ -264,13 +268,21 @@ public class AlignmentEditor extends EditorPart {
      * @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();
+    private boolean hasFocus(AlignmentArea area) {
+       return childHasFocus((Composite)area.getToolkitComponent());
+    }
+    
+    
+    public static boolean childHasFocus(Composite parent) {  //TODO Move to bioinfweb.commons.swt
+       return isChildComponent(parent, Display.getCurrent().getFocusControl());
+    }
+    
+    
+    public static boolean isChildComponent(Composite parent, Control child) {  //TODO Move to bioinfweb.commons.swt
+        while ((child != parent) && (child != null)) {
+            child = child.getParent();
         }
-        return (control == areaComponent); 
+        return (child == parent); 
     }
     
 
@@ -359,6 +371,8 @@ public class AlignmentEditor extends EditorPart {
     @Override
     public void createPartControl(Composite parent) {
                SWTComponentFactory.getInstance().getSWTComponent(getAlignmentsContainer(), parent, SWT.NONE);
+               Display.getCurrent().addFilter(SWT.FocusIn, ACTION_UPDATER);
+               Display.getCurrent().addFilter(SWT.FocusOut, ACTION_UPDATER);
                updateStatusBar();
 
                if (getEditorInput() instanceof AlignmentEditorInput) {
@@ -381,7 +395,15 @@ public class AlignmentEditor extends EditorPart {
        }
 
     
-    private void updateStatusBar() {
+    @Override
+       public void dispose() {
+               Display.getCurrent().removeFilter(SWT.FocusIn, ACTION_UPDATER);
+               Display.getCurrent().removeFilter(SWT.FocusOut, ACTION_UPDATER);
+               super.dispose();
+       }
+
+
+       private void updateStatusBar() {
         IActionBars bars = getEditorSite().getActionBars();
         bars.getStatusLineManager().setMessage("Edit mode: " +
                        (getReadsArea().getEditSettings().isInsert() ? "Insert" : "Overwrite") + "  " +
diff --git a/eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/AlignmentEditorActionUpdater.java b/eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/AlignmentEditorActionUpdater.java
new file mode 100644 (file)
index 0000000..280e40c
--- /dev/null
@@ -0,0 +1,52 @@
+package eu.etaxonomy.taxeditor.molecular.editor;\r
+\r
+\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Event;\r
+import org.eclipse.swt.widgets.Listener;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.eclipse.ui.actions.ActionFactory;\r
+import org.eclipse.ui.commands.ICommandService;\r
+\r
+import eu.etaxonomy.taxeditor.molecular.handler.AbstractAlignmentEditorHandler;\r
+import info.bioinfweb.libralign.alignmentarea.AlignmentArea;\r
+import info.bioinfweb.libralign.alignmentarea.selection.SelectionChangeEvent;\r
+import info.bioinfweb.libralign.alignmentarea.selection.SelectionListener;\r
+\r
+\r
+\r
+/**\r
+ * Listener used to update copy/paste events associated with {@link AlignmentEditor}.\r
+ * \r
+ * @author Ben Stöver\r
+ * @date 25.08.2015\r
+ */\r
+public class AlignmentEditorActionUpdater implements SelectionListener, Listener {\r
+       private void updateEvents() {\r
+        ((ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class)).refreshElements(\r
+                ActionFactory.COPY.getCommandId(), null);\r
+       }\r
+       \r
+       \r
+       @Override\r
+       public void handleEvent(Event event) {\r
+               AlignmentEditor editor = AbstractAlignmentEditorHandler.getActiveAlignmentEditor();\r
+               if (editor != null) {\r
+            updateEvents();\r
+               }\r
+       }\r
+\r
+       \r
+       @Override\r
+       public void selectionChanged(SelectionChangeEvent e) {\r
+               AlignmentEditor editor = AbstractAlignmentEditorHandler.getActiveAlignmentEditor();\r
+               if (editor != null) {\r
+                       if ((e.getSource() == editor.getReadsArea().getSelection()) || \r
+                                       (e.getSource() == editor.getEditableConsensusArea().getSelection())) {\r
+                               \r
+                               updateEvents();\r
+                       }\r
+               }\r
+       }\r
+}\r
index 0c8a6ed..1d44449 100644 (file)
@@ -29,15 +29,26 @@ import eu.etaxonomy.taxeditor.molecular.editor.AlignmentEditor;
  * @date 19.06.2015
  */
 public abstract class AbstractAlignmentEditorHandler extends AbstractHandler {
-    @Override
-    public Object execute(ExecutionEvent event) throws ExecutionException {
+       public static AlignmentEditor getActiveAlignmentEditor() {
         IEditorPart activeEditor = AbstractUtility.getActiveEditor();
         if (activeEditor instanceof AlignmentEditor) {
-            doExecute(event, (AlignmentEditor)activeEditor);
+            return (AlignmentEditor)activeEditor;
+        }
+        else {
+               return null;
+        }
+       }
+       
+       
+    @Override
+    public Object execute(ExecutionEvent event) throws ExecutionException {
+       AlignmentEditor editor = getActiveAlignmentEditor();
+        if (editor != null) {
+            doExecute(event, editor);
         }
         return null;
     }
 
 
-    public abstract void doExecute(ExecutionEvent event, AlignmentEditor editor) throws ExecutionException;
+    protected abstract void doExecute(ExecutionEvent event, AlignmentEditor editor) throws ExecutionException;
 }
index 6eb5645..feb6564 100644 (file)
@@ -1,20 +1,21 @@
 package eu.etaxonomy.taxeditor.molecular.handler;
 
 
+import java.util.Map;
+
 import info.bioinfweb.libralign.alignmentarea.AlignmentArea;
 import info.bioinfweb.libralign.alignmentarea.selection.SelectionModel;
 import info.bioinfweb.libralign.model.AlignmentModel;
 import info.bioinfweb.libralign.model.tokenset.AbstractTokenSet;
 
-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 org.eclipse.ui.commands.IElementUpdater;
+import org.eclipse.ui.menus.UIElement;
 
 import eu.etaxonomy.taxeditor.molecular.editor.AlignmentEditor;
 
@@ -33,20 +34,7 @@ import eu.etaxonomy.taxeditor.molecular.editor.AlignmentEditor;
  * @author Ben Stöver
  * @date 25.08.2015
  */
-public class AlignmentEditorCopyHandler extends AbstractHandler {
-    @Override
-    public Object execute(ExecutionEvent event) throws ExecutionException {
-        IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
-        if(activeEditor instanceof AlignmentEditor){
-            AlignmentArea focusedArea = ((AlignmentEditor)activeEditor).getFocusedArea();
-            if (focusedArea != null) {
-               copySelectionAsString(focusedArea);
-            }
-        }
-        return null;
-    }
-    
-    
+public class AlignmentEditorCopyHandler extends AbstractAlignmentEditorHandler implements IElementUpdater {
        @SuppressWarnings("unchecked")
     public static void copySelectionAsString(AlignmentArea area) {
        SelectionModel selection = area.getSelection();
@@ -71,4 +59,32 @@ public class AlignmentEditorCopyHandler extends AbstractHandler {
                        new Transfer[]{TextTransfer.getInstance()});
        }
     }
+       
+       
+    @Override
+       public void doExecute(ExecutionEvent event, AlignmentEditor editor) throws ExecutionException {
+        AlignmentArea focusedArea = editor.getFocusedArea();
+        if (focusedArea != null) {
+               copySelectionAsString(focusedArea);
+        }
+       }
+
+
+       @Override
+       public boolean isEnabled() {
+               AlignmentEditor editor = getActiveAlignmentEditor();
+               if (editor != null) {
+                       AlignmentArea focusedArea = editor.getFocusedArea();
+                       if (focusedArea != null) {
+                               return !focusedArea.getSelection().isEmpty();
+                       }
+               }
+               return false;
+       }
+
+
+       @Override
+       public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {
+               setBaseEnabled(isEnabled());
+    }
 }