Strings in ExportSingleReadAlignmentWizardPage externalized and translated.
[taxeditor.git] / eu.etaxonomy.taxeditor.molecular / src / main / java / eu / etaxonomy / taxeditor / molecular / io / wizard / ExportSingleReadAlignmentWizardPage.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.io.wizard;
11
12
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;
17
18 import java.io.File;
19
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.jface.wizard.WizardPage;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.SelectionAdapter;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.layout.FormAttachment;
32 import org.eclipse.swt.layout.FormData;
33 import org.eclipse.swt.layout.FormLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Combo;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.FileDialog;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Text;
40
41 import eu.etaxonomy.taxeditor.molecular.Messages;
42
43
44
45 /**
46 * Wizard page that allows to specify which parts of a single read alignment shall be exported to an alignment file.
47 *
48 * @author Ben Stöver
49 * @date 18.04.2016
50 */
51 public class ExportSingleReadAlignmentWizardPage extends WizardPage {
52 private Text consensusSequenceLabelTextField;
53 private Button exportConsensusSequenceCB;
54 private Button exportSingleReadsCB;
55 private Text fileTextField;
56 private Combo formatComboBox;
57
58
59 /**
60 * Create the wizard.
61 */
62 public ExportSingleReadAlignmentWizardPage() {
63 super("SingleReadSequencesExport"); //TODO Which pageName should be used here? Any conventions? //$NON-NLS-1$
64 setTitle(Messages.wizardExportAlignmentTitle);
65 setDescription(Messages.wizardExportAlignmentDescription);
66 }
67
68
69 /**
70 * Create contents of the wizard.
71 *
72 * @param parent the parent component
73 */
74 @Override
75 public void createControl(Composite parent) {
76 DataBindingContext dbc = new DataBindingContext();
77 WizardPageSupport.create(this, dbc);
78
79 Composite container = new Composite(parent, SWT.NULL);
80
81 setControl(container);
82 container.setLayout(new FormLayout());
83
84 exportSingleReadsCB = new Button(container, SWT.CHECK);
85 FormData fd_exportSingleReadsCB = new FormData();
86 fd_exportSingleReadsCB.left = new FormAttachment(0, 10);
87 exportSingleReadsCB.setLayoutData(fd_exportSingleReadsCB);
88 exportSingleReadsCB.setText(Messages.wizardExportAlignmentExportSingleReads);
89 final ISWTObservableValue exportSingleReadsObservable = SWTObservables.observeSelection(exportSingleReadsCB);
90 dbc.bindValue(exportSingleReadsObservable, getWizard().getModel().getExportSingleReadsObservable());
91 exportSingleReadsCB.setSelection(false);
92 exportSingleReadsCB.setSelection(true); // Must happen after the component has been bound to the model.
93
94
95 exportConsensusSequenceCB = new Button(container, SWT.CHECK);
96 exportConsensusSequenceCB.addSelectionListener(new SelectionAdapter() {
97 @Override
98 public void widgetSelected(SelectionEvent e) {
99 consensusSequenceLabelTextField.setEnabled(exportConsensusSequenceCB.getSelection());
100 }
101 });
102 FormData fd_exportConsensusSequenceCB = new FormData();
103 fd_exportConsensusSequenceCB.top = new FormAttachment(exportSingleReadsCB, 6);
104 fd_exportConsensusSequenceCB.left = new FormAttachment(0, 10);
105 exportConsensusSequenceCB.setLayoutData(fd_exportConsensusSequenceCB);
106 exportConsensusSequenceCB.setText(Messages.wizardExportAlignmentExportConsensusSeqLabel);
107 final ISWTObservableValue exportConsensusSequenceObservable = SWTObservables.observeSelection(exportConsensusSequenceCB);
108 dbc.bindValue(exportConsensusSequenceObservable, getWizard().getModel().getExportConsensusSequenceObservable());
109 exportConsensusSequenceCB.setSelection(false);
110 exportConsensusSequenceCB.setSelection(true); // Must happen after the component has been bound to the model.
111
112 consensusSequenceLabelTextField = new Text(container, SWT.BORDER);
113 FormData fd_consensusSequenceNameTextField = new FormData();
114 fd_consensusSequenceNameTextField.bottom = new FormAttachment(exportConsensusSequenceCB, 32, SWT.BOTTOM);
115 fd_consensusSequenceNameTextField.left = new FormAttachment(0, 32);
116 fd_consensusSequenceNameTextField.right = new FormAttachment(100, -10);
117 fd_consensusSequenceNameTextField.top = new FormAttachment(exportConsensusSequenceCB, 6);
118 consensusSequenceLabelTextField.setLayoutData(fd_consensusSequenceNameTextField);
119 final ISWTObservableValue consensusSequenceLabelObservable = SWTObservables.observeText(consensusSequenceLabelTextField, SWT.Modify);
120 dbc.bindValue(consensusSequenceLabelObservable, getWizard().getModel().getConsensusSequenceLabelObservable());
121 consensusSequenceLabelTextField.setText("ConsensusSequence"); // Must happen after the component has been bound to the model. //$NON-NLS-1$
122
123 Label lblFormat = new Label(container, SWT.NONE);
124 FormData fd_lblFormat = new FormData();
125 fd_lblFormat.top = new FormAttachment(consensusSequenceLabelTextField, 36);
126 fd_lblFormat.left = new FormAttachment(exportSingleReadsCB, 0, SWT.LEFT);
127 lblFormat.setLayoutData(fd_lblFormat);
128 lblFormat.setText(Messages.wizardExportAlignmentExportFormatLabel);
129
130 Label lblSelectTheData = new Label(container, SWT.NONE);
131 fd_exportSingleReadsCB.top = new FormAttachment(lblSelectTheData, 6);
132 FormData fd_lblSelectTheData = new FormData();
133 fd_lblSelectTheData.left = new FormAttachment(0, 10);
134 fd_lblSelectTheData.top = new FormAttachment(0, 10);
135 lblSelectTheData.setLayoutData(fd_lblSelectTheData);
136 lblSelectTheData.setText(Messages.wizardExportAlignmentDataLabel);
137
138 Label lblSelectTheExport = new Label(container, SWT.NONE);
139 FormData fd_lblSelectTheExport = new FormData();
140 fd_lblSelectTheExport.top = new FormAttachment(lblFormat, 57);
141 fd_lblSelectTheExport.left = new FormAttachment(exportSingleReadsCB, 0, SWT.LEFT);
142 lblSelectTheExport.setLayoutData(fd_lblSelectTheExport);
143 lblSelectTheExport.setText(Messages.wizardExportAlignmentDestinationLabel);
144
145 Label lblAlignmentFile = new Label(container, SWT.NONE);
146 FormData fd_lblAlignmentFile = new FormData();
147 fd_lblAlignmentFile.top = new FormAttachment(lblSelectTheExport, 9);
148 fd_lblAlignmentFile.left = new FormAttachment(exportSingleReadsCB, 0, SWT.LEFT);
149 lblAlignmentFile.setLayoutData(fd_lblAlignmentFile);
150 lblAlignmentFile.setText(Messages.wizardExportAlignmentFileLabel);
151
152 fileTextField = new Text(container, SWT.BORDER);
153 FormData fd_text = new FormData();
154 fd_text.right = new FormAttachment(100, -10);
155 fd_text.left = new FormAttachment(lblAlignmentFile, 6);
156 fd_text.top = new FormAttachment(lblSelectTheExport, 6);
157 fileTextField.setLayoutData(fd_text);
158 final ISWTObservableValue fileNameObservable = SWTObservables.observeText(fileTextField, SWT.Modify);
159 dbc.bindValue(fileNameObservable, getWizard().getModel().getFileNameObservable());
160
161 Button btnBrowse = new Button(container, SWT.NONE);
162 btnBrowse.addSelectionListener(new SelectionAdapter() {
163 @Override
164 public void widgetSelected(SelectionEvent e) {
165 FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
166 dialog.setText(Messages.wizardExportAlignmentFileDialogTitle);
167 ContentExtensionFileFilter filter = getSelectedFormat().createFileFilter(TestStrategy.EXTENSION);
168 dialog.setFilterExtensions(new String[]{filter.getExtensionsAsString()});
169 dialog.setFilterNames(new String[]{filter.getDescription()});
170 String selectedFile = dialog.open();
171 if (selectedFile != null) { // Otherwise selecting was canceled.
172 fileTextField.setText(selectedFile);
173 }
174 }
175 });
176 FormData fd_btnBrowse = new FormData();
177 fd_btnBrowse.left = new FormAttachment(100, -128);
178 fd_btnBrowse.bottom = new FormAttachment(fileTextField, 43, SWT.BOTTOM);
179 fd_btnBrowse.top = new FormAttachment(fileTextField, 13);
180 fd_btnBrowse.right = new FormAttachment(100, -10);
181 btnBrowse.setLayoutData(fd_btnBrowse);
182 btnBrowse.setText(Messages.wizardExportAlignmentBrowseButton);
183
184 formatComboBox = new Combo(container, SWT.READ_ONLY);
185 for (JPhyloIOFormatInfo formatInfo : ExportSingleReadAlignmentWizardModel.FORMAT_NAMES_TO_INFO_MAP.values()) {
186 formatComboBox.add(formatInfo.getFormatName());
187 }
188 FormData fd_formatComboBox = new FormData();
189 fd_formatComboBox.left = new FormAttachment(0, 10);
190 fd_formatComboBox.right = new FormAttachment(100, -10);
191 fd_formatComboBox.top = new FormAttachment(lblFormat, 6);
192 formatComboBox.setLayoutData(fd_formatComboBox);
193 final ISWTObservableValue formatObservable = SWTObservables.observeSelection(formatComboBox); // ViewerProperties.singleSelection().observe(formatComboBox); falls ein ComboViewer verwendet wird. (Dann kann direkt FormatInfo verwendet werden.)
194 dbc.bindValue(formatObservable, getWizard().getModel().getFormatInfoObservable());
195 formatComboBox.select(0); // Must happen after the component has been bound to the model.
196
197 Button appendExtensionButton = new Button(container, SWT.NONE);
198 appendExtensionButton.addSelectionListener(new SelectionAdapter() {
199 @Override
200 public void widgetSelected(SelectionEvent e) {
201 fileTextField.setText(fileTextField.getText() + ExtensionFileFilter.EXTENSION_SEPARATOR +
202 getSelectedFormat().createFileFilter(TestStrategy.EXTENSION).getDefaultExtension());
203 }
204 });
205 FormData fd_btnNewButton = new FormData();
206 fd_btnNewButton.left = new FormAttachment(btnBrowse, -276, SWT.LEFT);
207 fd_btnNewButton.right = new FormAttachment(btnBrowse, -6);
208 fd_btnNewButton.bottom = new FormAttachment(fileTextField, 43, SWT.BOTTOM);
209 fd_btnNewButton.top = new FormAttachment(fileTextField, 13);
210 appendExtensionButton.setLayoutData(fd_btnNewButton);
211 appendExtensionButton.setText(Messages.wizardExportAlignmentAppendExtensionButton);
212
213 dbc.addValidationStatusProvider(new MultiValidator() {
214 @Override
215 protected IStatus validate() {
216 String label = (String)consensusSequenceLabelObservable.getValue();
217 boolean exportConsensus = (Boolean)exportConsensusSequenceObservable.getValue();
218 boolean exportReads = (Boolean)exportSingleReadsObservable.getValue();
219
220 if (!exportConsensus && !exportReads) {
221 return ValidationStatus.error(Messages.wizardExportAlignmentErrorNothingToExport);
222 }
223 else if (exportConsensus && ((label == null) || label.isEmpty())) {
224 return ValidationStatus.error(Messages.wizardExportAlignmentErrorMissingSeqLabel);
225 }
226
227 String fileName = (String)fileNameObservable.getValue();
228 if ((fileName == null) || fileName.isEmpty()) {
229 return ValidationStatus.error(Messages.wizardExportAlignmentErrorMissingFileName);
230 }
231 else if (!getSelectedFormat().createFileFilter(TestStrategy.EXTENSION).accept(new File(fileName))) {
232 return ValidationStatus.warning(Messages.wizardExportAlignmentwarningMissingExtension);
233 }
234 else if (fileName.charAt(0) == ExtensionFileFilter.EXTENSION_SEPARATOR) {
235 return ValidationStatus.warning(Messages.wizardExportAlignmentWarningFileNameStartsDot);
236 }
237 else {
238 return ValidationStatus.ok();
239 }
240 }
241 });
242 }
243
244
245 private JPhyloIOFormatInfo getSelectedFormat() {
246 return ExportSingleReadAlignmentWizardModel.FORMAT_NAMES_TO_INFO_MAP.get(formatComboBox.getText());
247 }
248
249
250 /**
251 * {@inheritDoc}
252 */
253 @Override
254 public ExportSingleReadAlignmentWizard getWizard() {
255 return (ExportSingleReadAlignmentWizard)super.getWizard(); // Would throw an exception, if this page is used within another wizard.
256 }
257 }