KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.jdt.internal.ui.preferences;
13
14 import org.eclipse.core.runtime.Assert;
15
16 import org.eclipse.swt.events.DisposeEvent;
17 import org.eclipse.swt.events.DisposeListener;
18 import org.eclipse.swt.graphics.Font;
19
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.jface.resource.JFaceResources;
22 import org.eclipse.jface.util.IPropertyChangeListener;
23 import org.eclipse.jface.util.PropertyChangeEvent;
24
25 import org.eclipse.jface.text.source.SourceViewer;
26
27 import org.eclipse.jdt.ui.PreferenceConstants;
28 import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
29
30 /**
31  * Handles Java editor font changes for Java source preview viewers.
32  *
33  * @since 3.0
34  */

35 class JavaSourcePreviewerUpdater {
36     
37     /**
38      * Creates a Java source preview updater for the given viewer, configuration and preference store.
39      *
40      * @param viewer the viewer
41      * @param configuration the configuration
42      * @param preferenceStore the preference store
43      */

44     JavaSourcePreviewerUpdater(final SourceViewer viewer, final JavaSourceViewerConfiguration configuration, final IPreferenceStore preferenceStore) {
45         Assert.isNotNull(viewer);
46         Assert.isNotNull(configuration);
47         Assert.isNotNull(preferenceStore);
48         final IPropertyChangeListener fontChangeListener= new IPropertyChangeListener() {
49             /*
50              * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
51              */

52             public void propertyChange(PropertyChangeEvent event) {
53                 if (event.getProperty().equals(PreferenceConstants.EDITOR_TEXT_FONT)) {
54                     Font font= JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT);
55                     viewer.getTextWidget().setFont(font);
56                 }
57             }
58         };
59         final IPropertyChangeListener propertyChangeListener= new IPropertyChangeListener() {
60             /*
61              * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
62              */

63             public void propertyChange(PropertyChangeEvent event) {
64                 if (configuration.affectsTextPresentation(event)) {
65                     configuration.handlePropertyChangeEvent(event);
66                     viewer.invalidateTextPresentation();
67                 }
68             }
69         };
70         viewer.getTextWidget().addDisposeListener(new DisposeListener() {
71             /*
72              * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
73              */

74             public void widgetDisposed(DisposeEvent e) {
75                 preferenceStore.removePropertyChangeListener(propertyChangeListener);
76                 JFaceResources.getFontRegistry().removeListener(fontChangeListener);
77             }
78         });
79         JFaceResources.getFontRegistry().addListener(fontChangeListener);
80         preferenceStore.addPropertyChangeListener(propertyChangeListener);
81     }
82 }
83
Popular Tags