1) Fixed issue #872.
[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 //incastrix
21 //import org.eclipse.jface.text.Assert;
22 import org.eclipse.core.runtime.Assert;
23 import org.eclipse.jface.util.IPropertyChangeListener;
24 import org.eclipse.jface.util.PropertyChangeEvent;
25 import org.eclipse.swt.events.DisposeEvent;
26 import org.eclipse.swt.events.DisposeListener;
27 import org.eclipse.swt.graphics.Font;
28
29 /**
30  * Handles Java editor font changes for Java source preview viewers.
31  * 
32  * @since 3.0
33  */
34 class JavaSourcePreviewerUpdater {
35
36         /**
37          * Creates a Java source preview updater for the given viewer, configuration
38          * and preference store.
39          * 
40          * @param viewer
41          *            the viewer
42          * @param configuration
43          *            the configuration
44          * @param preferenceStore
45          *            the preference store
46          */
47         JavaSourcePreviewerUpdater(final SourceViewer viewer,
48                         final PHPSourceViewerConfiguration configuration,
49                         final IPreferenceStore preferenceStore) {
50                 Assert.isNotNull(viewer);
51                 Assert.isNotNull(configuration);
52                 Assert.isNotNull(preferenceStore);
53                 final IPropertyChangeListener fontChangeListener = new IPropertyChangeListener() {
54                         /*
55                          * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
56                          */
57                         public void propertyChange(PropertyChangeEvent event) {
58                                 if (event.getProperty().equals(
59                                                 PreferenceConstants.EDITOR_TEXT_FONT)) {
60                                         Font font = JFaceResources
61                                                         .getFont(PreferenceConstants.EDITOR_TEXT_FONT);
62                                         viewer.getTextWidget().setFont(font);
63                                 }
64                         }
65                 };
66                 final IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener() {
67                         /*
68                          * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
69                          */
70                         public void propertyChange(PropertyChangeEvent event) {
71                                 if (configuration.affectsTextPresentation(event)) {
72                                         configuration.handlePropertyChangeEvent(event);
73                                         viewer.invalidateTextPresentation();
74                                 }
75                         }
76                 };
77                 viewer.getTextWidget().addDisposeListener(new DisposeListener() {
78                         /*
79                          * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
80                          */
81                         public void widgetDisposed(DisposeEvent e) {
82                                 preferenceStore
83                                                 .removePropertyChangeListener(propertyChangeListener);
84                                 JFaceResources.getFontRegistry().removeListener(
85                                                 fontChangeListener);
86                         }
87                 });
88                 JFaceResources.getFontRegistry().addListener(fontChangeListener);
89                 preferenceStore.addPropertyChangeListener(propertyChangeListener);
90         }
91 }