ref #5969: check for db connection before ty to read the preferred nomenclatural...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / PreferencesUtil.java
index 7493ff0b911d77a987b196cfa5b22ebe9aadfd3b..e7eb2a4d1479a76c7ed3f1ebf670931871aadc13 100644 (file)
@@ -28,11 +28,16 @@ import java.util.UUID;
 import org.apache.commons.lang.StringUtils;
 import org.eclipse.equinox.internal.p2.ui.model.MetadataRepositoryElement;
 import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.jface.window.Window;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.PlatformUI;
 
+import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeConfigurator;
 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
+import eu.etaxonomy.cdm.api.service.IPreferenceService;
 import eu.etaxonomy.cdm.api.service.ITermService;
 import eu.etaxonomy.cdm.api.service.config.FindTaxaAndNamesConfiguratorImpl;
 import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
@@ -45,15 +50,19 @@ import eu.etaxonomy.cdm.model.common.Language;
 import eu.etaxonomy.cdm.model.common.MarkerType;
 import eu.etaxonomy.cdm.model.common.TermBase;
 import eu.etaxonomy.cdm.model.description.FeatureTree;
+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.cdm.strategy.match.DefaultMatchStrategy;
 import eu.etaxonomy.cdm.strategy.match.IMatchStrategy;
 import eu.etaxonomy.cdm.strategy.match.MatchException;
 import eu.etaxonomy.cdm.strategy.match.MatchMode;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
 import eu.etaxonomy.taxeditor.store.CdmStore;
-import eu.etaxonomy.taxeditor.store.StoreUtil;
 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 import eu.etaxonomy.taxeditor.ui.dialog.DefaultLanguageDialog;
 
@@ -77,6 +86,8 @@ public class PreferencesUtil implements IPreferenceKeys {
        public static final String P2_REPOSITORIES_DELIM = ",";
        public static final String P2_REPOSITORY_FIELDS_DELIM = ";";
 
+
+
        /**
         * <p>
         * getPreferenceStore
@@ -99,10 +110,36 @@ public class PreferencesUtil implements IPreferenceKeys {
         */
        public static void setPreferredNomenclaturalCode(
                        NomenclaturalCode preferredCode) {
+               ICdmApplicationConfiguration controller;
+               controller = CdmStore.getCurrentApplicationConfiguration();
+               PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
+               CdmPreference preference = null;
+               if (controller == null){
+                       return;
+               }
+               if (preferredCode == null){
+                       preference = controller.getPreferenceService().find(key);
+                       if (preference == null){
+                               return;
+                       } else{
+                               int index = StringUtils.lastIndexOf(preference.getValue(), ".");
+                               UUID uuid = UUID.fromString(preference.getValue().substring(index +1, preference.getValue().length()));
+                               preferredCode = NomenclaturalCode.getByUuid(uuid);
+                       }
+               } else{
+                       preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode, preferredCode.getKey());
+                       controller.getPreferenceService().set(preference);
+               }
+                
+           
                getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
-                               getPreferenceKey(preferredCode));
+                               getPreferenceKey(preferredCode));
        }
 
+       public static NomenclaturalCode getPreferredNomenclaturalCode(){
+               return getPreferredNomenclaturalCode(false);
+       }
+       
        /**
         * <p>
         * getPreferredNomenclaturalCode
@@ -110,11 +147,30 @@ public class PreferencesUtil implements IPreferenceKeys {
         *
         * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
         */
-       public static NomenclaturalCode getPreferredNomenclaturalCode() {
-
-               for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
-                       String preferredCode = getPreferenceStore().getString(
+       public static NomenclaturalCode getPreferredNomenclaturalCode(boolean preConnected) {
+               ICdmApplicationConfiguration controller;
+               CdmPreference pref = null;
+               if (!preConnected){
+                       try{
+                               controller = CdmStore.getCurrentApplicationConfiguration();
+                               PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
+                           pref = controller.getPreferenceService().find(key);
+                       }catch(Exception e){
+                               e.printStackTrace();
+                       }
+               }
+               
+           String preferredCode;
+           if(pref == null){
+               preferredCode = getPreferenceStore().getString(
                                        PREFERRED_NOMENCLATURAL_CODE_KEY);
+           }else{
+               preferredCode = pref.getValue();
+           }
+           
+               for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
+//                     String preferredCode = getPreferenceStore().getString(
+//                                     PREFERRED_NOMENCLATURAL_CODE_KEY);
                        if (getPreferenceKey(code).equals(preferredCode)) {
                                return code;
                        }
@@ -244,7 +300,7 @@ public class PreferencesUtil implements IPreferenceKeys {
         * @return
         */
        private static String getPreferenceKey(ICdmBase cdmBase) {
-               cdmBase = (ICdmBase) HibernateProxyHelper.deproxy(cdmBase);
+               cdmBase = HibernateProxyHelper.deproxy(cdmBase);
 
                String key = cdmBase.getClass().getName().concat(".")
                                .concat(cdmBase.getUuid().toString());
@@ -261,7 +317,7 @@ public class PreferencesUtil implements IPreferenceKeys {
         * @return
         */
        public static String getPreferenceKey(ISimpleTerm simpleTerm) {
-               simpleTerm = (ISimpleTerm) HibernateProxyHelper.deproxy(simpleTerm);
+               simpleTerm = HibernateProxyHelper.deproxy(simpleTerm);
                String key = simpleTerm.getClass().getName().concat(".")
                                .concat(simpleTerm.getUuid().toString());
                if (key.contains("javassist")) {
@@ -280,7 +336,7 @@ public class PreferencesUtil implements IPreferenceKeys {
         * @return
         */
        public static String getPreferenceKey(IDefinedTerm definedTerm) {
-               definedTerm = (IDefinedTerm) HibernateProxyHelper.deproxy(definedTerm);
+               definedTerm = HibernateProxyHelper.deproxy(definedTerm);
                String key = definedTerm.getClass().getName().concat(".")
                                .concat(definedTerm.getUuid().toString());
                if (key.contains("javassist")) {
@@ -306,6 +362,7 @@ public class PreferencesUtil implements IPreferenceKeys {
                                TAXON_SERVICE_CONFIGURATOR_NAMES));
                configurator.setDoTaxaByCommonNames(getPreferenceStore().getBoolean(
                                TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES));
+               //configurator.setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.valueOf(getPreferenceStore().getString(TAXON_SERVICE_CONFIGURATOR_MATCH_MODE)));
 
                return configurator;
        }
@@ -392,7 +449,8 @@ public class PreferencesUtil implements IPreferenceKeys {
                                "http://www.biodiversitylibrary.org/openurl");
                getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_WIDTH, "1000");
                getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_HEIGHT, "1000");
-               getPreferenceStore().setDefault(IPreferenceKeys.DISTRIBUTION_AREA_PREFRENCES_ACTIVE, true);
+               getPreferenceStore().setDefault(IPreferenceKeys.DISTRIBUTION_AREA_PREFRENCES_ACTIVE, false);
+               getPreferenceStore().setDefault(CHECKLIST_ID_IN_VOCABULARY, true);
        }
 
        /**
@@ -404,7 +462,7 @@ public class PreferencesUtil implements IPreferenceKeys {
                // First time Editor is opened, no nomenclatural code has been set
 
 
-               if (PreferencesUtil.getPreferredNomenclaturalCode() == null) {
+               if (PreferencesUtil.getPreferredNomenclaturalCode(true) == null) {
                        PreferencesUtil.setPreferredNomenclaturalCode(NomenclaturalCode.ICNAFP);
                        /*
 
@@ -426,15 +484,15 @@ public class PreferencesUtil implements IPreferenceKeys {
                                                        "The following has been set as your preferred nomenclatural code:\n\n\t"
                                                                        + NomenclaturalCodeHelper
                                                                                        .getDescription(code)
-                                                                       + "\n\nYou iour is not consistent throughout the application. For DescriptionElemts and also PolytomousKeyNode.Question and PolytomousKeyNode.Statement it shows only the languages where text is available whereas for PolytomousKeyNode.modyfingText it shows a strange list of about 10-20 (different for different nodes) most of them starting with can change the nomenclatural code at any time in the \"Preferences\" menu.");*/
+                                                                       + "\n\nYou can change the nomenclatural code at any time in the \"Preferences\" menu.");*/
                }
        }
 
        public static void checkDefaultLanguage(){
            if(PreferencesUtil.getPreferredDefaultLangugae() == null){
-              Shell shell = StoreUtil.getShell();
+              Shell shell = AbstractUtility.getShell();
               int open = new DefaultLanguageDialog(shell).open();
-              if(open == 0){//FIXME:window performed ok. Find variable for it
+              if(open == Window.OK){
                   PlatformUI.getWorkbench().restart();
               }
            }else{
@@ -585,7 +643,7 @@ public class PreferencesUtil implements IPreferenceKeys {
 
        private static String getMarkerTypeEditingPreferenceKey(
                        MarkerType markerType) {
-               markerType = (MarkerType) HibernateProxyHelper.deproxy(markerType);
+               markerType = HibernateProxyHelper.deproxy(markerType);
                return markerType.getClass().getName() + EDIT_MARKER_TYPE_PREFIX;
        }
 
@@ -627,6 +685,10 @@ public class PreferencesUtil implements IPreferenceKeys {
         */
     public void writePropertyToConfigFile(int setLanguage) throws IOException {
         File file = org.eclipse.core.runtime.preferences.ConfigurationScope.INSTANCE.getLocation().toFile();
+        //give warning to user if the directory has no write access
+        if(file == null){
+            throw new IOException();
+        }
         Properties properties = load(file.getAbsolutePath()+"/config.ini");
         switch(setLanguage){
         case 0:
@@ -727,4 +789,125 @@ public class PreferencesUtil implements IPreferenceKeys {
 
         return p2Repos;
     }
+
+    /**
+     * enables/disables nested composite. <br>
+     *
+     * @param ctrl - Composite to be en-/disabeld
+     * @param enabled - boolean
+     */
+    public static void recursiveSetEnabled(Control ctrl, boolean enabled) {
+        if (ctrl instanceof Composite) {
+            Composite comp = (Composite) ctrl;
+            for (Control c : comp.getChildren()) {
+                recursiveSetEnabled(c, enabled);
+            }
+        } else {
+            ctrl.setEnabled(enabled);
+        }
+    }
+
+    /**
+        * <p>
+        * getSortRanksNaturally
+        * </p>
+        *
+        * @return a boolean.
+        */
+       public static boolean getSortNodesNaturally() {
+               return getPreferenceStore().getBoolean(SORT_NODES_NATURALLY);
+       }
+
+       /**
+        * <p>
+        * setSortRanksNaturally
+        * </p>
+        *
+        * @param selection
+        *            a boolean.
+        */
+       public static void setSortNodesNaturally(boolean selection) {
+               getPreferenceStore().setValue(SORT_NODES_NATURALLY, selection);
+       }
+
+
+       /**
+        * <p>
+        * getSortRanksNaturally
+        * </p>
+        *
+        * @return a boolean.
+        */
+       public static boolean getSortNodesStrictlyAlphabetically() {
+               return getPreferenceStore().getBoolean(SORT_NODES_ALPHABETICALLY);
+       }
+
+       /**
+        * <p>
+        * setSortRanksNaturally
+        * </p>
+        *
+        * @param selection
+        *            a boolean.
+        */
+       public static void setSortNodesStrictlyAlphabetically(boolean selection) {
+               getPreferenceStore().setValue(SORT_NODES_ALPHABETICALLY, selection);
+       }
+
+       /**
+        * <p>
+        * setStoreNavigatorState
+        * </p>
+        *
+        * @param selection
+        *            a boolean.
+        */
+       public static boolean isStoreNavigatorState() {
+               return getPreferenceStore().getBoolean(RESTORE_NAVIGATOR_STATE);
+
+       }
+
+       /**
+        * <p>
+        * setStoreNavigatorState
+        * </p>
+        *
+        * @param selection
+        *            a boolean.
+        */
+       public static void setStoreNavigatorState(boolean selection) {
+               getPreferenceStore().setValue(RESTORE_NAVIGATOR_STATE, selection);
+
+       }
+
+    /**
+     * @return
+     */
+    public static boolean isShowUpWidgetIsDisposedMessages() {
+       return getPreferenceStore().getBoolean(IS_SHOW_UP_WIDGET_IS_DISPOSED);
+    }
+    public static void setShowUpWidgetIsDisposedMessages(boolean selection) {
+        getPreferenceStore().setValue(IS_SHOW_UP_WIDGET_IS_DISPOSED, selection);
+    }
+
+    /**
+     * @return
+     */
+    public static boolean isShowIdInVocabularyInChecklistEditor() {
+       return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_ID_IN_VOCABULARY);
+    }
+    public static void setShowIdInVocabularyInChecklistEditor(boolean selection) {
+        getPreferenceStore().setValue(CHECKLIST_ID_IN_VOCABULARY, selection);
+    }
+
+    /**
+     * @return
+     */
+    public static boolean isShowRankInChecklistEditor() {
+        return getPreferenceStore().getBoolean(IPreferenceKeys.CHECKLIST_SHOW_RANK);
+    }
+    public static void setShowRankInChecklistEditor(boolean selection) {
+        getPreferenceStore().setValue(CHECKLIST_SHOW_RANK, selection);
+    }
+
 }