1 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 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 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 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 72 public void propertyChange(PropertyChangeEvent event) { 73 74 String 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 98 private void format(final SourceViewer sourceViewer, final IPreferenceStore store) { 99 String 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 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 125 protected void initializeViewerColors(ISourceViewer viewer, IPreferenceStore store) { 126 127 StyledText styledText= viewer.getTextWidget(); 128 129 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 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 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 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 188 private Color createColor(IPreferenceStore store, String 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 |