intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.ui / src / net / sourceforge / phpeclipse / css / ui / internal / compare / CssMergeViewer.java
1 /*
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
7  * 
8  * Contributors:
9  *     Christopher Lenz - initial API and implementation
10  * 
11  * $Id: CssMergeViewer.java,v 1.1 2004-09-02 18:11:49 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.ui.internal.compare;
15
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;
21
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;
34
35 /**
36  * Merge viewer for CSS files.
37  */
38 public class CssMergeViewer extends TextMergeViewer {
39
40         // Constants ---------------------------------------------------------------
41
42         /**
43          * Alias for the preference constant <code>EDITOR_BACKGROUND_COLOR</code>.
44          */
45         private static final String PREFERENCE_BACKGROUND_COLOR =
46                 CssUIPreferences.EDITOR_BACKGROUND_COLOR;
47
48         /**
49          * Alias for the preference constant
50          * <code>EDITOR_BACKGROUND_DEFAULT_COLOR</code>.
51          */
52         private static final String PREFERENCE_BACKGROUND_DEFAULT_COLOR =
53                 CssUIPreferences.EDITOR_BACKGROUND_DEFAULT_COLOR;
54
55         // Instance Variables ------------------------------------------------------
56
57         /**
58          * The preference store.
59          */
60         private IPreferenceStore preferenceStore;
61
62         /**
63          * The listener for changes to the preference store. 
64          */
65         private IPropertyChangeListener propertyChangeListener;
66
67         /**
68          * The CSS text tools.
69          */
70         private CssTextTools textTools;
71
72         // Constructors ------------------------------------------------------------
73
74         /**
75          * Constructor.
76          * 
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
80          */
81         public CssMergeViewer(Composite parent, int style,
82                 CompareConfiguration configuration) {
83                 super(parent, style, configuration);
84         }
85
86         // TextMergeViewer Implementation ------------------------------------------
87
88         /*
89          * @see TextMergeViewer#configureTextViewer()
90          */
91         protected void configureTextViewer(TextViewer textViewer) {
92
93                 CssUI plugin = CssUI.getDefault();
94
95                 this.preferenceStore = plugin.getPreferenceStore();
96                 if (preferenceStore != null) {
97                          propertyChangeListener = new IPropertyChangeListener() {
98                                 public void propertyChange(PropertyChangeEvent event) {
99                                         handlePreferenceStoreChanged(event);
100                                 }
101                         };
102                         preferenceStore.addPropertyChangeListener(propertyChangeListener);
103                 }
104
105                 textTools = plugin.getTextTools();
106                 if (textViewer instanceof SourceViewer) {
107                         SourceViewer sourceViewer = (SourceViewer) textViewer;
108                         sourceViewer.configure(new CssSourceViewerConfiguration(
109                                 textTools, preferenceStore));
110                 }
111
112                 updateBackgroundColor();
113         }
114
115         /*
116          * @see TextMergeViewer#getDocumentPartitioner()
117          */
118         protected IDocumentPartitioner getDocumentPartitioner() {
119                 return textTools.createDocumentPartitioner();
120         }
121
122         /*
123          * @see org.eclipse.compare.contentmergeviewer.ContentMergeViewer#getTitle()
124          */
125         public String getTitle() {
126                 return CssUIMessages.getString(
127                         "CssMergeViewer.title"); //$NON-NLS-1$
128         }
129
130         /*
131          * @see org.eclipse.jface.viewers.ContentViewer#handleDispose(org.eclipse.swt.events.DisposeEvent)
132          */
133         protected void handleDispose(DisposeEvent event) {
134                 if (propertyChangeListener != null) {
135                         if (preferenceStore != null) {
136                                 preferenceStore.removePropertyChangeListener(
137                                         propertyChangeListener);
138                         }
139                         propertyChangeListener = null;
140                 }
141                 super.handleDispose(event);
142         }
143
144         // Private Methods ---------------------------------------------------------
145
146         /**
147          * Called when the preference store notifies us about a change.
148          * 
149          * @param event the change event
150          */
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();
158                 }
159         }
160
161         /**
162          * Updates the background color of the merge viewer to the value set in the
163          * preferences.
164          */
165         private void updateBackgroundColor() {
166                 boolean defaultBackgroundColor = preferenceStore.getBoolean(
167                         PREFERENCE_BACKGROUND_DEFAULT_COLOR);
168                 if (defaultBackgroundColor) {
169                         setBackgroundColor(null);
170                 } else {
171                         RGB backgroundColor = PreferenceConverter.getColor(
172                                 preferenceStore, CssUIPreferences.EDITOR_BACKGROUND_COLOR);
173                         setBackgroundColor(backgroundColor);
174                 }
175         }
176
177 }