1 package net.sourceforge.phpdt.sql.preferences;
 
   3 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
 
   5 import org.eclipse.jface.preference.ColorFieldEditor;
 
   6 import org.eclipse.jface.preference.IPreferenceStore;
 
   7 import org.eclipse.jface.preference.PreferenceConverter;
 
   8 import org.eclipse.jface.preference.PreferencePage;
 
   9 import org.eclipse.swt.SWT;
 
  10 import org.eclipse.swt.events.SelectionEvent;
 
  11 import org.eclipse.swt.events.SelectionListener;
 
  12 import org.eclipse.swt.graphics.Color;
 
  13 import org.eclipse.swt.graphics.FontData;
 
  14 import org.eclipse.swt.graphics.RGB;
 
  15 import org.eclipse.swt.layout.GridData;
 
  16 import org.eclipse.swt.layout.GridLayout;
 
  17 import org.eclipse.swt.widgets.Button;
 
  18 import org.eclipse.swt.widgets.Composite;
 
  19 import org.eclipse.swt.widgets.Control;
 
  20 import org.eclipse.swt.widgets.FontDialog;
 
  21 import org.eclipse.swt.widgets.Label;
 
  22 import org.eclipse.ui.IWorkbench;
 
  23 import org.eclipse.ui.IWorkbenchPreferencePage;
 
  25 import net.sourceforge.phpdt.sql.editors.ColorManager;
 
  26 import net.sourceforge.phpdt.sql.editors.SQLColorConstants;
 
  28 public class PreferencesPage
 
  29   extends PreferencePage
 
  30   implements IWorkbenchPreferencePage {
 
  31   FontDialog fontDialog;
 
  32   ColorFieldEditor backgroundColorEditor;
 
  34   ColorFieldEditor textColorEditor;
 
  36   ColorFieldEditor keywordColorEditor;
 
  38   ColorFieldEditor stringColorEditor;
 
  40   ColorFieldEditor numericColorEditor;
 
  42   ColorFieldEditor commentColorEditor;
 
  54   public void init(IWorkbench workbench) {
 
  55     //Initialize the preference store
 
  56     this.workbench = workbench;
 
  57     setPreferenceStore(PHPEclipseSQLPlugin.getDefault().getPreferenceStore());
 
  58     initializeColorDefaults(getPreferenceStore());
 
  61   private void initializeColorDefaults(IPreferenceStore store) {
 
  62     RGB BACKGROUND = new RGB(255, 255, 255);
 
  63     RGB COMMENT = new RGB(88, 148, 64);
 
  64     RGB IDENTIFIER = new RGB(0, 0, 0);
 
  65     RGB KEYWORD = new RGB(126, 0, 75);
 
  66     RGB STRING = new RGB(0, 0, 255);
 
  67     RGB NUMERIC = new RGB(255, 0, 0);
 
  68     RGB DEFAULT = new RGB(0, 0, 0);
 
  69     PreferenceConverter.setDefault(
 
  71       "phpeclipse.sql.background.color",
 
  73     PreferenceConverter.setDefault(store, "phpeclipse.sql.text.color", DEFAULT);
 
  74     PreferenceConverter.setDefault(
 
  76       "phpeclipse.sql.keyword.color",
 
  78     PreferenceConverter.setDefault(
 
  80       "phpeclipse.sql.comment.color",
 
  82     PreferenceConverter.setDefault(
 
  84       "phpeclipse.sql.string.color",
 
  86     PreferenceConverter.setDefault(
 
  88       "phpeclipse.sql.numeric.color",
 
  92   protected void performDefaults() {
 
 101     backgroundColorEditor.loadDefault();
 
 102     textColorEditor.loadDefault();
 
 103     keywordColorEditor.loadDefault();
 
 104     stringColorEditor.loadDefault();
 
 105     commentColorEditor.loadDefault();
 
 106     numericColorEditor.loadDefault();
 
 109    * Save the preferences to the preference store.
 
 111   public boolean performOk() {
 
 112     PreferenceConverter.setValue(
 
 113       getPreferenceStore(),
 
 114       "phpeclipse.sql.font",
 
 116     getPreferenceStore().setValue("phpeclipse.sql.text.bold", textFlag);
 
 117     getPreferenceStore().setValue("phpeclipse.sql.keyword.bold", keywordFlag);
 
 118     getPreferenceStore().setValue("phpeclipse.sql.string.bold", stringFlag);
 
 119     getPreferenceStore().setValue("phpeclipse.sql.comment.bold", commentFlag);
 
 120     getPreferenceStore().setValue("phpeclipse.sql.numeric.bold", numericFlag);
 
 121     backgroundColorEditor.store();
 
 122     textColorEditor.store();
 
 123     keywordColorEditor.store();
 
 124     stringColorEditor.store();
 
 125     commentColorEditor.store();
 
 126     numericColorEditor.store();
 
 127     return super.performOk();
 
 130   protected Control createContents(Composite parent) {
 
 131     Composite composite = new Composite(parent, SWT.NULL);
 
 133     GridLayout innerLayout = new GridLayout();
 
 134     innerLayout.numColumns = 4;
 
 135     composite.setLayout(innerLayout);
 
 138       PreferenceConverter.getFontData(
 
 139         getPreferenceStore(),
 
 140         "phpeclipse.sql.font");
 
 141     textFlag = getPreferenceStore().getBoolean("phpeclipse.sql.text.bold");
 
 143       getPreferenceStore().getBoolean("phpeclipse.sql.keyword.bold");
 
 144     stringFlag = getPreferenceStore().getBoolean("phpeclipse.sql.string.bold");
 
 146       getPreferenceStore().getBoolean("phpeclipse.sql.comment.bold");
 
 148       getPreferenceStore().getBoolean("phpeclipse.sql.numeric.bold");
 
 151       new FontDialog(workbench.getActiveWorkbenchWindow().getShell());
 
 152     Button fontButton = new Button(composite, SWT.PUSH);
 
 153     fontButton.setText("Pick Font");
 
 154     fontButton.addSelectionListener(new SelectionListener() {
 
 155       public void widgetDefaultSelected(SelectionEvent e) {
 
 157       public void widgetSelected(SelectionEvent e) {
 
 158         if (fontData != null) {
 
 159           fontDialog.setFontData(fontData);
 
 161         FontData data = fontDialog.open();
 
 168     Button defaultButton = new Button(composite, SWT.PUSH);
 
 169     defaultButton.setText("Default Font");
 
 170     defaultButton.addSelectionListener(new SelectionListener() {
 
 171       public void widgetDefaultSelected(SelectionEvent e) {
 
 173       public void widgetSelected(SelectionEvent e) {
 
 179     fontDisplay = new Label(composite, SWT.NULL);
 
 180     GridData data = new GridData(GridData.FILL_HORIZONTAL);
 
 181     data.grabExcessHorizontalSpace = true;
 
 182     fontDisplay.setLayoutData(data);
 
 185     ColorManager manager = new ColorManager();
 
 187     Composite comp = new Composite(composite, SWT.NULL);
 
 188     GridData layoutData = new GridData();
 
 189     layoutData.horizontalSpan = 2;
 
 190     comp.setLayoutData(layoutData);
 
 192     Color defaultColor = manager.getColor(SQLColorConstants.DEFAULT);
 
 193     backgroundColorEditor =
 
 194       new ColorFieldEditor(
 
 195         "phpeclipse.sql.background.color",
 
 199     Composite temp = new Composite(composite, SWT.NULL);
 
 202     comp = new Composite(composite, SWT.NULL);
 
 203     layoutData = new GridData();
 
 204     layoutData.horizontalSpan = 2;
 
 205     comp.setLayoutData(layoutData);
 
 208       new ColorFieldEditor(
 
 209         "phpeclipse.sql.text.color",
 
 210         "Default Text Color",
 
 213     boldText = new Button(composite, SWT.CHECK);
 
 214     boldText.setSelection(textFlag);
 
 215     boldText.setText("Bold");
 
 216     boldText.addSelectionListener(new SelectionListener() {
 
 217       public void widgetDefaultSelected(SelectionEvent e) {
 
 219       public void widgetSelected(SelectionEvent e) {
 
 220         textFlag = boldText.getSelection();
 
 224     comp = new Composite(composite, SWT.NULL);
 
 225     layoutData = new GridData();
 
 226     layoutData.horizontalSpan = 2;
 
 227     comp.setLayoutData(layoutData);
 
 230       new ColorFieldEditor(
 
 231         "phpeclipse.sql.keyword.color",
 
 232         "Keyword Text Color",
 
 235     boldKeyword = new Button(composite, SWT.CHECK);
 
 236     boldKeyword.setSelection(keywordFlag);
 
 237     boldKeyword.setText("Bold");
 
 238     boldKeyword.addSelectionListener(new SelectionListener() {
 
 239       public void widgetDefaultSelected(SelectionEvent e) {
 
 241       public void widgetSelected(SelectionEvent e) {
 
 242         keywordFlag = boldKeyword.getSelection();
 
 246     comp = new Composite(composite, SWT.NULL);
 
 247     layoutData = new GridData();
 
 248     layoutData.horizontalSpan = 2;
 
 249     comp.setLayoutData(layoutData);
 
 252       new ColorFieldEditor(
 
 253         "phpeclipse.sql.comment.color",
 
 254         "Comment Text Color",
 
 257     boldComment = new Button(composite, SWT.CHECK);
 
 258     boldComment.setSelection(commentFlag);
 
 259     boldComment.setText("Bold");
 
 260     boldComment.addSelectionListener(new SelectionListener() {
 
 261       public void widgetDefaultSelected(SelectionEvent e) {
 
 263       public void widgetSelected(SelectionEvent e) {
 
 264         commentFlag = boldComment.getSelection();
 
 268     comp = new Composite(composite, SWT.NULL);
 
 269     layoutData = new GridData();
 
 270     layoutData.horizontalSpan = 2;
 
 271     comp.setLayoutData(layoutData);
 
 274       new ColorFieldEditor(
 
 275         "phpeclipse.sql.string.color",
 
 279     boldString = new Button(composite, SWT.CHECK);
 
 280     boldString.setSelection(stringFlag);
 
 281     boldString.setText("Bold");
 
 282     boldString.addSelectionListener(new SelectionListener() {
 
 283       public void widgetDefaultSelected(SelectionEvent e) {
 
 285       public void widgetSelected(SelectionEvent e) {
 
 286         stringFlag = boldString.getSelection();
 
 290     comp = new Composite(composite, SWT.NULL);
 
 291     layoutData = new GridData();
 
 292     layoutData.horizontalSpan = 2;
 
 293     comp.setLayoutData(layoutData);
 
 296       new ColorFieldEditor(
 
 297         "phpeclipse.sql.numeric.color",
 
 298         "Numeric Text Color",
 
 301     boldNumeric = new Button(composite, SWT.CHECK);
 
 302     boldNumeric.setSelection(numericFlag);
 
 303     boldNumeric.setText("Bold");
 
 304     boldNumeric.addSelectionListener(new SelectionListener() {
 
 305       public void widgetDefaultSelected(SelectionEvent e) {
 
 307       public void widgetSelected(SelectionEvent e) {
 
 308         numericFlag = boldNumeric.getSelection();
 
 312     backgroundColorEditor.setPreferencePage(this);
 
 313     backgroundColorEditor.setPreferenceStore(getPreferenceStore());
 
 314     backgroundColorEditor.load();
 
 316     textColorEditor.setPreferencePage(this);
 
 317     textColorEditor.setPreferenceStore(getPreferenceStore());
 
 318     textColorEditor.load();
 
 320     keywordColorEditor.setPreferencePage(this);
 
 321     keywordColorEditor.setPreferenceStore(getPreferenceStore());
 
 322     keywordColorEditor.load();
 
 324     commentColorEditor.setPreferencePage(this);
 
 325     commentColorEditor.setPreferenceStore(getPreferenceStore());
 
 326     commentColorEditor.load();
 
 328     stringColorEditor.setPreferencePage(this);
 
 329     stringColorEditor.setPreferenceStore(getPreferenceStore());
 
 330     stringColorEditor.load();
 
 332     numericColorEditor.setPreferencePage(this);
 
 333     numericColorEditor.setPreferenceStore(getPreferenceStore());
 
 334     numericColorEditor.load();
 
 338   public void updateFontDisplay() {
 
 339     if (fontData == null) {
 
 340       fontDisplay.setText("Font: default");
 
 342       String style = "regular";
 
 343       if (fontData.getStyle() == SWT.BOLD) {
 
 345       } else if (fontData.getStyle() == SWT.ITALIC) {
 
 347       } else if (fontData.getStyle() == (SWT.BOLD | SWT.ITALIC)) {
 
 348         style = "bold italic";
 
 356           + fontData.getHeight());
 
 359   public void updateFlags() {
 
 360     boldText.setSelection(textFlag);
 
 361     boldKeyword.setSelection(keywordFlag);
 
 362     boldString.setSelection(stringFlag);
 
 363     boldNumeric.setSelection(numericFlag);
 
 364     boldComment.setSelection(commentFlag);