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
.io
.wizard
;
13 import info
.bioinfweb
.commons
.io
.ContentExtensionFileFilter
;
14 import info
.bioinfweb
.commons
.io
.ContentExtensionFileFilter
.TestStrategy
;
15 import info
.bioinfweb
.commons
.io
.ExtensionFileFilter
;
16 import info
.bioinfweb
.jphyloio
.formatinfo
.JPhyloIOFormatInfo
;
20 import org
.eclipse
.core
.databinding
.DataBindingContext
;
21 import org
.eclipse
.core
.databinding
.validation
.MultiValidator
;
22 import org
.eclipse
.core
.databinding
.validation
.ValidationStatus
;
23 import org
.eclipse
.core
.runtime
.IStatus
;
24 import org
.eclipse
.jface
.databinding
.swt
.ISWTObservableValue
;
25 import org
.eclipse
.jface
.databinding
.swt
.SWTObservables
;
26 import org
.eclipse
.jface
.databinding
.wizard
.WizardPageSupport
;
27 import org
.eclipse
.swt
.SWT
;
28 import org
.eclipse
.swt
.events
.SelectionAdapter
;
29 import org
.eclipse
.swt
.events
.SelectionEvent
;
30 import org
.eclipse
.swt
.layout
.FormAttachment
;
31 import org
.eclipse
.swt
.layout
.FormData
;
32 import org
.eclipse
.swt
.layout
.FormLayout
;
33 import org
.eclipse
.swt
.widgets
.Button
;
34 import org
.eclipse
.swt
.widgets
.Combo
;
35 import org
.eclipse
.swt
.widgets
.Composite
;
36 import org
.eclipse
.swt
.widgets
.FileDialog
;
37 import org
.eclipse
.swt
.widgets
.Label
;
38 import org
.eclipse
.swt
.widgets
.Text
;
40 import eu
.etaxonomy
.taxeditor
.molecular
.Messages
;
45 * Wizard page that allows to specify which parts of a single read alignment shall be exported to an alignment file.
47 * @author Ben Stöver
50 public class ExportSingleReadAlignmentFileFormatPage
extends AbstractExportSingleReadAlignmentWizardPage
{
51 private Text fileTextField
;
52 private Combo formatComboBox
;
58 public ExportSingleReadAlignmentFileFormatPage() {
59 super("ExportSingleReadAlignmentFileFormat"); //TODO Which pageName should be used here? Any conventions? //$NON-NLS-1$
60 setTitle(Messages
.wizardExportAlignmentTitle
);
61 setDescription(Messages
.wizardExportAlignmentDescription
);
66 * Create contents of the wizard.
68 * @param parent the parent component
71 public void createControl(Composite parent
) {
72 DataBindingContext dbc
= new DataBindingContext();
73 WizardPageSupport
.create(this, dbc
);
75 Composite container
= new Composite(parent
, SWT
.NULL
);
77 setControl(container
);
78 container
.setLayout(new FormLayout());
80 Label lblFormat
= new Label(container
, SWT
.NONE
);
81 FormData fd_lblFormat
= new FormData();
82 fd_lblFormat
.right
= new FormAttachment(100, -10);
83 fd_lblFormat
.top
= new FormAttachment(0, 10);
84 fd_lblFormat
.left
= new FormAttachment(0, 10);
85 lblFormat
.setLayoutData(fd_lblFormat
);
86 lblFormat
.setText(Messages
.wizardExportAlignmentExportFormatLabel
);
88 Label lblSelectTheExport
= new Label(container
, SWT
.NONE
);
89 FormData fd_lblSelectTheExport
= new FormData();
90 fd_lblSelectTheExport
.right
= new FormAttachment(100, -10);
91 fd_lblSelectTheExport
.left
= new FormAttachment(0, 10);
92 lblSelectTheExport
.setLayoutData(fd_lblSelectTheExport
);
93 lblSelectTheExport
.setText(Messages
.wizardExportAlignmentDestinationLabel
);
95 Label lblAlignmentFile
= new Label(container
, SWT
.NONE
);
96 FormData fd_lblAlignmentFile
= new FormData();
97 fd_lblAlignmentFile
.top
= new FormAttachment(lblSelectTheExport
, 9);
98 fd_lblAlignmentFile
.right
= new FormAttachment(0, 128);
99 fd_lblAlignmentFile
.left
= new FormAttachment(0, 10);
100 lblAlignmentFile
.setLayoutData(fd_lblAlignmentFile
);
101 lblAlignmentFile
.setText(Messages
.wizardExportAlignmentFileLabel
);
103 fileTextField
= new Text(container
, SWT
.BORDER
);
104 FormData fd_text
= new FormData();
105 fd_text
.left
= new FormAttachment(lblAlignmentFile
, 6);
106 fd_text
.right
= new FormAttachment(100, -10);
107 fd_text
.top
= new FormAttachment(lblSelectTheExport
, 6);
108 fileTextField
.setLayoutData(fd_text
);
109 final ISWTObservableValue fileNameObservable
= SWTObservables
.observeText(fileTextField
, SWT
.Modify
);
110 dbc
.bindValue(fileNameObservable
, getWizard().getModel().getFileNameObservable());
112 Button btnBrowse
= new Button(container
, SWT
.NONE
);
113 btnBrowse
.addSelectionListener(new SelectionAdapter() {
115 public void widgetSelected(SelectionEvent e
) {
116 FileDialog dialog
= new FileDialog(getShell(), SWT
.OPEN
);
117 dialog
.setText(Messages
.wizardExportAlignmentFileDialogTitle
);
118 ContentExtensionFileFilter filter
= getSelectedFormat().createFileFilter(TestStrategy
.EXTENSION
);
119 dialog
.setFilterExtensions(new String
[]{filter
.getExtensionsAsString()});
120 dialog
.setFilterNames(new String
[]{filter
.getDescription()});
121 String selectedFile
= dialog
.open();
122 if (selectedFile
!= null) { // Otherwise selecting was canceled.
123 fileTextField
.setText(selectedFile
);
127 FormData fd_btnBrowse
= new FormData();
128 fd_btnBrowse
.bottom
= new FormAttachment(fileTextField
, 43, SWT
.BOTTOM
);
129 fd_btnBrowse
.top
= new FormAttachment(fileTextField
, 13);
130 fd_btnBrowse
.left
= new FormAttachment(100, -128);
131 fd_btnBrowse
.right
= new FormAttachment(100, -10);
132 btnBrowse
.setLayoutData(fd_btnBrowse
);
133 btnBrowse
.setText(Messages
.wizardExportAlignmentBrowseButton
);
135 formatComboBox
= new Combo(container
, SWT
.READ_ONLY
);
136 fd_lblSelectTheExport
.top
= new FormAttachment(formatComboBox
, 23);
137 for (JPhyloIOFormatInfo formatInfo
: ExportSingleReadAlignmentWizardModel
.FORMATS
) {
138 formatComboBox
.add(formatInfo
.getFormatName());
140 FormData fd_formatComboBox
= new FormData();
141 fd_formatComboBox
.top
= new FormAttachment(lblFormat
, 6);
142 fd_formatComboBox
.left
= new FormAttachment(0, 10);
143 fd_formatComboBox
.right
= new FormAttachment(100, -10);
144 formatComboBox
.setLayoutData(fd_formatComboBox
);
145 final ISWTObservableValue formatObservable
= SWTObservables
.observeSingleSelectionIndex(formatComboBox
); // ViewerProperties.singleSelection().observe(formatComboBox); falls ein ComboViewer verwendet wird. (Dann kann direkt FormatInfo verwendet werden.)
146 dbc
.bindValue(formatObservable
, getWizard().getModel().getFormatInfoObservable());
147 formatComboBox
.select(0); // Must happen after the component has been bound to the model.
149 Button appendExtensionButton
= new Button(container
, SWT
.NONE
);
150 appendExtensionButton
.addSelectionListener(new SelectionAdapter() {
152 public void widgetSelected(SelectionEvent e
) {
153 fileTextField
.setText(fileTextField
.getText() + ExtensionFileFilter
.EXTENSION_SEPARATOR
+
154 getSelectedFormat().createFileFilter(TestStrategy
.EXTENSION
).getDefaultExtension());
157 FormData fd_btnNewButton
= new FormData();
158 fd_btnNewButton
.left
= new FormAttachment(btnBrowse
, -264, SWT
.LEFT
);
159 fd_btnNewButton
.bottom
= new FormAttachment(fileTextField
, 43, SWT
.BOTTOM
);
160 fd_btnNewButton
.right
= new FormAttachment(btnBrowse
, -6);
161 fd_btnNewButton
.top
= new FormAttachment(fileTextField
, 13);
162 appendExtensionButton
.setLayoutData(fd_btnNewButton
);
163 appendExtensionButton
.setText(Messages
.wizardExportAlignmentAppendExtensionButton
);
165 dbc
.addValidationStatusProvider(new MultiValidator() {
167 protected IStatus
validate() {
168 String fileName
= (String
)fileNameObservable
.getValue();
169 Integer index
= (Integer
)formatObservable
.getValue(); // If formatObservable.getValue() is not called from within this method, it is not called on modifications of the according combo anymore. (Would be interesting to know why.)
170 if ((fileName
== null) || fileName
.isEmpty()) {
171 return ValidationStatus
.error(Messages
.wizardExportAlignmentErrorMissingFileName
);
173 else if (!ExportSingleReadAlignmentWizardModel
.FORMATS
.get(index
).createFileFilter(TestStrategy
.EXTENSION
).accept(
174 new File(fileName
))) {
176 return ValidationStatus
.warning(Messages
.wizardExportAlignmentwarningMissingExtension
);
178 else if (fileName
.charAt(0) == ExtensionFileFilter
.EXTENSION_SEPARATOR
) {
179 return ValidationStatus
.warning(Messages
.wizardExportAlignmentWarningFileNameStartsDot
);
181 else if (new File(fileName
).exists()) {
182 return ValidationStatus
.warning(Messages
.wizardExportAlignmentWarningFileExists
);
185 return ValidationStatus
.ok();
192 private JPhyloIOFormatInfo
getSelectedFormat() {
193 return ExportSingleReadAlignmentWizardModel
.FORMATS
.get(formatComboBox
.getSelectionIndex());