Project

General

Profile

« Previous | Next » 

Revision dcf7fd09

Added by Patrick Plitzner almost 6 years ago

ref #7010 Adapt molecular handlers for multiple selection

View differences:

eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/e4/handler/EditSequenceHandlerE4.java
12 12
import org.eclipse.e4.ui.services.IServiceConstants;
13 13
import org.eclipse.e4.ui.workbench.modeling.EModelService;
14 14
import org.eclipse.e4.ui.workbench.modeling.EPartService;
15
import org.eclipse.jface.viewers.IStructuredSelection;
15 16
import org.eclipse.jface.viewers.TreeNode;
16 17
import org.eclipse.ui.PartInitException;
17 18

  
......
35 36

  
36 37

  
37 38
    @Execute
38
    public void execute(@Optional@Named(IServiceConstants.ACTIVE_SELECTION)TreeNode treeNodeOfSelection,
39
    public void execute(@Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
39 40
            EPartService partService, EModelService modelService, MApplication application) {
40
        if(treeNodeOfSelection != null && treeNodeOfSelection.getValue() instanceof Sequence){
41
            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.
42
            try {
43
                String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_MOLECULAR_EDITOR_E4_ALIGNMENTEDITORE4;
44
                MPart part = EditorUtil.showPart(partId, modelService, partService, application);
45
                AlignmentEditorE4 alignmentEditor = (AlignmentEditorE4) part.getObject();
46
                alignmentEditor.init(input);
47
            }
48
            catch (PartInitException e) {
49
                logger.error(Messages.EditSequenceHandler_COULD_NOT_OPEN, e);
50
            }
41
        Sequence sequence = (Sequence) ((TreeNode) selection.getFirstElement()).getValue();
42
        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.
43
        try {
44
            String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_MOLECULAR_EDITOR_E4_ALIGNMENTEDITORE4;
45
            MPart part = EditorUtil.showPart(partId, modelService, partService, application);
46
            AlignmentEditorE4 alignmentEditor = (AlignmentEditorE4) part.getObject();
47
            alignmentEditor.init(input);
48
        }
49
        catch (PartInitException e) {
50
            logger.error(Messages.EditSequenceHandler_COULD_NOT_OPEN, e);
51 51
        }
52 52
    }
53 53

  
54 54
    @CanExecute
55
    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode selectedTreeNode, MHandledMenuItem menuItem) {
55
    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, MHandledMenuItem menuItem) {
56 56
        boolean canExecute = false;
57
        if(selectedTreeNode!=null){
58
            Object value = selectedTreeNode.getValue();
59
            canExecute = value instanceof Sequence;
60
        }
57
        canExecute = selection.size()==1
58
                && selection.getFirstElement() instanceof TreeNode
59
                && ((TreeNode) selection.getFirstElement()).getValue() instanceof Sequence;
61 60
        menuItem.setVisible(canExecute);
62 61
        return canExecute;
63 62
    }
eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/e4/handler/ExportSequenceToFileHandlerE4.java
21 21
import org.eclipse.e4.core.di.annotations.Optional;
22 22
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
23 23
import org.eclipse.e4.ui.services.IServiceConstants;
24
import org.eclipse.jface.viewers.IStructuredSelection;
24 25
import org.eclipse.jface.viewers.TreeNode;
25 26
import org.eclipse.jface.wizard.WizardDialog;
26 27
import org.eclipse.swt.widgets.Shell;
......
55 56

  
56 57

  
57 58
    @Execute
58
    public Object execute(@Optional@Named(IServiceConstants.ACTIVE_SELECTION)TreeNode treeNodeOfSelection,
59
    public void execute(@Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
59 60
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
60
        if (treeNodeOfSelection != null && treeNodeOfSelection.getValue() instanceof Sequence) {
61
            Sequence sequence = (Sequence)treeNodeOfSelection.getValue();
62

  
63
            final ExportSingleReadAlignmentWizard wizard = new ExportSingleReadAlignmentWizard();
64
            final WizardDialog dialog = new WizardDialog(shell, wizard);
65
            if (dialog.open() == IStatus.OK) {
66
                // Prepare writer parameters:
67
                ReadWriteParameterMap parameters = new ReadWriteParameterMap();
68
                parameters.put(ReadWriteParameterNames.KEY_APPLICATION_NAME, ApplicationUtil.getTitle());
69
                //parameters.put(ReadWriteParameterNames.KEY_APPLICATION_VERSION, ApplicationUtil.getVersion());  // Setting the version unnecessary, since its already contained in the title.
70
                parameters.put(ReadWriteParameterNames.KEY_APPLICATION_URL, "http://cybertaxonomy.eu/taxeditor/");  //TODO Specify URL obtained from a central class? //$NON-NLS-1$
71
                parameters.put(ReadWriteParameterNames.KEY_SEQUENCE_EXTENSION_TOKEN, wizard.getModel().getElongationToken());
72

  
73
                // Create and register object translator for writing pherogram alignment shifts:
74
                ObjectTranslatorFactory translatorFactory = new ObjectTranslatorFactory();
75
                translatorFactory.addXSDTranslators(true);
76
                translatorFactory.addTranslator(new CDMPherogramAlignmentObjectTranslator(), true, SingleReadAlignmentRDFXMLConstants.DATA_TYPE_PHERORAGM_ALIGNMENT);
77
                parameters.put(ReadWriteParameterNames.KEY_OBJECT_TRANSLATOR_FACTORY, translatorFactory);
78

  
79
                // Create writer and document adapters:
80
                JPhyloIOEventWriter writer = factory.getWriter(wizard.getModel().getFormatInfo().getFormatID());
81
                ListBasedDocumentDataAdapter document = new ListBasedDocumentDataAdapter();
82
                document.getMatrices().add(new CDMSequenceMatrixAdapter(sequence, wizard.getModel().getConsensusSequenceLabel(),
83
                        wizard.getModel().isExportConsensusSequence(), wizard.getModel().isExportSingleReads()));
84

  
85
                // Write document:
86
                File file = new File(wizard.getModel().getFileName());
87
                if (!file.exists() || MessagingUtils.confirmDialog(Messages.exportSequenceToFileHandlerOverwriteTitle, String.format(
88
                        Messages.exportSequenceToFileHandlerOverwriteText, file.getAbsolutePath()))) {
89

  
90
                    try {
91
                        writer.writeDocument(document, file, parameters);
92
                    }
93
                    catch (IOException e) {
94
                        e.printStackTrace();
95
                        MessagingUtils.errorDialog(Messages.exportSequenceToFileHandlerIOErrorTitle, this,
96
                                String.format(Messages.exportSequenceToFileHandlerIOErrorMessage,
97
                                        file.getAbsolutePath()), TaxeditorMolecularPlugin.PLUGIN_ID, e, false);
98
                    }
61
        Sequence sequence = (Sequence) ((TreeNode) selection.getFirstElement()).getValue();
62

  
63
        final ExportSingleReadAlignmentWizard wizard = new ExportSingleReadAlignmentWizard();
64
        final WizardDialog dialog = new WizardDialog(shell, wizard);
65
        if (dialog.open() == IStatus.OK) {
66
            // Prepare writer parameters:
67
            ReadWriteParameterMap parameters = new ReadWriteParameterMap();
68
            parameters.put(ReadWriteParameterNames.KEY_APPLICATION_NAME, ApplicationUtil.getTitle());
69
            //parameters.put(ReadWriteParameterNames.KEY_APPLICATION_VERSION, ApplicationUtil.getVersion());  // Setting the version unnecessary, since its already contained in the title.
70
            parameters.put(ReadWriteParameterNames.KEY_APPLICATION_URL, "http://cybertaxonomy.eu/taxeditor/");  //TODO Specify URL obtained from a central class? //$NON-NLS-1$
71
            parameters.put(ReadWriteParameterNames.KEY_SEQUENCE_EXTENSION_TOKEN, wizard.getModel().getElongationToken());
72

  
73
            // Create and register object translator for writing pherogram alignment shifts:
74
            ObjectTranslatorFactory translatorFactory = new ObjectTranslatorFactory();
75
            translatorFactory.addXSDTranslators(true);
76
            translatorFactory.addTranslator(new CDMPherogramAlignmentObjectTranslator(), true, SingleReadAlignmentRDFXMLConstants.DATA_TYPE_PHERORAGM_ALIGNMENT);
77
            parameters.put(ReadWriteParameterNames.KEY_OBJECT_TRANSLATOR_FACTORY, translatorFactory);
78

  
79
            // Create writer and document adapters:
80
            JPhyloIOEventWriter writer = factory.getWriter(wizard.getModel().getFormatInfo().getFormatID());
81
            ListBasedDocumentDataAdapter document = new ListBasedDocumentDataAdapter();
82
            document.getMatrices().add(new CDMSequenceMatrixAdapter(sequence, wizard.getModel().getConsensusSequenceLabel(),
83
                    wizard.getModel().isExportConsensusSequence(), wizard.getModel().isExportSingleReads()));
84

  
85
            // Write document:
86
            File file = new File(wizard.getModel().getFileName());
87
            if (!file.exists() || MessagingUtils.confirmDialog(Messages.exportSequenceToFileHandlerOverwriteTitle, String.format(
88
                    Messages.exportSequenceToFileHandlerOverwriteText, file.getAbsolutePath()))) {
89

  
90
                try {
91
                    writer.writeDocument(document, file, parameters);
92
                }
93
                catch (IOException e) {
94
                    e.printStackTrace();
95
                    MessagingUtils.errorDialog(Messages.exportSequenceToFileHandlerIOErrorTitle, this,
96
                            String.format(Messages.exportSequenceToFileHandlerIOErrorMessage,
97
                                    file.getAbsolutePath()), TaxeditorMolecularPlugin.PLUGIN_ID, e, false);
99 98
                }
100

  
101 99
            }
100

  
102 101
        }
103
        return null;
104 102
    }
105 103

  
106 104
    @CanExecute
107
    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode selectedTreeNode, MHandledMenuItem menuItem) {
105
    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, MHandledMenuItem menuItem) {
108 106
        boolean canExecute = false;
109
        if(selectedTreeNode!=null){
110
            Object value = selectedTreeNode.getValue();
111
            canExecute = value instanceof Sequence;
112
        }
107
        canExecute = selection.size()==1
108
                && selection.getFirstElement() instanceof TreeNode
109
                && ((TreeNode) selection.getFirstElement()).getValue() instanceof Sequence;
113 110
        menuItem.setVisible(canExecute);
114 111
        return canExecute;
115 112
    }
eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/e4/handler/ShowPherogramHandlerE4.java
13 13
import org.eclipse.e4.ui.services.IServiceConstants;
14 14
import org.eclipse.e4.ui.workbench.modeling.EPartService;
15 15
import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
16
import org.eclipse.jface.viewers.IStructuredSelection;
16 17
import org.eclipse.jface.viewers.TreeNode;
17 18

  
18 19
import eu.etaxonomy.cdm.model.media.MediaUtils;
......
33 34
 */
34 35
public class ShowPherogramHandlerE4 {
35 36

  
36
	@Execute
37
	public Object execute(@Optional@Named(IServiceConstants.ACTIVE_SELECTION)TreeNode treeNodeOfSelection,
38
	        EPartService partService) {
39
		if (treeNodeOfSelection != null && treeNodeOfSelection.getValue() instanceof SingleRead) {
40
		    //TODO Can the parent node (containing the cut positions) be extracted from SingleRead?
41
			try {
42
			    SingleRead singleRead = (SingleRead)treeNodeOfSelection.getValue();
43
			    URI uri = null;
44
			    if (singleRead.getPherogram() != null) {  // Pherogram objects without URI are possible.
45
			        uri = MediaUtils.getFirstMediaRepresentationPart(singleRead.getPherogram()).getUri();
46
			    }
37
    @Execute
38
    public void execute(@Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
39
            EPartService partService) {
40
        //TODO Can the parent node (containing the cut positions) be extracted from SingleRead?
41
        try {
42
            SingleRead singleRead = (SingleRead) ((TreeNode) selection.getFirstElement()).getValue();
43
            URI uri = null;
44
            if (singleRead.getPherogram() != null) {  // Pherogram objects without URI are possible.
45
                uri = MediaUtils.getFirstMediaRepresentationPart(singleRead.getPherogram()).getUri();
46
            }
47 47

  
48
			    if (uri == null) {
49
	                MessagingUtils.messageDialog(Messages.ShowPherogramHandler_NO_PHEROGRAM, this,
50
	                        Messages.ShowPherogramHandler_NO_PHEROGRAM_MESSAGE);
51
			    }
52
			    else {
53
			        MPart part = partService.createPart(eu.etaxonomy.taxeditor.molecular.AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_MOLECULAR_EDITOR_E4_PHEROGRAMPARTE4);
54
			        part = partService.showPart(part, PartState.ACTIVATE);
55
			        PherogramPartE4 pherogramPart = (PherogramPartE4) part.getObject();
56
			        pherogramPart.init(new PherogramComponentModel(AlignmentEditorE4.readPherogram(uri)));
57
			    }
58
			}
59
	        catch (Exception e) {
60
	            MessagingUtils.errorDialog(Messages.ShowPherogramHandler_ERROR, null, e.getLocalizedMessage(), TaxeditorMolecularPlugin.PLUGIN_ID,
61
	                    e, false);
62
	        }
48
            if (uri == null) {
49
                MessagingUtils.messageDialog(Messages.ShowPherogramHandler_NO_PHEROGRAM, this,
50
                        Messages.ShowPherogramHandler_NO_PHEROGRAM_MESSAGE);
51
            }
52
            else {
53
                MPart part = partService.createPart(eu.etaxonomy.taxeditor.molecular.AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_MOLECULAR_EDITOR_E4_PHEROGRAMPARTE4);
54
                part = partService.showPart(part, PartState.ACTIVATE);
55
                PherogramPartE4 pherogramPart = (PherogramPartE4) part.getObject();
56
                pherogramPart.init(new PherogramComponentModel(AlignmentEditorE4.readPherogram(uri)));
57
            }
58
        }
59
        catch (Exception e) {
60
            MessagingUtils.errorDialog(Messages.ShowPherogramHandler_ERROR, null, e.getLocalizedMessage(), TaxeditorMolecularPlugin.PLUGIN_ID,
61
                    e, false);
63 62
        }
64
        return null;
65 63
    }
66 64

  
67 65
    @CanExecute
68
    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode selectedTreeNode, MHandledMenuItem menuItem) {
66
    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, MHandledMenuItem menuItem) {
69 67
        boolean canExecute = false;
70
        if(selectedTreeNode!=null){
71
            Object value = selectedTreeNode.getValue();
72
            canExecute = value instanceof SingleRead;
73
        }
68
        canExecute = selection.size()==1
69
                && selection.getFirstElement() instanceof TreeNode
70
                && ((TreeNode) selection.getFirstElement()).getValue() instanceof SingleRead;
74 71
        menuItem.setVisible(canExecute);
75 72
        return canExecute;
76 73
    }

Also available in: Unified diff