2 * Copyright (c) 2003-2004 Christopher Lenz 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
9 * Christopher Lenz - initial API and implementation
11 * $Id: CssMergeViewer.java,v 1.1 2004-09-02 18:11:49 jsurfer Exp $
14 package net.sourceforge.phpeclipse.css.ui.internal.compare;
16 import net.sourceforge.phpeclipse.css.ui.CssUI;
17 import net.sourceforge.phpeclipse.css.ui.internal.CssUIMessages;
18 import net.sourceforge.phpeclipse.css.ui.internal.CssUIPreferences;
19 import net.sourceforge.phpeclipse.css.ui.internal.text.CssSourceViewerConfiguration;
20 import net.sourceforge.phpeclipse.css.ui.text.CssTextTools;
22 import org.eclipse.compare.CompareConfiguration;
23 import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.preference.PreferenceConverter;
26 import org.eclipse.jface.text.IDocumentPartitioner;
27 import org.eclipse.jface.text.TextViewer;
28 import org.eclipse.jface.text.source.SourceViewer;
29 import org.eclipse.jface.util.IPropertyChangeListener;
30 import org.eclipse.jface.util.PropertyChangeEvent;
31 import org.eclipse.swt.events.DisposeEvent;
32 import org.eclipse.swt.graphics.RGB;
33 import org.eclipse.swt.widgets.Composite;
36 * Merge viewer for CSS files.
38 public class CssMergeViewer extends TextMergeViewer {
40 // Constants ---------------------------------------------------------------
43 * Alias for the preference constant <code>EDITOR_BACKGROUND_COLOR</code>.
45 private static final String PREFERENCE_BACKGROUND_COLOR =
46 CssUIPreferences.EDITOR_BACKGROUND_COLOR;
49 * Alias for the preference constant
50 * <code>EDITOR_BACKGROUND_DEFAULT_COLOR</code>.
52 private static final String PREFERENCE_BACKGROUND_DEFAULT_COLOR =
53 CssUIPreferences.EDITOR_BACKGROUND_DEFAULT_COLOR;
55 // Instance Variables ------------------------------------------------------
58 * The preference store.
60 private IPreferenceStore preferenceStore;
63 * The listener for changes to the preference store.
65 private IPropertyChangeListener propertyChangeListener;
70 private CssTextTools textTools;
72 // Constructors ------------------------------------------------------------
77 * @param parent the parent control
78 * @param style SWT style bits for top level composite of this viewer
79 * @param configuration the configuration object
81 public CssMergeViewer(Composite parent, int style,
82 CompareConfiguration configuration) {
83 super(parent, style, configuration);
86 // TextMergeViewer Implementation ------------------------------------------
89 * @see TextMergeViewer#configureTextViewer()
91 protected void configureTextViewer(TextViewer textViewer) {
93 CssUI plugin = CssUI.getDefault();
95 this.preferenceStore = plugin.getPreferenceStore();
96 if (preferenceStore != null) {
97 propertyChangeListener = new IPropertyChangeListener() {
98 public void propertyChange(PropertyChangeEvent event) {
99 handlePreferenceStoreChanged(event);
102 preferenceStore.addPropertyChangeListener(propertyChangeListener);
105 textTools = plugin.getTextTools();
106 if (textViewer instanceof SourceViewer) {
107 SourceViewer sourceViewer = (SourceViewer) textViewer;
108 sourceViewer.configure(new CssSourceViewerConfiguration(
109 textTools, preferenceStore));
112 updateBackgroundColor();
116 * @see TextMergeViewer#getDocumentPartitioner()
118 protected IDocumentPartitioner getDocumentPartitioner() {
119 return textTools.createDocumentPartitioner();
123 * @see org.eclipse.compare.contentmergeviewer.ContentMergeViewer#getTitle()
125 public String getTitle() {
126 return CssUIMessages.getString(
127 "CssMergeViewer.title"); //$NON-NLS-1$
131 * @see org.eclipse.jface.viewers.ContentViewer#handleDispose(org.eclipse.swt.events.DisposeEvent)
133 protected void handleDispose(DisposeEvent event) {
134 if (propertyChangeListener != null) {
135 if (preferenceStore != null) {
136 preferenceStore.removePropertyChangeListener(
137 propertyChangeListener);
139 propertyChangeListener = null;
141 super.handleDispose(event);
144 // Private Methods ---------------------------------------------------------
147 * Called when the preference store notifies us about a change.
149 * @param event the change event
151 protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
152 String p = event.getProperty();
153 if (PREFERENCE_BACKGROUND_COLOR.equals(p)
154 || PREFERENCE_BACKGROUND_DEFAULT_COLOR.equals(p)) {
155 updateBackgroundColor();
156 } else if (textTools.affectsPresentation(event)) {
157 invalidateTextPresentation();
162 * Updates the background color of the merge viewer to the value set in the
165 private void updateBackgroundColor() {
166 boolean defaultBackgroundColor = preferenceStore.getBoolean(
167 PREFERENCE_BACKGROUND_DEFAULT_COLOR);
168 if (defaultBackgroundColor) {
169 setBackgroundColor(null);
171 RGB backgroundColor = PreferenceConverter.getColor(
172 preferenceStore, CssUIPreferences.EDITOR_BACKGROUND_COLOR);
173 setBackgroundColor(backgroundColor);