Project

General

Profile

Download (20.6 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.taxeditor.preference;
10

    
11
import org.eclipse.jface.layout.GridLayoutFactory;
12
import org.eclipse.swt.SWT;
13
import org.eclipse.swt.custom.CLabel;
14
import org.eclipse.swt.events.SelectionAdapter;
15
import org.eclipse.swt.events.SelectionEvent;
16
import org.eclipse.swt.events.SelectionListener;
17
import org.eclipse.swt.layout.GridData;
18
import org.eclipse.swt.layout.GridLayout;
19
import org.eclipse.swt.widgets.Button;
20
import org.eclipse.swt.widgets.Combo;
21
import org.eclipse.swt.widgets.Composite;
22
import org.eclipse.swt.widgets.Control;
23
import org.eclipse.swt.widgets.Label;
24

    
25
import eu.etaxonomy.cdm.api.application.ICdmRepository;
26
import eu.etaxonomy.cdm.common.URI;
27
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
28
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
29
import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
30
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
31
import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
32
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
33
import eu.etaxonomy.taxeditor.l10n.Messages;
34
import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
35
import eu.etaxonomy.taxeditor.store.CdmStore;
36

    
37
/**
38
 * @author k.luther
39
 * @since 23.03.2018
40
 */
41
public class AbcdImportPreference extends CdmPreferencePage implements IE4PreferencePage, SelectionListener {
42

    
43
    private static final String CATALOGUE_NUMBER = "Catalogue number";
44

    
45
    private static final String BARCODE = "Barcode";
46

    
47
    private static final String ACCESSION_NUMBER = "Accession number";
48

    
49
    protected Abcd206ImportConfigurator configurator;
50

    
51
    protected Combo nomenclaturalCodeSelectionCombo;
52

    
53
    protected boolean allowOverride = true;
54
    protected boolean override = true;
55
    protected CdmPreference preference = null;
56
    Combo textDNAProviderString;
57
    private Composite composite;
58

    
59

    
60
    Button checkBoxMediaSpecimen;
61
    Button checkBoxIgnoreExisting;
62
    Button checkBoxIgnoreAuthorship;
63
    Combo checkBoxMapUnitId;
64
    Button checkBoxRemoveCountry;
65
    Button checkBoxMoveToDefaultClassification;
66
    Button checkBoxImportSiblings;
67
    Button checkBoxAddIndividualsAssociations;
68
    Button checkBoxReuseDescriptiveGroups;
69
    Button checkBoxReuseExistingTaxa;
70

    
71

    
72
    Combo useLocalOrAdmin;
73

    
74
    @Override
75
    protected Control createContents(Composite parent) {
76
        getValues();
77

    
78
        if (!isAdminPreference && !allowOverride){
79
            Label label = new Label(parent, SWT.NONE);
80
            label.setText("The CDM settings don't allow to set the abcd import preference locally. If you need to make local settings, please ask an administrator.");
81
            this.noDefaultAndApplyButton();
82
            return parent;
83
        }
84
        composite = new Composite(parent, SWT.NULL);
85
        GridLayout gridLayout = new GridLayout();
86
        composite.setLayout(gridLayout);
87

    
88
        final CLabel description = new CLabel(composite, SWT.NULL);
89
        description.setText(Messages.AbcdImportPreference_description);
90
        checkBoxMediaSpecimen = new Button(composite, SWT.CHECK);
91
        checkBoxMediaSpecimen.setText(Messages.AbcdImportPreference_media_as_mediaSpecimen);
92
        checkBoxMediaSpecimen
93
                .setToolTipText(Messages.AbcdImportPreference_media_as_subUnit);
94
        checkBoxMediaSpecimen.addSelectionListener(new SelectionAdapter() {
95
            @Override
96
            public void widgetSelected(SelectionEvent e) {
97
                configurator.setAddMediaAsMediaSpecimen(!configurator.isAddMediaAsMediaSpecimen());
98
                setApply(true);
99
            }
100
        });
101

    
102
        checkBoxIgnoreExisting = new Button(composite, SWT.CHECK);
103

    
104
        checkBoxIgnoreExisting.setText(Messages.AbcdImportPreference_not_import_existing_specimen);
105
        checkBoxIgnoreExisting
106
        .setToolTipText(Messages.AbcdImportPreference_not_import_existing_specimen_tooltip);
107
        checkBoxIgnoreExisting.addSelectionListener(new SelectionAdapter() {
108
            @Override
109
            public void widgetSelected(SelectionEvent e) {
110
                configurator.setIgnoreImportOfExistingSpecimen(!configurator.isIgnoreImportOfExistingSpecimen());
111
                setApply(true);
112
            }
113
        });
114

    
115
        checkBoxIgnoreAuthorship = new Button(composite, SWT.CHECK);
116

    
117
        checkBoxIgnoreAuthorship.setText(Messages.AbcdImportPreference_ignore_author);
118
        checkBoxIgnoreAuthorship
119
        .setToolTipText(Messages.AbcdImportPreference_ignore_author_tooltip);
120
        checkBoxIgnoreAuthorship.addSelectionListener(new SelectionAdapter() {
121
            @Override
122
            public void widgetSelected(SelectionEvent e) {
123
                configurator.setIgnoreAuthorship(!configurator.isIgnoreAuthorship());
124
                setApply(true);
125
            }
126
        });
127

    
128

    
129
        GridData gridData = new GridData();
130
        gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
131
        gridData.horizontalIndent = 9;
132
        gridData.minimumWidth=100;
133
//      classificationSelection.setLayoutData(gridData);
134
        Label unitIdLabel = new Label(composite, SWT.NONE);
135
        unitIdLabel.setText(Messages.AbcdImportPreference_map_unit_nr_catalog_number);
136
        checkBoxMapUnitId = new Combo(composite, SWT.BORDER| SWT.READ_ONLY);
137
        checkBoxMapUnitId.setLayoutData(gridData);
138
        checkBoxMapUnitId.add(ACCESSION_NUMBER);
139
        checkBoxMapUnitId.add(BARCODE);
140
        checkBoxMapUnitId.add(CATALOGUE_NUMBER);
141
        checkBoxMapUnitId
142
        .setToolTipText(Messages.AbcdImportPreference_map_unit_number_catalog_number_tooltip);
143
        checkBoxMapUnitId.addSelectionListener(this);
144

    
145
        checkBoxRemoveCountry = new Button(composite, SWT.CHECK);
146

    
147
        checkBoxRemoveCountry.setText(Messages.AbcdImportPreference_remove_country_from_locality);
148
        checkBoxRemoveCountry
149
        .setToolTipText(Messages.AbcdImportPreference_remove_country_from_locality_tooltip);
150
        checkBoxRemoveCountry.addSelectionListener(new SelectionAdapter() {
151
            @Override
152
            public void widgetSelected(SelectionEvent e) {
153
                configurator.setRemoveCountryFromLocalityText(!configurator.isRemoveCountryFromLocalityText());
154
                setApply(true);
155
            }
156
        });
157

    
158
        checkBoxMoveToDefaultClassification = new Button(composite, SWT.CHECK);
159
        checkBoxMoveToDefaultClassification.setSelection(configurator.isMoveNewTaxaToDefaultClassification());
160
        checkBoxMoveToDefaultClassification.setText(Messages.AbcdImportPreference_create_new_classification_new_taxa);
161
        checkBoxMoveToDefaultClassification
162
        .setToolTipText(Messages.AbcdImportPreference_create_new_classification_new_taxa_tooltip);
163
        checkBoxMoveToDefaultClassification.addSelectionListener(new SelectionAdapter() {
164
            @Override
165
            public void widgetSelected(SelectionEvent e) {
166
                configurator.setMoveNewTaxaToDefaultClassification(!configurator.isMoveNewTaxaToDefaultClassification());
167
                setApply(true);
168
            }
169
        });
170

    
171
        checkBoxImportSiblings = new Button(composite, SWT.CHECK);
172

    
173
        checkBoxImportSiblings.setText(Messages.AbcdImportPreference_import_all_children_for_cultures_or_tissues);
174
        checkBoxImportSiblings
175
            .setToolTipText(Messages.AbcdImportPreference_import_all_children_for_cultures_or_tissues_tooltip);
176
        checkBoxImportSiblings.addSelectionListener(new SelectionAdapter() {
177
            @Override
178
            public void widgetSelected(SelectionEvent e) {
179
                configurator.setGetSiblings(!configurator.isGetSiblings());
180
                setApply(true);
181
            }
182
        });
183

    
184
        checkBoxAddIndividualsAssociations = new Button(composite, SWT.CHECK);
185

    
186
        checkBoxAddIndividualsAssociations.setText(Messages.AbcdImportPreference_create_Individual_Association);
187
        checkBoxAddIndividualsAssociations
188
            .setToolTipText(Messages.AbcdImportPreference_create_Individual_Association_tooltip);
189
        checkBoxAddIndividualsAssociations.addSelectionListener(new SelectionAdapter() {
190
            @Override
191
            public void widgetSelected(SelectionEvent e) {
192
                configurator.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(!configurator.isAddIndividualsAssociationsSuchAsSpecimenAndObservations());
193
                setApply(true);
194
            }
195
        });
196

    
197
        checkBoxReuseDescriptiveGroups = new Button(composite, SWT.CHECK);
198

    
199
        checkBoxReuseDescriptiveGroups.setText(Messages.AbcdImportPreference_reuse_descriptive_group);
200
        checkBoxReuseDescriptiveGroups
201
            .setToolTipText(Messages.AbcdImportPreference_reuse_descriptive_group_tooltip);
202
        checkBoxReuseDescriptiveGroups.addSelectionListener(new SelectionAdapter() {
203
            @Override
204
            public void widgetSelected(SelectionEvent e) {
205
                configurator.setReuseExistingDescriptiveGroups(!configurator.isReuseExistingDescriptiveGroups());
206
                setApply(true);
207
            }
208
        });
209

    
210
        checkBoxReuseExistingTaxa = new Button(composite, SWT.CHECK);
211

    
212
        checkBoxReuseExistingTaxa.setText(Messages.AbcdImportPreference_reuse_existing_taxa);
213
        checkBoxReuseExistingTaxa
214
            .setToolTipText(Messages.AbcdImportPreference_reuse_existing_taxa_tooltip);
215
        checkBoxReuseExistingTaxa.addSelectionListener(new SelectionAdapter() {
216
            @Override
217
            public void widgetSelected(SelectionEvent e) {
218
                configurator.setReuseExistingTaxaWhenPossible(!configurator.isReuseExistingTaxaWhenPossible());
219
                setApply(true);
220
            }
221
        });
222
        GridData sepGrid = createTextGridData();
223
        Label separator = new Label(composite, SWT.HORIZONTAL|SWT.SEPARATOR);
224
        separator.setLayoutData(sepGrid);
225
        separator.setVisible(false);
226
        Label labelRef = new Label(composite, SWT.NONE);
227
        labelRef.setText("Biocase provider for associated DNA");
228
        textDNAProviderString = new Combo(composite, SWT.BORDER);
229

    
230
        gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
231
        gridData.horizontalIndent = 9;
232
        gridData.minimumWidth=200;
233

    
234
        textDNAProviderString.setLayoutData(gridData);
235
        GridLayoutFactory.fillDefaults();
236
        String biocaseProvider = null;
237
        CdmPreference pref = null;
238

    
239
        pref = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.BioCaseProvider);
240
        biocaseProvider =  pref!= null? pref.getValue():null;
241

    
242
        if (!isAdminPreference && ((pref != null && pref.isAllowOverride()) || pref == null) ){
243
            biocaseProvider = PreferencesUtil.getStringValue(PreferencePredicate.BioCaseProvider.getKey(), true);
244
        }
245
        if (biocaseProvider != null){
246
            String items[] = biocaseProvider.split(";");
247
            textDNAProviderString.setItems(items);
248
        }
249

    
250
        textDNAProviderString.setEnabled(true);
251
       // textDNAProviderString.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 1, 1));
252
        textDNAProviderString.addSelectionListener(this);
253

    
254
        Label nomenclaturalCodeLabel = new Label(composite, SWT.NONE);
255
        nomenclaturalCodeLabel.setText("Nomenclatural Code");
256
        nomenclaturalCodeSelectionCombo = new Combo(composite, SWT.BORDER| SWT.READ_ONLY);
257
        gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
258
        gridData.horizontalIndent = 9;
259
        nomenclaturalCodeSelectionCombo.setLayoutData(gridData);
260
        GridLayoutFactory.fillDefaults();
261
        for(NomenclaturalCode code: NomenclaturalCode.values()){
262
            nomenclaturalCodeSelectionCombo.add(code.getKey());
263
        }
264

    
265
        nomenclaturalCodeSelectionCombo.addSelectionListener(this);
266
        if (preference != null){
267
            allowOverride = preference.isAllowOverride();
268
        }
269

    
270
        Label sep = new Label(composite, SWT.HORIZONTAL|SWT.SEPARATOR);
271
        sep.setLayoutData(new GridData(SWT.DEFAULT, SWT.DEFAULT, true, false));
272
        GridLayoutFactory.fillDefaults();
273
        useLocalOrAdmin = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
274
        if (!isAdminPreference){
275
            useLocalOrAdmin.add(LocalOrDefaultEnum.Default.getLabel(), 0);
276
            useLocalOrAdmin.add(LocalOrDefaultEnum.Local.getLabel(), 1);
277

    
278
        }else{
279
            useLocalOrAdmin.add(LocalOrDefaultEnum.AllowOverride.getLabel(), 0);
280
            useLocalOrAdmin.add(LocalOrDefaultEnum.Database.getLabel(), 1);
281
        }
282

    
283
        if (!isAdminPreference){
284
            if (override && allowOverride){
285
                useLocalOrAdmin.select(1);
286
            }else{
287
                useLocalOrAdmin.select(0);
288
                composite.setEnabled(false);
289
            }
290
        }else{
291
            if (allowOverride){
292
                useLocalOrAdmin.select(0);
293
            }else{
294
                useLocalOrAdmin.select(1);
295
            }
296
        }
297
        useLocalOrAdmin.addSelectionListener(this);
298
        if (!allowOverride && !isAdminPreference){
299
            useLocalOrAdmin.setEnabled(false);
300
        }
301
        GridData gridDataLocal = new GridData();
302
        gridDataLocal = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
303
       // gridDataLocal.horizontalIndent = 9;
304
        gridDataLocal.minimumWidth=100;
305
        gridDataLocal.widthHint= nomenclaturalCodeSelectionCombo.getBorderWidth();
306
        useLocalOrAdmin.setLayoutData(gridDataLocal);
307
        GridLayoutFactory.fillDefaults();
308
        setSelections();
309

    
310
        return composite;
311
    }
312

    
313
    @Override
314
    public void widgetSelected(SelectionEvent e) {
315
        if (e.getSource().equals(nomenclaturalCodeSelectionCombo)){
316
            this.configurator.setNomenclaturalCode(NomenclaturalCode.getByKey(nomenclaturalCodeSelectionCombo.getText()));
317
        }
318
        if (e.getSource().equals(checkBoxMapUnitId)){
319
            String text = checkBoxMapUnitId.getText();
320
            if (text.equals(ACCESSION_NUMBER)){
321
                this.configurator.setMapUnitIdToAccessionNumber(true);
322
                this.configurator.setMapUnitIdToBarcode(false);
323
                this.configurator.setMapUnitIdToCatalogNumber(false);
324
            }else if (text.equals(BARCODE)){
325
                this.configurator.setMapUnitIdToAccessionNumber(false);
326
                this.configurator.setMapUnitIdToBarcode(true);
327
                this.configurator.setMapUnitIdToCatalogNumber(false);
328
            }else{
329
                this.configurator.setMapUnitIdToAccessionNumber(false);
330
                this.configurator.setMapUnitIdToBarcode(false);
331
                this.configurator.setMapUnitIdToCatalogNumber(true);
332
            }
333
        }
334
        if (e.getSource().equals(useLocalOrAdmin)){
335
            if (useLocalOrAdmin.getText().equals(LocalOrDefaultEnum.Default.getLabel()) || useLocalOrAdmin.getText().equals(LocalOrDefaultEnum.Database.getLabel())){
336
               composite.setEnabled(false);
337
               PreferencesUtil.extractAbcdConfiguratorFromPreferenceString(configurator, preference.getValue());
338
               setSelections();
339

    
340
               if (isAdminPreference){
341
                   allowOverride = false;
342
               }else{
343
                   override = false;
344
               }
345
            }else{
346
               composite.setEnabled(true);
347
               if (isAdminPreference){
348
                   allowOverride = true;
349
               }else{
350
                   override = true;
351
               }
352
            }
353
        }
354

    
355
        if (e.getSource().equals(textDNAProviderString)){
356
            String textDnaProvider = textDNAProviderString.getText();
357
            try{
358
                this.configurator.setDnaSoure(URI.create(textDnaProvider));
359
            }catch(IllegalArgumentException iae){
360
                //do not set as dna source
361
            }
362
        }
363
        this.setApply(true);
364

    
365
       // PreferencesUtil.recursiveSetEnabled(composite, override);
366
        setApply(true);
367

    
368
    }
369

    
370
    @Override
371
    public boolean performOk() {
372
        if (!isApply()){
373
            return true;
374
        }
375
        if (configurator != null){
376
            try{
377
                configurator.setDnaSoure(URI.create(textDNAProviderString.getText()));
378
            }catch(IllegalArgumentException e){
379
                //dna provider is not an uri
380
            }
381
            String configString = configurator.toString();
382

    
383

    
384
            if (override){
385
                PreferencesUtil.setStringValue(PreferencePredicate.AbcdImportConfig.getKey(), configString);
386
                PreferencesUtil.setBooleanValue( PreferencesUtil.prefOverrideKey(PreferencePredicate.AbcdImportConfig.getKey()), override);
387

    
388
            }else{
389
                PreferencesUtil.setBooleanValue( PreferencesUtil.prefOverrideKey(PreferencePredicate.AbcdImportConfig.getKey()), override);
390
            }
391
            PreferencesUtil.setStringValue(PreferencePredicate.AbcdImportConfig.getKey(), "");
392
        }
393
        return true;
394
    }
395

    
396
    @Override
397
    public void widgetDefaultSelected(SelectionEvent e) {
398
        // TODO Auto-generated method stub
399

    
400
    }
401

    
402
    @Override
403
    protected void performDefaults() {
404
        configurator = Abcd206ImportConfigurator.NewInstance(null,null);
405
        configurator.setNomenclaturalCode(PreferencesUtil.getPreferredNomenclaturalCode());
406
        override = false;
407
        setSelections();
408
        setApply(true);
409
    }
410

    
411
    @Override
412
    public void getValues() {
413
        isAdminPreference = false;
414
        configurator = Abcd206ImportConfigurator.NewInstance(null,null);
415
        ICdmRepository controller;
416
        controller = CdmStore.getCurrentApplicationConfiguration();
417
        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AbcdImportConfig);
418
        preference = controller.getPreferenceService().find(key);
419

    
420
        if (preference != null ){
421
            allowOverride = preference.isAllowOverride();
422
        }else{
423
            preference = CdmPreference.NewTaxEditorInstance(PreferencePredicate.AbcdImportConfig, configurator.toString());
424
        }
425

    
426
        override = PreferencesUtil.getBooleanValue(
427
                PreferencesUtil.prefOverrideKey(PreferencePredicate.AbcdImportConfig.getKey()), true) != null? PreferencesUtil.getBooleanValue(
428
                        PreferencesUtil.prefOverrideKey(PreferencePredicate.AbcdImportConfig.getKey()), true):false;
429
       if (allowOverride && override){
430
           String configString = PreferencesUtil.getStringValue(PreferencePredicate.AbcdImportConfig.getKey(), true);
431
           configurator = Abcd206ImportConfigurator.NewInstance(null, null);
432
           PreferencesUtil.extractAbcdConfiguratorFromPreferenceString(configurator, configString);
433
       }else{
434
           PreferencesUtil.extractAbcdConfiguratorFromPreferenceString(configurator, preference.getValue());
435
       }
436
    }
437

    
438
    protected void setSelections(){
439
        checkBoxMediaSpecimen.setSelection(configurator.isAddMediaAsMediaSpecimen());
440
        checkBoxIgnoreExisting.setSelection(configurator.isIgnoreImportOfExistingSpecimen());
441
        checkBoxIgnoreAuthorship.setSelection(configurator.isIgnoreAuthorship());
442
        if (configurator.isMapUnitIdToAccessionNumber()){
443
            checkBoxMapUnitId.select(0);
444
        }else if (configurator.isMapUnitIdToBarcode()){
445
            checkBoxMapUnitId.select(1);
446
        }else{
447
            checkBoxMapUnitId.select(2);
448
        }
449

    
450

    
451
        checkBoxRemoveCountry.setSelection(configurator.isRemoveCountryFromLocalityText());
452
        checkBoxImportSiblings.setSelection(configurator.isGetSiblings());
453
        checkBoxAddIndividualsAssociations.setSelection(configurator.isAddIndividualsAssociationsSuchAsSpecimenAndObservations());
454
        checkBoxReuseDescriptiveGroups.setSelection(configurator.isReuseExistingDescriptiveGroups());
455
        checkBoxReuseExistingTaxa.setSelection(configurator.isReuseExistingTaxaWhenPossible());
456

    
457
        textDNAProviderString.setText(configurator.getDnaSoure()!= null? configurator.getDnaSoure().toString():"");
458
        int index = 0;
459
        if (configurator.getNomenclaturalCode() != null){
460
            for (String label : nomenclaturalCodeSelectionCombo.getItems()){
461
                if (label.equals(configurator.getNomenclaturalCode().getKey())){
462
                    nomenclaturalCodeSelectionCombo.select(index);
463
                }
464
                index++;
465
            }
466
        }else{
467
            for (String label : nomenclaturalCodeSelectionCombo.getItems()){
468
                NomenclaturalCode code = PreferencesUtil.getPreferredNomenclaturalCode();
469
                if (label.equals(code.getKey())){
470
                    nomenclaturalCodeSelectionCombo.select(index);
471
                    configurator.setNomenclaturalCode(code);
472
                }
473
                index++;
474
            }
475
        }
476
    }
477
}
(1-1/60)