KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.io.BufferedReader JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import org.eclipse.core.runtime.Preferences;
21
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.events.SelectionListener;
26 import org.eclipse.swt.graphics.Font;
27 import org.eclipse.swt.graphics.FontMetrics;
28 import org.eclipse.swt.graphics.GC;
29 import org.eclipse.swt.graphics.Point;
30 import org.eclipse.swt.graphics.RGB;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Label;
37 import org.eclipse.swt.widgets.Link;
38 import org.eclipse.swt.widgets.ScrollBar;
39 import org.eclipse.swt.widgets.Scrollable;
40
41 import org.eclipse.jface.dialogs.Dialog;
42 import org.eclipse.jface.preference.ColorSelector;
43 import org.eclipse.jface.preference.IPreferenceStore;
44 import org.eclipse.jface.preference.PreferenceConverter;
45 import org.eclipse.jface.resource.JFaceResources;
46 import org.eclipse.jface.util.PropertyChangeEvent;
47 import org.eclipse.jface.viewers.ISelectionChangedListener;
48 import org.eclipse.jface.viewers.IStructuredSelection;
49 import org.eclipse.jface.viewers.ITreeContentProvider;
50 import org.eclipse.jface.viewers.LabelProvider;
51 import org.eclipse.jface.viewers.SelectionChangedEvent;
52 import org.eclipse.jface.viewers.StructuredSelection;
53 import org.eclipse.jface.viewers.StructuredViewer;
54 import org.eclipse.jface.viewers.TreeViewer;
55 import org.eclipse.jface.viewers.Viewer;
56 import org.eclipse.jface.viewers.ViewerComparator;
57
58 import org.eclipse.jface.text.BadLocationException;
59 import org.eclipse.jface.text.Document;
60 import org.eclipse.jface.text.IDocument;
61
62 import org.eclipse.ui.dialogs.PreferencesUtil;
63 import org.eclipse.ui.texteditor.ChainedPreferenceStore;
64
65 import org.eclipse.ui.editors.text.EditorsUI;
66
67 import org.eclipse.jdt.core.JavaCore;
68
69 import org.eclipse.jdt.ui.PreferenceConstants;
70 import org.eclipse.jdt.ui.text.IColorManager;
71 import org.eclipse.jdt.ui.text.IJavaPartitions;
72
73 import org.eclipse.jdt.internal.ui.JavaPlugin;
74 import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
75 import org.eclipse.jdt.internal.ui.javaeditor.SemanticHighlighting;
76 import org.eclipse.jdt.internal.ui.javaeditor.SemanticHighlightingManager;
77 import org.eclipse.jdt.internal.ui.javaeditor.SemanticHighlightings;
78 import org.eclipse.jdt.internal.ui.javaeditor.SemanticHighlightingManager.HighlightedRange;
79 import org.eclipse.jdt.internal.ui.text.JavaColorManager;
80 import org.eclipse.jdt.internal.ui.text.PreferencesAdapter;
81 import org.eclipse.jdt.internal.ui.text.SimpleJavaSourceViewerConfiguration;
82 import org.eclipse.jdt.internal.ui.util.PixelConverter;
83
84 /**
85  * Configures Java Editor hover preferences.
86  *
87  * @since 2.1
88  */

89 class JavaEditorColoringConfigurationBlock extends AbstractConfigurationBlock {
90     
91     /**
92      * Item in the highlighting color list.
93      *
94      * @since 3.0
95      */

96     private static class HighlightingColorListItem {
97         /** Display name */
98         private String JavaDoc fDisplayName;
99         /** Color preference key */
100         private String JavaDoc fColorKey;
101         /** Bold preference key */
102         private String JavaDoc fBoldKey;
103         /** Italic preference key */
104         private String JavaDoc fItalicKey;
105         /**
106          * Strikethrough preference key.
107          * @since 3.1
108          */

109         private String JavaDoc fStrikethroughKey;
110         /** Underline preference key.
111          * @since 3.1
112          */

113         private String JavaDoc fUnderlineKey;
114         
115         /**
116          * Initialize the item with the given values.
117          * @param displayName the display name
118          * @param colorKey the color preference key
119          * @param boldKey the bold preference key
120          * @param italicKey the italic preference key
121          * @param strikethroughKey the strikethrough preference key
122          * @param underlineKey the underline preference key
123          */

124         public HighlightingColorListItem(String JavaDoc displayName, String JavaDoc colorKey, String JavaDoc boldKey, String JavaDoc italicKey, String JavaDoc strikethroughKey, String JavaDoc underlineKey) {
125             fDisplayName= displayName;
126             fColorKey= colorKey;
127             fBoldKey= boldKey;
128             fItalicKey= italicKey;
129             fStrikethroughKey= strikethroughKey;
130             fUnderlineKey= underlineKey;
131         }
132         
133         /**
134          * @return the bold preference key
135          */

136         public String JavaDoc getBoldKey() {
137             return fBoldKey;
138         }
139         
140         /**
141          * @return the bold preference key
142          */

143         public String JavaDoc getItalicKey() {
144             return fItalicKey;
145         }
146         
147         /**
148          * @return the strikethrough preference key
149          * @since 3.1
150          */

151         public String JavaDoc getStrikethroughKey() {
152             return fStrikethroughKey;
153         }
154         
155         /**
156          * @return the underline preference key
157          * @since 3.1
158          */

159         public String JavaDoc getUnderlineKey() {
160             return fUnderlineKey;
161         }
162         
163         /**
164          * @return the color preference key
165          */

166         public String JavaDoc getColorKey() {
167             return fColorKey;
168         }
169         
170         /**
171          * @return the display name
172          */

173         public String JavaDoc getDisplayName() {
174             return fDisplayName;
175         }
176     }
177     
178     private static class SemanticHighlightingColorListItem extends HighlightingColorListItem {
179     
180         /** Enablement preference key */
181         private final String JavaDoc fEnableKey;
182         
183         /**
184          * Initialize the item with the given values.
185          * @param displayName the display name
186          * @param colorKey the color preference key
187          * @param boldKey the bold preference key
188          * @param italicKey the italic preference key
189          * @param strikethroughKey the strikethroughKey preference key
190          * @param underlineKey the underlineKey preference key
191          * @param enableKey the enable preference key
192          */

193         public SemanticHighlightingColorListItem(String JavaDoc displayName, String JavaDoc colorKey, String JavaDoc boldKey, String JavaDoc italicKey, String JavaDoc strikethroughKey, String JavaDoc underlineKey, String JavaDoc enableKey) {
194             super(displayName, colorKey, boldKey, italicKey, strikethroughKey, underlineKey);
195             fEnableKey= enableKey;
196         }
197     
198         /**
199          * @return the enablement preference key
200          */

201         public String JavaDoc getEnableKey() {
202             return fEnableKey;
203         }
204     }
205
206     /**
207      * Color list label provider.
208      *
209      * @since 3.0
210      */

211     private class ColorListLabelProvider extends LabelProvider {
212         /*
213          * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
214          */

215         public String JavaDoc getText(Object JavaDoc element) {
216             if (element instanceof String JavaDoc)
217                 return (String JavaDoc) element;
218             return ((HighlightingColorListItem)element).getDisplayName();
219         }
220     }
221
222     /**
223      * Color list content provider.
224      *
225      * @since 3.0
226      */

227     private class ColorListContentProvider implements ITreeContentProvider {
228     
229         /*
230          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
231          */

232         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
233             return new String JavaDoc[] {fJavaCategory, fJavadocCategory, fCommentsCategory};
234         }
235     
236         /*
237          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
238          */

239         public void dispose() {
240         }
241     
242         /*
243          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
244          */

245         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
246         }
247
248         public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
249             if (parentElement instanceof String JavaDoc) {
250                 String JavaDoc entry= (String JavaDoc) parentElement;
251                 if (fJavaCategory.equals(entry))
252                     return fListModel.subList(7, fListModel.size()).toArray();
253                 if (fJavadocCategory.equals(entry))
254                     return fListModel.subList(0, 4).toArray();
255                 if (fCommentsCategory.equals(entry))
256                     return fListModel.subList(4, 7).toArray();
257             }
258             return new Object JavaDoc[0];
259         }
260
261         public Object JavaDoc getParent(Object JavaDoc element) {
262             if (element instanceof String JavaDoc)
263                 return null;
264             int index= fListModel.indexOf(element);
265             if (index < 4)
266                 return fJavadocCategory;
267             if (index >= 7)
268                 return fJavaCategory;
269             return fCommentsCategory;
270         }
271
272         public boolean hasChildren(Object JavaDoc element) {
273             return element instanceof String JavaDoc;
274         }
275     }
276
277     private static final String JavaDoc BOLD= PreferenceConstants.EDITOR_BOLD_SUFFIX;
278     /**
279      * Preference key suffix for italic preferences.
280      * @since 3.0
281      */

282     private static final String JavaDoc ITALIC= PreferenceConstants.EDITOR_ITALIC_SUFFIX;
283     /**
284      * Preference key suffix for strikethrough preferences.
285      * @since 3.1
286      */

287     private static final String JavaDoc STRIKETHROUGH= PreferenceConstants.EDITOR_STRIKETHROUGH_SUFFIX;
288     /**
289      * Preference key suffix for underline preferences.
290      * @since 3.1
291      */

292     private static final String JavaDoc UNDERLINE= PreferenceConstants.EDITOR_UNDERLINE_SUFFIX;
293     
294     private static final String JavaDoc COMPILER_TASK_TAGS= JavaCore.COMPILER_TASK_TAGS;
295     /**
296      * The keys of the overlay store.
297      */

298     private final String JavaDoc[][] fSyntaxColorListModel= new String JavaDoc[][] {
299             { PreferencesMessages.JavaEditorPreferencePage_javaDocKeywords, PreferenceConstants.EDITOR_JAVADOC_KEYWORD_COLOR },
300             { PreferencesMessages.JavaEditorPreferencePage_javaDocHtmlTags, PreferenceConstants.EDITOR_JAVADOC_TAG_COLOR },
301             { PreferencesMessages.JavaEditorPreferencePage_javaDocLinks, PreferenceConstants.EDITOR_JAVADOC_LINKS_COLOR },
302             { PreferencesMessages.JavaEditorPreferencePage_javaDocOthers, PreferenceConstants.EDITOR_JAVADOC_DEFAULT_COLOR },
303             { PreferencesMessages.JavaEditorPreferencePage_multiLineComment, PreferenceConstants.EDITOR_MULTI_LINE_COMMENT_COLOR },
304             { PreferencesMessages.JavaEditorPreferencePage_singleLineComment, PreferenceConstants.EDITOR_SINGLE_LINE_COMMENT_COLOR },
305             { PreferencesMessages.JavaEditorPreferencePage_javaCommentTaskTags, PreferenceConstants.EDITOR_TASK_TAG_COLOR },
306             { PreferencesMessages.JavaEditorPreferencePage_keywords, PreferenceConstants.EDITOR_JAVA_KEYWORD_COLOR },
307             { PreferencesMessages.JavaEditorPreferencePage_returnKeyword, PreferenceConstants.EDITOR_JAVA_KEYWORD_RETURN_COLOR },
308             { PreferencesMessages.JavaEditorPreferencePage_operators, PreferenceConstants.EDITOR_JAVA_OPERATOR_COLOR },
309             { PreferencesMessages.JavaEditorPreferencePage_brackets, PreferenceConstants.EDITOR_JAVA_BRACKET_COLOR },
310             { PreferencesMessages.JavaEditorPreferencePage_strings, PreferenceConstants.EDITOR_STRING_COLOR },
311             { PreferencesMessages.JavaEditorPreferencePage_others, PreferenceConstants.EDITOR_JAVA_DEFAULT_COLOR },
312     };
313     
314     private final String JavaDoc fJavaCategory= PreferencesMessages.JavaEditorPreferencePage_coloring_category_java;
315     private final String JavaDoc fJavadocCategory= PreferencesMessages.JavaEditorPreferencePage_coloring_category_javadoc;
316     private final String JavaDoc fCommentsCategory= PreferencesMessages.JavaEditorPreferencePage_coloring_category_comments;
317     
318     private ColorSelector fSyntaxForegroundColorEditor;
319     private Label fColorEditorLabel;
320     private Button fBoldCheckBox;
321     private Button fEnableCheckbox;
322     /**
323      * Check box for italic preference.
324      * @since 3.0
325      */

326     private Button fItalicCheckBox;
327     /**
328      * Check box for strikethrough preference.
329      * @since 3.1
330      */

331     private Button fStrikethroughCheckBox;
332     /**
333      * Check box for underline preference.
334      * @since 3.1
335      */

336     private Button fUnderlineCheckBox;
337     /**
338      * Highlighting color list
339      * @since 3.0
340      */

341     private final java.util.List JavaDoc fListModel= new ArrayList JavaDoc();
342     /**
343      * Highlighting color list viewer
344      * @since 3.0
345      */

346     private StructuredViewer fListViewer;
347     /**
348      * Semantic highlighting manager
349      * @since 3.0
350      */

351     private SemanticHighlightingManager fSemanticHighlightingManager;
352     /**
353      * The previewer.
354      * @since 3.0
355      */

356     private JavaSourceViewer fPreviewViewer;
357     /**
358      * The color manager.
359      * @since 3.1
360      */

361     private IColorManager fColorManager;
362     /**
363      * The font metrics.
364      * @since 3.1
365      */

366     private FontMetrics fFontMetrics;
367
368     public JavaEditorColoringConfigurationBlock(OverlayPreferenceStore store) {
369         super(store);
370         
371         fColorManager= new JavaColorManager(false);
372         
373         for (int i= 0, n= fSyntaxColorListModel.length; i < n; i++)
374             fListModel.add(new HighlightingColorListItem (fSyntaxColorListModel[i][0], fSyntaxColorListModel[i][1], fSyntaxColorListModel[i][1] + BOLD, fSyntaxColorListModel[i][1] + ITALIC, fSyntaxColorListModel[i][1] + STRIKETHROUGH, fSyntaxColorListModel[i][1] + UNDERLINE));
375
376         SemanticHighlighting[] semanticHighlightings= SemanticHighlightings.getSemanticHighlightings();
377         for (int i= 0, n= semanticHighlightings.length; i < n; i++)
378             fListModel.add(
379                     new SemanticHighlightingColorListItem(
380                             semanticHighlightings[i].getDisplayName(),
381                             SemanticHighlightings.getColorPreferenceKey(semanticHighlightings[i]),
382                             SemanticHighlightings.getBoldPreferenceKey(semanticHighlightings[i]),
383                             SemanticHighlightings.getItalicPreferenceKey(semanticHighlightings[i]),
384                             SemanticHighlightings.getStrikethroughPreferenceKey(semanticHighlightings[i]),
385                             SemanticHighlightings.getUnderlinePreferenceKey(semanticHighlightings[i]),
386                             SemanticHighlightings.getEnabledPreferenceKey(semanticHighlightings[i])
387                     ));
388         
389         store.addKeys(createOverlayStoreKeys());
390     }
391
392     private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
393         
394         ArrayList JavaDoc overlayKeys= new ArrayList JavaDoc();
395         
396         for (int i= 0, n= fListModel.size(); i < n; i++) {
397             HighlightingColorListItem item= (HighlightingColorListItem) fListModel.get(i);
398             overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, item.getColorKey()));
399             overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, item.getBoldKey()));
400             overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, item.getItalicKey()));
401             overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, item.getStrikethroughKey()));
402             overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, item.getUnderlineKey()));
403             
404             if (item instanceof SemanticHighlightingColorListItem)
405                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, ((SemanticHighlightingColorListItem) item).getEnableKey()));
406         }
407         
408         OverlayPreferenceStore.OverlayKey[] keys= new OverlayPreferenceStore.OverlayKey[overlayKeys.size()];
409         overlayKeys.toArray(keys);
410         return keys;
411     }
412
413     /**
414      * Creates page for hover preferences.
415      *
416      * @param parent the parent composite
417      * @return the control for the preference page
418      */

419     public Control createControl(Composite parent) {
420         initializeDialogUnits(parent);
421
422         ScrolledPageContent scrolled= new ScrolledPageContent(parent, SWT.H_SCROLL | SWT.V_SCROLL);
423         scrolled.setExpandHorizontal(true);
424         scrolled.setExpandVertical(true);
425         
426         Control control= createSyntaxPage(scrolled);
427         
428         scrolled.setContent(control);
429         final Point size= control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
430         scrolled.setMinSize(size.x, size.y);
431         
432         return scrolled;
433     }
434     
435     /**
436      * Returns the number of pixels corresponding to the width of the given
437      * number of characters.
438      * <p>
439      * This method may only be called after <code>initializeDialogUnits</code>
440      * has been called.
441      * </p>
442      * <p>
443      * Clients may call this framework method, but should not override it.
444      * </p>
445      *
446      * @param chars
447      * the number of characters
448      * @return the number of pixels
449      */

450     private int convertWidthInCharsToPixels(int chars) {
451         // test for failure to initialize for backward compatibility
452
if (fFontMetrics == null)
453             return 0;
454         return Dialog.convertWidthInCharsToPixels(fFontMetrics, chars);
455     }
456
457     /**
458      * Returns the number of pixels corresponding to the height of the given
459      * number of characters.
460      * <p>
461      * This method may only be called after <code>initializeDialogUnits</code>
462      * has been called.
463      * </p>
464      * <p>
465      * Clients may call this framework method, but should not override it.
466      * </p>
467      *
468      * @param chars
469      * the number of characters
470      * @return the number of pixels
471      */

472     private int convertHeightInCharsToPixels(int chars) {
473         // test for failure to initialize for backward compatibility
474
if (fFontMetrics == null)
475             return 0;
476         return Dialog.convertHeightInCharsToPixels(fFontMetrics, chars);
477     }
478     
479     public void initialize() {
480         super.initialize();
481         
482         fListViewer.setInput(fListModel);
483         fListViewer.setSelection(new StructuredSelection(fJavaCategory));
484     }
485
486     public void performDefaults() {
487         super.performDefaults();
488         
489         handleSyntaxColorListSelection();
490
491         uninstallSemanticHighlighting();
492         installSemanticHighlighting();
493
494         fPreviewViewer.invalidateTextPresentation();
495     }
496
497     /*
498      * @see org.eclipse.jdt.internal.ui.preferences.IPreferenceConfigurationBlock#dispose()
499      */

500     public void dispose() {
501         uninstallSemanticHighlighting();
502         fColorManager.dispose();
503         
504         super.dispose();
505     }
506
507     private void handleSyntaxColorListSelection() {
508         HighlightingColorListItem item= getHighlightingColorListItem();
509         if (item == null) {
510             fEnableCheckbox.setEnabled(false);
511             fSyntaxForegroundColorEditor.getButton().setEnabled(false);
512             fColorEditorLabel.setEnabled(false);
513             fBoldCheckBox.setEnabled(false);
514             fItalicCheckBox.setEnabled(false);
515             fStrikethroughCheckBox.setEnabled(false);
516             fUnderlineCheckBox.setEnabled(false);
517             return;
518         }
519         RGB rgb= PreferenceConverter.getColor(getPreferenceStore(), item.getColorKey());
520         fSyntaxForegroundColorEditor.setColorValue(rgb);
521         fBoldCheckBox.setSelection(getPreferenceStore().getBoolean(item.getBoldKey()));
522         fItalicCheckBox.setSelection(getPreferenceStore().getBoolean(item.getItalicKey()));
523         fStrikethroughCheckBox.setSelection(getPreferenceStore().getBoolean(item.getStrikethroughKey()));
524         fUnderlineCheckBox.setSelection(getPreferenceStore().getBoolean(item.getUnderlineKey()));
525         if (item instanceof SemanticHighlightingColorListItem) {
526             fEnableCheckbox.setEnabled(true);
527             boolean enable= getPreferenceStore().getBoolean(((SemanticHighlightingColorListItem) item).getEnableKey());
528             fEnableCheckbox.setSelection(enable);
529             fSyntaxForegroundColorEditor.getButton().setEnabled(enable);
530             fColorEditorLabel.setEnabled(enable);
531             fBoldCheckBox.setEnabled(enable);
532             fItalicCheckBox.setEnabled(enable);
533             fStrikethroughCheckBox.setEnabled(enable);
534             fUnderlineCheckBox.setEnabled(enable);
535         } else {
536             fSyntaxForegroundColorEditor.getButton().setEnabled(true);
537             fColorEditorLabel.setEnabled(true);
538             fBoldCheckBox.setEnabled(true);
539             fItalicCheckBox.setEnabled(true);
540             fStrikethroughCheckBox.setEnabled(true);
541             fUnderlineCheckBox.setEnabled(true);
542             fEnableCheckbox.setEnabled(false);
543             fEnableCheckbox.setSelection(true);
544         }
545     }
546     
547     private Control createSyntaxPage(final Composite parent) {
548         
549         Composite colorComposite= new Composite(parent, SWT.NONE);
550         GridLayout layout= new GridLayout();
551         layout.marginHeight= 0;
552         layout.marginWidth= 0;
553         colorComposite.setLayout(layout);
554
555         Link link= new Link(colorComposite, SWT.NONE);
556         link.setText(PreferencesMessages.JavaEditorColoringConfigurationBlock_link);
557         link.addSelectionListener(new SelectionAdapter() {
558             public void widgetSelected(SelectionEvent e) {
559                 PreferencesUtil.createPreferenceDialogOn(parent.getShell(), e.text, null, null);
560             }
561         });
562         // TODO replace by link-specific tooltips when
563
// bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=88866 gets fixed
564
// link.setToolTipText(PreferencesMessages.JavaEditorColoringConfigurationBlock_link_tooltip);
565

566         GridData gridData= new GridData(SWT.FILL, SWT.BEGINNING, true, false);
567         gridData.widthHint= 150; // only expand further if anyone else requires it
568
gridData.horizontalSpan= 2;
569         link.setLayoutData(gridData);
570
571         addFiller(colorComposite, 1);
572         
573         Label label;
574         label= new Label(colorComposite, SWT.LEFT);
575         label.setText(PreferencesMessages.JavaEditorPreferencePage_coloring_element);
576         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
577     
578         Composite editorComposite= new Composite(colorComposite, SWT.NONE);
579         layout= new GridLayout();
580         layout.numColumns= 2;
581         layout.marginHeight= 0;
582         layout.marginWidth= 0;
583         editorComposite.setLayout(layout);
584         GridData gd= new GridData(SWT.FILL, SWT.BEGINNING, true, false);
585         editorComposite.setLayoutData(gd);
586     
587         fListViewer= new TreeViewer(editorComposite, SWT.SINGLE | SWT.BORDER);
588         fListViewer.setLabelProvider(new ColorListLabelProvider());
589         fListViewer.setContentProvider(new ColorListContentProvider());
590         fListViewer.setComparator(new ViewerComparator() {
591             public int category(Object JavaDoc element) {
592                 // don't sort the top level categories
593
if (fJavaCategory.equals(element))
594                     return 0;
595                 if (fJavadocCategory.equals(element))
596                     return 1;
597                 if (fCommentsCategory.equals(element))
598                     return 2;
599                 // to sort semantic settings after partition based ones:
600
// if (element instanceof SemanticHighlightingColorListItem)
601
// return 1;
602
return 0;
603             }
604         });
605         gd= new GridData(SWT.BEGINNING, SWT.BEGINNING, false, true);
606         gd.heightHint= convertHeightInCharsToPixels(9);
607         int maxWidth= 0;
608         for (Iterator JavaDoc it= fListModel.iterator(); it.hasNext();) {
609             HighlightingColorListItem item= (HighlightingColorListItem) it.next();
610             maxWidth= Math.max(maxWidth, convertWidthInCharsToPixels(item.getDisplayName().length()));
611         }
612         ScrollBar vBar= ((Scrollable) fListViewer.getControl()).getVerticalBar();
613         if (vBar != null)
614             maxWidth += vBar.getSize().x * 3; // scrollbars and tree indentation guess
615
gd.widthHint= maxWidth;
616         
617         fListViewer.getControl().setLayoutData(gd);
618                         
619         Composite stylesComposite= new Composite(editorComposite, SWT.NONE);
620         layout= new GridLayout();
621         layout.marginHeight= 0;
622         layout.marginWidth= 0;
623         layout.numColumns= 2;
624         stylesComposite.setLayout(layout);
625         stylesComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
626         
627         fEnableCheckbox= new Button(stylesComposite, SWT.CHECK);
628         fEnableCheckbox.setText(PreferencesMessages.JavaEditorPreferencePage_enable);
629         gd= new GridData(GridData.FILL_HORIZONTAL);
630         gd.horizontalAlignment= GridData.BEGINNING;
631         gd.horizontalSpan= 2;
632         fEnableCheckbox.setLayoutData(gd);
633         
634         fColorEditorLabel= new Label(stylesComposite, SWT.LEFT);
635         fColorEditorLabel.setText(PreferencesMessages.JavaEditorPreferencePage_color);
636         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
637         gd.horizontalIndent= 20;
638         fColorEditorLabel.setLayoutData(gd);
639     
640         fSyntaxForegroundColorEditor= new ColorSelector(stylesComposite);
641         Button foregroundColorButton= fSyntaxForegroundColorEditor.getButton();
642         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
643         foregroundColorButton.setLayoutData(gd);
644         
645         fBoldCheckBox= new Button(stylesComposite, SWT.CHECK);
646         fBoldCheckBox.setText(PreferencesMessages.JavaEditorPreferencePage_bold);
647         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
648         gd.horizontalIndent= 20;
649         gd.horizontalSpan= 2;
650         fBoldCheckBox.setLayoutData(gd);
651         
652         fItalicCheckBox= new Button(stylesComposite, SWT.CHECK);
653         fItalicCheckBox.setText(PreferencesMessages.JavaEditorPreferencePage_italic);
654         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
655         gd.horizontalIndent= 20;
656         gd.horizontalSpan= 2;
657         fItalicCheckBox.setLayoutData(gd);
658         
659         fStrikethroughCheckBox= new Button(stylesComposite, SWT.CHECK);
660         fStrikethroughCheckBox.setText(PreferencesMessages.JavaEditorPreferencePage_strikethrough);
661         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
662         gd.horizontalIndent= 20;
663         gd.horizontalSpan= 2;
664         fStrikethroughCheckBox.setLayoutData(gd);
665         
666         fUnderlineCheckBox= new Button(stylesComposite, SWT.CHECK);
667         fUnderlineCheckBox.setText(PreferencesMessages.JavaEditorPreferencePage_underline);
668         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
669         gd.horizontalIndent= 20;
670         gd.horizontalSpan= 2;
671         fUnderlineCheckBox.setLayoutData(gd);
672         
673         label= new Label(colorComposite, SWT.LEFT);
674         label.setText(PreferencesMessages.JavaEditorPreferencePage_preview);
675         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
676         
677         Control previewer= createPreviewer(colorComposite);
678         gd= new GridData(GridData.FILL_BOTH);
679         gd.widthHint= convertWidthInCharsToPixels(20);
680         gd.heightHint= convertHeightInCharsToPixels(5);
681         previewer.setLayoutData(gd);
682         
683         fListViewer.addSelectionChangedListener(new ISelectionChangedListener() {
684             public void selectionChanged(SelectionChangedEvent event) {
685                 handleSyntaxColorListSelection();
686             }
687         });
688         
689         foregroundColorButton.addSelectionListener(new SelectionListener() {
690             public void widgetDefaultSelected(SelectionEvent e) {
691                 // do nothing
692
}
693             public void widgetSelected(SelectionEvent e) {
694                 HighlightingColorListItem item= getHighlightingColorListItem();
695                 PreferenceConverter.setValue(getPreferenceStore(), item.getColorKey(), fSyntaxForegroundColorEditor.getColorValue());
696             }
697         });
698     
699         fBoldCheckBox.addSelectionListener(new SelectionListener() {
700             public void widgetDefaultSelected(SelectionEvent e) {
701                 // do nothing
702
}
703             public void widgetSelected(SelectionEvent e) {
704                 HighlightingColorListItem item= getHighlightingColorListItem();
705                 getPreferenceStore().setValue(item.getBoldKey(), fBoldCheckBox.getSelection());
706             }
707         });
708                 
709         fItalicCheckBox.addSelectionListener(new SelectionListener() {
710             public void widgetDefaultSelected(SelectionEvent e) {
711                 // do nothing
712
}
713             public void widgetSelected(SelectionEvent e) {
714                 HighlightingColorListItem item= getHighlightingColorListItem();
715                 getPreferenceStore().setValue(item.getItalicKey(), fItalicCheckBox.getSelection());
716             }
717         });
718         fStrikethroughCheckBox.addSelectionListener(new SelectionListener() {
719             public void widgetDefaultSelected(SelectionEvent e) {
720                 // do nothing
721
}
722             public void widgetSelected(SelectionEvent e) {
723                 HighlightingColorListItem item= getHighlightingColorListItem();
724                 getPreferenceStore().setValue(item.getStrikethroughKey(), fStrikethroughCheckBox.getSelection());
725             }
726         });
727         
728         fUnderlineCheckBox.addSelectionListener(new SelectionListener() {
729             public void widgetDefaultSelected(SelectionEvent e) {
730                 // do nothing
731
}
732             public void widgetSelected(SelectionEvent e) {
733                 HighlightingColorListItem item= getHighlightingColorListItem();
734                 getPreferenceStore().setValue(item.getUnderlineKey(), fUnderlineCheckBox.getSelection());
735             }
736         });
737                 
738         fEnableCheckbox.addSelectionListener(new SelectionListener() {
739             public void widgetDefaultSelected(SelectionEvent e) {
740                 // do nothing
741
}
742             public void widgetSelected(SelectionEvent e) {
743                 HighlightingColorListItem item= getHighlightingColorListItem();
744                 if (item instanceof SemanticHighlightingColorListItem) {
745                     boolean enable= fEnableCheckbox.getSelection();
746                     getPreferenceStore().setValue(((SemanticHighlightingColorListItem) item).getEnableKey(), enable);
747                     fEnableCheckbox.setSelection(enable);
748                     fSyntaxForegroundColorEditor.getButton().setEnabled(enable);
749                     fColorEditorLabel.setEnabled(enable);
750                     fBoldCheckBox.setEnabled(enable);
751                     fItalicCheckBox.setEnabled(enable);
752                     fStrikethroughCheckBox.setEnabled(enable);
753                     fUnderlineCheckBox.setEnabled(enable);
754                     uninstallSemanticHighlighting();
755                     installSemanticHighlighting();
756                 }
757             }
758         });
759         
760         colorComposite.layout(false);
761                 
762         return colorComposite;
763     }
764     
765     private void addFiller(Composite composite, int horizontalSpan) {
766         PixelConverter pixelConverter= new PixelConverter(composite);
767         Label filler= new Label(composite, SWT.LEFT );
768         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
769         gd.horizontalSpan= horizontalSpan;
770         gd.heightHint= pixelConverter.convertHeightInCharsToPixels(1) / 2;
771         filler.setLayoutData(gd);
772     }
773
774     private Control createPreviewer(Composite parent) {
775         
776         IPreferenceStore generalTextStore= EditorsUI.getPreferenceStore();
777         IPreferenceStore store= new ChainedPreferenceStore(new IPreferenceStore[] { getPreferenceStore(), new PreferencesAdapter(createTemporaryCorePreferenceStore()), generalTextStore });
778         fPreviewViewer= new JavaSourceViewer(parent, null, null, false, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER, store);
779         SimpleJavaSourceViewerConfiguration configuration= new SimpleJavaSourceViewerConfiguration(fColorManager, store, null, IJavaPartitions.JAVA_PARTITIONING, false);
780         fPreviewViewer.configure(configuration);
781         // fake 1.5 source to get 1.5 features right.
782
configuration.handlePropertyChangeEvent(new PropertyChangeEvent(this, JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_4, JavaCore.VERSION_1_5));
783         Font font= JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT);
784         fPreviewViewer.getTextWidget().setFont(font);
785         new JavaSourcePreviewerUpdater(fPreviewViewer, configuration, store);
786         fPreviewViewer.setEditable(false);
787         
788         String JavaDoc content= loadPreviewContentFromFile("ColorSettingPreviewCode.txt"); //$NON-NLS-1$
789
IDocument document= new Document(content);
790         JavaPlugin.getDefault().getJavaTextTools().setupJavaDocumentPartitioner(document, IJavaPartitions.JAVA_PARTITIONING);
791         fPreviewViewer.setDocument(document);
792     
793         installSemanticHighlighting();
794         
795         return fPreviewViewer.getControl();
796     }
797
798
799     private Preferences createTemporaryCorePreferenceStore() {
800         Preferences result= new Preferences();
801         
802         result.setValue(COMPILER_TASK_TAGS, "TASK,TODO"); //$NON-NLS-1$
803

804         return result;
805     }
806
807
808     private String JavaDoc loadPreviewContentFromFile(String JavaDoc filename) {
809         String JavaDoc line;
810         String JavaDoc separator= System.getProperty("line.separator"); //$NON-NLS-1$
811
StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(512);
812         BufferedReader JavaDoc reader= null;
813         try {
814             reader= new BufferedReader JavaDoc(new InputStreamReader JavaDoc(getClass().getResourceAsStream(filename)));
815             while ((line= reader.readLine()) != null) {
816                 buffer.append(line);
817                 buffer.append(separator);
818             }
819         } catch (IOException JavaDoc io) {
820             JavaPlugin.log(io);
821         } finally {
822             if (reader != null) {
823                 try { reader.close(); } catch (IOException JavaDoc e) {}
824             }
825         }
826         return buffer.toString();
827     }
828
829
830     /**
831      * Install Semantic Highlighting on the previewer
832      *
833      * @since 3.0
834      */

835     private void installSemanticHighlighting() {
836         if (fSemanticHighlightingManager == null) {
837             fSemanticHighlightingManager= new SemanticHighlightingManager();
838             fSemanticHighlightingManager.install(fPreviewViewer, fColorManager, getPreferenceStore(), createPreviewerRanges());
839         }
840     }
841
842
843     /**
844      * Uninstall Semantic Highlighting from the previewer
845      *
846      * @since 3.0
847      */

848     private void uninstallSemanticHighlighting() {
849         if (fSemanticHighlightingManager != null) {
850             fSemanticHighlightingManager.uninstall();
851             fSemanticHighlightingManager= null;
852         }
853     }
854
855
856     /**
857      * Create the hard coded previewer ranges
858      *
859      * @return the hard coded previewer ranges
860      * @since 3.0
861      */

862     private SemanticHighlightingManager.HighlightedRange[][] createPreviewerRanges() {
863         return new SemanticHighlightingManager.HighlightedRange[][] {
864             { createHighlightedRange( 6, 13, 9, SemanticHighlightings.DEPRECATED_MEMBER), createHighlightedRange( 6, 13, 9, SemanticHighlightings.CLASS), },
865             { createHighlightedRange( 6, 23, 1, SemanticHighlightings.TYPE_VARIABLE), createHighlightedRange( 6, 23, 1, SemanticHighlightings.TYPE_ARGUMENT), },
866             { createHighlightedRange( 6, 37, 9, SemanticHighlightings.INTERFACE) },
867             { createHighlightedRange( 6, 47, 6, SemanticHighlightings.TYPE_ARGUMENT), createHighlightedRange( 6, 47, 6, SemanticHighlightings.CLASS) },
868             { createHighlightedRange( 7, 6, 5, SemanticHighlightings.ENUM), },
869             { createHighlightedRange( 7, 14, 3, SemanticHighlightings.STATIC_FINAL_FIELD), createHighlightedRange(7, 14, 3, SemanticHighlightings.STATIC_FIELD), createHighlightedRange(7, 14, 3, SemanticHighlightings.FIELD) },
870             { createHighlightedRange( 7, 19, 5, SemanticHighlightings.STATIC_FINAL_FIELD), createHighlightedRange(7, 19, 5, SemanticHighlightings.STATIC_FIELD), createHighlightedRange(7, 19, 5, SemanticHighlightings.FIELD) },
871             { createHighlightedRange( 7, 26, 4, SemanticHighlightings.STATIC_FINAL_FIELD), createHighlightedRange(7, 26, 4, SemanticHighlightings.STATIC_FIELD), createHighlightedRange(7, 26, 4, SemanticHighlightings.FIELD) },
872             { createHighlightedRange( 9, 8, 6, SemanticHighlightings.CLASS), },
873             { createHighlightedRange( 9, 15, 11, SemanticHighlightings.STATIC_FIELD), createHighlightedRange(9, 15, 11, SemanticHighlightings.FIELD) },
874             { createHighlightedRange(11, 9, 1, SemanticHighlightings.TYPE_VARIABLE) },
875             { createHighlightedRange(11, 11, 5, SemanticHighlightings.FIELD) },
876             { createHighlightedRange(13, 2, 16, SemanticHighlightings.ANNOTATION) },
877             { createHighlightedRange(13, 19, 5, SemanticHighlightings.ANNOTATION_ELEMENT_REFERENCE) },
878             { createHighlightedRange(14, 12, 3, SemanticHighlightings.METHOD_DECLARATION), createHighlightedRange(14, 12, 3, SemanticHighlightings.METHOD) },
879             { createHighlightedRange(14, 24, 9, SemanticHighlightings.PARAMETER_VARIABLE) },
880             { createHighlightedRange(15, 2, 14, SemanticHighlightings.ABSTRACT_METHOD_INVOCATION), createHighlightedRange(15, 2, 14, SemanticHighlightings.METHOD) },
881             { createHighlightedRange(16, 6, 5, SemanticHighlightings.LOCAL_VARIABLE_DECLARATION) },
882             { createHighlightedRange(16, 16, 8, SemanticHighlightings.INHERITED_METHOD_INVOCATION), createHighlightedRange(16, 16, 8, SemanticHighlightings.METHOD) },
883             { createHighlightedRange(17, 2, 12, SemanticHighlightings.STATIC_METHOD_INVOCATION), createHighlightedRange(17, 2, 12, SemanticHighlightings.METHOD) },
884             { createHighlightedRange(18, 9, 3, SemanticHighlightings.METHOD) },
885             { createHighlightedRange(18, 13, 5, SemanticHighlightings.LOCAL_VARIABLE) },
886             { createHighlightedRange(18, 22, 9, SemanticHighlightings.AUTOBOXING) },
887         };
888     }
889
890
891     /**
892      * Create a highlighted range on the previewers document with the given line, column, length and key.
893      *
894      * @param line the line
895      * @param column the column
896      * @param length the length
897      * @param key the key
898      * @return the highlighted range
899      * @since 3.0
900      */

901     private HighlightedRange createHighlightedRange(int line, int column, int length, String JavaDoc key) {
902         try {
903             IDocument document= fPreviewViewer.getDocument();
904             int offset= document.getLineOffset(line) + column;
905             return new HighlightedRange(offset, length, key);
906         } catch (BadLocationException x) {
907             JavaPlugin.log(x);
908         }
909         return null;
910     }
911
912
913     /**
914      * Returns the current highlighting color list item.
915      *
916      * @return the current highlighting color list item
917      * @since 3.0
918      */

919     private HighlightingColorListItem getHighlightingColorListItem() {
920         IStructuredSelection selection= (IStructuredSelection) fListViewer.getSelection();
921         Object JavaDoc element= selection.getFirstElement();
922         if (element instanceof String JavaDoc)
923             return null;
924         return (HighlightingColorListItem) element;
925     }
926     
927     /**
928      * Initializes the computation of horizontal and vertical dialog units based
929      * on the size of current font.
930      * <p>
931      * This method must be called before any of the dialog unit based conversion
932      * methods are called.
933      * </p>
934      *
935      * @param testControl
936      * a control from which to obtain the current font
937      */

938     private void initializeDialogUnits(Control testControl) {
939         // Compute and store a font metric
940
GC gc = new GC(testControl);
941         gc.setFont(JFaceResources.getDialogFont());
942         fFontMetrics = gc.getFontMetrics();
943         gc.dispose();
944     }
945 }
946
Popular Tags