merge-update from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / PreferencesUtil.java
index 3eec36ec209fcd64034b4a86045f643bcd51506f..484a133579ee30ddfa7fe9602f0cc232ceb9c900 100644 (file)
@@ -9,17 +9,27 @@
 
 package eu.etaxonomy.taxeditor.preference;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.Set;
+import java.util.StringTokenizer;
 import java.util.UUID;
 
-import org.eclipse.jface.dialogs.Dialog;
-import org.eclipse.jface.dialogs.MessageDialog;
+import org.apache.commons.lang.StringUtils;
+import org.eclipse.equinox.internal.p2.ui.model.MetadataRepositoryElement;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.PlatformUI;
 
 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeConfigurator;
 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
@@ -40,10 +50,12 @@ 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.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;
 
 /**
  * <p>
@@ -62,6 +74,9 @@ public class PreferencesUtil implements IPreferenceKeys {
         */
        public static final String PREFERRED_TERMS_CHANGE = "preferred_terms";
 
+       public static final String P2_REPOSITORIES_DELIM = ",";
+       public static final String P2_REPOSITORY_FIELDS_DELIM = ";";
+
        /**
         * <p>
         * getPreferenceStore
@@ -107,6 +122,14 @@ public class PreferencesUtil implements IPreferenceKeys {
                return null;
        }
 
+       public static String getPreferredDefaultLangugae(){
+           String preferredLanguage = getPreferenceStore().getString(DEFAULT_LANGUAGE_EDITOR);
+           if(StringUtils.isNotEmpty(preferredLanguage) && StringUtils.isNotBlank(preferredLanguage)){
+               return preferredLanguage;
+           }
+           return null;
+       }
+
        /**
         * Get the match strategy for the given class that was stored in preferences
         * or the default strategy if it was not stored in preferences
@@ -127,7 +150,7 @@ public class PreferencesUtil implements IPreferenceKeys {
                                try {
                                        matchStrategy.setMatchMode(fieldName, matchMode);
                                } catch (MatchException e) {
-                                       StoreUtil.error(PreferencesUtil.class, e);
+                                       MessagingUtils.error(PreferencesUtil.class, e);
                                        throw new RuntimeException(e);
                                }
                        }
@@ -226,7 +249,7 @@ public class PreferencesUtil implements IPreferenceKeys {
                String key = cdmBase.getClass().getName().concat(".")
                                .concat(cdmBase.getUuid().toString());
                if (key.contains("javassist")) {
-                       StoreUtil.info("proxy");
+                       MessagingUtils.info("proxy");
                }
                return key;
        }
@@ -242,7 +265,7 @@ public class PreferencesUtil implements IPreferenceKeys {
                String key = simpleTerm.getClass().getName().concat(".")
                                .concat(simpleTerm.getUuid().toString());
                if (key.contains("javassist")) {
-                       StoreUtil.warn(PreferencesUtil.class,
+                       MessagingUtils.warn(PreferencesUtil.class,
                                        "Trying to persist a preference based on a proxy class.");
                }
                return key;
@@ -261,7 +284,7 @@ public class PreferencesUtil implements IPreferenceKeys {
                String key = definedTerm.getClass().getName().concat(".")
                                .concat(definedTerm.getUuid().toString());
                if (key.contains("javassist")) {
-                       StoreUtil.warn(PreferencesUtil.class,
+                       MessagingUtils.warn(PreferencesUtil.class,
                                        "Trying to persist a preference based on a proxy class.");
                }
                return key;
@@ -378,13 +401,17 @@ public class PreferencesUtil implements IPreferenceKeys {
         */
        public static void checkNomenclaturalCode() {
                // First time Editor is opened, no nomenclatural code has been set
+
+
                if (PreferencesUtil.getPreferredNomenclaturalCode() == null) {
+                       PreferencesUtil.setPreferredNomenclaturalCode(NomenclaturalCode.ICNAFP);
+                       /*
 
                        StoreUtil.info("No nomencatural code set.");
 
                        Shell shell = StoreUtil.getShell();
 
-                       // Query user re: preferred nom. code
+                Query user re: preferred nom. code
                        Dialog dialog = new InitNomenclaturalCodePrefDialog(shell);
                        dialog.open();
 
@@ -398,10 +425,23 @@ public class PreferencesUtil implements IPreferenceKeys {
                                                        "The following has been set as your preferred nomenclatural code:\n\n\t"
                                                                        + NomenclaturalCodeHelper
                                                                                        .getDescription(code)
-                                                                       + "\n\nYou 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();
+              int open = new DefaultLanguageDialog(shell).open();
+              if(open == 0){//FIXME:window performed ok. Find variable for it
+                  PlatformUI.getWorkbench().restart();
+              }
+           }else{
+               //TODO:In case of a reinstall, the config.ini will be overwritten
+               //     here you create config.ini with the stored key from preferences
+           }
+       }
+
        /**
         * <p>
         * getMapServiceAccessPoint
@@ -486,8 +526,8 @@ public class PreferencesUtil implements IPreferenceKeys {
                String languageUuidString = getPreferenceStore().getString(
                                GLOBAL_LANGUAGE_UUID);
 
-               if (CdmUtils.isEmpty(languageUuidString)) {
-                       return Language.DEFAULT();
+               if (CdmUtils.isBlank(languageUuidString)) {
+                       return Language.getDefaultLanguage();
                }
 
                UUID languageUuid = UUID.fromString(languageUuidString);
@@ -568,4 +608,113 @@ public class PreferencesUtil implements IPreferenceKeys {
                return configurator;
        }
 
+       /**
+        * This method will write language properties to the config.ini located in the configuration folder
+        * of the Taxonomic Ediitor. <b>This method is only used to set the default language for Taxonomic Editor.</b>
+        *
+        * @param setLanguage 0 is for german and 1 for english.
+        * @throws IOException
+        */
+    public void writePropertyToConfigFile(int setLanguage) throws IOException {
+        File file = org.eclipse.core.runtime.preferences.ConfigurationScope.INSTANCE.getLocation().toFile();
+        Properties properties = load(file.getAbsolutePath()+"/config.ini");
+        switch(setLanguage){
+        case 0:
+            properties.setProperty("osgi.nl", "de");
+            PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "de");
+            break;
+        case 1:
+            properties.setProperty("osgi.nl", "en");
+            PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "en");
+            break;
+        default:
+            break;
+        }
+        save(file+"/config.ini", properties);
+    }
+
+    /**
+     * This method loads a property from a given file and returns it.
+     *
+     * @param filename
+     * @return
+     * @throws IOException
+     */
+    private Properties load(String filename) throws IOException {
+        FileInputStream in = new FileInputStream(filename);
+        Properties prop = new Properties();
+        prop.load(in);
+        in.close();
+        return prop;
+    }
+
+    /**
+     * This method saves a property to the specified file.
+     *
+     * @param filename
+     * @param properties
+     * @throws IOException
+     */
+    private void save(String filename, Properties properties) throws IOException{
+        FileOutputStream fos =  new FileOutputStream(filename);
+        properties.store(fos, "");
+        fos.close();
+    }
+
+    /**
+     * Saves a list of P2 Metadata Repositories as string with specified delimiters
+     *
+     * @param p2Repos
+     */
+    public static void setP2Repositories(List<MetadataRepositoryElement> p2Repos) {
+        StringBuilder sb = new StringBuilder();
+        for(MetadataRepositoryElement p2Repo : p2Repos) {
+            sb.append(P2_REPOSITORIES_DELIM);
+            if(p2Repo.getName() == null || p2Repo.getName().isEmpty()) {
+                sb.append("-");
+            } else {
+                sb.append(p2Repo.getName());
+            }
+            sb.append(P2_REPOSITORY_FIELDS_DELIM);
+            sb.append(p2Repo.getLocation().toString());
+            sb.append(P2_REPOSITORY_FIELDS_DELIM);
+            sb.append(String.valueOf(p2Repo.isEnabled()));
+        }
+        getPreferenceStore().setValue(P2_REPOSITORY_LIST, sb.toString());
+    }
+
+
+    /**
+     * Retrieves a list of previously saved P2 repositories
+     *
+     * @return
+     */
+    public static List<MetadataRepositoryElement> getP2Repositories() {
+        List<MetadataRepositoryElement> p2Repos = new ArrayList<MetadataRepositoryElement>();
+        String p2ReposPref =  getPreferenceStore().getString(P2_REPOSITORY_LIST);
+        if(p2ReposPref != null && !p2ReposPref.isEmpty()) {
+            StringTokenizer p2ReposPrefST = new StringTokenizer(p2ReposPref,P2_REPOSITORIES_DELIM);
+
+            while(p2ReposPrefST.hasMoreTokens()) {
+                String p2RepoStr = p2ReposPrefST.nextToken();
+                StringTokenizer p2ReposStrST = new StringTokenizer(p2RepoStr,P2_REPOSITORY_FIELDS_DELIM);
+                if(p2ReposStrST.countTokens()==3) {
+                    String nickname = p2ReposStrST.nextToken();
+                    URI uri = null;
+                    try {
+                        uri = new URI(p2ReposStrST.nextToken());
+                    } catch (URISyntaxException e) {
+                        continue;
+                    }
+                    boolean enabled = Boolean.parseBoolean(p2ReposStrST.nextToken());
+                    MetadataRepositoryElement mre = new MetadataRepositoryElement(null, uri, true);
+                    mre.setNickname(nickname);
+                    mre.setEnabled(enabled);
+                    p2Repos.add(mre);
+                }
+            }
+        }
+
+        return p2Repos;
+    }
 }