KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.jdt.internal.ui.preferences;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStreamReader JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.core.runtime.IStatus;
22
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.DisposeEvent;
25 import org.eclipse.swt.events.DisposeListener;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.events.SelectionListener;
29 import org.eclipse.swt.graphics.Color;
30 import org.eclipse.swt.graphics.Font;
31 import org.eclipse.swt.graphics.RGB;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Link;
39
40 import org.eclipse.jface.dialogs.Dialog;
41 import org.eclipse.jface.preference.ColorSelector;
42 import org.eclipse.jface.preference.IPreferenceStore;
43 import org.eclipse.jface.preference.PreferenceConverter;
44 import org.eclipse.jface.preference.PreferencePage;
45 import org.eclipse.jface.resource.JFaceResources;
46 import org.eclipse.jface.util.IPropertyChangeListener;
47 import org.eclipse.jface.util.PropertyChangeEvent;
48 import org.eclipse.jface.viewers.IColorProvider;
49 import org.eclipse.jface.viewers.ISelectionChangedListener;
50 import org.eclipse.jface.viewers.IStructuredContentProvider;
51 import org.eclipse.jface.viewers.IStructuredSelection;
52 import org.eclipse.jface.viewers.LabelProvider;
53 import org.eclipse.jface.viewers.SelectionChangedEvent;
54 import org.eclipse.jface.viewers.StructuredSelection;
55 import org.eclipse.jface.viewers.TableViewer;
56 import org.eclipse.jface.viewers.Viewer;
57
58 import org.eclipse.jface.text.Document;
59 import org.eclipse.jface.text.IDocument;
60 import org.eclipse.jface.text.source.SourceViewer;
61
62 import org.eclipse.ui.IWorkbench;
63 import org.eclipse.ui.IWorkbenchPreferencePage;
64 import org.eclipse.ui.PlatformUI;
65 import org.eclipse.ui.dialogs.PreferencesUtil;
66 import org.eclipse.ui.model.WorkbenchViewerSorter;
67 import org.eclipse.ui.texteditor.ChainedPreferenceStore;
68
69 import org.eclipse.jdt.ui.PreferenceConstants;
70 import org.eclipse.jdt.ui.text.IColorManager;
71
72 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
73 import org.eclipse.jdt.internal.ui.JavaPlugin;
74 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
75 import org.eclipse.jdt.internal.ui.dialogs.StatusUtil;
76 import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
77 import org.eclipse.jdt.internal.ui.propertiesfileeditor.IPropertiesFilePartitions;
78 import org.eclipse.jdt.internal.ui.propertiesfileeditor.PropertiesFileDocumentSetupParticipant;
79 import org.eclipse.jdt.internal.ui.propertiesfileeditor.PropertiesFileSourceViewerConfiguration;
80 import org.eclipse.jdt.internal.ui.text.JavaColorManager;
81 import org.eclipse.jdt.internal.ui.util.PixelConverter;
82
83 /**
84  * The page for setting the properties file editor preferences.
85  *
86  * @since 3.1
87  */

88 public class PropertiesFileEditorPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
89     
90     
91     private static class SourcePreviewerUpdater {
92         
93         /**
94          * Creates a Java source preview updater for the given viewer, configuration and preference store.
95          *
96          * @param viewer the viewer
97          * @param configuration the configuration
98          * @param preferenceStore the preference store
99          */

100         SourcePreviewerUpdater(final SourceViewer viewer, final PropertiesFileSourceViewerConfiguration configuration, final IPreferenceStore preferenceStore) {
101             Assert.isNotNull(viewer);
102             Assert.isNotNull(configuration);
103             Assert.isNotNull(preferenceStore);
104             final IPropertyChangeListener fontChangeListener= new IPropertyChangeListener() {
105                 /*
106                  * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
107                  */

108                 public void propertyChange(PropertyChangeEvent event) {
109                     if (event.getProperty().equals(PreferenceConstants.PROPERTIES_FILE_EDITOR_TEXT_FONT)) {
110                         Font font= JFaceResources.getFont(PreferenceConstants.PROPERTIES_FILE_EDITOR_TEXT_FONT);
111                         viewer.getTextWidget().setFont(font);
112                     }
113                 }
114             };
115             final IPropertyChangeListener propertyChangeListener= new IPropertyChangeListener() {
116                 /*
117                  * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
118                  */

119                 public void propertyChange(PropertyChangeEvent event) {
120                     if (configuration.affectsTextPresentation(event)) {
121                         configuration.handlePropertyChangeEvent(event);
122                         viewer.invalidateTextPresentation();
123                     }
124                 }
125             };
126             viewer.getTextWidget().addDisposeListener(new DisposeListener() {
127                 /*
128                  * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
129                  */

130                 public void widgetDisposed(DisposeEvent e) {
131                     preferenceStore.removePropertyChangeListener(propertyChangeListener);
132                     JFaceResources.getFontRegistry().removeListener(fontChangeListener);
133                 }
134             });
135             JFaceResources.getFontRegistry().addListener(fontChangeListener);
136             preferenceStore.addPropertyChangeListener(propertyChangeListener);
137         }
138     }
139     
140     
141     /**
142      * Item in the highlighting color list.
143      */

144     private static class HighlightingColorListItem {
145         /** Display name */
146         private String JavaDoc fDisplayName;
147         /** Color preference key */
148         private String JavaDoc fColorKey;
149         /** Bold preference key */
150         private String JavaDoc fBoldKey;
151         /** Italic preference key */
152         private String JavaDoc fItalicKey;
153         /**
154          * Strikethrough preference key.
155          * @since 3.1
156          */

157         private String JavaDoc fStrikethroughKey;
158         /**
159          * Underline preference key.
160          * @since 3.1
161          */

162         private String JavaDoc fUnderlineKey;
163         /** Item color */
164         private Color fItemColor;
165         
166         /**
167          * Initialize the item with the given values.
168          *
169          * @param displayName the display name
170          * @param colorKey the color preference key
171          * @param boldKey the bold preference key
172          * @param italicKey the italic preference key
173          * @param strikethroughKey the strikethrough preference key
174          * @param underlineKey the underline preference key
175          * @param itemColor the item color
176          */

177         public HighlightingColorListItem(String JavaDoc displayName, String JavaDoc colorKey, String JavaDoc boldKey, String JavaDoc italicKey, String JavaDoc strikethroughKey, String JavaDoc underlineKey, Color itemColor) {
178             fDisplayName= displayName;
179             fColorKey= colorKey;
180             fBoldKey= boldKey;
181             fItalicKey= italicKey;
182             fStrikethroughKey= strikethroughKey;
183             fUnderlineKey= underlineKey;
184             fItemColor= itemColor;
185         }
186         
187         /**
188          * @return the bold preference key
189          */

190         public String JavaDoc getBoldKey() {
191             return fBoldKey;
192         }
193         
194         /**
195          * @return the italic preference key
196          */

197         public String JavaDoc getItalicKey() {
198             return fItalicKey;
199         }
200         /**
201          * @return the strikethrough preference key
202          * @since 3.1
203          */

204         public String JavaDoc getStrikethroughKey() {
205             return fStrikethroughKey;
206         }
207         
208         /**
209          * @return the underline preference key
210          * @since 3.1
211          */

212         public String JavaDoc getUnderlineKey() {
213             return fUnderlineKey;
214         }
215         
216         /**
217          * @return the color preference key
218          */

219         public String JavaDoc getColorKey() {
220             return fColorKey;
221         }
222         
223         /**
224          * @return the display name
225          */

226         public String JavaDoc getDisplayName() {
227             return fDisplayName;
228         }
229         
230         /**
231          * @return the item color
232          */

233         public Color getItemColor() {
234             return fItemColor;
235         }
236     }
237     
238     /**
239      * Color list label provider.
240      */

241     private class ColorListLabelProvider extends LabelProvider implements IColorProvider {
242
243         /*
244          * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
245          */

246         public String JavaDoc getText(Object JavaDoc element) {
247             return ((HighlightingColorListItem)element).getDisplayName();
248         }
249         
250         /*
251          * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
252          */

253         public Color getForeground(Object JavaDoc element) {
254             return ((HighlightingColorListItem)element).getItemColor();
255         }
256
257         /*
258          * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
259          */

260         public Color getBackground(Object JavaDoc element) {
261             return null;
262         }
263     }
264     
265     /**
266      * Color list content provider.
267      */

268     private class ColorListContentProvider implements IStructuredContentProvider {
269
270         /*
271          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
272          */

273         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
274             return ((java.util.List JavaDoc)inputElement).toArray();
275         }
276
277         /*
278          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
279          */

280         public void dispose() {
281         }
282
283         /*
284          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
285          */

286         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
287         }
288     }
289     
290     private static final String JavaDoc BOLD= PreferenceConstants.EDITOR_BOLD_SUFFIX;
291     /**
292      * Preference key suffix for italic preferences.
293      */

294     private static final String JavaDoc ITALIC= PreferenceConstants.EDITOR_ITALIC_SUFFIX;
295     
296     /**
297      * Preference key suffix for strikethrough preferences.
298      * @since 3.1
299      */

300     private static final String JavaDoc STRIKETHROUGH= PreferenceConstants.EDITOR_STRIKETHROUGH_SUFFIX;
301     
302     /**
303      * Preference key suffix for underline preferences.
304      * @since 3.1
305      */

306     private static final String JavaDoc UNDERLINE= PreferenceConstants.EDITOR_UNDERLINE_SUFFIX;
307     
308     /** The keys of the overlay store. */
309     private final String JavaDoc[][] fSyntaxColorListModel= new String JavaDoc[][] {
310         { PreferencesMessages.PropertiesFileEditorPreferencePage_key, PreferenceConstants.PROPERTIES_FILE_COLORING_KEY },
311         { PreferencesMessages.PropertiesFileEditorPreferencePage_value, PreferenceConstants.PROPERTIES_FILE_COLORING_VALUE },
312         { PreferencesMessages.PropertiesFileEditorPreferencePage_assignment, PreferenceConstants.PROPERTIES_FILE_COLORING_ASSIGNMENT },
313         { PreferencesMessages.PropertiesFileEditorPreferencePage_argument, PreferenceConstants.PROPERTIES_FILE_COLORING_ARGUMENT },
314         { PreferencesMessages.PropertiesFileEditorPreferencePage_comment, PreferenceConstants.PROPERTIES_FILE_COLORING_COMMENT }
315     };
316     
317     private OverlayPreferenceStore fOverlayStore;
318     
319     private ColorSelector fSyntaxForegroundColorEditor;
320     private Button fBoldCheckBox;
321
322     /**
323      * Check box for italic preference.
324      */

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

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

337     private Button fUnderlineCheckBox;
338     
339     private SourceViewer fPreviewViewer;
340     
341     /**
342      * Tells whether the fields are initialized.
343      */

344     private boolean fFieldsInitialized= false;
345     
346     /**
347      * List of master/slave listeners when there's a dependency.
348      *
349      * @see #createDependency(Button, String, Control)
350      */

351     private ArrayList JavaDoc fMasterSlaveListeners= new ArrayList JavaDoc();
352
353     /**
354      * Highlighting color list
355      */

356     private final List JavaDoc fHighlightingColorList= new ArrayList JavaDoc();
357     /**
358      * Highlighting color list viewer
359      */

360     private TableViewer fHighlightingColorListViewer;
361     
362     /**
363      * The color manager.
364      */

365     private IColorManager fColorManager;
366     
367
368     /**
369      * Creates a new preference page.
370      */

371     public PropertiesFileEditorPreferencePage() {
372         setPreferenceStore(JavaPlugin.getDefault().getPreferenceStore());
373         
374         fOverlayStore= new OverlayPreferenceStore(getPreferenceStore(), createOverlayStoreKeys());
375     }
376     
377     private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
378         
379         ArrayList JavaDoc overlayKeys= new ArrayList JavaDoc();
380
381         for (int i= 0; i < fSyntaxColorListModel.length; i++) {
382             String JavaDoc colorKey= fSyntaxColorListModel[i][1];
383             overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, colorKey));
384             overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, colorKey + BOLD));
385             overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, colorKey + ITALIC));
386             overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, colorKey + STRIKETHROUGH));
387             overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, colorKey + UNDERLINE));
388         }
389         
390         OverlayPreferenceStore.OverlayKey[] keys= new OverlayPreferenceStore.OverlayKey[overlayKeys.size()];
391         overlayKeys.toArray(keys);
392         return keys;
393     }
394     
395     /*
396      * @see IWorkbenchPreferencePage#init()
397      */

398     public void init(IWorkbench workbench) {
399     }
400
401     /*
402      * @see PreferencePage#createControl(Composite)
403      */

404     public void createControl(Composite parent) {
405         super.createControl(parent);
406         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.PROPERTIES_FILE_EDITOR_PREFERENCE_PAGE);
407     }
408
409     private void handleSyntaxColorListSelection() {
410         HighlightingColorListItem item= getHighlightingColorListItem();
411         RGB rgb= PreferenceConverter.getColor(fOverlayStore, item.getColorKey());
412         fSyntaxForegroundColorEditor.setColorValue(rgb);
413         fBoldCheckBox.setSelection(fOverlayStore.getBoolean(item.getBoldKey()));
414         fItalicCheckBox.setSelection(fOverlayStore.getBoolean(item.getItalicKey()));
415         fStrikethroughCheckBox.setSelection(fOverlayStore.getBoolean(item.getStrikethroughKey()));
416         fUnderlineCheckBox.setSelection(fOverlayStore.getBoolean(item.getUnderlineKey()));
417
418         fSyntaxForegroundColorEditor.getButton().setEnabled(true);
419         fBoldCheckBox.setEnabled(true);
420         fItalicCheckBox.setEnabled(true);
421         fStrikethroughCheckBox.setEnabled(true);
422         fUnderlineCheckBox.setEnabled(true);
423     }
424
425     private Control createSyntaxPage(Composite parent) {
426
427         Label label= new Label(parent, SWT.LEFT);
428         label.setText(PreferencesMessages.PropertiesFileEditorPreferencePage_foreground);
429         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
430
431         Composite editorComposite= new Composite(parent, SWT.NONE);
432         GridLayout layout= new GridLayout();
433         layout.numColumns= 2;
434         layout.marginHeight= 0;
435         layout.marginWidth= 0;
436         editorComposite.setLayout(layout);
437         GridData gd= new GridData(SWT.FILL, SWT.BEGINNING, true, false);
438         editorComposite.setLayoutData(gd);
439
440         fHighlightingColorListViewer= new TableViewer(editorComposite, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
441         fHighlightingColorListViewer.setLabelProvider(new ColorListLabelProvider());
442         fHighlightingColorListViewer.setContentProvider(new ColorListContentProvider());
443         fHighlightingColorListViewer.setComparator(new WorkbenchViewerSorter());
444         gd= new GridData(SWT.BEGINNING, SWT.FILL, false, true);
445         gd.heightHint= convertHeightInCharsToPixels(5);
446         fHighlightingColorListViewer.getControl().setLayoutData(gd);
447                         
448         Composite stylesComposite= new Composite(editorComposite, SWT.NONE);
449         layout= new GridLayout();
450         layout.marginHeight= 0;
451         layout.marginWidth= 0;
452         layout.numColumns= 2;
453         stylesComposite.setLayout(layout);
454         stylesComposite.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false));
455         
456         gd= new GridData(GridData.FILL_HORIZONTAL);
457         gd.horizontalAlignment= GridData.BEGINNING;
458         gd.horizontalSpan= 2;
459         
460         label= new Label(stylesComposite, SWT.LEFT);
461         label.setText(PreferencesMessages.PropertiesFileEditorPreferencePage_color);
462         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
463         gd.horizontalIndent= 20;
464         label.setLayoutData(gd);
465
466         fSyntaxForegroundColorEditor= new ColorSelector(stylesComposite);
467         Button foregroundColorButton= fSyntaxForegroundColorEditor.getButton();
468         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
469         foregroundColorButton.setLayoutData(gd);
470         
471         fBoldCheckBox= new Button(stylesComposite, SWT.CHECK);
472         fBoldCheckBox.setText(PreferencesMessages.PropertiesFileEditorPreferencePage_bold);
473         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
474         gd.horizontalIndent= 20;
475         gd.horizontalSpan= 2;
476         fBoldCheckBox.setLayoutData(gd);
477         
478         fItalicCheckBox= new Button(stylesComposite, SWT.CHECK);
479         fItalicCheckBox.setText(PreferencesMessages.PropertiesFileEditorPreferencePage_italic);
480         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
481         gd.horizontalIndent= 20;
482         gd.horizontalSpan= 2;
483         fItalicCheckBox.setLayoutData(gd);
484         
485         fStrikethroughCheckBox= new Button(stylesComposite, SWT.CHECK);
486         fStrikethroughCheckBox.setText(PreferencesMessages.PropertiesFileEditorPreferencePage_strikethrough);
487         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
488         gd.horizontalIndent= 20;
489         gd.horizontalSpan= 2;
490         fStrikethroughCheckBox.setLayoutData(gd);
491         
492         fUnderlineCheckBox= new Button(stylesComposite, SWT.CHECK);
493         fUnderlineCheckBox.setText(PreferencesMessages.PropertiesFileEditorPreferencePage_underline);
494         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
495         gd.horizontalIndent= 20;
496         gd.horizontalSpan= 2;
497         fUnderlineCheckBox.setLayoutData(gd);
498         
499         label= new Label(parent, SWT.LEFT);
500         label.setText(PreferencesMessages.PropertiesFileEditorPreferencePage_preview);
501         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
502         
503         Control previewer= createPreviewer(parent);
504         gd= new GridData(GridData.FILL_BOTH);
505         gd.widthHint= convertWidthInCharsToPixels(20);
506         gd.heightHint= convertHeightInCharsToPixels(5);
507         previewer.setLayoutData(gd);
508
509         
510         fHighlightingColorListViewer.addSelectionChangedListener(new ISelectionChangedListener() {
511             public void selectionChanged(SelectionChangedEvent event) {
512                 handleSyntaxColorListSelection();
513             }
514         });
515         
516         foregroundColorButton.addSelectionListener(new SelectionListener() {
517             public void widgetDefaultSelected(SelectionEvent e) {
518                 // do nothing
519
}
520             public void widgetSelected(SelectionEvent e) {
521                 HighlightingColorListItem item= getHighlightingColorListItem();
522                 PreferenceConverter.setValue(fOverlayStore, item.getColorKey(), fSyntaxForegroundColorEditor.getColorValue());
523             }
524         });
525
526         fBoldCheckBox.addSelectionListener(new SelectionListener() {
527             public void widgetDefaultSelected(SelectionEvent e) {
528                 // do nothing
529
}
530             public void widgetSelected(SelectionEvent e) {
531                 HighlightingColorListItem item= getHighlightingColorListItem();
532                 fOverlayStore.setValue(item.getBoldKey(), fBoldCheckBox.getSelection());
533             }
534         });
535                 
536         fItalicCheckBox.addSelectionListener(new SelectionListener() {
537             public void widgetDefaultSelected(SelectionEvent e) {
538                 // do nothing
539
}
540             public void widgetSelected(SelectionEvent e) {
541                 HighlightingColorListItem item= getHighlightingColorListItem();
542                 fOverlayStore.setValue(item.getItalicKey(), fItalicCheckBox.getSelection());
543             }
544         });
545         
546         fStrikethroughCheckBox.addSelectionListener(new SelectionListener() {
547             public void widgetDefaultSelected(SelectionEvent e) {
548                 // do nothing
549
}
550             public void widgetSelected(SelectionEvent e) {
551                 HighlightingColorListItem item= getHighlightingColorListItem();
552                 fOverlayStore.setValue(item.getStrikethroughKey(), fStrikethroughCheckBox.getSelection());
553             }
554         });
555         
556         fUnderlineCheckBox.addSelectionListener(new SelectionListener() {
557             public void widgetDefaultSelected(SelectionEvent e) {
558                 // do nothing
559
}
560             public void widgetSelected(SelectionEvent e) {
561                 HighlightingColorListItem item= getHighlightingColorListItem();
562                 fOverlayStore.setValue(item.getUnderlineKey(), fUnderlineCheckBox.getSelection());
563             }
564         });
565         
566         parent.layout();
567         
568         return parent;
569     }
570     
571     private Control createPreviewer(Composite parent) {
572         
573         IPreferenceStore store= new ChainedPreferenceStore(new IPreferenceStore[] { fOverlayStore, JavaPlugin.getDefault().getCombinedPreferenceStore()});
574         fPreviewViewer= new JavaSourceViewer(parent, null, null, false, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER, store);
575         fColorManager= new JavaColorManager(false);
576         PropertiesFileSourceViewerConfiguration configuration= new PropertiesFileSourceViewerConfiguration(fColorManager, store, null, IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING);
577         fPreviewViewer.configure(configuration);
578         Font font= JFaceResources.getFont(PreferenceConstants.PROPERTIES_FILE_EDITOR_TEXT_FONT);
579         fPreviewViewer.getTextWidget().setFont(font);
580         new SourcePreviewerUpdater(fPreviewViewer, configuration, store);
581         fPreviewViewer.setEditable(false);
582         
583         String JavaDoc content= loadPreviewContentFromFile("PropertiesFileEditorColorSettingPreviewCode.txt"); //$NON-NLS-1$
584
IDocument document= new Document(content);
585         PropertiesFileDocumentSetupParticipant.setupDocument(document);
586         fPreviewViewer.setDocument(document);
587
588         return fPreviewViewer.getControl();
589     }
590     
591     private static void indent(Control control) {
592         GridData gridData= new GridData();
593         gridData.horizontalIndent= 20;
594         control.setLayoutData(gridData);
595     }
596     
597     private void createDependency(final Button master, String JavaDoc masterKey, final Control slave) {
598         indent(slave);
599         boolean masterState= fOverlayStore.getBoolean(masterKey);
600         slave.setEnabled(masterState);
601         SelectionListener listener= new SelectionListener() {
602             public void widgetSelected(SelectionEvent e) {
603                 slave.setEnabled(master.getSelection());
604             }
605
606             public void widgetDefaultSelected(SelectionEvent e) {}
607         };
608         master.addSelectionListener(listener);
609         fMasterSlaveListeners.add(listener);
610     }
611
612     /*
613      * @see PreferencePage#createContents(Composite)
614      */

615     protected Control createContents(Composite parent) {
616         fOverlayStore.load();
617         fOverlayStore.start();
618
619         Composite contents= new Composite(parent, SWT.NONE);
620         GridLayout layout= new GridLayout();
621         layout.marginHeight= 0;
622         layout.marginWidth= 0;
623         contents.setLayout(layout);
624         contents.setLayoutData(new GridData(GridData.FILL_BOTH));
625         
626         createHeader(contents);
627         
628         createSyntaxPage(contents);
629
630         initialize();
631         
632         Dialog.applyDialogFont(contents);
633         return contents;
634     }
635
636     private void createHeader(Composite contents) {
637         String JavaDoc text= PreferencesMessages.PropertiesFileEditorPreferencePage_link;
638         Link link= new Link(contents, SWT.NONE);
639         link.setText(text);
640         link.addSelectionListener(new SelectionAdapter() {
641             public void widgetSelected(SelectionEvent e) {
642                 PreferencesUtil.createPreferenceDialogOn(getShell(), "org.eclipse.ui.preferencePages.GeneralTextEditor", null, null); //$NON-NLS-1$
643
}
644         });
645         // TODO replace by link-specific tooltips when
646
// bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=88866 gets fixed
647
link.setToolTipText(PreferencesMessages.PropertiesFileEditorPreferencePage_link_tooltip);
648
649         
650         GridData gridData= new GridData(SWT.FILL, SWT.BEGINNING, true, false);
651         gridData.widthHint= 150; // only expand further if anyone else requires it
652
link.setLayoutData(gridData);
653         
654         addFiller(contents);
655     }
656
657     private void addFiller(Composite composite) {
658         PixelConverter pixelConverter= new PixelConverter(composite);
659         
660         Label filler= new Label(composite, SWT.LEFT );
661         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
662         gd.horizontalSpan= 2;
663         gd.heightHint= pixelConverter.convertHeightInCharsToPixels(1) / 2;
664         filler.setLayoutData(gd);
665     }
666
667     private void initialize() {
668         
669         initializeFields();
670         
671         for (int i= 0, n= fSyntaxColorListModel.length; i < n; i++)
672             fHighlightingColorList.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, null));
673
674         fHighlightingColorListViewer.setInput(fHighlightingColorList);
675         fHighlightingColorListViewer.setSelection(new StructuredSelection(fHighlightingColorListViewer.getElementAt(0)));
676     }
677     
678     private void initializeFields() {
679         fFieldsInitialized= true;
680         updateStatus(new StatusInfo());
681         
682         // Update slaves
683
Iterator JavaDoc iter= fMasterSlaveListeners.iterator();
684         while (iter.hasNext()) {
685             SelectionListener listener= (SelectionListener)iter.next();
686             listener.widgetSelected(null);
687         }
688     }
689
690     /*
691      * @see PreferencePage#performOk()
692      */

693     public boolean performOk() {
694         fOverlayStore.propagate();
695         JavaPlugin.getDefault().savePluginPreferences();
696         return true;
697     }
698     
699     /*
700      * @see PreferencePage#performDefaults()
701      */

702     protected void performDefaults() {
703         
704         fOverlayStore.loadDefaults();
705
706         initializeFields();
707
708         handleSyntaxColorListSelection();
709
710         super.performDefaults();
711
712         fPreviewViewer.invalidateTextPresentation();
713     }
714     
715     /*
716      * @see DialogPage#dispose()
717      */

718     public void dispose() {
719         
720         if (fOverlayStore != null) {
721             fOverlayStore.stop();
722             fOverlayStore= null;
723         }
724         
725         fColorManager.dispose();
726
727         super.dispose();
728     }
729     
730     private String JavaDoc loadPreviewContentFromFile(String JavaDoc filename) {
731         String JavaDoc line;
732         String JavaDoc separator= System.getProperty("line.separator"); //$NON-NLS-1$
733
StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(512);
734         BufferedReader JavaDoc reader= null;
735         try {
736             reader= new BufferedReader JavaDoc(new InputStreamReader JavaDoc(getClass().getResourceAsStream(filename)));
737             while ((line= reader.readLine()) != null) {
738                 buffer.append(line);
739                 buffer.append(separator);
740             }
741         } catch (IOException JavaDoc io) {
742             JavaPlugin.log(io);
743         } finally {
744             if (reader != null) {
745                 try { reader.close(); } catch (IOException JavaDoc e) {}
746             }
747         }
748         return buffer.toString();
749     }
750     
751     void updateStatus(IStatus status) {
752         if (!fFieldsInitialized)
753             return;
754         
755         setValid(!status.matches(IStatus.ERROR));
756         StatusUtil.applyToStatusLine(this, status);
757     }
758     
759     /**
760      * Returns the current highlighting color list item.
761      *
762      * @return the current highlighting color list item
763      */

764     private HighlightingColorListItem getHighlightingColorListItem() {
765         IStructuredSelection selection= (IStructuredSelection) fHighlightingColorListViewer.getSelection();
766         return (HighlightingColorListItem) selection.getFirstElement();
767     }
768 }
769
Popular Tags