Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.molecular / src / main / java / eu / etaxonomy / taxeditor / molecular / io / wizard / ExportSingleReadAlignmentFileFormatPage.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.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;
39
40 import eu.etaxonomy.taxeditor.molecular.Messages;
41
42
43
44 /**
45 * Wizard page that allows to specify which parts of a single read alignment shall be exported to an alignment file.
46 *
47 * @author Ben Stöver
48 * @date 18.04.2016
49 */
50 public class ExportSingleReadAlignmentFileFormatPage extends AbstractExportSingleReadAlignmentWizardPage {
51 private Text fileTextField;
52 private Combo formatComboBox;
53
54
55 /**
56 * Create the wizard.
57 */
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);
62 }
63
64
65 /**
66 * Create contents of the wizard.
67 *
68 * @param parent the parent component
69 */
70 @Override
71 public void createControl(Composite parent) {
72 DataBindingContext dbc = new DataBindingContext();
73 WizardPageSupport.create(this, dbc);
74
75 Composite container = new Composite(parent, SWT.NULL);
76
77 setControl(container);
78 container.setLayout(new FormLayout());
79
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);
87
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);
94
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);
102
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());
111
112 Button btnBrowse = new Button(container, SWT.NONE);
113 btnBrowse.addSelectionListener(new SelectionAdapter() {
114 @Override
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);
124 }
125 }
126 });
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);
134
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());
139 }
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.
148
149 Button appendExtensionButton = new Button(container, SWT.NONE);
150 appendExtensionButton.addSelectionListener(new SelectionAdapter() {
151 @Override
152 public void widgetSelected(SelectionEvent e) {
153 fileTextField.setText(fileTextField.getText() + ExtensionFileFilter.EXTENSION_SEPARATOR +
154 getSelectedFormat().createFileFilter(TestStrategy.EXTENSION).getDefaultExtension());
155 }
156 });
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);
164
165 dbc.addValidationStatusProvider(new MultiValidator() {
166 @Override
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);
172 }
173 else if (!ExportSingleReadAlignmentWizardModel.FORMATS.get(index).createFileFilter(TestStrategy.EXTENSION).accept(
174 new File(fileName))) {
175
176 return ValidationStatus.warning(Messages.wizardExportAlignmentwarningMissingExtension);
177 }
178 else if (fileName.charAt(0) == ExtensionFileFilter.EXTENSION_SEPARATOR) {
179 return ValidationStatus.warning(Messages.wizardExportAlignmentWarningFileNameStartsDot);
180 }
181 else if (new File(fileName).exists()) {
182 return ValidationStatus.warning(Messages.wizardExportAlignmentWarningFileExists);
183 }
184 else {
185 return ValidationStatus.ok();
186 }
187 }
188 });
189 }
190
191
192 private JPhyloIOFormatInfo getSelectedFormat() {
193 return ExportSingleReadAlignmentWizardModel.FORMATS.get(formatComboBox.getSelectionIndex());
194 }
195 }