+/**
+* Copyright (C) 2018 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.taxeditor.preference;
+
+import java.net.URI;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.CLabel;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Text;
+
+import eu.etaxonomy.cdm.api.application.ICdmRepository;
+import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
+import eu.etaxonomy.cdm.model.metadata.CdmPreference;
+import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
+import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
+import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
+import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
+import eu.etaxonomy.taxeditor.l10n.Messages;
+import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
+import eu.etaxonomy.taxeditor.store.CdmStore;
+
+/**
+ * @author k.luther
+ * @since 23.03.2018
+ *
+ */
+public class AbcdImportPreference extends CdmPreferencePage implements IE4PreferencePage, SelectionListener {
+
+ private Abcd206ImportConfigurator configurator;
+
+ private Combo nomenclaturalCodeSelectionCombo;
+
+ private boolean allowOverride = true;
+ private boolean override = true;
+ private CdmPreference preference = null;
+ Text textDNAProviderString;
+ private Composite composite;
+ Combo overrideCombo;
+
+ @Override
+ protected Control createContents(Composite parent) {
+
+
+
+
+ getValues();
+
+
+ //configurator = PreferencesUtil.getAbcdImportConfigurationPreference();
+ final CLabel description = new CLabel(parent, SWT.NULL);
+ description.setText(Messages.AbcdImportPreference_description);
+// final CLabel overrideDescription = new CLabel(parent, SWT.NULL);
+// overrideDescription.setText("");
+// GridData textGrid = createTextGridData();
+// textGrid.verticalSpan = 2;
+// overrideDescription.setLayoutData(textGrid);
+
+ overrideCombo = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
+
+ overrideCombo.add(LocalOrDefaultEnum.Default.getLabel(), 0);
+ overrideCombo.add(LocalOrDefaultEnum.Local.getLabel(), 1);
+ overrideCombo.addSelectionListener(this);
+ if (override){
+ overrideCombo.select(1);
+ }else{
+ overrideCombo.select(0);
+ }
+
+ composite = new Composite(parent, SWT.NULL);
+
+ GridLayout gridLayout = new GridLayout();
+ composite.setLayout(gridLayout);
+ Button checkBoxMediaSpecimen = new Button(composite, SWT.CHECK);
+ checkBoxMediaSpecimen.setSelection(configurator.isAddMediaAsMediaSpecimen());
+ checkBoxMediaSpecimen.setText(Messages.AbcdImportPreference_media_as_mediaSpecimen);
+ checkBoxMediaSpecimen
+ .setToolTipText(Messages.AbcdImportPreference_media_as_subUnit);
+ checkBoxMediaSpecimen.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ configurator.setAddMediaAsMediaSpecimen(!configurator.isAddMediaAsMediaSpecimen());
+ setApply(true);
+ }
+ });
+
+ Button checkBoxIgnoreExisting = new Button(composite, SWT.CHECK);
+ checkBoxIgnoreExisting.setSelection(configurator.isIgnoreImportOfExistingSpecimen());
+ checkBoxIgnoreExisting.setText(Messages.AbcdImportPreference_not_import_existing_specimen);
+ checkBoxIgnoreExisting
+ .setToolTipText(Messages.AbcdImportPreference_not_import_existing_specimen_tooltip);
+ checkBoxIgnoreExisting.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ configurator.setIgnoreImportOfExistingSpecimen(!configurator.isIgnoreImportOfExistingSpecimen());
+ setApply(true);
+ }
+ });
+
+ Button checkBoxIgnoreAuthorship = new Button(composite, SWT.CHECK);
+ checkBoxIgnoreAuthorship.setSelection(configurator.isIgnoreAuthorship());
+ checkBoxIgnoreAuthorship.setText(Messages.AbcdImportPreference_ignore_author);
+ checkBoxIgnoreAuthorship
+ .setToolTipText(Messages.AbcdImportPreference_ignore_author_tooltip);
+ checkBoxIgnoreAuthorship.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ configurator.setIgnoreAuthorship(!configurator.isIgnoreAuthorship());
+ setApply(true);
+ }
+ });
+
+ Button checkBoxMapUnitIdToCatalogNumber = new Button(composite, SWT.CHECK);
+ checkBoxMapUnitIdToCatalogNumber.setSelection(configurator.isMapUnitIdToCatalogNumber());
+ checkBoxMapUnitIdToCatalogNumber.setText(Messages.AbcdImportPreference_map_unit_nr_catalog_number);
+ checkBoxMapUnitIdToCatalogNumber
+ .setToolTipText(Messages.AbcdImportPreference_map_unit_number_catalog_number_tooltip);
+ checkBoxMapUnitIdToCatalogNumber.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ configurator.setMapUnitIdToCatalogNumber(!configurator.isMapUnitIdToCatalogNumber());
+ setApply(true);
+ }
+ });
+//TODO: only one of the mappings can be checked!
+ Button checkBoxMapUnitIdToAccessionNumber = new Button(composite, SWT.CHECK);
+ checkBoxMapUnitIdToAccessionNumber.setSelection(configurator.isMapUnitIdToAccessionNumber());
+ checkBoxMapUnitIdToAccessionNumber.setText(Messages.AbcdImportPreference_map_unit_number_to_accession_number);
+ checkBoxMapUnitIdToAccessionNumber
+ .setToolTipText(Messages.AbcdImportPreference_map_unit_number_accession_number_tooltip);
+ checkBoxMapUnitIdToAccessionNumber.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ configurator.setMapUnitIdToAccessionNumber(!configurator.isMapUnitIdToAccessionNumber());
+ setApply(true);
+ }
+ });
+
+ Button checkBoxMapUnitIdToBarcode = new Button(composite, SWT.CHECK);
+ checkBoxMapUnitIdToBarcode.setSelection(configurator.isMapUnitIdToBarcode());
+ checkBoxMapUnitIdToBarcode.setText(Messages.AbcdImportPreference_map_unit_number_barcode);
+ checkBoxMapUnitIdToBarcode
+ .setToolTipText(Messages.AbcdImportPreference_map_unit_number_barcode_tooltip);
+ checkBoxMapUnitIdToBarcode.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ configurator.setMapUnitIdToBarcode(!configurator.isMapUnitIdToBarcode());
+ setApply(true);
+ }
+ });
+
+ Button checkBoxRemoveCountry = new Button(composite, SWT.CHECK);
+ checkBoxRemoveCountry.setSelection(configurator.isRemoveCountryFromLocalityText());
+ checkBoxRemoveCountry.setText(Messages.AbcdImportPreference_remove_country_from_locality);
+ checkBoxRemoveCountry
+ .setToolTipText(Messages.AbcdImportPreference_remove_country_from_locality_tooltip);
+ checkBoxRemoveCountry.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ configurator.setRemoveCountryFromLocalityText(!configurator.isRemoveCountryFromLocalityText());
+ setApply(true);
+ }
+ });
+
+ Button checkBoxMoveToDefaultClassification = new Button(composite, SWT.CHECK);
+ checkBoxMoveToDefaultClassification.setSelection(configurator.isMoveNewTaxaToDefaultClassification());
+ checkBoxMoveToDefaultClassification.setText(Messages.AbcdImportPreference_create_new_classification_new_taxa);
+ checkBoxMoveToDefaultClassification
+ .setToolTipText(Messages.AbcdImportPreference_create_new_classification_new_taxa_tooltip);
+ checkBoxMoveToDefaultClassification.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ configurator.setMoveNewTaxaToDefaultClassification(!configurator.isMoveNewTaxaToDefaultClassification());
+ setApply(true);
+ }
+ });
+
+ Button checkBoxImportSiblings = new Button(composite, SWT.CHECK);
+ checkBoxImportSiblings.setSelection(configurator.isGetSiblings());
+ checkBoxImportSiblings.setText(Messages.AbcdImportPreference_import_all_children_for_cultures_or_tissues);
+ checkBoxImportSiblings
+ .setToolTipText(Messages.AbcdImportPreference_import_all_children_for_cultures_or_tissues_tooltip);
+ checkBoxImportSiblings.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ configurator.setGetSiblings(!configurator.isGetSiblings());
+ setApply(true);
+ }
+ });
+
+ Button checkBoxAddIndividualsAssociations = new Button(composite, SWT.CHECK);
+ checkBoxAddIndividualsAssociations.setSelection(configurator.isAddIndividualsAssociationsSuchAsSpecimenAndObservations());
+ checkBoxAddIndividualsAssociations.setText(Messages.AbcdImportPreference_create_Individual_Association);
+ checkBoxAddIndividualsAssociations
+ .setToolTipText(Messages.AbcdImportPreference_create_Individual_Association_tooltip);
+ checkBoxAddIndividualsAssociations.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ configurator.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(!configurator.isAddIndividualsAssociationsSuchAsSpecimenAndObservations());
+ setApply(true);
+ }
+ });
+
+ Button checkBoxReuseDescriptiveGroups = new Button(composite, SWT.CHECK);
+ checkBoxReuseDescriptiveGroups.setSelection(configurator.isReuseExistingDescriptiveGroups());
+ checkBoxReuseDescriptiveGroups.setText(Messages.AbcdImportPreference_reuse_descriptive_group);
+ checkBoxReuseDescriptiveGroups
+ .setToolTipText(Messages.AbcdImportPreference_reuse_descriptive_group_tooltip);
+ checkBoxReuseDescriptiveGroups.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ configurator.setReuseExistingDescriptiveGroups(!configurator.isReuseExistingDescriptiveGroups());
+ setApply(true);
+ }
+ });
+
+ Button checkBoxReuseExistingTaxa = new Button(composite, SWT.CHECK);
+ checkBoxReuseExistingTaxa.setSelection(configurator.isReuseExistingTaxaWhenPossible());
+ checkBoxReuseExistingTaxa.setText(Messages.AbcdImportPreference_reuse_existing_taxa);
+ checkBoxReuseExistingTaxa
+ .setToolTipText(Messages.AbcdImportPreference_reuse_existing_taxa_tooltip);
+ checkBoxReuseExistingTaxa.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ configurator.setReuseExistingTaxaWhenPossible(!configurator.isReuseExistingTaxaWhenPossible());
+ setApply(true);
+ }
+ });
+
+ Label labelRef = new Label(composite, SWT.NONE);
+ labelRef.setText("Biocase provider for associated DNA");
+ new Label(composite, SWT.NONE);
+ textDNAProviderString = new Text(composite, SWT.NONE);
+ textDNAProviderString.setEnabled(true);
+ textDNAProviderString.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 1, 1));
+ textDNAProviderString.setText(configurator.getDnaSoure().toString());
+ GridData gridData = new GridData();
+ gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
+ gridData.horizontalIndent = 5;
+// classificationSelection.setLayoutData(gridData);
+
+ nomenclaturalCodeSelectionCombo = new Combo(composite, SWT.BORDER| SWT.READ_ONLY);
+ nomenclaturalCodeSelectionCombo.setLayoutData(gridData);
+ for(NomenclaturalCode code: NomenclaturalCode.values()){
+ nomenclaturalCodeSelectionCombo.add(code.getKey());
+ }
+ int index = 0;
+ if (configurator.getNomenclaturalCode() != null){
+ for (String label : nomenclaturalCodeSelectionCombo.getItems()){
+ if (label.equals(configurator.getNomenclaturalCode().getKey())){
+ nomenclaturalCodeSelectionCombo.select(index);
+
+ }
+ index++;
+ }
+ }
+
+ // TODO remember last selection
+ nomenclaturalCodeSelectionCombo.addSelectionListener(this);
+ if (preference != null){
+ allowOverride = preference.isAllowOverride();
+ }
+
+ if (!override){
+ //composite.setEnabled(false);
+ PreferencesUtil.recursiveSetEnabled(composite, override);
+ }
+
+ return composite;
+ }
+
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ if (e.getSource().equals(nomenclaturalCodeSelectionCombo)){
+ this.configurator.setNomenclaturalCode(NomenclaturalCode.getByKey(nomenclaturalCodeSelectionCombo.getText()));
+ }
+ if (e.getSource().equals(overrideCombo)) {
+ override = overrideCombo.getText().equals(LocalOrDefaultEnum.Local.getLabel())?true:false;
+ }
+ PreferencesUtil.recursiveSetEnabled(composite, override);
+ setApply(true);
+
+ }
+
+ @Override
+ public boolean performOk() {
+ if (!isApply()){
+ return true;
+ }
+ if (configurator != null){
+ String configString = configurator.toString();
+
+
+ if (override){
+ PreferencesUtil.setStringValue(PreferencePredicate.AbcdImportConfig.getKey(), configString);
+ PreferencesUtil.setBooleanValue( PreferencesUtil.prefOverrideKey(PreferencePredicate.AbcdImportConfig.getKey()), override);
+
+ }else{
+ PreferencesUtil.setBooleanValue( PreferencesUtil.prefOverrideKey(PreferencePredicate.AbcdImportConfig.getKey()), override);
+ }
+
+
+ }
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void widgetDefaultSelected(SelectionEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ protected void performDefaults() {
+ configurator = Abcd206ImportConfigurator.NewInstance(null,null);
+ preference = CdmPreference.NewTaxEditorInstance(PreferencePredicate.AbcdImportConfig, configurator.toString());
+ preference.setAllowOverride(true);
+ super.performDefaults();
+ }
+
+ @Override
+ public void getValues() {
+ isAdminPreference = false;
+ configurator = Abcd206ImportConfigurator.NewInstance(null,null);
+ ICdmRepository controller;
+ controller = CdmStore.getCurrentApplicationConfiguration();
+ PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AbcdImportConfig);
+ preference = controller.getPreferenceService().find(key);
+
+ if (preference != null ){
+ allowOverride = preference.isAllowOverride();
+ }else{
+ preference = CdmPreference.NewTaxEditorInstance(PreferencePredicate.AbcdImportConfig, configurator.toString());
+ }
+
+ override = PreferencesUtil.getBooleanValue(
+ PreferencesUtil.prefOverrideKey(PreferencePredicate.AbcdImportConfig.getKey()), true) != null? PreferencesUtil.getBooleanValue(
+ PreferencesUtil.prefOverrideKey(PreferencePredicate.AbcdImportConfig.getKey()), true):false;
+
+ String configString;
+ if (!override){
+ configString = preference.getValue();
+ }else{
+ configString = PreferencesUtil.getStringValue(PreferencePredicate.AbcdImportConfig.getKey(), true);
+ }
+ if(configString!=null){
+ String[] configArray = configString.split(";"); //$NON-NLS-1$
+
+ for (String configItem: configArray){
+ String[] keyValue = configItem.split(":"); //$NON-NLS-1$
+ if(keyValue.length==2){
+ String keyString = keyValue[0];
+ String valueString = keyValue[1];
+ if (keyString.equals("ignoreImportOfExistingSpecimen")){ //$NON-NLS-1$
+ configurator.setIgnoreImportOfExistingSpecimen(Boolean.valueOf(valueString));
+ }else if (keyString.equals("addIndividualsAssociationsSuchAsSpecimenAndObservations")){ //$NON-NLS-1$
+ configurator.setAddIndividualsAssociationsSuchAsSpecimenAndObservations(Boolean.valueOf(valueString));
+ }else if (keyString.equals("reuseExistingTaxaWhenPossible")){ //$NON-NLS-1$
+ configurator.setReuseExistingTaxaWhenPossible(Boolean.valueOf(valueString));
+ }else if (keyString.equals("ignoreAuthorship")){ //$NON-NLS-1$
+ configurator.setIgnoreAuthorship(Boolean.valueOf(valueString));
+ }else if (keyString.equals("addMediaAsMediaSpecimen")){ //$NON-NLS-1$
+ configurator.setAddMediaAsMediaSpecimen(Boolean.valueOf(valueString));
+ }else if (keyString.equals("reuseExistingMetaData")){ //$NON-NLS-1$
+ configurator.setReuseExistingMetaData(Boolean.valueOf(valueString));
+ }else if (keyString.equals("reuseExistingDescriptiveGroups")){ //$NON-NLS-1$
+ configurator.setReuseExistingDescriptiveGroups(Boolean.valueOf(valueString));
+ }else if (keyString.equals("allowReuseOtherClassifications")){ //$NON-NLS-1$
+ configurator.setAllowReuseOtherClassifications(Boolean.valueOf(valueString));
+ }else if (keyString.equals("deduplicateReferences")){ //$NON-NLS-1$
+ configurator.setDeduplicateReferences(Boolean.valueOf(valueString));
+ }else if (keyString.equals("deduplicateClassifications")){ //$NON-NLS-1$
+ configurator.setDeduplicateClassifications(Boolean.valueOf(valueString));
+ }else if (keyString.equals("moveNewTaxaToDefaultClassification")){ //$NON-NLS-1$
+ configurator.setMoveNewTaxaToDefaultClassification(Boolean.valueOf(valueString));
+ }else if (keyString.equals("mapUnitIdToCatalogNumber")){ //$NON-NLS-1$
+ configurator.setMapUnitIdToCatalogNumber(Boolean.valueOf(valueString));
+ }else if (keyString.equals("mapUnitIdToAccessionNumber")){ //$NON-NLS-1$
+ configurator.setMapUnitIdToAccessionNumber(Boolean.valueOf(valueString));
+ }else if (keyString.equals("mapUnitIdToBarcode")){ //$NON-NLS-1$
+ configurator.setMapUnitIdToBarcode(Boolean.valueOf(valueString));
+ }else if (keyString.equals("overwriteExistingSpecimens")){ //$NON-NLS-1$
+ configurator.setOverwriteExistingSpecimens(Boolean.valueOf(valueString));
+ }else if (keyString.equals("nomenclaturalCode")){ //$NON-NLS-1$
+ configurator.setNomenclaturalCode(NomenclaturalCode.fromString(valueString));
+ }else if (keyString.equals("getSiblings")){ //$NON-NLS-1$
+ configurator.setGetSiblings(Boolean.valueOf(valueString));
+ }else if (keyString.equals("removeCountryFromLocalityText")){ //$NON-NLS-1$
+ configurator.setRemoveCountryFromLocalityText(Boolean.valueOf(valueString));
+ }else if (keyString.equals("dnaSource")){ //$NON-NLS-1$
+ configurator.setDnaSoure(URI.create(valueString));
+ }
+
+ }
+ }
+ }
+ }
+
+}