Project

General

Profile

Download (13.4 KB) Statistics
| Branch: | Tag: | Revision:
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

    
42

    
43
/**
44
 * Wizard page that allows to specify which parts of a single read alignment shall be exported to an alignment file.
45
 *
46
 * @author Ben Stöver
47
 * @date 18.04.2016
48
 */
49
public class ExportSingleReadAlignmentWizardPage extends WizardPage {
50
    private Text consensusSequenceLabelTextField;
51
    private Button exportConsensusSequenceCB;
52
    private Button exportSingleReadsCB;
53
    private Text fileTextField;
54
    private Combo formatComboBox;
55

    
56

    
57
    /**
58
     * Create the wizard.
59
     */
60
    public ExportSingleReadAlignmentWizardPage() {
61
        super("SingleReadSequencesExport");  //TODO Which pageName should be used here? Any conventions?
62
        setTitle("Sequence export");  //TODO multi language
63
        setDescription("Defines which sequences of the single read alignment shall be exported.");  //TODO multi language
64
    }
65

    
66

    
67
    /**
68
     * Create contents of the wizard.
69
     *
70
     * @param parent the parent component
71
     */
72
    @Override
73
    public void createControl(Composite parent) {
74
        DataBindingContext dbc = new DataBindingContext();
75
        WizardPageSupport.create(this, dbc);
76

    
77
        Composite container = new Composite(parent, SWT.NULL);
78

    
79
        setControl(container);
80
        container.setLayout(new FormLayout());
81

    
82
        exportSingleReadsCB = new Button(container, SWT.CHECK);
83
        FormData fd_exportSingleReadsCB = new FormData();
84
        fd_exportSingleReadsCB.left = new FormAttachment(0, 10);
85
        exportSingleReadsCB.setLayoutData(fd_exportSingleReadsCB);
86
        exportSingleReadsCB.setText("Export single reads");
87
        final ISWTObservableValue exportSingleReadsObservable = SWTObservables.observeSelection(exportSingleReadsCB);
88
        dbc.bindValue(exportSingleReadsObservable, getWizard().getModel().getExportSingleReadsObservable());
89
        exportSingleReadsCB.setSelection(false);
90
        exportSingleReadsCB.setSelection(true);  // Must happen after the component has been bound to the model.
91

    
92

    
93
        exportConsensusSequenceCB = new Button(container, SWT.CHECK);
94
        exportConsensusSequenceCB.addSelectionListener(new SelectionAdapter() {
95
            @Override
96
            public void widgetSelected(SelectionEvent e) {
97
                consensusSequenceLabelTextField.setEnabled(exportConsensusSequenceCB.getSelection());
98
            }
99
        });
100
        FormData fd_exportConsensusSequenceCB = new FormData();
101
        fd_exportConsensusSequenceCB.top = new FormAttachment(exportSingleReadsCB, 6);
102
        fd_exportConsensusSequenceCB.left = new FormAttachment(0, 10);
103
        exportConsensusSequenceCB.setLayoutData(fd_exportConsensusSequenceCB);
104
        exportConsensusSequenceCB.setText("Export consensus sequence");
105
        final ISWTObservableValue exportConsensusSequenceObservable = SWTObservables.observeSelection(exportConsensusSequenceCB);
106
        dbc.bindValue(exportConsensusSequenceObservable, getWizard().getModel().getExportConsensusSequenceObservable());
107
        exportConsensusSequenceCB.setSelection(false);
108
        exportConsensusSequenceCB.setSelection(true);  // Must happen after the component has been bound to the model.
109

    
110
        consensusSequenceLabelTextField = new Text(container, SWT.BORDER);
111
        FormData fd_consensusSequenceNameTextField = new FormData();
112
        fd_consensusSequenceNameTextField.bottom = new FormAttachment(exportConsensusSequenceCB, 32, SWT.BOTTOM);
113
        fd_consensusSequenceNameTextField.left = new FormAttachment(0, 32);
114
        fd_consensusSequenceNameTextField.right = new FormAttachment(100, -10);
115
        fd_consensusSequenceNameTextField.top = new FormAttachment(exportConsensusSequenceCB, 6);
116
        consensusSequenceLabelTextField.setLayoutData(fd_consensusSequenceNameTextField);
117
        final ISWTObservableValue consensusSequenceLabelObservable = SWTObservables.observeText(consensusSequenceLabelTextField, SWT.Modify);
118
        dbc.bindValue(consensusSequenceLabelObservable, getWizard().getModel().getConsensusSequenceLabelObservable());
119
        consensusSequenceLabelTextField.setText("ConsensusSequence");  // Must happen after the component has been bound to the model.
120

    
121
        Label lblFormat = new Label(container, SWT.NONE);
122
        FormData fd_lblFormat = new FormData();
123
        fd_lblFormat.top = new FormAttachment(consensusSequenceLabelTextField, 36);
124
        fd_lblFormat.left = new FormAttachment(exportSingleReadsCB, 0, SWT.LEFT);
125
        lblFormat.setLayoutData(fd_lblFormat);
126
        lblFormat.setText("Select the export format:");  //TODO multi language
127

    
128
        Label lblSelectTheData = new Label(container, SWT.NONE);
129
        fd_exportSingleReadsCB.top = new FormAttachment(lblSelectTheData, 6);
130
        FormData fd_lblSelectTheData = new FormData();
131
        fd_lblSelectTheData.left = new FormAttachment(0, 10);
132
        fd_lblSelectTheData.top = new FormAttachment(0, 10);
133
        lblSelectTheData.setLayoutData(fd_lblSelectTheData);
134
        lblSelectTheData.setText("Select the data to export:");  //TODO multi language
135

    
136
        Label lblSelectTheExport = new Label(container, SWT.NONE);
137
        FormData fd_lblSelectTheExport = new FormData();
138
        fd_lblSelectTheExport.top = new FormAttachment(lblFormat, 57);
139
        fd_lblSelectTheExport.left = new FormAttachment(exportSingleReadsCB, 0, SWT.LEFT);
140
        lblSelectTheExport.setLayoutData(fd_lblSelectTheExport);
141
        lblSelectTheExport.setText("Select the export destination:");  //TODO multi language
142

    
143
        Label lblAlignmentFile = new Label(container, SWT.NONE);
144
        FormData fd_lblAlignmentFile = new FormData();
145
        fd_lblAlignmentFile.top = new FormAttachment(lblSelectTheExport, 9);
146
        fd_lblAlignmentFile.left = new FormAttachment(exportSingleReadsCB, 0, SWT.LEFT);
147
        lblAlignmentFile.setLayoutData(fd_lblAlignmentFile);
148
        lblAlignmentFile.setText("Alignment file:");  //TODO multi language
149

    
150
        fileTextField = new Text(container, SWT.BORDER);
151
        FormData fd_text = new FormData();
152
        fd_text.right = new FormAttachment(100, -10);
153
        fd_text.left = new FormAttachment(lblAlignmentFile, 6);
154
        fd_text.top = new FormAttachment(lblSelectTheExport, 6);
155
        fileTextField.setLayoutData(fd_text);
156
        final ISWTObservableValue fileNameObservable = SWTObservables.observeText(fileTextField, SWT.Modify);
157
        dbc.bindValue(fileNameObservable, getWizard().getModel().getFileNameObservable());
158

    
159
        Button btnBrowse = new Button(container, SWT.NONE);
160
        btnBrowse.addSelectionListener(new SelectionAdapter() {
161
            @Override
162
            public void widgetSelected(SelectionEvent e) {
163
                FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
164
                dialog.setText("Export to");  //TODO multi language
165
                ContentExtensionFileFilter filter = getSelectedFormat().createFileFilter(TestStrategy.EXTENSION);
166
                dialog.setFilterExtensions(new String[]{filter.getExtensionsAsString()});
167
                dialog.setFilterNames(new String[]{filter.getDescription()});
168
                String selectedFile = dialog.open();
169
                if (selectedFile != null) {  // Otherwise selecting was canceled.
170
                    fileTextField.setText(selectedFile);
171
                }
172
            }
173
        });
174
        FormData fd_btnBrowse = new FormData();
175
        fd_btnBrowse.left = new FormAttachment(100, -128);
176
        fd_btnBrowse.bottom = new FormAttachment(fileTextField, 43, SWT.BOTTOM);
177
        fd_btnBrowse.top = new FormAttachment(fileTextField, 13);
178
        fd_btnBrowse.right = new FormAttachment(100, -10);
179
        btnBrowse.setLayoutData(fd_btnBrowse);
180
        btnBrowse.setText("Browse...");
181

    
182
        formatComboBox = new Combo(container, SWT.READ_ONLY);
183
        for (JPhyloIOFormatInfo formatInfo : ExportSingleReadAlignmentWizardModel.FORMAT_NAMES_TO_INFO_MAP.values()) {
184
            formatComboBox.add(formatInfo.getFormatName());
185
        }
186
        FormData fd_formatComboBox = new FormData();
187
        fd_formatComboBox.left = new FormAttachment(0, 10);
188
        fd_formatComboBox.right = new FormAttachment(100, -10);
189
        fd_formatComboBox.top = new FormAttachment(lblFormat, 6);
190
        formatComboBox.setLayoutData(fd_formatComboBox);
191
        final ISWTObservableValue formatObservable = SWTObservables.observeSelection(formatComboBox);  // ViewerProperties.singleSelection().observe(formatComboBox); falls ein ComboViewer verwendet wird. (Dann kann direkt FormatInfo verwendet werden.)
192
        dbc.bindValue(formatObservable, getWizard().getModel().getFormatInfoObservable());
193
        formatComboBox.select(0);  // Must happen after the component has been bound to the model.
194

    
195
        Button appendExtensionButton = new Button(container, SWT.NONE);
196
        appendExtensionButton.addSelectionListener(new SelectionAdapter() {
197
            @Override
198
            public void widgetSelected(SelectionEvent e) {
199
                fileTextField.setText(fileTextField.getText() + ExtensionFileFilter.EXTENSION_SEPARATOR +
200
                        getSelectedFormat().createFileFilter(TestStrategy.EXTENSION).getDefaultExtension());
201
            }
202
        });
203
        FormData fd_btnNewButton = new FormData();
204
        fd_btnNewButton.left = new FormAttachment(btnBrowse, -217, SWT.LEFT);
205
        fd_btnNewButton.right = new FormAttachment(btnBrowse, -6);
206
        fd_btnNewButton.bottom = new FormAttachment(fileTextField, 43, SWT.BOTTOM);
207
        fd_btnNewButton.top = new FormAttachment(fileTextField, 13);
208
        appendExtensionButton.setLayoutData(fd_btnNewButton);
209
        appendExtensionButton.setText("Append default extension");  //TODO multi language
210

    
211
        dbc.addValidationStatusProvider(new MultiValidator() {
212
            @Override
213
            protected IStatus validate() {
214
                String label = (String)consensusSequenceLabelObservable.getValue();
215
                boolean exportConsensus = (Boolean)exportConsensusSequenceObservable.getValue();
216
                boolean exportReads = (Boolean)exportSingleReadsObservable.getValue();
217

    
218
                if (!exportConsensus && !exportReads) {
219
                    return ValidationStatus.error("Either single reads or the consensus sequence have to be selected for export.");  //TODO multi language
220
                }
221
                else if (exportConsensus && ((label == null) || label.isEmpty())) {
222
                    return ValidationStatus.error("The consensus sequence label must not be empty.");  //TODO multi language
223
                }
224

    
225
                String fileName = (String)fileNameObservable.getValue();
226
                if ((fileName == null) || fileName.isEmpty()) {
227
                    return ValidationStatus.error("The file name must not be empty.");  //TODO multi language
228
                }
229
                else if (!getSelectedFormat().createFileFilter(TestStrategy.EXTENSION).accept(new File(fileName))) {
230
                    return ValidationStatus.warning("The file name does have a valid extension for the selected format.");  //TODO multi language
231
                }
232
                else if (fileName.charAt(0) == ExtensionFileFilter.EXTENSION_SEPARATOR) {
233
                    return ValidationStatus.warning("File starting with '" + ExtensionFileFilter.EXTENSION_SEPARATOR +
234
                            "' are not supported on all operating systems.");  //TODO multi language
235
                }
236
                else {
237
                    return ValidationStatus.ok();
238
                }
239
            }
240
        });
241
    }
242

    
243

    
244
    private JPhyloIOFormatInfo getSelectedFormat() {
245
        return ExportSingleReadAlignmentWizardModel.FORMAT_NAMES_TO_INFO_MAP.get(formatComboBox.getText());
246
    }
247

    
248

    
249
    /**
250
     * {@inheritDoc}
251
     */
252
    @Override
253
    public ExportSingleReadAlignmentWizard getWizard() {
254
        return (ExportSingleReadAlignmentWizard)super.getWizard();  // Would throw an exception, if this page is used within another wizard.
255
    }
256
}
(3-3/3)