Drastically reducing code by using field editors for preferences
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / NomenclaturalCodePreferences.java
index bde2e26f4d1e06e7877f0c1218fc00a78b697864..fd3b21782651b73a7abb12e5aa8af18fa1e0a94a 100644 (file)
@@ -1,24 +1,17 @@
 /**
-* Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy 
-* http://www.e-taxonomy.eu
-* 
-* The contents of this file are subject to the Mozilla Public License Version 1.1
-* See LICENSE.TXT at the top of this package for the full license terms.
-*/
+ * Copyright (C) 2007 EDIT
+ * European Distributed Institute of Taxonomy 
+ * http://www.e-taxonomy.eu
+ 
+ * The contents of this file are subject to the Mozilla Public License Version 1.1
+ * See LICENSE.TXT at the top of this package for the full license terms.
+ */
 package eu.etaxonomy.taxeditor.preference;
 
-import java.util.HashMap;
-import java.util.Map;
+import java.util.List;
 
-import org.eclipse.jface.preference.PreferencePage;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.SelectionAdapter;
-import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Button;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Control;
+import org.eclipse.jface.preference.FieldEditorPreferencePage;
+import org.eclipse.jface.preference.RadioGroupFieldEditor;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchPreferencePage;
 
@@ -27,92 +20,59 @@ import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 
 /**
- * <p>NomenclaturalCodePreferences class.</p>
- *
+ * <p>
+ * NomenclaturalCodePreferences class.
+ * </p>
+ * 
  * @author p.ciardelli
  * @created 16.09.2008
  * @version 1.0
  */
-public class NomenclaturalCodePreferences extends PreferencePage implements
-               IWorkbenchPreferencePage {
-
-       /** Constant <code>PLUGIN_ID="eu.etaxonomy.taxeditor.preferences.nome"{trunked}</code> */
-       public static final String PLUGIN_ID = "eu.etaxonomy.taxeditor.preferences.nomenclatural";
-       
-       private NomenclaturalCode preferredCode;
+public class NomenclaturalCodePreferences extends FieldEditorPreferencePage
+               implements IWorkbenchPreferencePage {
 
-       private Map<NomenclaturalCode, Button> buttons = new HashMap<NomenclaturalCode, Button>();
-               
-       /**
-        * <p>Constructor for NomenclaturalCodePreferences.</p>
+       /*
+        * (non-Javadoc)
+        * 
+        * @see
+        * org.eclipse.jface.preference.FieldEditorPreferencePage#createFieldEditors
+        * ()
         */
-       public NomenclaturalCodePreferences() {
-               super();
-               setDescription("Choose which nomenclatural code you would like to use for scientific names unless otherwise specified.");
+       @Override
+       protected void createFieldEditors() {
+
+               addField(new RadioGroupFieldEditor(
+                               IPreferenceKeys.PREFERRED_NOMENCLATURAL_CODE_KEY,
+                               "Available Codes", 1, getLabelAndValues(),
+                               getFieldEditorParent(), true));
+
        }
 
        /**
-        * {@inheritDoc}
-        *
-        * Create contents of the preference page
+        * @return
         */
-       @Override
-       public Control createContents(Composite parent) {
-               
-               Composite container = new Composite(parent, SWT.NULL);
-               container.setLayout(new GridLayout());
-               
-               for (final NomenclaturalCode code : NomenclaturalCodeHelper.getSupportedCodes()) {
-                       Button button = new Button(container, SWT.RADIO);
-                       button.setText(NomenclaturalCodeHelper.getDescription(code));
-                       button.addSelectionListener(new SelectionAdapter() {
-                               public void widgetSelected(SelectionEvent e) {
-                                       preferredCode = code;
-                               }
-                       });
-                       buttons.put(code, button);
-               }
-               
-               preferredCode = PreferencesUtil.getPreferredNomenclaturalCode();
-               if (preferredCode == null) {
-                       preferredCode = NomenclaturalCodeHelper.getDefaultCode();
-                       PreferencesUtil.setPreferredNomenclaturalCode(preferredCode);
+       private String[][] getLabelAndValues() {
+               List<NomenclaturalCode> supportedCodes = NomenclaturalCodeHelper
+                               .getSupportedCodes();
+               String[][] labelAndValues = new String[supportedCodes.size()][2];
+               for (int i = 0; i < supportedCodes.size(); i++) {
+                       labelAndValues[i][0] = NomenclaturalCodeHelper
+                                       .getDescription(supportedCodes.get(i));
+                       labelAndValues[i][1] = PreferencesUtil
+                                       .getPreferenceKey(supportedCodes.get(i));
                }
-               setButton(preferredCode);               
-               
-               return container;
+               return labelAndValues;
        }
 
        /**
         * {@inheritDoc}
-        *
+        * 
         * Initialize the preference page
         */
        public void init(IWorkbench workbench) {
-               setPreferenceStore(TaxeditorStorePlugin.getDefault().getPreferenceStore());
-       }
-       
-       /**
-        * <p>performDefaults</p>
-        */
-       protected void performDefaults() {
-               setButton(NomenclaturalCodeHelper.getDefaultCode());
-       }
-       
-       /**
-        * <p>performOk</p>
-        *
-        * @return a boolean.
-        */
-       public boolean performOk() {
-               PreferencesUtil.setPreferredNomenclaturalCode(preferredCode);
-               return true;
-       }
-       
-       private void setButton(NomenclaturalCode preferredCode) {
-               this.preferredCode = preferredCode;
-               for (NomenclaturalCode code : buttons.keySet()) {
-                       buttons.get(code).setSelection(code.equals(preferredCode));
-               }
+               setPreferenceStore(TaxeditorStorePlugin.getDefault()
+                               .getPreferenceStore());
+               setDescription("Choose which nomenclatural code you would like to use for scientific names unless otherwise specified.");
        }
+
 }