refactored default language selection methods.
authorAlexander Oppermann <a.oppermann@bgbm.org>
Mon, 25 Aug 2014 06:45:26 +0000 (06:45 +0000)
committerAlexander Oppermann <a.oppermann@bgbm.org>
Mon, 25 Aug 2014 06:45:26 +0000 (06:45 +0000)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/preference/DefaultLanguageEditorPreferencePage.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/preference/PreferencesUtil.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/DefaultLanguageDialog.java

index 99a719961515d78cb4f0da88f55c9d3d0efa4e65..9b76bf99d82d3204966a7a473e725cf1fdbe1afc 100644 (file)
 
 package eu.etaxonomy.taxeditor.preference;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.util.Properties;
 
 import org.apache.commons.lang.StringUtils;
 import org.eclipse.jface.preference.PreferencePage;
@@ -24,7 +20,6 @@ import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.layout.RowLayout;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Label;
@@ -39,13 +34,7 @@ import org.eclipse.ui.IWorkbenchPreferencePage;
 public class DefaultLanguageEditorPreferencePage extends PreferencePage implements IWorkbenchPreferencePage{
 
     private CCombo combo;
-
-       private Composite createComposite(Composite parent){
-               Composite composite = new Composite(parent, SWT.NULL);
-               composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1));
-               composite.setLayout(new RowLayout(SWT.HORIZONTAL));
-               return composite;
-       }
+    PreferencesUtil preferencesUtil = new PreferencesUtil();
 
        /* (non-Javadoc)
         * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
@@ -83,7 +72,7 @@ public class DefaultLanguageEditorPreferencePage extends PreferencePage implemen
             @Override
             public void widgetSelected(SelectionEvent e) {
                 try {
-                    writeConfigAndRestart(combo.getSelectionIndex());
+                    preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
                 } catch (IOException e1) {
                     e1.printStackTrace();
                 }
@@ -127,38 +116,6 @@ public class DefaultLanguageEditorPreferencePage extends PreferencePage implemen
         }
     }
 
-    private void writeConfigAndRestart(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);
-    }
-
-    private Properties load(String filename) throws IOException {
-        FileInputStream in = new FileInputStream(filename);
-        Properties prop = new Properties();
-        prop.load(in);
-        in.close();
-        return prop;
-    }
-
-    private void save(String filename, Properties properties) throws IOException{
-        FileOutputStream fos =  new FileOutputStream(filename);
-        properties.store(fos, "");
-        fos.close();
-    }
-
        /* (non-Javadoc)
         * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
         */
@@ -173,7 +130,7 @@ public class DefaultLanguageEditorPreferencePage extends PreferencePage implemen
        @Override
        public boolean performOk() {
         try {
-            writeConfigAndRestart(combo.getSelectionIndex());
+            preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
         } catch (IOException e) {
             e.printStackTrace();
         }
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();
+    }
+
 }
index 1f836ce04dacf84534796387b4cc0eba7e4bf6a0..f2549c44dacd8b155babbe1cd866eb9231cbf6d6 100644 (file)
@@ -9,11 +9,7 @@
 */
 package eu.etaxonomy.taxeditor.ui.dialog;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.util.Properties;
 
 import org.eclipse.jface.dialogs.IMessageProvider;
 import org.eclipse.jface.dialogs.TitleAreaDialog;
@@ -28,7 +24,6 @@ import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Shell;
 
-import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 
 
@@ -41,6 +36,8 @@ public class DefaultLanguageDialog extends TitleAreaDialog{
 
 
     private CCombo combo;
+    final PreferencesUtil preferencesUtil = new PreferencesUtil();
+
     /**
      * @param parentShell
      */
@@ -79,9 +76,9 @@ public class DefaultLanguageDialog extends TitleAreaDialog{
         return composite;
     }
 
-
     /**
      *
+     * @param parent
      */
     private void createComboElement(Composite parent) {
         Composite container1 = new Composite(parent, SWT.NONE);
@@ -110,7 +107,7 @@ public class DefaultLanguageDialog extends TitleAreaDialog{
             @Override
             public void widgetSelected(SelectionEvent e) {
                 try {
-                    writeConfigAndRestart(combo.getSelectionIndex());
+                    preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
                 } catch (IOException e1) {
                     e1.printStackTrace();
                 }
@@ -118,53 +115,18 @@ public class DefaultLanguageDialog extends TitleAreaDialog{
 
             @Override
             public void widgetDefaultSelected(SelectionEvent e) {
-                // TODO Auto-generated method stub
-
             }
         });
     }
 
     @Override
     protected void okPressed() {
-      try {
-        writeConfigAndRestart(combo.getSelectionIndex());
-    } catch (IOException e) {
-        e.printStackTrace();
-    }
-      super.okPressed();
-    }
-
-
-    private void writeConfigAndRestart(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;
+        try {
+            preferencesUtil.writePropertyToConfigFile(combo.getSelectionIndex());
+        } catch (IOException e) {
+            e.printStackTrace();
         }
-        save(file+"/config.ini", properties);
-    }
-
-    private Properties load(String filename) throws IOException {
-        FileInputStream in = new FileInputStream(filename);
-        Properties prop = new Properties();
-        prop.load(in);
-        in.close();
-        return prop;
-    }
-
-    private void save(String filename, Properties properties) throws IOException{
-        FileOutputStream fos =  new FileOutputStream(filename);
-        properties.store(fos, "");
-        fos.close();
+        super.okPressed();
     }
 
     private enum Language{
@@ -182,14 +144,4 @@ public class DefaultLanguageDialog extends TitleAreaDialog{
             return label;
         }
     }
-    private static final GridLayout GRID_LAYOUT (int columns, boolean equalwidth){
-        GridLayout layout = new GridLayout();
-        layout.marginTop = 0;
-        layout.marginRight = 0;
-        layout.marginBottom = 0;
-        layout.marginLeft = 0;
-        layout.numColumns = columns;
-        layout.makeColumnsEqualWidth = equalwidth;
-        return layout;
-    }
 }