net.certiv.fluentmark.preferences.PreviewSourceUpdater.java Source code

Java tutorial

Introduction

Here is the source code for net.certiv.fluentmark.preferences.PreviewSourceUpdater.java

Source

/*******************************************************************************
 * Copyright (c) 2016 - 2017 Certiv Analytics and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package net.certiv.fluentmark.preferences;

import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Font;

import net.certiv.fluentmark.editor.FluentSourceViewerConfiguration;

import org.eclipse.core.runtime.Assert;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;

import org.eclipse.jface.text.source.SourceViewer;

/**
 * Handles editor font changes for source preview viewers.
 *
 * @since 3.0
 */
public class PreviewSourceUpdater {

    /**
     * Creates a source preview updater for the given viewer, configuration and preference store.
     *
     * @param viewer the viewer
     * @param configuration the configuration
     * @param preferenceStore the preference store
     */
    public PreviewSourceUpdater(final SourceViewer viewer, final FluentSourceViewerConfiguration configuration,
            final IPreferenceStore preferenceStore) {
        Assert.isNotNull(viewer);
        Assert.isNotNull(configuration);
        Assert.isNotNull(preferenceStore);
        final IPropertyChangeListener fontChangeListener = new IPropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent event) {
                if (event.getProperty().equals(Prefs.EDITOR_TEXT_FONT)) {
                    Font font = JFaceResources.getFont(Prefs.EDITOR_TEXT_FONT);
                    viewer.getTextWidget().setFont(font);
                }
            }
        };
        final IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent event) {
                if (configuration.affectsTextPresentation(event)) {
                    configuration.handlePropertyChangeEvent(event);
                    viewer.invalidateTextPresentation();
                }
            }
        };
        viewer.getTextWidget().addDisposeListener(new DisposeListener() {

            @Override
            public void widgetDisposed(DisposeEvent e) {
                preferenceStore.removePropertyChangeListener(propertyChangeListener);
                JFaceResources.getFontRegistry().removeListener(fontChangeListener);
            }
        });
        JFaceResources.getFontRegistry().addListener(fontChangeListener);
        preferenceStore.addPropertyChangeListener(propertyChangeListener);
    }
}