replace cybertaxonomy.eu by cybertaxonomy.org in taxeditor
[taxeditor.git] / eu.etaxonomy.taxeditor.molecular / src / main / java / eu / etaxonomy / taxeditor / molecular / editor / e4 / handler / ExportSequenceToFileHandlerE4.java
1 // $Id$
2 /**
3 * Copyright (C) 2016 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.molecular.editor.e4.handler;
11
12
13 import java.io.File;
14 import java.io.IOException;
15
16 import javax.inject.Named;
17
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.e4.core.di.annotations.CanExecute;
20 import org.eclipse.e4.core.di.annotations.Execute;
21 import org.eclipse.e4.core.di.annotations.Optional;
22 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
23 import org.eclipse.e4.ui.services.IServiceConstants;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.viewers.TreeNode;
26 import org.eclipse.jface.wizard.WizardDialog;
27 import org.eclipse.swt.widgets.Shell;
28
29 import eu.etaxonomy.cdm.model.molecular.Sequence;
30 import eu.etaxonomy.taxeditor.model.MessagingUtils;
31 import eu.etaxonomy.taxeditor.molecular.TaxeditorMolecularPlugin;
32 import eu.etaxonomy.taxeditor.molecular.io.CDMPherogramAlignmentObjectTranslator;
33 import eu.etaxonomy.taxeditor.molecular.io.CDMSequenceMatrixAdapter;
34 import eu.etaxonomy.taxeditor.molecular.io.SingleReadAlignmentRDFXMLConstants;
35 import eu.etaxonomy.taxeditor.molecular.io.wizard.ExportSingleReadAlignmentWizard;
36 import eu.etaxonomy.taxeditor.molecular.l10n.Messages;
37 import eu.etaxonomy.taxeditor.util.ApplicationUtil;
38 import info.bioinfweb.jphyloio.JPhyloIOEventWriter;
39 import info.bioinfweb.jphyloio.ReadWriteParameterMap;
40 import info.bioinfweb.jphyloio.ReadWriteParameterNames;
41 import info.bioinfweb.jphyloio.dataadapters.implementations.ListBasedDocumentDataAdapter;
42 import info.bioinfweb.jphyloio.factory.JPhyloIOReaderWriterFactory;
43 import info.bioinfweb.jphyloio.objecttranslation.ObjectTranslatorFactory;
44
45
46
47 /**
48 * Allows to export a single read alignment to various alignment formats using
49 * <a href="http://bioinfweb.info/JPhyloIO/"><i>JPhyloIO</i></a>.
50 *
51 * @author Ben Stöver
52 * @date 24.04.2016
53 */
54 public class ExportSequenceToFileHandlerE4 {
55 private static final JPhyloIOReaderWriterFactory factory = new JPhyloIOReaderWriterFactory();
56
57
58 @Execute
59 public void execute(@Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
60 @Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
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, "https://cybertaxonomy.org/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 }
99 }
100
101 }
102 }
103
104 @CanExecute
105 public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, MHandledMenuItem menuItem) {
106 boolean canExecute = false;
107 canExecute = selection.size()==1
108 && selection.getFirstElement() instanceof TreeNode
109 && ((TreeNode) selection.getFirstElement()).getValue() instanceof Sequence;
110 menuItem.setVisible(canExecute);
111 return canExecute;
112 }
113 }