ref #7849: remove OVERRIDE constants from IPreferenceKeys and handle it by method
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / SpecimenOrObservationPreferences.java
index 735af8ce460b61c1429b86aa3ab3ea4ead88ca39..6e5da1f2c2faa3ece439838e81accb4535f57bab 100644 (file)
@@ -8,18 +8,18 @@
 */
 package eu.etaxonomy.taxeditor.preference;
 
-import org.eclipse.jface.preference.BooleanFieldEditor;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Label;
 
-import eu.etaxonomy.cdm.api.application.ICdmRepository;
-import eu.etaxonomy.cdm.api.service.IPreferenceService;
 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.taxeditor.preference.menu.FieldEditorPreferencePageE4;
+import eu.etaxonomy.taxeditor.l10n.Messages;
+import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
@@ -27,17 +27,27 @@ import eu.etaxonomy.taxeditor.store.CdmStore;
  * @date 13.02.2014
  *
  */
-public class SpecimenOrObservationPreferences extends FieldEditorPreferencePageE4 {
+public class SpecimenOrObservationPreferences extends CdmPreferencePage {
 
-    public SpecimenOrObservationPreferences(){
-        super();
-        setPreferenceStore(PreferencesUtil.getPreferenceStore());
-        if (getPreferenceStore().getBoolean(IPreferenceKeys.ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY)){
-            setDescription("Set the preferences for the display of specimen and obeservations.");
-        }else{
-            setDescription("The CDM settings don't allow to set the preferences for the display of specimen and obeservations locally. If you need to make local settings, please ask an administrator.");
-        }
-    }
+    private static final String LOCAL_SETTINGS_NOT_ALLOWED = "The CDM settings don't allow to set the preferences for the display of specimen and obeservations locally. If you need to make local settings, please ask an administrator.";
+    private static final String DESCRIPTION = "Set the preferences for the display of specimen and observations.";
+
+    protected boolean isShowSpecimenRelatedIssues;
+    protected boolean allowOverrideShowSpecimenRelatedIssues;
+    protected boolean overrideShowSpecimenRelatedIssues;
+    protected boolean isShowCollectingAreaInGeneralSection;
+    protected boolean allowOverrideShowCollectingAreaInGeneralSection;
+    protected boolean overrideShowCollectionAreaInGeneralSection;
+    protected boolean isDeterminationOnlyForFieldUnits;
+    protected boolean allowOverrideDeterminationOnlyForFieldUnits;
+    protected boolean overrideDeterminationOnlyForFieldUnits;
+    protected boolean isShowTaxonAssociation;
+    protected boolean allowOverrideShowTaxonAssociation;
+    protected boolean overrideShowTaxonAssociation;
+    protected boolean isShowLifeForm;
+    protected boolean allowOverrideShowLifeForm;
+    protected boolean overrideShowLifeForm;
+    Composite composite;
 
 
     @Override
@@ -47,53 +57,269 @@ public class SpecimenOrObservationPreferences extends FieldEditorPreferencePageE
         if(!CdmStore.isActive()){
             return;
         }
-        ICdmRepository controller = CdmStore.getCurrentApplicationConfiguration();
-        if (controller != null){
-            IPreferenceService service = controller.getPreferenceService();
-            PrefKey keyTaxonAssociation = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.ShowTaxonAssociations);
-            CdmPreference prefTaxonAssociation = service.find(keyTaxonAssociation);
-            PrefKey keyDeterminationFieldUnit = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.DeterminationOnlyForFieldUnits);
-            CdmPreference prefDeterminationFieldUnit = service.find(keyDeterminationFieldUnit);
-            PrefKey keyCollectingAreas = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.ShowCollectingAreasInGeneralSection);
-            CdmPreference prefCollectingAreas = service.find(keyCollectingAreas);
-
-            getPreferenceStore().setValue(IPreferenceKeys.ALLOW_OVERRIDE_SPECIMEN_PREF, prefTaxonAssociation == null? true : prefTaxonAssociation.isAllowOverride());
-         }
-        if (getPreferenceStore().getBoolean(IPreferenceKeys.ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY)) {
-            setDescription("Set the preferences for the display of specimen and obeservations.");
+
+
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    protected Control createContents(Composite parent) {
+        getValues();
+
+        boolean isEditingAllowed = true;
+        if(!isAdminPreference){
+            CdmPreferenceCache cache = CdmPreferenceCache.instance();
+            CdmPreference showSpecimenPref = cache.get(EditorPreferencePredicate.ShowSpecimen.getKey());
+            if (showSpecimenPref != null){
+                if (!showSpecimenPref.isAllowOverride() ){
+                    Label label = new Label(parent, SWT.NONE);
+                    label.setText(LOCAL_SETTINGS_NOT_ALLOWED);
+                    isEditingAllowed = false;
+
+                }
+            }
+        }
+
+        Label label = new Label(parent, SWT.NONE);
+        label.setText(DESCRIPTION);
+        Composite titleComp = createComposite(parent);
+
+        final Button showSpecimenButton = new Button(titleComp, SWT.CHECK);
+
+        showSpecimenButton.setText(Messages.DatabasePreferncesPage_Show_Specimen);
+
+        showSpecimenButton.setSelection(isShowSpecimenRelatedIssues);
+        showSpecimenButton.addSelectionListener(new SelectionAdapter(){
+             @Override
+             public void widgetSelected(SelectionEvent e) {
+                 isShowSpecimenRelatedIssues = showSpecimenButton.getSelection();
+                 if(isShowSpecimenRelatedIssues){
+                     composite.setVisible(true);
+                     composite.setEnabled(true);
+                 }else{
+                     composite.setVisible(false);
+                     composite.setEnabled(false);
+                 }
+              }
+         });
+        Button allowOverrideIsShowSpecimenRelatedIssuesButton = createAllowOverrideButton(titleComp);
+        if (isAdminPreference){
+            allowOverrideIsShowSpecimenRelatedIssuesButton.setSelection(allowOverrideShowSpecimenRelatedIssues);
+            allowOverrideIsShowSpecimenRelatedIssuesButton.addSelectionListener(new SelectionAdapter(){
+                @Override
+                public void widgetSelected(SelectionEvent e) {
+                    allowOverrideShowSpecimenRelatedIssues = allowOverrideIsShowSpecimenRelatedIssuesButton.getSelection();
+                }
+            });
         }else{
-            setDescription("The CDM settings don't allow to set the preferences for the display of specimen and obeservations locally. If you need to make local settings, please ask an administrator.");
+            allowOverrideIsShowSpecimenRelatedIssuesButton.setSelection(overrideShowSpecimenRelatedIssues);
+            allowOverrideIsShowSpecimenRelatedIssuesButton.addSelectionListener(new SelectionAdapter(){
+                @Override
+                public void widgetSelected(SelectionEvent e) {
+                    overrideShowSpecimenRelatedIssues = allowOverrideIsShowSpecimenRelatedIssuesButton.getSelection();
+                }
+            });
+            if (!allowOverrideShowSpecimenRelatedIssues){
+                showSpecimenButton.setEnabled(false);
+                allowOverrideIsShowSpecimenRelatedIssuesButton.setEnabled(false);
+            }
+        }
+        composite = createComposite(parent);
+        composite.setEnabled(isShowSpecimenRelatedIssues);
+        GridData gridData = createTextGridData();
+
+        Label separator= new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
+        separator.setLayoutData(gridData);
+
+        final Button showCollectingAreaInGeneralSectionButton = new Button(composite, SWT.CHECK);
+
+        showCollectingAreaInGeneralSectionButton.setText(Messages.DatabasePreferncesPage_Show_Collecting_Areas_in_general_section);
+
+        showCollectingAreaInGeneralSectionButton.setSelection(isShowCollectingAreaInGeneralSection);
+        showCollectingAreaInGeneralSectionButton.addSelectionListener(new SelectionAdapter(){
+             @Override
+             public void widgetSelected(SelectionEvent e) {
+                 isShowCollectingAreaInGeneralSection = showCollectingAreaInGeneralSectionButton.getSelection();
 
+              }
+         });
+        Button allowOverrideShowCollectingAreaButton = createAllowOverrideButton(composite);
+        if (isAdminPreference){
+            allowOverrideShowCollectingAreaButton.setSelection(allowOverrideShowCollectingAreaInGeneralSection);
+            allowOverrideShowCollectingAreaButton.addSelectionListener(new SelectionAdapter(){
+                @Override
+                public void widgetSelected(SelectionEvent e) {
+                    allowOverrideShowCollectingAreaInGeneralSection = allowOverrideShowCollectingAreaButton.getSelection();
+                }
+            });
+        }else{
+            allowOverrideShowCollectingAreaButton.setSelection(overrideShowCollectionAreaInGeneralSection);
+            allowOverrideShowCollectingAreaButton.addSelectionListener(new SelectionAdapter(){
+                @Override
+                public void widgetSelected(SelectionEvent e) {
+                    overrideShowCollectionAreaInGeneralSection = allowOverrideShowCollectingAreaButton.getSelection();
+                }
+            });
+            if (!allowOverrideShowCollectingAreaInGeneralSection){
+                showCollectingAreaInGeneralSectionButton.setEnabled(false);
+                allowOverrideShowCollectingAreaButton.setEnabled(false);
+            }
+        }
+
+        final Button determinationOnlyForFieldUnitsButton = new Button(composite, SWT.CHECK);
+        determinationOnlyForFieldUnitsButton.setText(Messages.DatabasePreferncesPage_Determination_only_for_field_unnits);
+        determinationOnlyForFieldUnitsButton.setSelection(isDeterminationOnlyForFieldUnits);
+        determinationOnlyForFieldUnitsButton.addSelectionListener(new SelectionAdapter(){
+             @Override
+             public void widgetSelected(SelectionEvent e) {
+                 isDeterminationOnlyForFieldUnits = determinationOnlyForFieldUnitsButton.getSelection();
+             }
+         });
+
+        Button allowOverridesDeterminationOnlyForFieldUnitsButton = createAllowOverrideButton(composite);
+        if (isAdminPreference){
+            allowOverridesDeterminationOnlyForFieldUnitsButton.setSelection(allowOverrideDeterminationOnlyForFieldUnits);
+            allowOverridesDeterminationOnlyForFieldUnitsButton.addSelectionListener(new SelectionAdapter(){
+                @Override
+                public void widgetSelected(SelectionEvent e) {
+                    allowOverrideDeterminationOnlyForFieldUnits = allowOverridesDeterminationOnlyForFieldUnitsButton.getSelection();
+                }
+            });
+        }else{
+            allowOverridesDeterminationOnlyForFieldUnitsButton.setSelection(overrideDeterminationOnlyForFieldUnits);
+            allowOverridesDeterminationOnlyForFieldUnitsButton.addSelectionListener(new SelectionAdapter(){
+                @Override
+                public void widgetSelected(SelectionEvent e) {
+                    overrideDeterminationOnlyForFieldUnits = allowOverridesDeterminationOnlyForFieldUnitsButton.getSelection();
+                }
+            });
+            if (!allowOverrideDeterminationOnlyForFieldUnits){
+                determinationOnlyForFieldUnitsButton.setEnabled(false);
+                allowOverridesDeterminationOnlyForFieldUnitsButton.setEnabled(false);
+            }
         }
+
+        final Button showTaxonAssociationButton = new Button(composite, SWT.CHECK);
+
+        showTaxonAssociationButton.setText(Messages.DatabasePreferncesPage_Taxon_Associations);
+
+        showTaxonAssociationButton.setSelection(isShowTaxonAssociation);
+        showTaxonAssociationButton.addSelectionListener(new SelectionAdapter(){
+             @Override
+             public void widgetSelected(SelectionEvent e) {
+                 isShowTaxonAssociation = showTaxonAssociationButton.getSelection();
+
+              }
+         });
+        Button allowOverrideIsShowTaxonAssociationButton = createAllowOverrideButton(composite);
+        if (isAdminPreference){
+            allowOverrideIsShowTaxonAssociationButton.setSelection(allowOverrideShowTaxonAssociation);
+            allowOverrideIsShowTaxonAssociationButton.addSelectionListener(new SelectionAdapter(){
+                @Override
+                public void widgetSelected(SelectionEvent e) {
+                    allowOverrideShowTaxonAssociation = allowOverrideIsShowTaxonAssociationButton.getSelection();
+                }
+            });
+        }else{
+            allowOverrideIsShowTaxonAssociationButton.setSelection(overrideShowTaxonAssociation);
+            allowOverrideIsShowTaxonAssociationButton.addSelectionListener(new SelectionAdapter(){
+                @Override
+                public void widgetSelected(SelectionEvent e) {
+                    overrideShowTaxonAssociation = allowOverrideIsShowTaxonAssociationButton.getSelection();
+                }
+            });
+            if (!allowOverrideShowTaxonAssociation){
+                showTaxonAssociationButton.setEnabled(false);
+                allowOverrideIsShowTaxonAssociationButton.setEnabled(false);
+            }
+        }
+
+        final Button showLifeFormButton = new Button(composite, SWT.CHECK);
+        showLifeFormButton.setText(Messages.DatabasePreferncesPage_Life_Form);
+        showLifeFormButton.setSelection(isShowLifeForm);
+        showLifeFormButton.addSelectionListener(new SelectionAdapter(){
+             @Override
+             public void widgetSelected(SelectionEvent e) {
+                 isShowLifeForm = showLifeFormButton.getSelection();
+
+              }
+         });
+
+        Button allowOverrideShowLifeFormButton = createAllowOverrideButton(composite);
+        if (isAdminPreference){
+            allowOverrideShowLifeFormButton.setSelection(allowOverrideShowLifeForm);
+            allowOverrideShowLifeFormButton.addSelectionListener(new SelectionAdapter(){
+                @Override
+                public void widgetSelected(SelectionEvent e) {
+                    allowOverrideShowLifeForm = allowOverrideShowLifeFormButton.getSelection();
+                }
+            });
+        }else{
+            allowOverrideShowLifeFormButton.setSelection(overrideShowLifeForm);
+            allowOverrideShowLifeFormButton.addSelectionListener(new SelectionAdapter(){
+                @Override
+                public void widgetSelected(SelectionEvent e) {
+                    overrideShowLifeForm = allowOverrideShowLifeFormButton.getSelection();
+                }
+            });
+            if (!allowOverrideShowLifeForm){
+                showLifeFormButton.setEnabled(false);
+                allowOverrideShowLifeFormButton.setEnabled(false);
+            }
+        }
+
+        if (!isEditingAllowed){
+            PreferencesUtil.recursiveSetEnabled(composite, false);
+        }
+        return composite;
     }
 
+
+    /**
+     * {@inheritDoc}
+     */
     @Override
-    protected void createFieldEditors() {
-        if (getPreferenceStore().getBoolean(IPreferenceKeys.ALLOW_OVERRIDE_SPECIMEN_PREF)) {
-            addField(new BooleanFieldEditor(IPreferenceKeys.DETERMINATION_ONLY_FOR_FIELD_UNITS,
-                    "Taxon determination only on FieldUnit level",
-                    getFieldEditorParent()));
-            addField(new BooleanFieldEditor(IPreferenceKeys.SHOW_COLLECTING_AREAS_IN_GENERAL_SECTION,
-                    "Show \"Collecting Areas\" in \"General\" section of details view",
-                    getFieldEditorParent()));
-            addField(new BooleanFieldEditor(IPreferenceKeys.SHOW_TAXON_ASSOCIATIONS,
-                    "Show taxon associations of a specimen in the details view",
-                    getFieldEditorParent()));
-            addField(new BooleanFieldEditor(IPreferenceKeys.SHOW_LIFE_FORM,
-                    "Show taxon associations of a specimen in the details view",
-                    getFieldEditorParent()));
-            Label label = new Label(getFieldEditorParent(), SWT.NONE);
-
-            label.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 1, 1));
-
-            addField(new BooleanFieldEditor(
-                    IPreferenceKeys.OVERRIDE_SPECIMEN_PREF,
-                    "Use local specimen preferences",
-                    getFieldEditorParent()));
+    protected void getValues() {
+        CdmPreferenceCache cache = CdmPreferenceCache.instance();
 
+        isShowSpecimenRelatedIssues = PreferencesUtil.getBooleanValue(EditorPreferencePredicate.ShowSpecimen.getKey());
+        allowOverrideShowSpecimenRelatedIssues = cache.get(EditorPreferencePredicate.ShowSpecimen.getKey())!= null ? cache.get(EditorPreferencePredicate.ShowSpecimen.getKey()).isAllowOverride():true;
 
-        }
+        isShowCollectingAreaInGeneralSection = PreferencesUtil.getBooleanValue(EditorPreferencePredicate.ShowCollectingAreasInGeneralSection.getKey());
+        allowOverrideShowCollectingAreaInGeneralSection = cache.get(EditorPreferencePredicate.ShowCollectingAreasInGeneralSection.getKey())!= null ? cache.get(EditorPreferencePredicate.ShowCollectingAreasInGeneralSection.getKey()).isAllowOverride():true;
+
+        isDeterminationOnlyForFieldUnits = PreferencesUtil.getBooleanValue(EditorPreferencePredicate.DeterminationOnlyForFieldUnits.getKey());
+        allowOverrideDeterminationOnlyForFieldUnits = cache.get(EditorPreferencePredicate.DeterminationOnlyForFieldUnits.getKey())!= null ? cache.get(EditorPreferencePredicate.DeterminationOnlyForFieldUnits.getKey()).isAllowOverride():true;
+
+        isShowTaxonAssociation = PreferencesUtil.getBooleanValue(EditorPreferencePredicate.ShowTaxonAssociations.getKey());
+        allowOverrideShowTaxonAssociation = cache.get(EditorPreferencePredicate.ShowTaxonAssociations.getKey())!= null ? cache.get(EditorPreferencePredicate.ShowTaxonAssociations.getKey()).isAllowOverride():true;
+
+        isShowLifeForm = PreferencesUtil.getBooleanValue(EditorPreferencePredicate.ShowLifeForm.getKey());
+        allowOverrideShowLifeForm = cache.get(EditorPreferencePredicate.ShowLifeForm.getKey())!= null ? cache.get(EditorPreferencePredicate.ShowLifeForm.getKey()).isAllowOverride():true;
+
+
+    }
 
+    @Override
+    public boolean performOk() {
+        if (allowOverrideShowSpecimenRelatedIssues){
+            PreferencesUtil.setBooleanValue(EditorPreferencePredicate.ShowSpecimen.getKey(), isShowSpecimenRelatedIssues);
+        }
+        if (allowOverrideShowCollectingAreaInGeneralSection){
+            PreferencesUtil.setBooleanValue(EditorPreferencePredicate.ShowCollectingAreasInGeneralSection.getKey(), isShowCollectingAreaInGeneralSection);
+        }
+        if (allowOverrideDeterminationOnlyForFieldUnits){
+            PreferencesUtil.setBooleanValue(EditorPreferencePredicate.DeterminationOnlyForFieldUnits.getKey(), isDeterminationOnlyForFieldUnits);
+        }
+        if (allowOverrideShowTaxonAssociation){
+            PreferencesUtil.setBooleanValue(EditorPreferencePredicate.ShowTaxonAssociations.getKey(), isShowTaxonAssociation);
+        }
+        if (allowOverrideShowLifeForm){
+            PreferencesUtil.setBooleanValue(EditorPreferencePredicate.ShowLifeForm.getKey(), isShowLifeForm);
+        }
+        return true;
     }