7947a8e74610b8556b31bcb5adfc79048c70d1af
[phpeclipse.git] /
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: XMLMergeViewer.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.XMLConfiguration;
18 import net.sourceforge.phpeclipse.xml.ui.text.XMLTextTools;
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 XMLMergeViewer 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 XML text tools.
51          */
52         private XMLTextTools textTools;
53
54         /*
55          * @see TextMergeViewer#TextMergeViewer(Composite, int,
56          *      CompareConfiguration)
57          */
58         public XMLMergeViewer(Composite parent, int style,
59                         CompareConfiguration configuration) {
60                 super(parent, style, configuration);
61         }
62
63         // TextMergeViewer Implementation
64         // ------------------------------------------
65
66         /*
67          * @see TextMergeViewer#configureTextViewer()
68          */
69         protected void configureTextViewer(TextViewer textViewer) {
70                 XMLPlugin plugin = XMLPlugin.getDefault();
71
72                 preferenceStore = plugin.getPreferenceStore();
73                 if (preferenceStore != null) {
74                         propertyChangeListener = new IPropertyChangeListener() {
75                                 public void propertyChange(PropertyChangeEvent event) {
76                                         handlePreferenceStoreChanged(event);
77                                 }
78                         };
79                         preferenceStore.addPropertyChangeListener(propertyChangeListener);
80                 }
81
82                 textTools = plugin.getXMLTextTools();
83
84                 if (textViewer instanceof SourceViewer) {
85                         SourceViewer sourceViewer = (SourceViewer) textViewer;
86                         sourceViewer.configure(new XMLConfiguration(textTools));
87                 }
88
89                 updateBackgroundColor();
90         }
91
92         /*
93          * @see TextMergeViewer#getDocumentPartitioner()
94          */
95         protected IDocumentPartitioner getDocumentPartitioner() {
96                 return textTools.createXMLPartitioner();
97         }
98
99         /*
100          * @see org.eclipse.compare.contentmergeviewer.ContentMergeViewer#getTitle()
101          */
102         public String getTitle() {
103                 return XMLPlugin.getResourceString("XMLMergeViewer.title"); //$NON-NLS-1$
104         }
105
106         /*
107          * @see org.eclipse.jface.viewers.ContentViewer#handleDispose(org.eclipse.swt.events.DisposeEvent)
108          */
109         protected void handleDispose(DisposeEvent event) {
110                 if (propertyChangeListener != null) {
111                         if (preferenceStore != null) {
112                                 preferenceStore
113                                                 .removePropertyChangeListener(propertyChangeListener);
114                         }
115
116                         propertyChangeListener = null;
117                 }
118
119                 super.handleDispose(event);
120         }
121
122         protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
123                 String p = event.getProperty();
124
125                 if (p.equals(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND)
126                                 || p
127                                                 .equals(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)) {
128                         updateBackgroundColor();
129                 } else if (textTools.affectsBehavior(event)) {
130                         invalidateTextPresentation();
131                 }
132         }
133
134         private void updateBackgroundColor() {
135                 boolean defaultBackgroundColor = preferenceStore
136                                 .getBoolean(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT);
137
138                 if (defaultBackgroundColor) {
139                         setBackgroundColor(null);
140                 } else {
141                         RGB backgroundColor = PreferenceConverter.getColor(preferenceStore,
142                                         AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND);
143                         setBackgroundColor(backgroundColor);
144                 }
145         }
146 }