KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > editors > text > TextEditorPreferencePage2


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.editors.text;
13
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.runtime.IStatus;
21
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.ModifyEvent;
24 import org.eclipse.swt.events.ModifyListener;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.SelectionListener;
27 import org.eclipse.swt.graphics.RGB;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.List;
35 import org.eclipse.swt.widgets.Text;
36
37 import org.eclipse.jface.dialogs.Dialog;
38 import org.eclipse.jface.dialogs.DialogPage;
39 import org.eclipse.jface.dialogs.IMessageProvider;
40 import org.eclipse.jface.preference.PreferenceConverter;
41 import org.eclipse.jface.preference.PreferencePage;
42
43 import org.eclipse.ui.editors.text.ITextEditorHelpContextIds;
44
45 import org.eclipse.ui.IWorkbench;
46 import org.eclipse.ui.IWorkbenchPreferencePage;
47 import org.eclipse.ui.help.WorkbenchHelp;
48 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
49
50
51
52 /**
53  * The preference page for setting the editor options.
54  * <p>
55  * This class is internal and not intended to be used by clients.</p>
56  *
57  * @since 2.1
58  */

59 public class TextEditorPreferencePage2 extends PreferencePage implements IWorkbenchPreferencePage {
60     
61     private final String JavaDoc[][] fAppearanceColorListModel= new String JavaDoc[][] {
62         {TextEditorMessages.getString("TextEditorPreferencePage.lineNumberForegroundColor"), AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER_COLOR, null}, //$NON-NLS-1$
63
{TextEditorMessages.getString("TextEditorPreferencePage.currentLineHighlighColor"), AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE_COLOR, null}, //$NON-NLS-1$
64
{TextEditorMessages.getString("TextEditorPreferencePage.printMarginColor"), AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLOR, null}, //$NON-NLS-1$
65
{TextEditorMessages.getString("TextEditorPreferencePage.selectionForegroundColor"), AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR}, //$NON-NLS-1$
66
{TextEditorMessages.getString("TextEditorPreferencePage.selectionBackgroundColor"), AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR}, //$NON-NLS-1$
67
};
68     
69     private OverlayPreferenceStore fOverlayStore;
70     
71     private Map JavaDoc fCheckBoxes= new HashMap JavaDoc();
72     private SelectionListener fCheckBoxListener= new SelectionListener() {
73         public void widgetDefaultSelected(SelectionEvent e) {
74         }
75         public void widgetSelected(SelectionEvent e) {
76             Button button= (Button) e.widget;
77             fOverlayStore.setValue((String JavaDoc) fCheckBoxes.get(button), button.getSelection());
78         }
79     };
80     
81     private Map JavaDoc fTextFields= new HashMap JavaDoc();
82     private ModifyListener fTextFieldListener= new ModifyListener() {
83         public void modifyText(ModifyEvent e) {
84             Text text= (Text) e.widget;
85             fOverlayStore.setValue((String JavaDoc) fTextFields.get(text), text.getText());
86         }
87     };
88
89     private ArrayList JavaDoc fNumberFields= new ArrayList JavaDoc();
90     private ModifyListener fNumberFieldListener= new ModifyListener() {
91         public void modifyText(ModifyEvent e) {
92             numberFieldChanged((Text) e.widget);
93         }
94     };
95     
96     private List JavaDoc fAppearanceColorList;
97     private ColorEditor fAppearanceColorEditor;
98     private Button fAppearanceColorDefault;
99     
100     /**
101      * Tells whether the fields are initialized.
102      * @since 3.0
103      */

104     private boolean fFieldsInitialized= false;
105     
106     /**
107      * List of master/slave listeners when there's a dependency.
108      *
109      * @see #createDependency(Button, String, Control)
110      * @since 3.0
111      */

112     private ArrayList JavaDoc fMasterSlaveListeners= new ArrayList JavaDoc();
113
114     
115     public TextEditorPreferencePage2() {
116         setDescription(TextEditorMessages.getString("TextEditorPreferencePage.description")); //$NON-NLS-1$
117
setPreferenceStore(EditorsPlugin.getDefault().getPreferenceStore());
118         
119         fOverlayStore= createOverlayStore();
120     }
121     
122     private OverlayPreferenceStore createOverlayStore() {
123         
124         ArrayList JavaDoc overlayKeys= new ArrayList JavaDoc();
125         
126         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE_COLOR));
127         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE));
128         
129         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.INT, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH));
130
131         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLOR));
132         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.INT, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN));
133         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN));
134         
135         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_OVERVIEW_RULER));
136         
137         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER_COLOR));
138         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER));
139         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_USE_CUSTOM_CARETS));
140         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_WIDE_CARET));
141         
142         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR));
143         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR));
144         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR));
145         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR));
146
147         OverlayPreferenceStore.OverlayKey[] keys= new OverlayPreferenceStore.OverlayKey[overlayKeys.size()];
148         overlayKeys.toArray(keys);
149         return new OverlayPreferenceStore(getPreferenceStore(), keys);
150     }
151     
152     /*
153      * @see IWorkbenchPreferencePage#init()
154      */

155     public void init(IWorkbench workbench) {
156     }
157
158     /*
159      * @see PreferencePage#createControl(Composite)
160      */

161     public void createControl(Composite parent) {
162         super.createControl(parent);
163         WorkbenchHelp.setHelp(getControl(), ITextEditorHelpContextIds.TEXT_EDITOR_PREFERENCE_PAGE);
164     }
165
166     private void handleAppearanceColorListSelection() {
167         int i= fAppearanceColorList.getSelectionIndex();
168         String JavaDoc key= fAppearanceColorListModel[i][1];
169         RGB rgb= PreferenceConverter.getColor(fOverlayStore, key);
170         fAppearanceColorEditor.setColorValue(rgb);
171         updateAppearanceColorWidgets(fAppearanceColorListModel[i][2]);
172     }
173
174     private void updateAppearanceColorWidgets(String JavaDoc systemDefaultKey) {
175         if (systemDefaultKey == null) {
176             fAppearanceColorDefault.setSelection(false);
177             fAppearanceColorDefault.setVisible(false);
178             fAppearanceColorEditor.getButton().setEnabled(true);
179         } else {
180             boolean systemDefault= fOverlayStore.getBoolean(systemDefaultKey);
181             fAppearanceColorDefault.setSelection(systemDefault);
182             fAppearanceColorDefault.setVisible(true);
183             fAppearanceColorEditor.getButton().setEnabled(!systemDefault);
184         }
185     }
186     
187     private Control createAppearancePage(Composite parent) {
188
189         Composite appearanceComposite= new Composite(parent, SWT.NONE);
190         GridLayout layout= new GridLayout(); layout.numColumns= 2;
191         appearanceComposite.setLayout(layout);
192
193         String JavaDoc label= TextEditorMessages.getString("TextEditorPreferencePage.displayedTabWidth"); //$NON-NLS-1$
194
addTextField(appearanceComposite, label, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH, 3, 0, true);
195
196         label= TextEditorMessages.getString("TextEditorPreferencePage.printMarginColumn"); //$NON-NLS-1$
197
addTextField(appearanceComposite, label, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN, 3, 0, true);
198                 
199         label= TextEditorMessages.getString("TextEditorPreferencePage.showOverviewRuler"); //$NON-NLS-1$
200
addCheckBox(appearanceComposite, label, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_OVERVIEW_RULER, 0);
201                 
202         label= TextEditorMessages.getString("TextEditorPreferencePage.showLineNumbers"); //$NON-NLS-1$
203
addCheckBox(appearanceComposite, label, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER, 0);
204
205         label= TextEditorMessages.getString("TextEditorPreferencePage.highlightCurrentLine"); //$NON-NLS-1$
206
addCheckBox(appearanceComposite, label, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE, 0);
207                 
208         label= TextEditorMessages.getString("TextEditorPreferencePage.showPrintMargin"); //$NON-NLS-1$
209
addCheckBox(appearanceComposite, label, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN, 0);
210
211         label= TextEditorMessages.getString("TextEditorPreferencePage.accessibility.disableCustomCarets"); //$NON-NLS-1$
212
Button master= addCheckBox(appearanceComposite, label, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_USE_CUSTOM_CARETS, 0);
213
214         label= TextEditorMessages.getString("TextEditorPreferencePage.accessibility.wideCaret"); //$NON-NLS-1$
215
Button slave= addCheckBox(appearanceComposite, label, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_WIDE_CARET, 0);
216         createDependency(master, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_USE_CUSTOM_CARETS, slave);
217
218         Label l= new Label(appearanceComposite, SWT.LEFT );
219         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
220         gd.horizontalSpan= 2;
221         gd.heightHint= convertHeightInCharsToPixels(1) / 2;
222         l.setLayoutData(gd);
223         
224         l= new Label(appearanceComposite, SWT.LEFT);
225         l.setText(TextEditorMessages.getString("TextEditorPreferencePage.appearanceOptions")); //$NON-NLS-1$
226
gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
227         gd.horizontalSpan= 2;
228         l.setLayoutData(gd);
229
230         Composite editorComposite= new Composite(appearanceComposite, SWT.NONE);
231         layout= new GridLayout();
232         layout.numColumns= 2;
233         layout.marginHeight= 0;
234         layout.marginWidth= 0;
235         editorComposite.setLayout(layout);
236         gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL);
237         gd.horizontalSpan= 2;
238         editorComposite.setLayoutData(gd);
239
240         fAppearanceColorList= new List JavaDoc(editorComposite, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
241         gd= new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.FILL_HORIZONTAL);
242         gd.heightHint= convertHeightInCharsToPixels(5);
243         fAppearanceColorList.setLayoutData(gd);
244                         
245         Composite stylesComposite= new Composite(editorComposite, SWT.NONE);
246         layout= new GridLayout();
247         layout.marginHeight= 0;
248         layout.marginWidth= 0;
249         layout.numColumns= 2;
250         stylesComposite.setLayout(layout);
251         stylesComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
252         
253         l= new Label(stylesComposite, SWT.LEFT);
254         l.setText(TextEditorMessages.getString("TextEditorPreferencePage.color")); //$NON-NLS-1$
255
gd= new GridData();
256         gd.horizontalAlignment= GridData.BEGINNING;
257         l.setLayoutData(gd);
258
259         fAppearanceColorEditor= new ColorEditor(stylesComposite);
260         Button foregroundColorButton= fAppearanceColorEditor.getButton();
261         gd= new GridData(GridData.FILL_HORIZONTAL);
262         gd.horizontalAlignment= GridData.BEGINNING;
263         foregroundColorButton.setLayoutData(gd);
264
265         SelectionListener colorDefaultSelectionListener= new SelectionListener() {
266             public void widgetSelected(SelectionEvent e) {
267                 boolean systemDefault= fAppearanceColorDefault.getSelection();
268                 fAppearanceColorEditor.getButton().setEnabled(!systemDefault);
269                 
270                 int i= fAppearanceColorList.getSelectionIndex();
271                 String JavaDoc key= fAppearanceColorListModel[i][2];
272                 if (key != null)
273                     fOverlayStore.setValue(key, systemDefault);
274             }
275             public void widgetDefaultSelected(SelectionEvent e) {}
276         };
277         
278         fAppearanceColorDefault= new Button(stylesComposite, SWT.CHECK);
279         fAppearanceColorDefault.setText(TextEditorMessages.getString("TextEditorPreferencePage.systemDefault")); //$NON-NLS-1$
280
gd= new GridData(GridData.FILL_HORIZONTAL);
281         gd.horizontalAlignment= GridData.BEGINNING;
282         gd.horizontalSpan= 2;
283         fAppearanceColorDefault.setLayoutData(gd);
284         fAppearanceColorDefault.setVisible(false);
285         fAppearanceColorDefault.addSelectionListener(colorDefaultSelectionListener);
286         
287         fAppearanceColorList.addSelectionListener(new SelectionListener() {
288             public void widgetDefaultSelected(SelectionEvent e) {
289                 // do nothing
290
}
291             public void widgetSelected(SelectionEvent e) {
292                 handleAppearanceColorListSelection();
293             }
294         });
295         foregroundColorButton.addSelectionListener(new SelectionListener() {
296             public void widgetDefaultSelected(SelectionEvent e) {
297                 // do nothing
298
}
299             public void widgetSelected(SelectionEvent e) {
300                 int i= fAppearanceColorList.getSelectionIndex();
301                 String JavaDoc key= fAppearanceColorListModel[i][1];
302                 
303                 PreferenceConverter.setValue(fOverlayStore, key, fAppearanceColorEditor.getColorValue());
304             }
305         });
306         
307         return appearanceComposite;
308     }
309     
310     /*
311      * @see PreferencePage#createContents(Composite)
312      */

313     protected Control createContents(Composite parent) {
314         
315         initializeDefaultColors();
316
317         fOverlayStore.load();
318         fOverlayStore.start();
319         
320         Control control= createAppearancePage(parent);
321
322         initialize();
323         Dialog.applyDialogFont(control);
324         return control;
325     }
326     
327     private void initialize() {
328         
329         initializeFields();
330         
331         for (int i= 0; i < fAppearanceColorListModel.length; i++)
332             fAppearanceColorList.add(fAppearanceColorListModel[i][0]);
333         fAppearanceColorList.getDisplay().asyncExec(new Runnable JavaDoc() {
334             public void run() {
335                 if (fAppearanceColorList != null && !fAppearanceColorList.isDisposed()) {
336                     fAppearanceColorList.select(0);
337                     handleAppearanceColorListSelection();
338                 }
339             }
340         });
341     }
342     
343     private void initializeFields() {
344         
345         Iterator JavaDoc e= fCheckBoxes.keySet().iterator();
346         while (e.hasNext()) {
347             Button b= (Button) e.next();
348             String JavaDoc key= (String JavaDoc) fCheckBoxes.get(b);
349             b.setSelection(fOverlayStore.getBoolean(key));
350         }
351         
352         e= fTextFields.keySet().iterator();
353         while (e.hasNext()) {
354             Text t= (Text) e.next();
355             String JavaDoc key= (String JavaDoc) fTextFields.get(t);
356             t.setText(fOverlayStore.getString(key));
357         }
358         
359         fFieldsInitialized= true;
360         updateStatus(validatePositiveNumber("0")); //$NON-NLS-1$
361

362         // Update slaves
363
Iterator JavaDoc iter= fMasterSlaveListeners.iterator();
364         while (iter.hasNext()) {
365             SelectionListener listener= (SelectionListener)iter.next();
366             listener.widgetSelected(null);
367         }
368     }
369     
370     private void initializeDefaultColors() {
371         if (!getPreferenceStore().contains(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR)) {
372             RGB rgb= getControl().getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION).getRGB();
373             PreferenceConverter.setDefault(fOverlayStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR, rgb);
374             PreferenceConverter.setDefault(getPreferenceStore(), AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR, rgb);
375         }
376         if (!getPreferenceStore().contains(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR)) {
377             RGB rgb= getControl().getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT).getRGB();
378             PreferenceConverter.setDefault(fOverlayStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR, rgb);
379             PreferenceConverter.setDefault(getPreferenceStore(), AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR, rgb);
380         }
381     }
382     
383     /*
384      * @see PreferencePage#performOk()
385      */

386     public boolean performOk() {
387         fOverlayStore.propagate();
388         EditorsPlugin.getDefault().savePluginPreferences();
389         return true;
390     }
391     
392     /*
393      * @see PreferencePage#performDefaults()
394      */

395     protected void performDefaults() {
396
397         fOverlayStore.loadDefaults();
398         
399         initializeFields();
400
401         handleAppearanceColorListSelection();
402
403         super.performDefaults();
404     }
405     
406     /*
407      * @see DialogPage#dispose()
408      */

409     public void dispose() {
410         
411         if (fOverlayStore != null) {
412             fOverlayStore.stop();
413             fOverlayStore= null;
414         }
415         
416         super.dispose();
417     }
418     
419     private Button addCheckBox(Composite parent, String JavaDoc label, String JavaDoc key, int indentation) {
420         Button checkBox= new Button(parent, SWT.CHECK);
421         checkBox.setText(label);
422         
423         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
424         gd.horizontalIndent= indentation;
425         gd.horizontalSpan= 2;
426         checkBox.setLayoutData(gd);
427         checkBox.addSelectionListener(fCheckBoxListener);
428         
429         fCheckBoxes.put(checkBox, key);
430         
431         return checkBox;
432     }
433     
434     private Control addTextField(Composite composite, String JavaDoc label, String JavaDoc key, int textLimit, int indentation, boolean isNumber) {
435         
436         Label labelControl= new Label(composite, SWT.NONE);
437         labelControl.setText(label);
438         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
439         gd.horizontalIndent= indentation;
440         labelControl.setLayoutData(gd);
441         
442         Text textControl= new Text(composite, SWT.BORDER | SWT.SINGLE);
443         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
444         gd.widthHint= convertWidthInCharsToPixels(textLimit + 1);
445         textControl.setLayoutData(gd);
446         textControl.setTextLimit(textLimit);
447         fTextFields.put(textControl, key);
448         if (isNumber) {
449             fNumberFields.add(textControl);
450             textControl.addModifyListener(fNumberFieldListener);
451         } else {
452             textControl.addModifyListener(fTextFieldListener);
453         }
454             
455         return textControl;
456     }
457     
458     private void createDependency(final Button master, String JavaDoc masterKey, final Control slave) {
459         indent(slave);
460         
461         boolean masterState= fOverlayStore.getBoolean(masterKey);
462         slave.setEnabled(masterState);
463         
464         SelectionListener listener= new SelectionListener() {
465             public void widgetSelected(SelectionEvent e) {
466                 slave.setEnabled(master.getSelection());
467             }
468
469             public void widgetDefaultSelected(SelectionEvent e) {}
470         };
471         master.addSelectionListener(listener);
472         fMasterSlaveListeners.add(listener);
473     }
474     
475     private static void indent(Control control) {
476         GridData gridData= new GridData();
477         gridData.horizontalIndent= 20;
478         control.setLayoutData(gridData);
479     }
480     
481     private void numberFieldChanged(Text textControl) {
482         String JavaDoc number= textControl.getText();
483         IStatus status= validatePositiveNumber(number);
484         if (!status.matches(IStatus.ERROR))
485             fOverlayStore.setValue((String JavaDoc) fTextFields.get(textControl), number);
486         updateStatus(status);
487     }
488     
489     private IStatus validatePositiveNumber(String JavaDoc number) {
490         StatusInfo status= new StatusInfo();
491         if (number.length() == 0) {
492             status.setError(TextEditorMessages.getString("TextEditorPreferencePage.empty_input")); //$NON-NLS-1$
493
} else {
494             try {
495                 int value= Integer.parseInt(number);
496                 if (value < 0)
497                     status.setError(TextEditorMessages.getFormattedString("TextEditorPreferencePage.invalid_input", number)); //$NON-NLS-1$
498
} catch (NumberFormatException JavaDoc e) {
499                 status.setError(TextEditorMessages.getFormattedString("TextEditorPreferencePage.invalid_input", number)); //$NON-NLS-1$
500
}
501         }
502         return status;
503     }
504     
505     void updateStatus(IStatus status) {
506         if (!fFieldsInitialized)
507             return;
508         
509         if (!status.matches(IStatus.ERROR)) {
510             for (int i= 0; i < fNumberFields.size(); i++) {
511                 Text text= (Text) fNumberFields.get(i);
512                 IStatus s= validatePositiveNumber(text.getText());
513                 status= s.getSeverity() > status.getSeverity() ? s : status;
514             }
515         }
516         setValid(!status.matches(IStatus.ERROR));
517         applyToStatusLine(this, status);
518     }
519
520     /**
521      * Applies the status to the status line of a dialog page.
522      *
523      * @param page the dialog page
524      * @param status the status
525      */

526     public void applyToStatusLine(DialogPage page, IStatus status) {
527         String JavaDoc message= status.getMessage();
528         switch (status.getSeverity()) {
529             case IStatus.OK:
530                 page.setMessage(message, IMessageProvider.NONE);
531                 page.setErrorMessage(null);
532                 break;
533             case IStatus.WARNING:
534                 page.setMessage(message, IMessageProvider.WARNING);
535                 page.setErrorMessage(null);
536                 break;
537             case IStatus.INFO:
538                 page.setMessage(message, IMessageProvider.INFORMATION);
539                 page.setErrorMessage(null);
540                 break;
541             default:
542                 if (message.length() == 0) {
543                     message= null;
544                 }
545                 page.setMessage(null);
546                 page.setErrorMessage(message);
547                 break;
548         }
549     }
550 }
551
Popular Tags