fix #761 also see #762.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / preferences / JavaSourcePreviewerUpdater.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.internal.ui.preferences;
13
14 import net.sourceforge.phpdt.ui.PreferenceConstants;
15 import net.sourceforge.phpdt.ui.text.PHPSourceViewerConfiguration;
16
17 import org.eclipse.jface.preference.IPreferenceStore;
18 import org.eclipse.jface.resource.JFaceResources;
19 import org.eclipse.jface.text.source.SourceViewer;
20 import org.eclipse.jface.util.Assert;
21 import org.eclipse.jface.util.IPropertyChangeListener;
22 import org.eclipse.jface.util.PropertyChangeEvent;
23 import org.eclipse.swt.events.DisposeEvent;
24 import org.eclipse.swt.events.DisposeListener;
25 import org.eclipse.swt.graphics.Font;
26
27 /**
28  * Handles Java editor font changes for Java source preview viewers.
29  * 
30  * @since 3.0
31  */
32 class JavaSourcePreviewerUpdater {
33
34         /**
35          * Creates a Java source preview updater for the given viewer, configuration
36          * and preference store.
37          * 
38          * @param viewer
39          *            the viewer
40          * @param configuration
41          *            the configuration
42          * @param preferenceStore
43          *            the preference store
44          */
45         JavaSourcePreviewerUpdater(final SourceViewer viewer,
46                         final PHPSourceViewerConfiguration configuration,
47                         final IPreferenceStore preferenceStore) {
48                 Assert.isNotNull(viewer);
49                 Assert.isNotNull(configuration);
50                 Assert.isNotNull(preferenceStore);
51                 final IPropertyChangeListener fontChangeListener = new IPropertyChangeListener() {
52                         /*
53                          * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
54                          */
55                         public void propertyChange(PropertyChangeEvent event) {
56                                 if (event.getProperty().equals(
57                                                 PreferenceConstants.EDITOR_TEXT_FONT)) {
58                                         Font font = JFaceResources
59                                                         .getFont(PreferenceConstants.EDITOR_TEXT_FONT);
60                                         viewer.getTextWidget().setFont(font);
61                                 }
62                         }
63                 };
64                 final IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener() {
65                         /*
66                          * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
67                          */
68                         public void propertyChange(PropertyChangeEvent event) {
69                                 if (configuration.affectsTextPresentation(event)) {
70                                         configuration.handlePropertyChangeEvent(event);
71                                         viewer.invalidateTextPresentation();
72                                 }
73                         }
74                 };
75                 viewer.getTextWidget().addDisposeListener(new DisposeListener() {
76                         /*
77                          * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
78                          */
79                         public void widgetDisposed(DisposeEvent e) {
80                                 preferenceStore
81                                                 .removePropertyChangeListener(propertyChangeListener);
82                                 JFaceResources.getFontRegistry().removeListener(
83                                                 fontChangeListener);
84                         }
85                 });
86                 JFaceResources.getFontRegistry().addListener(fontChangeListener);
87                 preferenceStore.addPropertyChangeListener(propertyChangeListener);
88         }
89 }