Fixed persistency of Matching preferences #5337
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / matching / AbstractMatchingPreferences.java
index 6d2ddb9bc92792faa37e28d4b6bfc938a0a071ed..be84b00cfff03c850b7222227c69cf3bd5c2fad4 100644 (file)
@@ -17,15 +17,10 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.eclipse.jface.preference.PreferencePage;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.custom.CLabel;
-import org.eclipse.swt.events.SelectionAdapter;
-import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.jface.preference.ComboFieldEditor;
+import org.eclipse.jface.preference.FieldEditorPreferencePage;
 import org.eclipse.swt.widgets.Combo;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Control;
+import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchPreferencePage;
 
 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
@@ -37,13 +32,11 @@ import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
- * <p>Abstract AbstractMatchingPreferences class.</p>
- *
  * @author n.hoffmann
  * @created Jan 22, 2010
  * @version 1.0
  */
-public abstract class AbstractMatchingPreferences<T extends IdentifiableEntity> extends PreferencePage implements
+public abstract class AbstractMatchingPreferences<T extends IdentifiableEntity> extends FieldEditorPreferencePage implements
                IWorkbenchPreferencePage {
 
        /**
@@ -60,96 +53,30 @@ public abstract class AbstractMatchingPreferences<T extends IdentifiableEntity>
 
        protected Map<String, Combo> matchModeCombos = new HashMap<String, Combo>();
 
-       /* (non-Javadoc)
-        * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
+    @Override
+    public void init(IWorkbench workbench) {
+        setPreferenceStore(PreferencesUtil.getPreferenceStore());
+    }
+
+       /**
+        * {@inheritDoc}
         */
-       /** {@inheritDoc} */
        @Override
-       protected Control createContents(Composite parent) {
-               final Composite composite = new Composite(parent, SWT.NULL);
-               GridLayout gridLayout = new GridLayout();
-               gridLayout.numColumns = 2;
-               composite.setLayout(gridLayout);
-
-               if(!CdmStore.isActive()) {
+       protected void createFieldEditors() {
+           if(!CdmStore.isActive()) {
             MessagingUtils.noDataSourceWarningDialog(null);
         }else{
             for(String fieldName : getFieldNames()){
-                createFieldWidget(composite, fieldName);
+                String[][] comboValues = new String[getMatchModeList().size()][2];
+                for (int i=0;i<getMatchModeList().size();i++) {
+                    comboValues[i][0] = getMatchModeList().get(i).name();
+                    comboValues[i][1] = getMatchModeList().get(i).name();
+                }
+                addField(new ComboFieldEditor(this.getClass().getCanonicalName()+fieldName,
+                        fieldName, comboValues,
+                        getFieldEditorParent()));
             }
         }
-
-               return composite;
-       }
-
-       /**
-        * Creates a widget for a field consisting of the label and a combo
-        *
-        * @see {@link #createMatchModeCombo(Composite, String, MatchMode)}
-        * @param composite
-        * @param fieldName
-        */
-       private void createFieldWidget(Composite parent, String fieldName) {
-           CLabel label = new CLabel(parent, SWT.NONE);
-           label.setText(fieldName);
-           MatchMode matchMode = matchStrategy.getMatchMode(fieldName);
-
-           createMatchModeCombo(parent, fieldName, matchMode);
-       }
-
-       /**
-        * Creates a combo for a field with the currently selected match mode for that field preselected
-        *
-        * @param parent
-        * @param matchMode
-        */
-       private void createMatchModeCombo(Composite parent, String fieldName, MatchMode selectedMatchMode) {
-               Combo matchModeCombo = new Combo(parent, SWT.NULL);
-
-               for (MatchMode matchMode : getMatchModeList()) {
-                       matchModeCombo.add(matchMode.name());
-               }
-
-               int index = getMatchModeList().indexOf(selectedMatchMode);
-
-               matchModeCombo.select(index);
-
-               matchModeCombo.addSelectionListener(new MatchModeComboSelectionListener(matchModeCombo, fieldName));
-
-               matchModeCombos.put(fieldName, matchModeCombo);
-       }
-
-       /**
-        * This listener updates the cache strategy when a value was changed in one of the combos
-        *
-        * @author n.hoffmann
-        * @created Jan 28, 2010
-        * @version 1.0
-        */
-       private class MatchModeComboSelectionListener extends SelectionAdapter{
-
-               private final Combo matchModeCombo;
-               private final String fieldName;
-
-               MatchModeComboSelectionListener(Combo matchModeCombo, String fieldName){
-                       this.matchModeCombo = matchModeCombo;
-                       this.fieldName = fieldName;
-               }
-
-               /* (non-Javadoc)
-                * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
-                */
-               @Override
-               public void widgetSelected(SelectionEvent event) {
-                       super.widgetSelected(event);
-                       MatchMode matchMode = getMatchModeList().get(matchModeCombo.getSelectionIndex());
-                       try {
-                               matchStrategy.setMatchMode(fieldName, matchMode);
-                       } catch (MatchException e) {
-                               MessagingUtils.error(this.getClass(), e);
-                               throw new RuntimeException(e);
-                       }
-               }
        }
 
        /**
@@ -202,40 +129,6 @@ public abstract class AbstractMatchingPreferences<T extends IdentifiableEntity>
            return fields;
        }
 
-       /* (non-Javadoc)
-        * @see org.eclipse.jface.preference.PreferencePage#performApply()
-        */
-       /** {@inheritDoc} */
-       @Override
-       protected void performApply() {
-           PreferencesUtil.setMatchStrategy(matchStrategy);
-               super.performApply();
-       }
-
-       /* (non-Javadoc)
-        * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
-        */
-       /** {@inheritDoc} */
-       @Override
-       protected void performDefaults() {
-               try {
-                       // set match strategy to default
-                       matchStrategy = getDefaultMatchStrategy();
-
-                       // set combos to their default values
-                       for(String fieldName : matchModeCombos.keySet()){
-                               Combo combo = matchModeCombos.get(fieldName);
-                               MatchMode matchMode = matchStrategy.getMatchMode(fieldName);
-                               combo.select(matchModeList.indexOf(matchMode));
-                       }
-
-               } catch (MatchException e) {
-                       MessagingUtils.error(this.getClass(), e);
-                       throw new RuntimeException(e);
-               }
-               super.performDefaults();
-       }
-
        /**
         * Returns the default match strategy for the respective class
         *