From 479060ec15f3b1ce7ec21fa57f25529645b18c7e Mon Sep 17 00:00:00 2001 From: "b.stoever" Date: Tue, 25 Aug 2015 18:13:05 +0000 Subject: [PATCH] AlignmentEditorCopyHandler now sets current action status. --- .gitattributes | 1 + .../molecular/editor/AlignmentEditor.java | 42 +++++++++++---- .../editor/AlignmentEditorActionUpdater.java | 52 +++++++++++++++++++ .../AbstractAlignmentEditorHandler.java | 19 +++++-- .../handler/AlignmentEditorCopyHandler.java | 50 ++++++++++++------ 5 files changed, 133 insertions(+), 31 deletions(-) create mode 100644 eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/AlignmentEditorActionUpdater.java diff --git a/.gitattributes b/.gitattributes index e6bb9df13..52155c083 100644 --- a/.gitattributes +++ b/.gitattributes @@ -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 diff --git a/eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/AlignmentEditor.java b/eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/AlignmentEditor.java index b97df81c6..9ed9208f0 100644 --- a/eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/AlignmentEditor.java +++ b/eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/AlignmentEditor.java @@ -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 index 000000000..280e40cb3 --- /dev/null +++ b/eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/AlignmentEditorActionUpdater.java @@ -0,0 +1,52 @@ +package eu.etaxonomy.taxeditor.molecular.editor; + + +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Listener; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.actions.ActionFactory; +import org.eclipse.ui.commands.ICommandService; + +import eu.etaxonomy.taxeditor.molecular.handler.AbstractAlignmentEditorHandler; +import info.bioinfweb.libralign.alignmentarea.AlignmentArea; +import info.bioinfweb.libralign.alignmentarea.selection.SelectionChangeEvent; +import info.bioinfweb.libralign.alignmentarea.selection.SelectionListener; + + + +/** + * Listener used to update copy/paste events associated with {@link AlignmentEditor}. + * + * @author Ben Stöver + * @date 25.08.2015 + */ +public class AlignmentEditorActionUpdater implements SelectionListener, Listener { + private void updateEvents() { + ((ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class)).refreshElements( + ActionFactory.COPY.getCommandId(), null); + } + + + @Override + public void handleEvent(Event event) { + AlignmentEditor editor = AbstractAlignmentEditorHandler.getActiveAlignmentEditor(); + if (editor != null) { + updateEvents(); + } + } + + + @Override + public void selectionChanged(SelectionChangeEvent e) { + AlignmentEditor editor = AbstractAlignmentEditorHandler.getActiveAlignmentEditor(); + if (editor != null) { + if ((e.getSource() == editor.getReadsArea().getSelection()) || + (e.getSource() == editor.getEditableConsensusArea().getSelection())) { + + updateEvents(); + } + } + } +} diff --git a/eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AbstractAlignmentEditorHandler.java b/eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AbstractAlignmentEditorHandler.java index 0c8a6edb1..1d44449b9 100644 --- a/eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AbstractAlignmentEditorHandler.java +++ b/eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AbstractAlignmentEditorHandler.java @@ -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; } 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 index 6eb5645c8..feb656475 100644 --- 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 @@ -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()); + } } -- 2.34.1