Intermediate commit in implementing wizard validation (before moving to MultiValidator).
[taxeditor.git] / eu.etaxonomy.taxeditor.molecular / src / main / java / eu / etaxonomy / taxeditor / molecular / handler / ExportSequenceToFileHandler.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.handler;
11
12
13 import info.bioinfweb.jphyloio.JPhyloIOEventWriter;
14 import info.bioinfweb.jphyloio.ReadWriteParameterMap;
15 import info.bioinfweb.jphyloio.ReadWriteParameterNames;
16 import info.bioinfweb.jphyloio.dataadapters.implementations.ListBasedDocumentDataAdapter;
17 import info.bioinfweb.jphyloio.factory.JPhyloIOReaderWriterFactory;
18 import info.bioinfweb.jphyloio.objecttranslation.ObjectTranslatorFactory;
19
20 import java.io.File;
21 import java.io.IOException;
22
23 import org.eclipse.core.commands.AbstractHandler;
24 import org.eclipse.core.commands.ExecutionEvent;
25 import org.eclipse.core.commands.ExecutionException;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.TreeNode;
29 import org.eclipse.jface.wizard.WizardDialog;
30 import org.eclipse.ui.handlers.HandlerUtil;
31
32 import eu.etaxonomy.cdm.model.molecular.Sequence;
33 import eu.etaxonomy.taxeditor.editor.EditorUtil;
34 import eu.etaxonomy.taxeditor.model.MessagingUtils;
35 import eu.etaxonomy.taxeditor.molecular.TaxeditorMolecularPlugin;
36 import eu.etaxonomy.taxeditor.molecular.io.CDMPherogramAlignmentObjectTranslator;
37 import eu.etaxonomy.taxeditor.molecular.io.CDMSequenceMatrixAdapter;
38 import eu.etaxonomy.taxeditor.molecular.io.SingleReadAlignmentRDFXMLConstants;
39 import eu.etaxonomy.taxeditor.molecular.io.wizard.ExportSingleReadAlignmentWizard;
40
41
42
43 /**
44 * Allows to export a single read alignment to various alignment formats using
45 * <a href="http://bioinfweb.info/JPhyloIO/">JPhyloIO</a>.
46 *
47 * @author Ben Stöver
48 * @date 24.04.2016
49 */
50 public class ExportSequenceToFileHandler extends AbstractHandler {
51 private static final JPhyloIOReaderWriterFactory factory = new JPhyloIOReaderWriterFactory();
52
53
54 @Override
55 public Object execute(ExecutionEvent event) throws ExecutionException {
56 ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
57 TreeNode treeNodeOfSelection = EditorUtil.getTreeNodeOfSelection(currentSelection);
58 if (treeNodeOfSelection != null && treeNodeOfSelection.getValue() instanceof Sequence) {
59 Sequence sequence = (Sequence)treeNodeOfSelection.getValue();
60
61 final ExportSingleReadAlignmentWizard wizard = new ExportSingleReadAlignmentWizard();
62 final WizardDialog dialog = new WizardDialog(HandlerUtil.getActiveShell(event), wizard);
63 if (dialog.open() == IStatus.OK) { //TODO Is the window disposed?
64 // Prepare writer parameters:
65 ReadWriteParameterMap parameters = new ReadWriteParameterMap();
66 ObjectTranslatorFactory translatorFactory = new ObjectTranslatorFactory();
67 translatorFactory.addXSDTranslators(true);
68 translatorFactory.addTranslator(new CDMPherogramAlignmentObjectTranslator(), true, SingleReadAlignmentRDFXMLConstants.DATA_TYPE_PHERORAGM_ALIGNMENT);
69 parameters.put(ReadWriteParameterNames.KEY_OBJECT_TRANSLATOR_FACTORY, translatorFactory);
70
71 // Create writer and document adapters:
72 JPhyloIOEventWriter writer = factory.getWriter(wizard.getModel().getFormatInfo().getFormatID());
73 ListBasedDocumentDataAdapter document = new ListBasedDocumentDataAdapter();
74 document.getMatrices().add(new CDMSequenceMatrixAdapter(sequence, wizard.getModel().getConsensusSequenceLabel()));
75
76 // Write document:
77 File file = new File(wizard.getModel().getFileName());
78 try {
79 writer.writeDocument(document, file, parameters);
80 }
81 catch (IOException e) {
82 e.printStackTrace();
83 MessagingUtils.errorDialog("IO error", this,
84 "An error occured when trying to export a consensus sequence alignment to the file \"" +
85 file.getAbsolutePath() + "\".", TaxeditorMolecularPlugin.PLUGIN_ID, e, false); //TODO set pluginID
86 //TODO Use multi language error message.
87 }
88 }
89 }
90 return null;
91 }
92 }