Merge branch 'release/5.19.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.molecular / src / main / java / eu / etaxonomy / taxeditor / molecular / editor / e4 / handler / ShowPherogramHandlerE4.java
1 package eu.etaxonomy.taxeditor.molecular.editor.e4.handler;
2
3
4 import eu.etaxonomy.cdm.common.URI;
5
6 import javax.inject.Named;
7
8 import org.eclipse.e4.core.di.annotations.CanExecute;
9 import org.eclipse.e4.core.di.annotations.Execute;
10 import org.eclipse.e4.core.di.annotations.Optional;
11 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
12 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
13 import org.eclipse.e4.ui.services.IServiceConstants;
14 import org.eclipse.e4.ui.workbench.modeling.EPartService;
15 import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.jface.viewers.TreeNode;
18
19 import eu.etaxonomy.cdm.model.media.MediaUtils;
20 import eu.etaxonomy.cdm.model.molecular.SingleRead;
21 import eu.etaxonomy.taxeditor.model.MessagingUtils;
22 import eu.etaxonomy.taxeditor.molecular.TaxeditorMolecularPlugin;
23 import eu.etaxonomy.taxeditor.molecular.editor.e4.AlignmentEditorE4;
24 import eu.etaxonomy.taxeditor.molecular.editor.e4.PherogramPartE4;
25 import eu.etaxonomy.taxeditor.molecular.l10n.Messages;
26 import info.bioinfweb.libralign.pherogram.model.PherogramComponentModel;
27
28
29
30 /**
31 * Displays an undistorted pherogram with {@link PherogramPartE4}.
32 *
33 * @author Ben Stöver
34 */
35 public class ShowPherogramHandlerE4 {
36
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
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 }
63 }
64
65 @CanExecute
66 public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, MHandledMenuItem menuItem) {
67 boolean canExecute = false;
68 canExecute = selection.size()==1
69 && selection.getFirstElement() instanceof TreeNode
70 && ((TreeNode) selection.getFirstElement()).getValue() instanceof SingleRead;
71 menuItem.setVisible(canExecute);
72 return canExecute;
73 }
74 }