ref #7010 Adapt molecular handlers for multiple selection
authorPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 6 Jun 2018 15:12:43 +0000 (17:12 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 13 Jun 2018 08:05:47 +0000 (10:05 +0200)
eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/e4/handler/EditSequenceHandlerE4.java
eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/e4/handler/ExportSequenceToFileHandlerE4.java
eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/e4/handler/ShowPherogramHandlerE4.java

index 2a0de7ac651ef761aee659931202b7cfefdad8d6..fd88c41b3732d3e5ea5a40009c05f8481c16ea8a 100644 (file)
@@ -12,6 +12,7 @@ import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
 import org.eclipse.e4.ui.services.IServiceConstants;
 import org.eclipse.e4.ui.workbench.modeling.EModelService;
 import org.eclipse.e4.ui.workbench.modeling.EPartService;
+import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.TreeNode;
 import org.eclipse.ui.PartInitException;
 
@@ -35,29 +36,27 @@ public class EditSequenceHandlerE4 {
 
 
     @Execute
-    public void execute(@Optional@Named(IServiceConstants.ACTIVE_SELECTION)TreeNode treeNodeOfSelection,
+    public void execute(@Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
             EPartService partService, EModelService modelService, MApplication application) {
-        if(treeNodeOfSelection != null && treeNodeOfSelection.getValue() instanceof Sequence){
-            AlignmentEditorInput input = new AlignmentEditorInput(((Sequence)treeNodeOfSelection.getValue()).getUuid());  //TODO Should there always be a new instance created here? What if the specified CDM node is already opened in an AlignmentEditor? => Possible create Singleton that keeps instances by sequence objects in a map.
-            try {
-                String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_MOLECULAR_EDITOR_E4_ALIGNMENTEDITORE4;
-                MPart part = EditorUtil.showPart(partId, modelService, partService, application);
-                AlignmentEditorE4 alignmentEditor = (AlignmentEditorE4) part.getObject();
-                alignmentEditor.init(input);
-            }
-            catch (PartInitException e) {
-                logger.error(Messages.EditSequenceHandler_COULD_NOT_OPEN, e);
-            }
+        Sequence sequence = (Sequence) ((TreeNode) selection.getFirstElement()).getValue();
+        AlignmentEditorInput input = new AlignmentEditorInput(sequence.getUuid());  //TODO Should there always be a new instance created here? What if the specified CDM node is already opened in an AlignmentEditor? => Possible create Singleton that keeps instances by sequence objects in a map.
+        try {
+            String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_MOLECULAR_EDITOR_E4_ALIGNMENTEDITORE4;
+            MPart part = EditorUtil.showPart(partId, modelService, partService, application);
+            AlignmentEditorE4 alignmentEditor = (AlignmentEditorE4) part.getObject();
+            alignmentEditor.init(input);
+        }
+        catch (PartInitException e) {
+            logger.error(Messages.EditSequenceHandler_COULD_NOT_OPEN, e);
         }
     }
 
     @CanExecute
-    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode selectedTreeNode, MHandledMenuItem menuItem) {
+    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, MHandledMenuItem menuItem) {
         boolean canExecute = false;
-        if(selectedTreeNode!=null){
-            Object value = selectedTreeNode.getValue();
-            canExecute = value instanceof Sequence;
-        }
+        canExecute = selection.size()==1
+                && selection.getFirstElement() instanceof TreeNode
+                && ((TreeNode) selection.getFirstElement()).getValue() instanceof Sequence;
         menuItem.setVisible(canExecute);
         return canExecute;
     }
index cf007efcd8337c498893e4750fb80bf8c6c8b9aa..204bd1f7c953171e8b408c67ba57fdd115ade542 100644 (file)
@@ -21,6 +21,7 @@ import org.eclipse.e4.core.di.annotations.Execute;
 import org.eclipse.e4.core.di.annotations.Optional;
 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
 import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.TreeNode;
 import org.eclipse.jface.wizard.WizardDialog;
 import org.eclipse.swt.widgets.Shell;
@@ -55,61 +56,57 @@ public class ExportSequenceToFileHandlerE4 {
 
 
     @Execute
-    public Object execute(@Optional@Named(IServiceConstants.ACTIVE_SELECTION)TreeNode treeNodeOfSelection,
+    public void execute(@Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
             @Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
-        if (treeNodeOfSelection != null && treeNodeOfSelection.getValue() instanceof Sequence) {
-            Sequence sequence = (Sequence)treeNodeOfSelection.getValue();
-
-            final ExportSingleReadAlignmentWizard wizard = new ExportSingleReadAlignmentWizard();
-            final WizardDialog dialog = new WizardDialog(shell, wizard);
-            if (dialog.open() == IStatus.OK) {
-                // Prepare writer parameters:
-                ReadWriteParameterMap parameters = new ReadWriteParameterMap();
-                parameters.put(ReadWriteParameterNames.KEY_APPLICATION_NAME, ApplicationUtil.getTitle());
-                //parameters.put(ReadWriteParameterNames.KEY_APPLICATION_VERSION, ApplicationUtil.getVersion());  // Setting the version unnecessary, since its already contained in the title.
-                parameters.put(ReadWriteParameterNames.KEY_APPLICATION_URL, "http://cybertaxonomy.eu/taxeditor/");  //TODO Specify URL obtained from a central class? //$NON-NLS-1$
-                parameters.put(ReadWriteParameterNames.KEY_SEQUENCE_EXTENSION_TOKEN, wizard.getModel().getElongationToken());
-
-                // Create and register object translator for writing pherogram alignment shifts:
-                ObjectTranslatorFactory translatorFactory = new ObjectTranslatorFactory();
-                translatorFactory.addXSDTranslators(true);
-                translatorFactory.addTranslator(new CDMPherogramAlignmentObjectTranslator(), true, SingleReadAlignmentRDFXMLConstants.DATA_TYPE_PHERORAGM_ALIGNMENT);
-                parameters.put(ReadWriteParameterNames.KEY_OBJECT_TRANSLATOR_FACTORY, translatorFactory);
-
-                // Create writer and document adapters:
-                JPhyloIOEventWriter writer = factory.getWriter(wizard.getModel().getFormatInfo().getFormatID());
-                ListBasedDocumentDataAdapter document = new ListBasedDocumentDataAdapter();
-                document.getMatrices().add(new CDMSequenceMatrixAdapter(sequence, wizard.getModel().getConsensusSequenceLabel(),
-                        wizard.getModel().isExportConsensusSequence(), wizard.getModel().isExportSingleReads()));
-
-                // Write document:
-                File file = new File(wizard.getModel().getFileName());
-                if (!file.exists() || MessagingUtils.confirmDialog(Messages.exportSequenceToFileHandlerOverwriteTitle, String.format(
-                        Messages.exportSequenceToFileHandlerOverwriteText, file.getAbsolutePath()))) {
-
-                    try {
-                        writer.writeDocument(document, file, parameters);
-                    }
-                    catch (IOException e) {
-                        e.printStackTrace();
-                        MessagingUtils.errorDialog(Messages.exportSequenceToFileHandlerIOErrorTitle, this,
-                                String.format(Messages.exportSequenceToFileHandlerIOErrorMessage,
-                                        file.getAbsolutePath()), TaxeditorMolecularPlugin.PLUGIN_ID, e, false);
-                    }
+        Sequence sequence = (Sequence) ((TreeNode) selection.getFirstElement()).getValue();
+
+        final ExportSingleReadAlignmentWizard wizard = new ExportSingleReadAlignmentWizard();
+        final WizardDialog dialog = new WizardDialog(shell, wizard);
+        if (dialog.open() == IStatus.OK) {
+            // Prepare writer parameters:
+            ReadWriteParameterMap parameters = new ReadWriteParameterMap();
+            parameters.put(ReadWriteParameterNames.KEY_APPLICATION_NAME, ApplicationUtil.getTitle());
+            //parameters.put(ReadWriteParameterNames.KEY_APPLICATION_VERSION, ApplicationUtil.getVersion());  // Setting the version unnecessary, since its already contained in the title.
+            parameters.put(ReadWriteParameterNames.KEY_APPLICATION_URL, "http://cybertaxonomy.eu/taxeditor/");  //TODO Specify URL obtained from a central class? //$NON-NLS-1$
+            parameters.put(ReadWriteParameterNames.KEY_SEQUENCE_EXTENSION_TOKEN, wizard.getModel().getElongationToken());
+
+            // Create and register object translator for writing pherogram alignment shifts:
+            ObjectTranslatorFactory translatorFactory = new ObjectTranslatorFactory();
+            translatorFactory.addXSDTranslators(true);
+            translatorFactory.addTranslator(new CDMPherogramAlignmentObjectTranslator(), true, SingleReadAlignmentRDFXMLConstants.DATA_TYPE_PHERORAGM_ALIGNMENT);
+            parameters.put(ReadWriteParameterNames.KEY_OBJECT_TRANSLATOR_FACTORY, translatorFactory);
+
+            // Create writer and document adapters:
+            JPhyloIOEventWriter writer = factory.getWriter(wizard.getModel().getFormatInfo().getFormatID());
+            ListBasedDocumentDataAdapter document = new ListBasedDocumentDataAdapter();
+            document.getMatrices().add(new CDMSequenceMatrixAdapter(sequence, wizard.getModel().getConsensusSequenceLabel(),
+                    wizard.getModel().isExportConsensusSequence(), wizard.getModel().isExportSingleReads()));
+
+            // Write document:
+            File file = new File(wizard.getModel().getFileName());
+            if (!file.exists() || MessagingUtils.confirmDialog(Messages.exportSequenceToFileHandlerOverwriteTitle, String.format(
+                    Messages.exportSequenceToFileHandlerOverwriteText, file.getAbsolutePath()))) {
+
+                try {
+                    writer.writeDocument(document, file, parameters);
+                }
+                catch (IOException e) {
+                    e.printStackTrace();
+                    MessagingUtils.errorDialog(Messages.exportSequenceToFileHandlerIOErrorTitle, this,
+                            String.format(Messages.exportSequenceToFileHandlerIOErrorMessage,
+                                    file.getAbsolutePath()), TaxeditorMolecularPlugin.PLUGIN_ID, e, false);
                 }
-
             }
+
         }
-        return null;
     }
 
     @CanExecute
-    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode selectedTreeNode, MHandledMenuItem menuItem) {
+    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, MHandledMenuItem menuItem) {
         boolean canExecute = false;
-        if(selectedTreeNode!=null){
-            Object value = selectedTreeNode.getValue();
-            canExecute = value instanceof Sequence;
-        }
+        canExecute = selection.size()==1
+                && selection.getFirstElement() instanceof TreeNode
+                && ((TreeNode) selection.getFirstElement()).getValue() instanceof Sequence;
         menuItem.setVisible(canExecute);
         return canExecute;
     }
index acd421205c207cc01af57591b66abd9a229864a2..64da38df8decdc3ac853175b637331cba0fe7493 100644 (file)
@@ -13,6 +13,7 @@ import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
 import org.eclipse.e4.ui.services.IServiceConstants;
 import org.eclipse.e4.ui.workbench.modeling.EPartService;
 import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
+import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.TreeNode;
 
 import eu.etaxonomy.cdm.model.media.MediaUtils;
@@ -33,44 +34,40 @@ import info.bioinfweb.libralign.pherogram.model.PherogramComponentModel;
  */
 public class ShowPherogramHandlerE4 {
 
-       @Execute
-       public Object execute(@Optional@Named(IServiceConstants.ACTIVE_SELECTION)TreeNode treeNodeOfSelection,
-               EPartService partService) {
-               if (treeNodeOfSelection != null && treeNodeOfSelection.getValue() instanceof SingleRead) {
-                   //TODO Can the parent node (containing the cut positions) be extracted from SingleRead?
-                       try {
-                           SingleRead singleRead = (SingleRead)treeNodeOfSelection.getValue();
-                           URI uri = null;
-                           if (singleRead.getPherogram() != null) {  // Pherogram objects without URI are possible.
-                               uri = MediaUtils.getFirstMediaRepresentationPart(singleRead.getPherogram()).getUri();
-                           }
+    @Execute
+    public void execute(@Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
+            EPartService partService) {
+        //TODO Can the parent node (containing the cut positions) be extracted from SingleRead?
+        try {
+            SingleRead singleRead = (SingleRead) ((TreeNode) selection.getFirstElement()).getValue();
+            URI uri = null;
+            if (singleRead.getPherogram() != null) {  // Pherogram objects without URI are possible.
+                uri = MediaUtils.getFirstMediaRepresentationPart(singleRead.getPherogram()).getUri();
+            }
 
-                           if (uri == null) {
-                       MessagingUtils.messageDialog(Messages.ShowPherogramHandler_NO_PHEROGRAM, this,
-                               Messages.ShowPherogramHandler_NO_PHEROGRAM_MESSAGE);
-                           }
-                           else {
-                               MPart part = partService.createPart(eu.etaxonomy.taxeditor.molecular.AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_MOLECULAR_EDITOR_E4_PHEROGRAMPARTE4);
-                               part = partService.showPart(part, PartState.ACTIVATE);
-                               PherogramPartE4 pherogramPart = (PherogramPartE4) part.getObject();
-                               pherogramPart.init(new PherogramComponentModel(AlignmentEditorE4.readPherogram(uri)));
-                           }
-                       }
-               catch (Exception e) {
-                   MessagingUtils.errorDialog(Messages.ShowPherogramHandler_ERROR, null, e.getLocalizedMessage(), TaxeditorMolecularPlugin.PLUGIN_ID,
-                           e, false);
-               }
+            if (uri == null) {
+                MessagingUtils.messageDialog(Messages.ShowPherogramHandler_NO_PHEROGRAM, this,
+                        Messages.ShowPherogramHandler_NO_PHEROGRAM_MESSAGE);
+            }
+            else {
+                MPart part = partService.createPart(eu.etaxonomy.taxeditor.molecular.AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_MOLECULAR_EDITOR_E4_PHEROGRAMPARTE4);
+                part = partService.showPart(part, PartState.ACTIVATE);
+                PherogramPartE4 pherogramPart = (PherogramPartE4) part.getObject();
+                pherogramPart.init(new PherogramComponentModel(AlignmentEditorE4.readPherogram(uri)));
+            }
+        }
+        catch (Exception e) {
+            MessagingUtils.errorDialog(Messages.ShowPherogramHandler_ERROR, null, e.getLocalizedMessage(), TaxeditorMolecularPlugin.PLUGIN_ID,
+                    e, false);
         }
-        return null;
     }
 
     @CanExecute
-    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode selectedTreeNode, MHandledMenuItem menuItem) {
+    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, MHandledMenuItem menuItem) {
         boolean canExecute = false;
-        if(selectedTreeNode!=null){
-            Object value = selectedTreeNode.getValue();
-            canExecute = value instanceof SingleRead;
-        }
+        canExecute = selection.size()==1
+                && selection.getFirstElement() instanceof TreeNode
+                && ((TreeNode) selection.getFirstElement()).getValue() instanceof SingleRead;
         menuItem.setVisible(canExecute);
         return canExecute;
     }