package net.sourceforge.phpeclipse;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import net.sourceforge.phpeclipse.preferences.ColorEditor;
import net.sourceforge.phpeclipse.preferences.OverlayPreferenceStore;
import net.sourceforge.phpeclipse.preferences.PHPPreferencesMessages;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
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.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

/*
 * The preference page for setting the PHP Editor options.
 */
public class PHPSyntaxEditorPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {

  public final OverlayPreferenceStore.OverlayKey[] Keys =
    new OverlayPreferenceStore.OverlayKey[] {
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_MULTILINE_COMMENT),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_MULTILINE_COMMENT_BOLD),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_MULTILINE_COMMENT_ITALIC),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_MULTILINE_COMMENT_UNDERLINE),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_SINGLELINE_COMMENT),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_SINGLELINE_COMMENT_BOLD),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_SINGLELINE_COMMENT_ITALIC),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_SINGLELINE_COMMENT_UNDERLINE),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_KEYWORD),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_KEYWORD_BOLD),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_KEYWORD_ITALIC),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_KEYWORD_UNDERLINE),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_VARIABLE),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_VARIABLE_BOLD),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_VARIABLE_ITALIC),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_VARIABLE_UNDERLINE),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_TYPE),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_TYPE_BOLD),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_TYPE_ITALIC),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_TYPE_UNDERLINE),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_CONSTANT),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_CONSTANT_BOLD),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_CONSTANT_ITALIC),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_CONSTANT_UNDERLINE),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_FUNCTIONNAME),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_FUNCTIONNAME_BOLD),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_FUNCTIONNAME_ITALIC),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_FUNCTIONNAME_UNDERLINE),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_STRING),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_STRING_BOLD),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_STRING_ITALIC),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_STRING_UNDERLINE),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_DEFAULT),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_DEFAULT_BOLD),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_DEFAULT_ITALIC),
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHP_DEFAULT_UNDERLINE),
      
			new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHPDOC_KEYWORD),
			new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHPDOC_KEYWORD_BOLD),
			new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHPDOC_KEYWORD_ITALIC),
			new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHPDOC_KEYWORD_UNDERLINE),
			new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHPDOC_TAG),
			new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHPDOC_TAG_BOLD),
			new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHPDOC_TAG_ITALIC),
			new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHPDOC_TAG_UNDERLINE),
			new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHPDOC_LINK),
			new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHPDOC_LINK_BOLD),
			new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHPDOC_LINK_ITALIC),
			new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHPDOC_LINK_UNDERLINE),
			new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHPDOC_DEFAULT),
			new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHPDOC_DEFAULT_BOLD),
			new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHPDOC_DEFAULT_ITALIC),
			new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IPreferenceConstants.PHPDOC_DEFAULT_UNDERLINE),
      
      new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_USERDEF_XMLFILE),
  //    new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, IPreferenceConstants.PHP_EDITOR_BACKGROUND)
  };

  private final String[][] SyntaxColorListModel =
    new String[][] {
      {
        PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.multiLineComment"),
        IPreferenceConstants.PHP_MULTILINE_COMMENT },
      {
      PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.singleLineComment"),
        IPreferenceConstants.PHP_SINGLELINE_COMMENT },
        {
      PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.keywords"), IPreferenceConstants.PHP_KEYWORD }, {
      PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.variables"), IPreferenceConstants.PHP_VARIABLE }, {
      PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.types"), IPreferenceConstants.PHP_TYPE }, {
      PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.constants"), IPreferenceConstants.PHP_CONSTANT }, {
        PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.functions"), IPreferenceConstants.PHP_FUNCTIONNAME }, {
      PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.strings"), IPreferenceConstants.PHP_STRING }, {
      PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.others"), IPreferenceConstants.PHP_DEFAULT }, {
	  PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.phpdoc_keywords"), IPreferenceConstants.PHPDOC_KEYWORD }, {
	  PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.phpdoc_tags"), IPreferenceConstants.PHPDOC_TAG }, {
	  PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.phpdoc_links"), IPreferenceConstants.PHPDOC_LINK }, {
	  PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.phpdoc_others"), IPreferenceConstants.PHPDOC_DEFAULT }
  };

  private OverlayPreferenceStore OverlayStore;

  private Map ColorButtons = new HashMap();
  private SelectionListener ColorButtonListener = new SelectionListener() {
    public void widgetDefaultSelected(SelectionEvent e) {
    }
    public void widgetSelected(SelectionEvent e) {
      ColorEditor editor = (ColorEditor) e.widget.getData();
      PreferenceConverter.setValue(OverlayStore, (String) ColorButtons.get(editor), editor.getColorValue());
    }
  };

  private Map CheckBoxes = new HashMap();
  private SelectionListener CheckBoxListener = new SelectionListener() {
    public void widgetDefaultSelected(SelectionEvent e) {
    }
    public void widgetSelected(SelectionEvent e) {
      Button button = (Button) e.widget;
      OverlayStore.setValue((String) CheckBoxes.get(button), button.getSelection());
    }
  };

  private List SyntaxColorList;
  /** The ColorEditor that choose the foreground color. */
  private ColorEditor SyntaxForegroundColorEditor;
  private Button BoldCheckBox;
  private Button ItalicCheckBox;
  private Button UnderlineCheckBox;
  private FileFieldEditor userdefPHPSyntaxFileFFE;
  // private BooleanFieldEditor showLineNumber;
  // private IntegerFieldEditor formatterTabSize;
  // private BooleanFieldEditor spacesForTabs;

  public PHPSyntaxEditorPreferencePage() {
    setDescription(PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.description")); //$NON-NLS-1$
    setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
    OverlayStore = new OverlayPreferenceStore(getPreferenceStore(), Keys);
  }

  public void init(IWorkbench workbench) {
  }

  public void createControl(Composite parent) {
    super.createControl(parent);
  }

  private void handleSyntaxColorListSelection() {
    int i = SyntaxColorList.getSelectionIndex();
    String key = SyntaxColorListModel[i][1];
    RGB rgb = PreferenceConverter.getColor(OverlayStore, key);
    SyntaxForegroundColorEditor.setColorValue(rgb);
    BoldCheckBox.setSelection(OverlayStore.getBoolean(key + "_bold"));
    ItalicCheckBox.setSelection(OverlayStore.getBoolean(key + "_italic"));
    UnderlineCheckBox.setSelection(OverlayStore.getBoolean(key + "_underline"));
  }

  /**
   * Create the group of options for other parameters (background color for example).
   * @param parent the parent component
   */
  private void backgroundOptionPage(Composite parent) {
    Label label = new Label(parent, SWT.LEFT);
    label.setText(PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.color")); //$NON-NLS-1$
    GridData gd = new GridData();
    gd.horizontalAlignment = GridData.BEGINNING;
    label.setLayoutData(gd);
//    final ColorEditor syntaxBackgroundColorEditor = new ColorEditor(parent);
//    RGB rgb = PreferenceConverter.getColor(OverlayStore, IPreferenceConstants.PHP_EDITOR_BACKGROUND);
//    syntaxBackgroundColorEditor.setColorValue(rgb);
//    Button backgroundColorButton = syntaxBackgroundColorEditor.getButton();
//    gd = new GridData(GridData.FILL_HORIZONTAL);
//    gd.horizontalAlignment = GridData.BEGINNING;
//    backgroundColorButton.setLayoutData(gd);
//    backgroundColorButton.addSelectionListener(new SelectionListener() {
//      public void widgetDefaultSelected(SelectionEvent e) {
//        // do nothing
//      }
//      public void widgetSelected(SelectionEvent e) {
//        PreferenceConverter.setValue(OverlayStore, IPreferenceConstants.PHP_EDITOR_BACKGROUND, syntaxBackgroundColorEditor.getColorValue());
//      }
//    });
  }

  /**
   * Create the group of options for the syntax parameters.
   * @param parent the parent component
   * @return
   */
  private Control createSyntaxPage(Composite parent) {

    Composite colorComposite = new Composite(parent, SWT.NULL);
    colorComposite.setLayout(new GridLayout());

    Label label = new Label(colorComposite, SWT.LEFT);
    label.setText(PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.syntax")); //$NON-NLS-1$
    label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    Composite editorComposite = new Composite(colorComposite, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    editorComposite.setLayout(layout);
    GridData gd = new GridData(GridData.FILL_BOTH);
    editorComposite.setLayoutData(gd);

    SyntaxColorList = new List(editorComposite, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
    gd = new GridData(GridData.FILL_BOTH);
    gd.heightHint = convertHeightInCharsToPixels(5);
    SyntaxColorList.setLayoutData(gd);

    Composite stylesComposite = new Composite(editorComposite, SWT.NONE);
    layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    layout.numColumns = 2;
    stylesComposite.setLayout(layout);
    stylesComposite.setLayoutData(new GridData(GridData.FILL_BOTH));

    label = new Label(stylesComposite, SWT.LEFT);
    label.setText(PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.color")); //$NON-NLS-1$
    gd = new GridData();
    gd.horizontalAlignment = GridData.BEGINNING;
    label.setLayoutData(gd);

    SyntaxForegroundColorEditor = new ColorEditor(stylesComposite);
    Button foregroundColorButton = SyntaxForegroundColorEditor.getButton();
    gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalAlignment = GridData.BEGINNING;
    foregroundColorButton.setLayoutData(gd);

    BoldCheckBox = new Button(stylesComposite, SWT.CHECK);
    BoldCheckBox.setText(PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.bold")); //$NON-NLS-1$
    gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalAlignment = GridData.BEGINNING;
    gd.horizontalSpan = 2;
    BoldCheckBox.setLayoutData(gd);

    ItalicCheckBox = new Button(stylesComposite, SWT.CHECK);
    ItalicCheckBox.setText(PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.italic")); //$NON-NLS-1$
    ItalicCheckBox.setEnabled(false);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalAlignment = GridData.BEGINNING;
    gd.horizontalSpan = 2;
    ItalicCheckBox.setLayoutData(gd);

    UnderlineCheckBox = new Button(stylesComposite, SWT.CHECK);
    UnderlineCheckBox.setText(PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.underline")); //$NON-NLS-1$
    UnderlineCheckBox.setEnabled(false);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalAlignment = GridData.BEGINNING;
    gd.horizontalSpan = 2;
    UnderlineCheckBox.setLayoutData(gd);

    Composite customSyntaxComposite = new Composite(colorComposite, SWT.NONE);
    layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    layout.numColumns = 3;
    stylesComposite.setLayout(layout);
    stylesComposite.setLayoutData(new GridData(GridData.FILL_BOTH));

    userdefPHPSyntaxFileFFE =
      new FileFieldEditor(
        IPreferenceConstants.PHP_USERDEF_XMLFILE,
        PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.syntaxdialog"),
        colorComposite);
    userdefPHPSyntaxFileFFE.setPreferencePage(this);
    userdefPHPSyntaxFileFFE.setPreferenceStore(getPreferenceStore());
    userdefPHPSyntaxFileFFE.load();

    SyntaxColorList.addSelectionListener(new SelectionListener() {
      public void widgetDefaultSelected(SelectionEvent e) {
        // do nothing
      }
      public void widgetSelected(SelectionEvent e) {
        handleSyntaxColorListSelection();
      }
    });

    foregroundColorButton.addSelectionListener(new SelectionListener() {
      public void widgetDefaultSelected(SelectionEvent e) {
        // do nothing
      }
      public void widgetSelected(SelectionEvent e) {
        int i = SyntaxColorList.getSelectionIndex();
        String key = SyntaxColorListModel[i][1];

        PreferenceConverter.setValue(OverlayStore, key, SyntaxForegroundColorEditor.getColorValue());
      }
    });
    BoldCheckBox.addSelectionListener(new SelectionListener() {
      public void widgetDefaultSelected(SelectionEvent e) {
        // do nothing
      }
      public void widgetSelected(SelectionEvent e) {
        int i = SyntaxColorList.getSelectionIndex();
        String key = SyntaxColorListModel[i][1];
        OverlayStore.setValue(key + "_bold", BoldCheckBox.getSelection());
      }
    });

    ItalicCheckBox.addSelectionListener(new SelectionListener() {
      public void widgetDefaultSelected(SelectionEvent e) {
        // do nothing
      }
      public void widgetSelected(SelectionEvent e) {
        int i = SyntaxColorList.getSelectionIndex();
        String key = SyntaxColorListModel[i][1];
        OverlayStore.setValue(key + "_italic", ItalicCheckBox.getSelection());
      }
    });

    UnderlineCheckBox.addSelectionListener(new SelectionListener() {
      public void widgetDefaultSelected(SelectionEvent e) {
        // do nothing
      }
      public void widgetSelected(SelectionEvent e) {
        int i = SyntaxColorList.getSelectionIndex();
        String key = SyntaxColorListModel[i][1];
        OverlayStore.setValue(key + "_underline", UnderlineCheckBox.getSelection());
      }
    });
    return colorComposite;
  }

  private void initializeViewerColors(ISourceViewer viewer) {

    IPreferenceStore store = OverlayStore;
    if (store != null) {

      StyledText styledText = viewer.getTextWidget();
    }
  }

  private Color createColor(IPreferenceStore store, String key, Display display) {
    RGB rgb = null;
    if (store.contains(key)) {
      if (store.isDefault(key))
        rgb = PreferenceConverter.getDefaultColor(store, key);
      else
        rgb = PreferenceConverter.getColor(store, key);
      if (rgb != null)
        return new Color(display, rgb);
    }
    return null;
  }

  private static void setEnabled(Control control, boolean enable) {
    control.setEnabled(enable);
    if (control instanceof Composite) {
      Composite composite = (Composite) control;
      Control[] children = composite.getChildren();
      for (int i = 0; i < children.length; i++)
        setEnabled(children[i], enable);
    }
  }

  private static void indent(Control control) {
    GridData gridData = new GridData();
    gridData.horizontalIndent = 20;
    control.setLayoutData(gridData);
  }

  private static void createDependency(final Button master, final Control slave) {
    indent(slave);
    master.addSelectionListener(new SelectionListener() {
      public void widgetSelected(SelectionEvent e) {
        slave.setEnabled(master.getSelection());
      }

      public void widgetDefaultSelected(SelectionEvent e) {
      }
    });
  }

  protected Control createContents(Composite parent) {
    OverlayStore.load();
    OverlayStore.start();
    //Create overall composite
    Composite composite = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    composite.setLayout(layout);
    Composite syntaxComposite = new Composite(composite, SWT.NULL);
    syntaxComposite.setLayout(new GridLayout());
    layout = new GridLayout();
    layout.numColumns = 3;
    Group syntaxGroup = new Group(syntaxComposite, SWT.NONE);
    syntaxGroup.setText(PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.foreground"));
    syntaxGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    syntaxGroup.setLayout(layout);
    createSyntaxPage(syntaxGroup);

    Composite backgroundOptions = new Composite(composite,SWT.NULL);
    backgroundOptions.setLayout(new GridLayout());
    layout = new GridLayout();
    layout.numColumns = 3;
    Group backgroundOptionsGroup = new Group(backgroundOptions,SWT.NONE);
    backgroundOptionsGroup.setText(PHPPreferencesMessages.getString("PHPEditorSyntaxPreferencePage.background"));
    backgroundOptionsGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    backgroundOptionsGroup.setLayout(layout);
    backgroundOptionPage(backgroundOptionsGroup);

    initialize();
    
   
//    showLineNumber = new BooleanFieldEditor(PHPeclipsePlugin.LINE_NUMBER_RULER, 
//    "Show line numbers", composite);
//    showLineNumber.setPreferencePage(this);
//    showLineNumber.setPreferenceStore(getPreferenceStore());
//    showLineNumber.load();
    
//    formatterTabSize = new IntegerFieldEditor(PHPeclipsePlugin.FORMATTER_TAB_SIZE, 
//    "Displayed tab width", composite, 3);
//    formatterTabSize.setPreferencePage(this);
//    formatterTabSize.setPreferenceStore(getPreferenceStore());
//    formatterTabSize.load();
//    
//    spacesForTabs = new BooleanFieldEditor(PHPeclipsePlugin.SPACES_FOR_TABS, 
//    "Spaces for Tabs", composite);
//    spacesForTabs.setPreferencePage(this);
//    spacesForTabs.setPreferenceStore(getPreferenceStore());
//    spacesForTabs.load();
    return composite;
  }

  private void initialize() {
    initializeFields();
    for (int i = 0; i < SyntaxColorListModel.length; i++)
      SyntaxColorList.add(SyntaxColorListModel[i][0]);
    SyntaxColorList.getDisplay().asyncExec(new Runnable() {
      public void run() {
        if (SyntaxColorList != null && !SyntaxColorList.isDisposed()) {
          SyntaxColorList.select(0);
          handleSyntaxColorListSelection();
        }
      }
    });
  }

  private void initializeFields() {

    Iterator e = ColorButtons.keySet().iterator();
    while (e.hasNext()) {
      ColorEditor c = (ColorEditor) e.next();
      String key = (String) ColorButtons.get(c);
      RGB rgb = PreferenceConverter.getColor(OverlayStore, key);
      c.setColorValue(rgb);
    }

    e = CheckBoxes.keySet().iterator();
    while (e.hasNext()) {
      Button b = (Button) e.next();
      String key = (String) CheckBoxes.get(b);
      b.setSelection(OverlayStore.getBoolean(key));
    }
  }

  public boolean performOk() {
    OverlayStore.propagate();
    IPreferenceStore store = getPreferenceStore();
    PHPeclipsePlugin.getDefault().savePluginPreferences();
    userdefPHPSyntaxFileFFE.store();
   // showLineNumber.store();
   // spacesForTabs.store();
   // formatterTabSize.store();
    return true;
  }

  protected void performDefaults() {
    OverlayStore.loadDefaults();
    initializeFields();
    handleSyntaxColorListSelection();
    userdefPHPSyntaxFileFFE.loadDefault();
  //  showLineNumber.loadDefault();
  //  spacesForTabs.loadDefault();
  //  showLineNumber.loadDefault();
  //  formatterTabSize.loadDefault();
    super.performDefaults();
  }

  public void dispose() {
    if (OverlayStore != null) {
      OverlayStore.stop();
      OverlayStore = null;
    }
    super.dispose();
  }

  private Control addColorButton(Composite composite, String label, String key, int indentation) {
    Label labelControl = new Label(composite, SWT.NONE);
    labelControl.setText(label);
    GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
    gd.horizontalIndent = indentation;
    labelControl.setLayoutData(gd);
    ColorEditor editor = new ColorEditor(composite);
    Button button = editor.getButton();
    button.setData(editor);
    gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
    button.setLayoutData(gd);
    button.addSelectionListener(ColorButtonListener);
    ColorButtons.put(editor, key);
    return composite;
  }

  private Button addCheckBox(Composite parent, String label, String key, int indentation) {
    Button checkBox = new Button(parent, SWT.CHECK);
    checkBox.setText(label);
    GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
    gd.horizontalIndent = indentation;
    gd.horizontalSpan = 2;
    checkBox.setLayoutData(gd);
    checkBox.addSelectionListener(CheckBoxListener);
    CheckBoxes.put(checkBox, key);
    return checkBox;
  }

  private void updateStatus(IStatus status) {
  }

  /**
   * @deprecated Inline to avoid reference to preference page
   */
  public static boolean indicateQuixFixableProblems() {
    //		return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CORRECTION_INDICATION);
    return false;
  }

  /**
   * @deprecated Inline to avoid reference to preference page
   */
  static public boolean synchronizeOutlineOnCursorMove() {
    //		return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE);
    return false;
  }

}