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 9c9bd5aee17d34aaf503280f48c7394b1a51b4bd..6e5da1f2c2faa3ece439838e81accb4535f57bab 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
 * Copyright (C) 2014 EDIT
 * European Distributed Institute of Taxonomy
 */
 package eu.etaxonomy.taxeditor.preference;
 
-import org.eclipse.jface.preference.BooleanFieldEditor;
-import org.eclipse.jface.preference.FieldEditorPreferencePage;
-import org.eclipse.ui.IWorkbench;
-import org.eclipse.ui.IWorkbenchPreferencePage;
+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.model.metadata.CdmPreference;
+import eu.etaxonomy.taxeditor.l10n.Messages;
+import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
+import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
  * @author pplitzner
  * @date 13.02.2014
  *
  */
-public class SpecimenOrObservationPreferences extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
+public class SpecimenOrObservationPreferences extends CdmPreferencePage {
+
+    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
+    public void init() {
+        super.init();
+//        setPreferenceStore(PreferencesUtil.getPreferenceStore());
+        if(!CdmStore.isActive()){
+            return;
+        }
+
 
-    /* (non-Javadoc)
-     * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
+    }
+
+    /**
+     * {@inheritDoc}
      */
     @Override
-    public void init(IWorkbench workbench) {
-        setPreferenceStore(PreferencesUtil.getPreferenceStore());
+    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{
+            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;
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.jface.preference.FieldEditorPreferencePage#createFieldEditors()
+
+    /**
+     * {@inheritDoc}
      */
     @Override
-    protected void createFieldEditors() {
-        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()));
+    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;
     }