KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > JavaEditorAppearanceConfigurationBlock


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

11
12 package org.eclipse.jdt.internal.ui.preferences;
13
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.events.SelectionListener;
20 import org.eclipse.swt.graphics.FontMetrics;
21 import org.eclipse.swt.graphics.GC;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.graphics.RGB;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Link;
31 import org.eclipse.swt.widgets.List;
32 import org.eclipse.swt.widgets.Shell;
33
34 import org.eclipse.jface.dialogs.Dialog;
35 import org.eclipse.jface.preference.ColorSelector;
36 import org.eclipse.jface.preference.PreferenceConverter;
37 import org.eclipse.jface.preference.PreferencePage;
38 import org.eclipse.jface.resource.JFaceResources;
39
40 import org.eclipse.ui.dialogs.PreferencesUtil;
41
42 import org.eclipse.jdt.ui.PreferenceConstants;
43
44 import org.eclipse.jdt.internal.ui.util.PixelConverter;
45
46 /**
47  * Configures Java Editor hover preferences.
48  *
49  * @since 2.1
50  */

51 class JavaEditorAppearanceConfigurationBlock extends AbstractConfigurationBlock {
52
53     private final String JavaDoc[][] fAppearanceColorListModel= new String JavaDoc[][] {
54             {PreferencesMessages.JavaEditorPreferencePage_matchingBracketsHighlightColor2, PreferenceConstants.EDITOR_MATCHING_BRACKETS_COLOR, null},
55             {PreferencesMessages.JavaEditorPreferencePage_backgroundForCompletionProposals, PreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND, null },
56             {PreferencesMessages.JavaEditorPreferencePage_foregroundForCompletionProposals, PreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND, null },
57             {PreferencesMessages.JavaEditorPreferencePage_backgroundForMethodParameters, PreferenceConstants.CODEASSIST_PARAMETERS_BACKGROUND, null },
58             {PreferencesMessages.JavaEditorPreferencePage_foregroundForMethodParameters, PreferenceConstants.CODEASSIST_PARAMETERS_FOREGROUND, null },
59             {PreferencesMessages.JavaEditorPreferencePage_backgroundForCompletionReplacement, PreferenceConstants.CODEASSIST_REPLACEMENT_BACKGROUND, null },
60             {PreferencesMessages.JavaEditorPreferencePage_foregroundForCompletionReplacement, PreferenceConstants.CODEASSIST_REPLACEMENT_FOREGROUND, null },
61             {PreferencesMessages.JavaEditorPreferencePage_sourceHoverBackgroundColor, PreferenceConstants.EDITOR_SOURCE_HOVER_BACKGROUND_COLOR, PreferenceConstants.EDITOR_SOURCE_HOVER_BACKGROUND_COLOR_SYSTEM_DEFAULT},
62             
63         };
64
65     private List JavaDoc fAppearanceColorList;
66     private ColorSelector fAppearanceColorEditor;
67     private Button fAppearanceColorDefault;
68
69     private FontMetrics fFontMetrics;
70
71     public JavaEditorAppearanceConfigurationBlock(PreferencePage mainPreferencePage, OverlayPreferenceStore store) {
72         super(store, mainPreferencePage);
73         getPreferenceStore().addKeys(createOverlayStoreKeys());
74     }
75
76
77     private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
78         
79         ArrayList JavaDoc overlayKeys= new ArrayList JavaDoc();
80
81         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, PreferenceConstants.EDITOR_MATCHING_BRACKETS_COLOR));
82         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_MATCHING_BRACKETS));
83         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_QUICKASSIST_LIGHTBULB));
84         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_SUB_WORD_NAVIGATION));
85         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_EVALUTE_TEMPORARY_PROBLEMS));
86         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_SHOW_SEGMENTS));
87         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, PreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND));
88         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, PreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND));
89         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, PreferenceConstants.CODEASSIST_PARAMETERS_BACKGROUND));
90         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, PreferenceConstants.CODEASSIST_PARAMETERS_FOREGROUND));
91         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, PreferenceConstants.CODEASSIST_REPLACEMENT_BACKGROUND));
92         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, PreferenceConstants.CODEASSIST_REPLACEMENT_FOREGROUND));
93         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, PreferenceConstants.EDITOR_SOURCE_HOVER_BACKGROUND_COLOR));
94         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_SOURCE_HOVER_BACKGROUND_COLOR_SYSTEM_DEFAULT));
95
96         OverlayPreferenceStore.OverlayKey[] keys= new OverlayPreferenceStore.OverlayKey[overlayKeys.size()];
97         overlayKeys.toArray(keys);
98         return keys;
99     }
100
101     /**
102      * Creates page for appearance preferences.
103      *
104      * @param parent the parent composite
105      * @return the control for the preference page
106      */

107     public Control createControl(Composite parent) {
108         initializeDialogUnits(parent);
109         
110         ScrolledPageContent scrolled= new ScrolledPageContent(parent, SWT.H_SCROLL | SWT.V_SCROLL);
111         scrolled.setExpandHorizontal(true);
112         scrolled.setExpandVertical(true);
113         
114
115         Composite composite= new Composite(scrolled, SWT.NONE);
116         GridLayout layout= new GridLayout();
117         layout.marginWidth= 0;
118         layout.marginHeight= 0;
119         composite.setLayout(layout);
120         
121         createHeader(composite);
122         createAppearancePage(composite);
123         
124         scrolled.setContent(composite);
125         final Point size= composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
126         scrolled.setMinSize(size.x, size.y);
127         return scrolled;
128
129     }
130
131     private void createHeader(Composite contents) {
132         final Shell shell= contents.getShell();
133         String JavaDoc text= PreferencesMessages.JavaEditorPreferencePage_link;
134         Link link= new Link(contents, SWT.NONE);
135         link.setText(text);
136         link.addSelectionListener(new SelectionAdapter() {
137             public void widgetSelected(SelectionEvent e) {
138                 PreferencesUtil.createPreferenceDialogOn(shell, "org.eclipse.ui.preferencePages.GeneralTextEditor", null, null); //$NON-NLS-1$
139
}
140         });
141         // TODO replace by link-specific tooltips when
142
// bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=88866 gets fixed
143
link.setToolTipText(PreferencesMessages.JavaEditorPreferencePage_link_tooltip);
144         
145         
146         GridData gridData= new GridData(SWT.FILL, SWT.BEGINNING, true, false);
147         gridData.widthHint= 150; // only expand further if anyone else requires it
148
link.setLayoutData(gridData);
149         
150         addFiller(contents);
151     }
152     
153     private void addFiller(Composite composite) {
154         PixelConverter pixelConverter= new PixelConverter(composite);
155         
156         Label filler= new Label(composite, SWT.LEFT );
157         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
158         gd.horizontalSpan= 2;
159         gd.heightHint= pixelConverter.convertHeightInCharsToPixels(1) / 2;
160         filler.setLayoutData(gd);
161     }
162     
163     /**
164      * Returns the number of pixels corresponding to the width of the given
165      * number of characters.
166      * <p>
167      * This method may only be called after <code>initializeDialogUnits</code>
168      * has been called.
169      * </p>
170      * <p>
171      * Clients may call this framework method, but should not override it.
172      * </p>
173      *
174      * @param chars
175      * the number of characters
176      * @return the number of pixels
177      */

178     protected int convertWidthInCharsToPixels(int chars) {
179         // test for failure to initialize for backward compatibility
180
if (fFontMetrics == null)
181             return 0;
182         return Dialog.convertWidthInCharsToPixels(fFontMetrics, chars);
183     }
184
185     /**
186      * Returns the number of pixels corresponding to the height of the given
187      * number of characters.
188      * <p>
189      * This method may only be called after <code>initializeDialogUnits</code>
190      * has been called.
191      * </p>
192      * <p>
193      * Clients may call this framework method, but should not override it.
194      * </p>
195      *
196      * @param chars
197      * the number of characters
198      * @return the number of pixels
199      */

200     protected int convertHeightInCharsToPixels(int chars) {
201         // test for failure to initialize for backward compatibility
202
if (fFontMetrics == null)
203             return 0;
204         return Dialog.convertHeightInCharsToPixels(fFontMetrics, chars);
205     }
206     
207     private Control createAppearancePage(Composite parent) {
208
209         Composite appearanceComposite= new Composite(parent, SWT.NONE);
210         GridLayout layout= new GridLayout();
211         layout.numColumns= 2;
212         appearanceComposite.setLayout(layout);
213
214         String JavaDoc label;
215         
216         label= PreferencesMessages.JavaEditorPreferencePage_subWordNavigation;
217         addCheckBox(appearanceComposite, label, PreferenceConstants.EDITOR_SUB_WORD_NAVIGATION, 0);
218
219         label= PreferencesMessages.JavaEditorPreferencePage_analyseAnnotationsWhileTyping;
220         addCheckBox(appearanceComposite, label, PreferenceConstants.EDITOR_EVALUTE_TEMPORARY_PROBLEMS, 0);
221         
222         String JavaDoc text= PreferencesMessages.SmartTypingConfigurationBlock_annotationReporting_link;
223         addLink(appearanceComposite, text, INDENT);
224         
225         Label spacer= new Label(appearanceComposite, SWT.LEFT );
226         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
227         gd.horizontalSpan= 2;
228         gd.heightHint= convertHeightInCharsToPixels(1) / 2;
229         spacer.setLayoutData(gd);
230         
231         label= PreferencesMessages.JavaEditorPreferencePage_highlightMatchingBrackets;
232         addCheckBox(appearanceComposite, label, PreferenceConstants.EDITOR_MATCHING_BRACKETS, 0);
233
234         label= PreferencesMessages.JavaEditorPreferencePage_quickassist_lightbulb;
235         addCheckBox(appearanceComposite, label, PreferenceConstants.EDITOR_QUICKASSIST_LIGHTBULB, 0);
236         
237         label= PreferencesMessages.JavaEditorPreferencePage_showJavaElementOnly;
238         addCheckBox(appearanceComposite, label, PreferenceConstants.EDITOR_SHOW_SEGMENTS, 0);
239
240         Label l= new Label(appearanceComposite, SWT.LEFT );
241         gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
242         gd.horizontalSpan= 2;
243         gd.heightHint= convertHeightInCharsToPixels(1) / 2;
244         l.setLayoutData(gd);
245         
246         l= new Label(appearanceComposite, SWT.LEFT);
247         l.setText(PreferencesMessages.JavaEditorPreferencePage_appearanceOptions);
248         gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
249         gd.horizontalSpan= 2;
250         l.setLayoutData(gd);
251
252         Composite editorComposite= new Composite(appearanceComposite, SWT.NONE);
253         layout= new GridLayout();
254         layout.numColumns= 2;
255         layout.marginHeight= 0;
256         layout.marginWidth= 0;
257         editorComposite.setLayout(layout);
258         gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL);
259         gd.horizontalSpan= 2;
260         editorComposite.setLayoutData(gd);
261
262         fAppearanceColorList= new List JavaDoc(editorComposite, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
263         gd= new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.FILL_HORIZONTAL);
264         gd.heightHint= convertHeightInCharsToPixels(12);
265         fAppearanceColorList.setLayoutData(gd);
266                         
267         Composite stylesComposite= new Composite(editorComposite, SWT.NONE);
268         layout= new GridLayout();
269         layout.marginHeight= 0;
270         layout.marginWidth= 0;
271         layout.numColumns= 2;
272         stylesComposite.setLayout(layout);
273         stylesComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
274         
275         l= new Label(stylesComposite, SWT.LEFT);
276         l.setText(PreferencesMessages.JavaEditorPreferencePage_color);
277         gd= new GridData();
278         gd.horizontalAlignment= GridData.BEGINNING;
279         l.setLayoutData(gd);
280
281         fAppearanceColorEditor= new ColorSelector(stylesComposite);
282         Button foregroundColorButton= fAppearanceColorEditor.getButton();
283         gd= new GridData(GridData.FILL_HORIZONTAL);
284         gd.horizontalAlignment= GridData.BEGINNING;
285         foregroundColorButton.setLayoutData(gd);
286
287         SelectionListener colorDefaultSelectionListener= new SelectionListener() {
288             public void widgetSelected(SelectionEvent e) {
289                 boolean systemDefault= fAppearanceColorDefault.getSelection();
290                 fAppearanceColorEditor.getButton().setEnabled(!systemDefault);
291                 
292                 int i= fAppearanceColorList.getSelectionIndex();
293                 if (i == -1)
294                     return;
295
296                 String JavaDoc key= fAppearanceColorListModel[i][2];
297                 if (key != null)
298                     getPreferenceStore().setValue(key, systemDefault);
299             }
300             public void widgetDefaultSelected(SelectionEvent e) {}
301         };
302         
303         fAppearanceColorDefault= new Button(stylesComposite, SWT.CHECK);
304         fAppearanceColorDefault.setText(PreferencesMessages.JavaEditorPreferencePage_systemDefault);
305         gd= new GridData(GridData.FILL_HORIZONTAL);
306         gd.horizontalAlignment= GridData.BEGINNING;
307         gd.horizontalSpan= 2;
308         fAppearanceColorDefault.setLayoutData(gd);
309         fAppearanceColorDefault.setVisible(false);
310         fAppearanceColorDefault.addSelectionListener(colorDefaultSelectionListener);
311         
312         fAppearanceColorList.addSelectionListener(new SelectionListener() {
313             public void widgetDefaultSelected(SelectionEvent e) {
314                 // do nothing
315
}
316             public void widgetSelected(SelectionEvent e) {
317                 handleAppearanceColorListSelection();
318             }
319         });
320         foregroundColorButton.addSelectionListener(new SelectionListener() {
321             public void widgetDefaultSelected(SelectionEvent e) {
322                 // do nothing
323
}
324             public void widgetSelected(SelectionEvent e) {
325                 int i= fAppearanceColorList.getSelectionIndex();
326                 if (i == -1)
327                     return;
328
329                 String JavaDoc key= fAppearanceColorListModel[i][1];
330                 PreferenceConverter.setValue(getPreferenceStore(), key, fAppearanceColorEditor.getColorValue());
331             }
332         });
333         return appearanceComposite;
334     }
335
336
337     private void addLink(Composite composite, String JavaDoc text, int indent) {
338         GridData gd;
339         final Link link= new Link(composite, SWT.NONE);
340         link.setText(text);
341         gd= new GridData(SWT.FILL, SWT.BEGINNING, true, false);
342         gd.widthHint= 300; // don't get wider initially
343
gd.horizontalSpan= 2;
344         gd.horizontalIndent= indent;
345         link.setLayoutData(gd);
346         link.addSelectionListener(new SelectionAdapter() {
347             public void widgetSelected(SelectionEvent e) {
348                 PreferencesUtil.createPreferenceDialogOn(link.getShell(), e.text, null, null);
349             }
350         });
351     }
352     
353     private void handleAppearanceColorListSelection() {
354         int i= fAppearanceColorList.getSelectionIndex();
355         if (i == -1)
356             return;
357         String JavaDoc key= fAppearanceColorListModel[i][1];
358         RGB rgb= PreferenceConverter.getColor(getPreferenceStore(), key);
359         fAppearanceColorEditor.setColorValue(rgb);
360         updateAppearanceColorWidgets(fAppearanceColorListModel[i][2]);
361     }
362
363     private void updateAppearanceColorWidgets(String JavaDoc systemDefaultKey) {
364         if (systemDefaultKey == null) {
365             fAppearanceColorDefault.setSelection(false);
366             fAppearanceColorDefault.setVisible(false);
367             fAppearanceColorEditor.getButton().setEnabled(true);
368         } else {
369             boolean systemDefault= getPreferenceStore().getBoolean(systemDefaultKey);
370             fAppearanceColorDefault.setSelection(systemDefault);
371             fAppearanceColorDefault.setVisible(true);
372             fAppearanceColorEditor.getButton().setEnabled(!systemDefault);
373         }
374     }
375
376     /*
377      * @see org.eclipse.jdt.internal.ui.preferences.IPreferenceConfigurationBlock#initialize()
378      */

379     public void initialize() {
380         super.initialize();
381         initializeDefaultColors();
382         
383         for (int i= 0; i < fAppearanceColorListModel.length; i++)
384             fAppearanceColorList.add(fAppearanceColorListModel[i][0]);
385         
386         fAppearanceColorList.getDisplay().asyncExec(new Runnable JavaDoc() {
387             public void run() {
388                 if (fAppearanceColorList != null && !fAppearanceColorList.isDisposed()) {
389                     fAppearanceColorList.select(0);
390                     handleAppearanceColorListSelection();
391                 }
392             }
393         });
394
395     }
396
397     /**
398      * Initializes the default colors.
399      *
400      * @since 3.3
401      */

402     private void initializeDefaultColors() {
403         if (getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SOURCE_HOVER_BACKGROUND_COLOR_SYSTEM_DEFAULT)) {
404             RGB rgb= fAppearanceColorList.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND).getRGB();
405             PreferenceConverter.setValue(getPreferenceStore(), PreferenceConstants.EDITOR_SOURCE_HOVER_BACKGROUND_COLOR, rgb);
406         }
407     }
408
409     /*
410      * @see org.eclipse.jdt.internal.ui.preferences.IPreferenceConfigurationBlock#performDefaults()
411      */

412     public void performDefaults() {
413         super.performDefaults();
414         initializeDefaultColors();
415         handleAppearanceColorListSelection();
416     }
417
418     /**
419      * Initializes the computation of horizontal and vertical dialog units based
420      * on the size of current font.
421      * <p>
422      * This method must be called before any of the dialog unit based conversion
423      * methods are called.
424      * </p>
425      *
426      * @param testControl
427      * a control from which to obtain the current font
428      */

429     protected void initializeDialogUnits(Control testControl) {
430         // Compute and store a font metric
431
GC gc = new GC(testControl);
432         gc.setFont(JFaceResources.getDialogFont());
433         fFontMetrics = gc.getFontMetrics();
434         gc.dispose();
435     }
436     
437 }
438
Popular Tags