package com.googlecode.jslint4java.eclipse.ui.preferences;

import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.googlecode.jslint4java.Option;
import com.googlecode.jslint4java.StringArray;
import com.googlecode.jslint4java.eclipse.ui.JSLintUIPlugin;

/**
 * This class represents a preference page that is contributed to the Preferences dialog. By
 * subclassing <samp>FieldEditorPreferencePage</samp>, we can use the field support built into JFace
 * that allows us to create a page that is small and knows how to save, restore and apply itself.
 * <p>
 * This page is used to modify preferences only. They are stored in the preference store that
 * belongs to the main plug-in class. That way, preferences can be accessed directly via the
 * preference store.
 */

public class OptionsPreferencePage extends FieldEditorPreferencePage implements
        IWorkbenchPreferencePage {

    public OptionsPreferencePage() {
        super(GRID);
        setPreferenceStore(JSLintUIPlugin.getDefault().getPreferenceStore());
    }

    /**
     * Creates the field editors. Field editors are abstractions of the common GUI blocks needed to
     * manipulate various types of preferences. Each field editor knows how to save and restore
     * itself.
     */
    @Override
    public void createFieldEditors() {
        for (Option o : Option.values()) {
            FieldEditor ed = null;
            if (o.getType() == Boolean.class) {
                ed = new BooleanFieldEditor(o.getLowerName(), o.getDescription(), getFieldEditorParent());
            } else if (o.getType() == Integer.class ) {
                ed = new IntegerFieldEditor(o.getLowerName(), o.getDescription(), getFieldEditorParent());
            } else if (o.getType() == StringArray.class) {
                ed = new StringFieldEditor(o.getLowerName(), o.getDescription(), getFieldEditorParent());
            }
            if (ed != null) {
                addField(ed);
            }
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
     */
    public void init(IWorkbench workbench) {
    }

}
@@ -53,16 +53,68 @@
 ences;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 import o
@@ -1685,77 +1685,217 @@
     
-for (Option o : Option.values()) %7B%0A            FieldEditor ed = null;
+// Try to order so that booleans come first on their own.%0A        List%3COption%3E bools = new ArrayList%3COption%3E();%0A        List%3COption%3E others = new ArrayList%3COption%3E();%0A        for (Option o : Option.values()) %7B
 %0A   
@@ -1903,19 +1903,16 @@
         
-if 
 (o.getTy
@@ -1936,207 +1936,665 @@
 lass
-) %7B%0A                ed = new BooleanFieldEditor(o.getLowerName(), o.getDescription(), getFieldEditorParent());%0A            %7D else if (o.getType() == Integer.class ) %7B%0A        
+ ? bools : others).add(o);%0A        %7D%0A        // The order we create the field editors is the order they end up being shown in.%0A        for (Option o : bools) %7B%0A            addField(makeFieldEditor(o));%0A        %7D%0A        for (Option o : others) %7B%0A            FieldEditor fe = makeFieldEditor(o);%0A            if (fe != null) %7B%0A                addField(fe);%0A            %7D%0A        %7D%0A    %7D%0A%0A    /*%0A     * (non-Javadoc)%0A     *%0A     * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)%0A     */%0A    public void init(IWorkbench workbench) %7B%0A
     
+%7D%0A%0A
     
-ed = new Integer
+private BooleanFieldEditor makeBooleanFieldEditor(Option o) %7B%0A        return new Boolean
 Fiel
@@ -2672,53 +2672,157 @@
     
+%7D%0A%0A
     
-    %7D else if (o.getType() == StringArray
+/** Nasty. */%0A    private FieldEditor makeFieldEditor(Option o) %7B%0A        Class%3C?%3E optionType = o.getType();%0A        if (optionType == Boolean
 .cla
@@ -2843,80 +2843,122 @@
     
-  
-  ed = new StringFieldEditor(o.getLowerName(), o.getDescription(), get
+return makeBooleanFieldEditor(o);%0A        %7D else if (optionType == Integer.class) %7B%0A            return makeInteger
 Fiel
@@ -2960,32 +2960,26 @@
 rFieldEditor
-Parent()
+(o
 );%0A         
@@ -2981,276 +2981,505 @@
     
-    %7D%0A            if (ed != null) %7B%0A                addField(ed);%0A            %7D%0A        %7D%0A    %7D%0A%0A    /*%0A     * (non-Javadoc)
+%7D else if (optionType == StringArray.class) %7B%0A            return makeStringArrayFieldEditor(o);%0A        %7D else %7B%0A            return null;%0A        %7D%0A    %7D%0A%0A    private IntegerFieldEditor makeIntegerFieldEditor(Option o) %7B
 %0A     
-*%0A
    
-  * @see org.eclipse.ui.IWorkbenchPreferencePage#in
+return new IntegerFieldEditor(o.getLowerName(), o.getDescription(), getFieldEditorParent());%0A    %7D%0A%0A    private StringFieldEd
 it
-(
 or
-g.eclipse.ui.IWorkbench)%0A     */%0A    public void init(IWorkbench workbench) %7B
+ makeStringArrayFieldEditor(Option o) %7B%0A        return new StringFieldEditor(o.getLowerName(), o.getDescription(), getFieldEditorParent());
 %0A   

Put boolean fields first.