KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > AntPreviewerUpdater


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ant.internal.ui.preferences;
13
14 import org.eclipse.ant.internal.ui.AntSourceViewerConfiguration;
15 import org.eclipse.ant.internal.ui.editor.formatter.FormattingPreferences;
16 import org.eclipse.ant.internal.ui.editor.formatter.XmlFormatter;
17 import org.eclipse.jface.preference.IPreferenceStore;
18 import org.eclipse.jface.preference.PreferenceConverter;
19 import org.eclipse.jface.resource.JFaceResources;
20 import org.eclipse.jface.text.source.ISourceViewer;
21 import org.eclipse.jface.text.source.SourceViewer;
22 import org.eclipse.jface.util.IPropertyChangeListener;
23 import org.eclipse.jface.util.PropertyChangeEvent;
24 import org.eclipse.swt.custom.StyledText;
25 import org.eclipse.swt.events.DisposeEvent;
26 import org.eclipse.swt.events.DisposeListener;
27 import org.eclipse.swt.graphics.Color;
28 import org.eclipse.swt.graphics.Font;
29 import org.eclipse.swt.graphics.RGB;
30 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.ui.texteditor.AbstractTextEditor;
32
33 /**
34  * Handles changes for Ant preview viewers.
35  *
36  * @since 3.0
37  */

38 class AntPreviewerUpdater {
39     
40     private Color fForegroundColor= null;
41     private Color fBackgroundColor= null;
42     private Color fSelectionBackgroundColor= null;
43     private Color fSelectionForegroundColor= null;
44     
45     
46     /**
47      * Creates a source preview updater for the given viewer, configuration and preference store.
48      *
49      * @param viewer the viewer
50      * @param configuration the configuration
51      * @param preferenceStore the preference store
52      */

53     public AntPreviewerUpdater(final SourceViewer viewer, final AntSourceViewerConfiguration configuration, final IPreferenceStore preferenceStore) {
54         
55         initializeViewerColors(viewer, preferenceStore);
56         
57         final IPropertyChangeListener fontChangeListener= new IPropertyChangeListener() {
58             /*
59              * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
60              */

61             public void propertyChange(PropertyChangeEvent event) {
62                 if (event.getProperty().equals(JFaceResources.TEXT_FONT)) {
63                     Font font= JFaceResources.getFont(JFaceResources.TEXT_FONT);
64                     viewer.getTextWidget().setFont(font);
65                 }
66             }
67         };
68         final IPropertyChangeListener propertyChangeListener= new IPropertyChangeListener() {
69             /*
70              * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
71              */

72             public void propertyChange(PropertyChangeEvent event) {
73                 
74                 String JavaDoc property= event.getProperty();
75                     
76                 if (AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND.equals(property) || AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT.equals(property) ||
77                         AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND.equals(property) || AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property) ||
78                         AbstractTextEditor.PREFERENCE_COLOR_SELECTION_FOREGROUND.equals(property) || AbstractTextEditor.PREFERENCE_COLOR_SELECTION_FOREGROUND_SYSTEM_DEFAULT.equals(property) ||
79                         AbstractTextEditor.PREFERENCE_COLOR_SELECTION_BACKGROUND.equals(property) || AbstractTextEditor.PREFERENCE_COLOR_SELECTION_BACKGROUND_SYSTEM_DEFAULT.equals(property))
80                     {
81                         initializeViewerColors(viewer, preferenceStore);
82                 }
83                 
84                 if (configuration.affectsTextPresentation(event)) {
85                     configuration.adaptToPreferenceChange(event);
86                     viewer.invalidateTextPresentation();
87                 }
88                 
89                 if (FormattingPreferences.affectsFormatting(event)) {
90                     format(viewer, preferenceStore);
91                 }
92             }
93
94             /**
95              * @param viewer
96              * @param preferenceStore
97              */

98             private void format(final SourceViewer sourceViewer, final IPreferenceStore store) {
99                 String JavaDoc contents= sourceViewer.getDocument().get();
100                 FormattingPreferences prefs= new FormattingPreferences();
101                 prefs.setPreferenceStore(store);
102                 contents= XmlFormatter.format(contents, prefs);
103                 viewer.getDocument().set(contents);
104             }
105         };
106         viewer.getTextWidget().addDisposeListener(new DisposeListener() {
107             /*
108              * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
109              */

110             public void widgetDisposed(DisposeEvent e) {
111                 preferenceStore.removePropertyChangeListener(propertyChangeListener);
112                 JFaceResources.getFontRegistry().removeListener(fontChangeListener);
113             }
114         });
115         JFaceResources.getFontRegistry().addListener(fontChangeListener);
116         preferenceStore.addPropertyChangeListener(propertyChangeListener);
117     }
118     
119     /**
120      * Initializes the given viewer's colors.
121      *
122      * @param viewer the viewer to be initialized
123      * @since 2.0
124      */

125     protected void initializeViewerColors(ISourceViewer viewer, IPreferenceStore store) {
126             
127         StyledText styledText= viewer.getTextWidget();
128         
129         // ----------- foreground color --------------------
130
Color color= store.getBoolean(AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT)
131             ? null
132             : createColor(store, AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND, styledText.getDisplay());
133         styledText.setForeground(color);
134             
135         if (fForegroundColor != null) {
136             fForegroundColor.dispose();
137         }
138         
139         fForegroundColor= color;
140         
141         // ---------- background color ----------------------
142
color= store.getBoolean(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)
143             ? null
144             : createColor(store, AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND, styledText.getDisplay());
145         styledText.setBackground(color);
146             
147         if (fBackgroundColor != null) {
148             fBackgroundColor.dispose();
149         }
150             
151         fBackgroundColor= color;
152         
153         // ----------- selection foreground color --------------------
154
color= store.getBoolean(AbstractTextEditor.PREFERENCE_COLOR_SELECTION_FOREGROUND_SYSTEM_DEFAULT)
155             ? null
156             : createColor(store, AbstractTextEditor.PREFERENCE_COLOR_SELECTION_FOREGROUND, styledText.getDisplay());
157         styledText.setSelectionForeground(color);
158             
159         if (fSelectionForegroundColor != null) {
160             fSelectionForegroundColor.dispose();
161         }
162         
163         fSelectionForegroundColor= color;
164         
165         // ---------- selection background color ----------------------
166
color= store.getBoolean(AbstractTextEditor.PREFERENCE_COLOR_SELECTION_BACKGROUND_SYSTEM_DEFAULT)
167             ? null
168             : createColor(store, AbstractTextEditor.PREFERENCE_COLOR_SELECTION_BACKGROUND, styledText.getDisplay());
169         styledText.setSelectionBackground(color);
170             
171         if (fSelectionBackgroundColor != null) {
172             fSelectionBackgroundColor.dispose();
173         }
174             
175         fSelectionBackgroundColor= color;
176     }
177     
178     /**
179      * Creates a color from the information stored in the given preference store.
180      * Returns <code>null</code> if there is no such information available.
181      *
182      * @param store the store to read from
183      * @param key the key used for the lookup in the preference store
184      * @param display the display used create the color
185      * @return the created color according to the specification in the preference store
186      * @since 2.0
187      */

188     private Color createColor(IPreferenceStore store, String JavaDoc key, Display display) {
189     
190         RGB rgb= null;
191         
192         if (store.contains(key)) {
193             
194             if (store.isDefault(key))
195                 rgb= PreferenceConverter.getDefaultColor(store, key);
196             else
197                 rgb= PreferenceConverter.getColor(store, key);
198         
199             if (rgb != null)
200                 return new Color(display, rgb);
201         }
202         
203         return null;
204     }
205
206     public void dispose() {
207         if (fForegroundColor != null) {
208             fForegroundColor.dispose();
209             fForegroundColor= null;
210         }
211         
212         if (fBackgroundColor != null) {
213             fBackgroundColor.dispose();
214             fBackgroundColor= null;
215         }
216         
217         if (fSelectionForegroundColor != null) {
218             fSelectionForegroundColor.dispose();
219             fSelectionForegroundColor= null;
220         }
221         
222         if (fSelectionBackgroundColor != null) {
223             fSelectionBackgroundColor.dispose();
224             fSelectionBackgroundColor= null;
225         }
226     }
227 }
228
Popular Tags