a833e926434a7cac4b4edd6d158ec66a259972db
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / xml / ui / internal / compare / DTDMergeViewer.java
1 /*
2  * Copyright (c) 2002-2004 Widespace, OU 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  *     Igor Malinin - initial contribution
10  *
11  * $Id: DTDMergeViewer.java,v 1.3 2006-10-21 23:14:13 pombredanne Exp $
12  */
13
14 package net.sourceforge.phpeclipse.xml.ui.internal.compare;
15
16 import net.sourceforge.phpeclipse.xml.ui.XMLPlugin;
17 import net.sourceforge.phpeclipse.xml.ui.internal.text.DTDConfiguration;
18 import net.sourceforge.phpeclipse.xml.ui.text.DTDTextTools;
19
20 import org.eclipse.compare.CompareConfiguration;
21 import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.preference.PreferenceConverter;
24 import org.eclipse.jface.text.IDocumentPartitioner;
25 import org.eclipse.jface.text.TextViewer;
26 import org.eclipse.jface.text.source.SourceViewer;
27 import org.eclipse.jface.util.IPropertyChangeListener;
28 import org.eclipse.jface.util.PropertyChangeEvent;
29 import org.eclipse.swt.events.DisposeEvent;
30 import org.eclipse.swt.graphics.RGB;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.ui.texteditor.AbstractTextEditor;
33
34 /**
35  * @author Igor Malinin
36  */
37 public class DTDMergeViewer extends TextMergeViewer {
38
39         /**
40          * The preference store.
41          */
42         private IPreferenceStore preferenceStore;
43
44         /**
45          * The listener for changes to the preference store.
46          */
47         private IPropertyChangeListener propertyChangeListener;
48
49         /**
50          * The DTD text tools.
51          */
52         private DTDTextTools textTools;
53
54         /*
55          * @see TextMergeViewer#TextMergeViewer(Composite, int,
56          *      CompareConfiguration)
57          */
58         public DTDMergeViewer(Composite parent, int style,
59                         CompareConfiguration configuration) {
60                 super(parent, style, configuration);
61         }
62
63         // TextMergeViewer Implementation ------------------------------------------
64
65         /*
66          * @see TextMergeViewer#configureTextViewer()
67          */
68         protected void configureTextViewer(TextViewer textViewer) {
69                 XMLPlugin plugin = XMLPlugin.getDefault();
70
71                 preferenceStore = plugin.getPreferenceStore();
72                 if (preferenceStore != null) {
73                         propertyChangeListener = new IPropertyChangeListener() {
74                                 public void propertyChange(PropertyChangeEvent event) {
75                                         handlePreferenceStoreChanged(event);
76                                 }
77                         };
78                         preferenceStore.addPropertyChangeListener(propertyChangeListener);
79                 }
80
81                 textTools = plugin.getDTDTextTools();
82
83                 if (textViewer instanceof SourceViewer) {
84                         SourceViewer sourceViewer = (SourceViewer) textViewer;
85                         sourceViewer.configure(new DTDConfiguration(textTools));
86                 }
87
88                 updateBackgroundColor();
89         }
90
91         /*
92          * @see TextMergeViewer#getDocumentPartitioner()
93          */
94         protected IDocumentPartitioner getDocumentPartitioner() {
95                 return textTools.createDTDPartitioner();
96         }
97
98         /*
99          * @see org.eclipse.compare.contentmergeviewer.ContentMergeViewer#getTitle()
100          */
101         public String getTitle() {
102                 return XMLPlugin.getResourceString("DTDMergeViewer.title"); //$NON-NLS-1$
103         }
104
105         /*
106          * @see org.eclipse.jface.viewers.ContentViewer#handleDispose(org.eclipse.swt.events.DisposeEvent)
107          */
108         protected void handleDispose(DisposeEvent event) {
109                 if (propertyChangeListener != null) {
110                         if (preferenceStore != null) {
111                                 preferenceStore
112                                                 .removePropertyChangeListener(propertyChangeListener);
113                         }
114
115                         propertyChangeListener = null;
116                 }
117
118                 super.handleDispose(event);
119         }
120
121         protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
122                 String p = event.getProperty();
123
124                 if (p.equals(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND)
125                                 || p
126                                                 .equals(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)) {
127                         updateBackgroundColor();
128                 } else if (textTools.affectsBehavior(event)) {
129                         invalidateTextPresentation();
130                 }
131         }
132
133         private void updateBackgroundColor() {
134                 boolean defaultBackgroundColor = preferenceStore
135                                 .getBoolean(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT);
136
137                 if (defaultBackgroundColor) {
138                         setBackgroundColor(null);
139                 } else {
140                         RGB backgroundColor = PreferenceConverter.getColor(preferenceStore,
141                                         AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND);
142                         setBackgroundColor(backgroundColor);
143                 }
144         }
145 }