3 * Copyright (C) 2016 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.molecular
.handler
;
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
;
21 import java
.io
.IOException
;
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
;
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
;
44 * Allows to export a single read alignment to various alignment formats using
45 * <a href="http://bioinfweb.info/JPhyloIO/">JPhyloIO</a>.
50 public class ExportSequenceToFileHandler
extends AbstractHandler
{
51 private static final JPhyloIOReaderWriterFactory factory
= new JPhyloIOReaderWriterFactory();
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();
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
);
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()));
77 File file
= new File(wizard
.getModel().getFileName());
79 writer
.writeDocument(document
, file
, parameters
);
81 catch (IOException e
) {
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.