59f7575c9eaae840fe8e618ec5c3959ec1d45dcc
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / AbcdImportPreference.java
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 java.net.URI;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.custom.CLabel;
15 import org.eclipse.swt.events.SelectionAdapter;
16 import org.eclipse.swt.events.SelectionEvent;
17 import org.eclipse.swt.events.SelectionListener;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Combo;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.ui.forms.widgets.TableWrapData;
26
27 import eu.etaxonomy.cdm.api.application.ICdmRepository;
28 import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
29 import eu.etaxonomy.cdm.model.metadata.CdmPreference;
30 import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
31 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
32 import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
33 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
34 import eu.etaxonomy.taxeditor.l10n.Messages;
35 import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
36 import eu.etaxonomy.taxeditor.store.CdmStore;
37
38 /**
39 * @author k.luther
40 * @since 23.03.2018
41 *
42 */
43 public class AbcdImportPreference extends CdmPreferencePage implements IE4PreferencePage, SelectionListener {
44
45 /**
46 *
47 */
48 private static final String CATALOGUE_NUMBER = "Catalogue number";
49
50 /**
51 *
52 */
53 private static final String BARCODE = "Barcode";
54
55 /**
56 *
57 */
58 private static final String ACCESSION_NUMBER = "Accession number";
59
60 protected Abcd206ImportConfigurator configurator;
61
62 protected Combo nomenclaturalCodeSelectionCombo;
63
64 protected boolean allowOverride = true;
65 protected boolean override = true;
66 protected CdmPreference preference = null;
67 Combo textDNAProviderString;
68 private Composite composite;
69
70
71 Button checkBoxMediaSpecimen;
72 Button checkBoxIgnoreExisting;
73 Button checkBoxIgnoreAuthorship;
74 Combo checkBoxMapUnitId;
75 // Button checkBoxMapUnitIdToAccessionNumber;
76 // Button checkBoxMapUnitIdToBarcode;
77 Button checkBoxRemoveCountry;
78 Button checkBoxMoveToDefaultClassification;
79 Button checkBoxImportSiblings;
80 Button checkBoxAddIndividualsAssociations;
81 Button checkBoxReuseDescriptiveGroups;
82 Button checkBoxReuseExistingTaxa;
83
84
85 Combo useLocalOrAdmin;
86
87 @Override
88 protected Control createContents(Composite parent) {
89 getValues();
90
91 if (!isAdminPreference && !allowOverride){
92 Label label = new Label(parent, SWT.NONE);
93 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.");
94 this.noDefaultAndApplyButton();
95 return parent;
96 }
97 composite = new Composite(parent, SWT.NULL);
98 GridLayout gridLayout = new GridLayout();
99 composite.setLayout(gridLayout);
100
101 final CLabel description = new CLabel(composite, SWT.NULL);
102 description.setText(Messages.AbcdImportPreference_description);
103 checkBoxMediaSpecimen = new Button(composite, SWT.CHECK);
104 checkBoxMediaSpecimen.setText(Messages.AbcdImportPreference_media_as_mediaSpecimen);
105 checkBoxMediaSpecimen
106 .setToolTipText(Messages.AbcdImportPreference_media_as_subUnit);
107 checkBoxMediaSpecimen.addSelectionListener(new SelectionAdapter() {
108 @Override
109 public void widgetSelected(SelectionEvent e) {
110 configurator.setAddMediaAsMediaSpecimen(!configurator.isAddMediaAsMediaSpecimen());
111 setApply(true);
112 }
113 });
114
115 checkBoxIgnoreExisting = new Button(composite, SWT.CHECK);
116
117 checkBoxIgnoreExisting.setText(Messages.AbcdImportPreference_not_import_existing_specimen);
118 checkBoxIgnoreExisting
119 .setToolTipText(Messages.AbcdImportPreference_not_import_existing_specimen_tooltip);
120 checkBoxIgnoreExisting.addSelectionListener(new SelectionAdapter() {
121 @Override
122 public void widgetSelected(SelectionEvent e) {
123 configurator.setIgnoreImportOfExistingSpecimen(!configurator.isIgnoreImportOfExistingSpecimen());
124 setApply(true);
125 }
126 });
127
128 checkBoxIgnoreAuthorship = new Button(composite, SWT.CHECK);
129
130 checkBoxIgnoreAuthorship.setText(Messages.AbcdImportPreference_ignore_author);
131 checkBoxIgnoreAuthorship
132 .setToolTipText(Messages.AbcdImportPreference_ignore_author_tooltip);
133 checkBoxIgnoreAuthorship.addSelectionListener(new SelectionAdapter() {
134 @Override
135 public void widgetSelected(SelectionEvent e) {
136 configurator.setIgnoreAuthorship(!configurator.isIgnoreAuthorship());
137 setApply(true);
138 }
139 });
140
141
142 GridData gridData = new GridData();
143 gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
144 gridData.horizontalIndent = 5;
145 gridData.minimumWidth=100;
146 // classificationSelection.setLayoutData(gridData);
147 Label unitIdLabel = new Label(composite, SWT.NONE);
148 unitIdLabel.setText(Messages.AbcdImportPreference_map_unit_nr_catalog_number);
149 checkBoxMapUnitId = new Combo(composite, SWT.BORDER| SWT.READ_ONLY);
150 checkBoxMapUnitId.setLayoutData(gridData);
151 checkBoxMapUnitId.add(ACCESSION_NUMBER);
152 checkBoxMapUnitId.add(BARCODE);
153 checkBoxMapUnitId.add(CATALOGUE_NUMBER);
154 checkBoxMapUnitId
155 .setToolTipText(Messages.AbcdImportPreference_map_unit_number_catalog_number_tooltip);
156 checkBoxMapUnitId.addSelectionListener(this);
157
158 checkBoxRemoveCountry = new Button(composite, SWT.CHECK);
159
160 checkBoxRemoveCountry.setText(Messages.AbcdImportPreference_remove_country_from_locality);
161 checkBoxRemoveCountry
162 .setToolTipText(Messages.AbcdImportPreference_remove_country_from_locality_tooltip);
163 checkBoxRemoveCountry.addSelectionListener(new SelectionAdapter() {
164 @Override
165 public void widgetSelected(SelectionEvent e) {
166 configurator.setRemoveCountryFromLocalityText(!configurator.isRemoveCountryFromLocalityText());
167 setApply(true);
168 }
169 });
170
171 checkBoxMoveToDefaultClassification = new Button(composite, SWT.CHECK);
172 checkBoxMoveToDefaultClassification.setSelection(configurator.isMoveNewTaxaToDefaultClassification());
173 checkBoxMoveToDefaultClassification.setText(Messages.AbcdImportPreference_create_new_classification_new_taxa);
174 checkBoxMoveToDefaultClassification
175 .setToolTipText(Messages.AbcdImportPreference_create_new_classification_new_taxa_tooltip);
176 checkBoxMoveToDefaultClassification.addSelectionListener(new SelectionAdapter() {
177 @Override
178 public void widgetSelected(SelectionEvent e) {
179 configurator.setMoveNewTaxaToDefaultClassification(!configurator.isMoveNewTaxaToDefaultClassification());
180 setApply(true);
181 }
182 });
183
184 checkBoxImportSiblings = new Button(composite, SWT.CHECK);
185
186 checkBoxImportSiblings.setText(Messages.AbcdImportPreference_import_all_children_for_cultures_or_tissues);
187 checkBoxImportSiblings
188 .setToolTipText(Messages.AbcdImportPreference_import_all_children_for_cultures_or_tissues_tooltip);
189 checkBoxImportSiblings.addSelectionListener(new SelectionAdapter() {
190 @Override
191 public void widgetSelected(SelectionEvent e) {
192 configurator.setGetSiblings(!configurator.isGetSiblings());
193 setApply(true);
194 }
195 });
196
197 checkBoxAddIndividualsAssociations = new Button(composite, SWT.CHECK);
198
199 checkBoxAddIndividualsAssociations.setText(Messages.AbcdImportPreference_create_Individual_Association);
200 checkBoxAddIndividualsAssociations
201 .setToolTipText(Messages.AbcdImportPreference_create_Individual_Association_tooltip);
202 checkBoxAddIndividualsAssociations.addSelectionListener(new SelectionAdapter() {
203 @Override
204 public void widgetSelected(SelectionEvent e) {
205 configurator.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(!configurator.isAddIndividualsAssociationsSuchAsSpecimenAndObservations());
206 setApply(true);
207 }
208 });
209
210 checkBoxReuseDescriptiveGroups = new Button(composite, SWT.CHECK);
211
212 checkBoxReuseDescriptiveGroups.setText(Messages.AbcdImportPreference_reuse_descriptive_group);
213 checkBoxReuseDescriptiveGroups
214 .setToolTipText(Messages.AbcdImportPreference_reuse_descriptive_group_tooltip);
215 checkBoxReuseDescriptiveGroups.addSelectionListener(new SelectionAdapter() {
216 @Override
217 public void widgetSelected(SelectionEvent e) {
218 configurator.setReuseExistingDescriptiveGroups(!configurator.isReuseExistingDescriptiveGroups());
219 setApply(true);
220 }
221 });
222
223 checkBoxReuseExistingTaxa = new Button(composite, SWT.CHECK);
224
225 checkBoxReuseExistingTaxa.setText(Messages.AbcdImportPreference_reuse_existing_taxa);
226 checkBoxReuseExistingTaxa
227 .setToolTipText(Messages.AbcdImportPreference_reuse_existing_taxa_tooltip);
228 checkBoxReuseExistingTaxa.addSelectionListener(new SelectionAdapter() {
229 @Override
230 public void widgetSelected(SelectionEvent e) {
231 configurator.setReuseExistingTaxaWhenPossible(!configurator.isReuseExistingTaxaWhenPossible());
232 setApply(true);
233 }
234 });
235
236 Label labelRef = new Label(composite, SWT.NONE);
237 labelRef.setText("Biocase provider for associated DNA");
238 // new Label(composite, SWT.NONE);
239
240 textDNAProviderString = new Combo(composite, SWT.BORDER);
241 CdmPreference biocaseProvider = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.BioCaseProvider);
242 if (biocaseProvider != null){
243 String items[] = biocaseProvider.getValue().split(";");
244 textDNAProviderString.setItems(items);
245 }
246
247
248 textDNAProviderString.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
249 textDNAProviderString.setEnabled(true);
250
251 textDNAProviderString.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 1, 1));
252 textDNAProviderString.addSelectionListener(this);
253
254 // classificationSelection.setLayoutData(gridData);
255 Label nomenclaturalCodeLabel = new Label(composite, SWT.NONE);
256 nomenclaturalCodeLabel.setText("Nomenclatural Code");
257 nomenclaturalCodeSelectionCombo = new Combo(composite, SWT.BORDER| SWT.READ_ONLY);
258 nomenclaturalCodeSelectionCombo.setLayoutData(gridData);
259 nomenclaturalCodeSelectionCombo.add(" - ");
260 for(NomenclaturalCode code: NomenclaturalCode.values()){
261 nomenclaturalCodeSelectionCombo.add(code.getKey());
262 }
263
264
265
266 // TODO remember last selection
267 nomenclaturalCodeSelectionCombo.addSelectionListener(this);
268 if (preference != null){
269 allowOverride = preference.isAllowOverride();
270 }
271
272 useLocalOrAdmin = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
273 if (!isAdminPreference){
274 useLocalOrAdmin.add(LocalOrDefaultEnum.Default.getLabel(), 0);
275 useLocalOrAdmin.add(LocalOrDefaultEnum.Local.getLabel(), 1);
276
277 }else{
278 useLocalOrAdmin.add(LocalOrDefaultEnum.AllowOverride.getLabel(), 0);
279 useLocalOrAdmin.add(LocalOrDefaultEnum.Database.getLabel(), 1);
280 }
281
282 if (!isAdminPreference){
283 if (override && allowOverride){
284 useLocalOrAdmin.select(1);
285 }else{
286 useLocalOrAdmin.select(0);
287 composite.setEnabled(false);
288 }
289 }else{
290 if (allowOverride){
291 useLocalOrAdmin.select(0);
292 }else{
293 useLocalOrAdmin.select(1);
294 }
295 }
296 useLocalOrAdmin.addSelectionListener(this);
297 if (!allowOverride && !isAdminPreference){
298 useLocalOrAdmin.setEnabled(false);
299 }
300 setSelections();
301
302 return composite;
303 }
304
305 @Override
306 public void widgetSelected(SelectionEvent e) {
307 if (e.getSource().equals(nomenclaturalCodeSelectionCombo)){
308 this.configurator.setNomenclaturalCode(NomenclaturalCode.getByKey(nomenclaturalCodeSelectionCombo.getText()));
309 }
310 if (e.getSource().equals(checkBoxMapUnitId)){
311 String text = checkBoxMapUnitId.getText();
312 if (text.equals(ACCESSION_NUMBER)){
313 this.configurator.setMapUnitIdToAccessionNumber(true);
314 this.configurator.setMapUnitIdToBarcode(false);
315 this.configurator.setMapUnitIdToCatalogNumber(false);
316 }else if (text.equals(BARCODE)){
317 this.configurator.setMapUnitIdToAccessionNumber(false);
318 this.configurator.setMapUnitIdToBarcode(true);
319 this.configurator.setMapUnitIdToCatalogNumber(false);
320 }else{
321 this.configurator.setMapUnitIdToAccessionNumber(false);
322 this.configurator.setMapUnitIdToBarcode(false);
323 this.configurator.setMapUnitIdToCatalogNumber(true);
324 }
325 }
326 if (e.getSource().equals(useLocalOrAdmin)){
327 if (useLocalOrAdmin.getText().equals(LocalOrDefaultEnum.Default.getLabel()) || useLocalOrAdmin.getText().equals(LocalOrDefaultEnum.Database.getLabel())){
328 composite.setEnabled(false);
329 PreferencesUtil.extractAbcdConfiguratorFromPreferenceString(configurator, preference.getValue());
330 setSelections();
331
332 if (isAdminPreference){
333 allowOverride = false;
334 }else{
335 override = false;
336 }
337 }else{
338 composite.setEnabled(true);
339 if (isAdminPreference){
340 allowOverride = true;
341 }else{
342 override = true;
343 }
344 }
345 }
346
347 if (e.getSource().equals(textDNAProviderString)){
348 String textDnaProvider = textDNAProviderString.getText();
349 try{
350 this.configurator.setDnaSoure(URI.create(textDnaProvider));
351 }catch(IllegalArgumentException iae){
352 //do not set as dna source
353 }
354 }
355 this.setApply(true);
356
357
358 // PreferencesUtil.recursiveSetEnabled(composite, override);
359 setApply(true);
360
361 }
362
363 @Override
364 public boolean performOk() {
365 if (!isApply()){
366 return true;
367 }
368 if (configurator != null){
369 try{
370 configurator.setDnaSoure(URI.create(textDNAProviderString.getText()));
371 }catch(IllegalArgumentException e){
372 //dna provider is not an uri
373 }
374 String configString = configurator.toString();
375
376
377 if (override){
378 PreferencesUtil.setStringValue(PreferencePredicate.AbcdImportConfig.getKey(), configString);
379 PreferencesUtil.setBooleanValue( PreferencesUtil.prefOverrideKey(PreferencePredicate.AbcdImportConfig.getKey()), override);
380
381 }else{
382 PreferencesUtil.setBooleanValue( PreferencesUtil.prefOverrideKey(PreferencePredicate.AbcdImportConfig.getKey()), override);
383 }
384
385
386 }
387 return true;
388 }
389
390 /**
391 * {@inheritDoc}
392 */
393 @Override
394 public void widgetDefaultSelected(SelectionEvent e) {
395 // TODO Auto-generated method stub
396
397 }
398
399 @Override
400 protected void performDefaults() {
401 configurator = Abcd206ImportConfigurator.NewInstance(null,null);
402 override = false;
403 setSelections();
404 setApply(true);
405 }
406
407 @Override
408 public void getValues() {
409 isAdminPreference = false;
410 configurator = Abcd206ImportConfigurator.NewInstance(null,null);
411 ICdmRepository controller;
412 controller = CdmStore.getCurrentApplicationConfiguration();
413 PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AbcdImportConfig);
414 preference = controller.getPreferenceService().find(key);
415
416 if (preference != null ){
417 allowOverride = preference.isAllowOverride();
418 }else{
419 preference = CdmPreference.NewTaxEditorInstance(PreferencePredicate.AbcdImportConfig, configurator.toString());
420 }
421
422 override = PreferencesUtil.getBooleanValue(
423 PreferencesUtil.prefOverrideKey(PreferencePredicate.AbcdImportConfig.getKey()), true) != null? PreferencesUtil.getBooleanValue(
424 PreferencesUtil.prefOverrideKey(PreferencePredicate.AbcdImportConfig.getKey()), true):false;
425 if (allowOverride && override){
426 configurator = PreferencesUtil.getLocalAbcdImportConfigurator();
427 }else{
428 PreferencesUtil.extractAbcdConfiguratorFromPreferenceString(configurator, preference.getValue());
429 }
430 }
431
432 protected void setSelections(){
433 checkBoxMediaSpecimen.setSelection(configurator.isAddMediaAsMediaSpecimen());
434 checkBoxIgnoreExisting.setSelection(configurator.isIgnoreImportOfExistingSpecimen());
435 checkBoxIgnoreAuthorship.setSelection(configurator.isIgnoreAuthorship());
436 if (configurator.isMapUnitIdToAccessionNumber()){
437 checkBoxMapUnitId.select(0);
438 }else if (configurator.isMapUnitIdToBarcode()){
439 checkBoxMapUnitId.select(1);
440 }else{
441 checkBoxMapUnitId.select(2);
442 }
443
444
445 checkBoxRemoveCountry.setSelection(configurator.isRemoveCountryFromLocalityText());
446 checkBoxImportSiblings.setSelection(configurator.isGetSiblings());
447 checkBoxAddIndividualsAssociations.setSelection(configurator.isAddIndividualsAssociationsSuchAsSpecimenAndObservations());
448 checkBoxReuseDescriptiveGroups.setSelection(configurator.isReuseExistingDescriptiveGroups());
449 checkBoxReuseExistingTaxa.setSelection(configurator.isReuseExistingTaxaWhenPossible());
450
451 textDNAProviderString.setText(configurator.getDnaSoure()!= null? configurator.getDnaSoure().toString():"");
452 int index = 0;
453 if (configurator.getNomenclaturalCode() != null){
454 for (String label : nomenclaturalCodeSelectionCombo.getItems()){
455 if (label.equals(configurator.getNomenclaturalCode().getKey())){
456 nomenclaturalCodeSelectionCombo.select(index);
457 }
458 index++;
459 }
460 }else{
461 nomenclaturalCodeSelectionCombo.select(index);
462 }
463
464 }
465
466 }