refactored default language selection methods.
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / PreferencesUtil.java
index 095623cc49d7296ebf4b53d0c45b2211a8ebd88e..a2df5b14019443544e4af0cbefa9d2bab40f0abf 100644 (file)
@@ -9,10 +9,15 @@
 
 package eu.etaxonomy.taxeditor.preference;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
 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.UUID;
 
@@ -595,4 +600,57 @@ 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();
+    }
+
 }