KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > text > XMLConfiguration


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.text;
12
13 import org.eclipse.jface.text.IDocument;
14 import org.eclipse.jface.text.ITextDoubleClickStrategy;
15 import org.eclipse.jface.text.TextAttribute;
16 import org.eclipse.jface.text.presentation.IPresentationReconciler;
17 import org.eclipse.jface.text.presentation.PresentationReconciler;
18 import org.eclipse.jface.text.quickassist.IQuickAssistAssistant;
19 import org.eclipse.jface.text.source.IAnnotationHover;
20 import org.eclipse.jface.text.source.ISourceViewer;
21 import org.eclipse.jface.util.PropertyChangeEvent;
22 import org.eclipse.pde.internal.ui.editor.PDESourcePage;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.graphics.Color;
25
26
27 public class XMLConfiguration extends ChangeAwareSourceViewerConfiguration {
28     private AnnotationHover fAnnotationHover;
29     private XMLDoubleClickStrategy fDoubleClickStrategy;
30     private XMLTagScanner fTagScanner;
31     private XMLScanner fPdeScanner;
32
33     private TextAttribute fXMLCommentAttr;
34     private MultilineDamagerRepairer fDamagerRepairer;
35     private PDEQuickAssistAssistant fQuickAssistant;
36     
37     public XMLConfiguration(IColorManager colorManager) {
38         this(colorManager, null);
39     }
40     
41     public XMLConfiguration(IColorManager colorManager, PDESourcePage page) {
42         super(page, colorManager);
43     }
44     
45     public String JavaDoc[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
46         return new String JavaDoc[] {
47             IDocument.DEFAULT_CONTENT_TYPE,
48             XMLPartitionScanner.XML_COMMENT,
49             XMLPartitionScanner.XML_TAG };
50     }
51     
52     public ITextDoubleClickStrategy getDoubleClickStrategy(
53         ISourceViewer sourceViewer,
54         String JavaDoc contentType) {
55         if (fDoubleClickStrategy == null)
56             fDoubleClickStrategy = new XMLDoubleClickStrategy();
57         return fDoubleClickStrategy;
58     }
59     
60     protected XMLScanner getPDEScanner() {
61         if (fPdeScanner == null)
62             fPdeScanner = new XMLScanner(fColorManager);
63         return fPdeScanner;
64     }
65     
66     protected XMLTagScanner getPDETagScanner() {
67         if (fTagScanner == null)
68             fTagScanner = new XMLTagScanner(fColorManager);
69         return fTagScanner;
70     }
71     
72     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
73         PresentationReconciler reconciler = new PresentationReconciler();
74
75         MultilineDamagerRepairer dr = new MultilineDamagerRepairer(getPDEScanner());
76         reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
77         reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
78
79         dr = new MultilineDamagerRepairer(getPDETagScanner());
80         reconciler.setDamager(dr, XMLPartitionScanner.XML_TAG);
81         reconciler.setRepairer(dr, XMLPartitionScanner.XML_TAG);
82     
83         fXMLCommentAttr = BasePDEScanner.createTextAttribute(fColorManager, IPDEColorConstants.P_XML_COMMENT);
84         fDamagerRepairer = new MultilineDamagerRepairer(null, fXMLCommentAttr);
85         reconciler.setDamager(fDamagerRepairer, XMLPartitionScanner.XML_COMMENT);
86         reconciler.setRepairer(fDamagerRepairer, XMLPartitionScanner.XML_COMMENT);
87
88         return reconciler;
89     }
90     
91     public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
92         if (fAnnotationHover == null)
93             fAnnotationHover = new AnnotationHover();
94         return fAnnotationHover;
95     }
96     
97     /**
98      * Preference colors or fonts have changed.
99      * Update the default tokens of the scanners.
100      */

101     public void adaptToPreferenceChange(PropertyChangeEvent event) {
102         if (fTagScanner == null)
103             return; //property change before the editor is fully created
104
if (affectsColorPresentation(event))
105             fColorManager.handlePropertyChangeEvent(event);
106         fTagScanner.adaptToPreferenceChange(event);
107         fPdeScanner.adaptToPreferenceChange(event);
108         String JavaDoc property= event.getProperty();
109         if (property.startsWith(IPDEColorConstants.P_XML_COMMENT)) {
110             adaptTextAttribute(event);
111         }
112     }
113     
114     private void adaptTextAttribute(PropertyChangeEvent event) {
115         String JavaDoc property = event.getProperty();
116         if (property.endsWith(IPDEColorConstants.P_BOLD_SUFFIX)) {
117             fXMLCommentAttr = adaptToStyleChange(event, SWT.BOLD, fXMLCommentAttr);
118         } else if (property.endsWith(IPDEColorConstants.P_ITALIC_SUFFIX)) {
119             fXMLCommentAttr = adaptToStyleChange(event, SWT.ITALIC, fXMLCommentAttr);
120         } else {
121             fXMLCommentAttr = new TextAttribute(fColorManager.getColor(event.getProperty()),
122                                                fXMLCommentAttr.getBackground(),
123                                                fXMLCommentAttr.getStyle());
124         }
125         fDamagerRepairer.setDefaultTextAttribute(fXMLCommentAttr);
126     }
127     
128     private TextAttribute adaptToStyleChange(PropertyChangeEvent event, int styleAttribute, TextAttribute textAttribute) {
129         boolean eventValue = false;
130         Object JavaDoc value = event.getNewValue();
131         if (value instanceof Boolean JavaDoc)
132             eventValue = ((Boolean JavaDoc)value).booleanValue();
133         
134         boolean activeValue = (textAttribute.getStyle() & styleAttribute) == styleAttribute;
135         if (activeValue != eventValue) {
136             Color foreground = textAttribute.getForeground();
137             Color background = textAttribute.getBackground();
138             int style = eventValue ? textAttribute.getStyle() | styleAttribute : textAttribute.getStyle() & ~styleAttribute;
139             textAttribute= new TextAttribute(foreground, background, style);
140         }
141         return textAttribute;
142     }
143     
144     public boolean affectsTextPresentation(PropertyChangeEvent event) {
145         String JavaDoc property = event.getProperty();
146         return property.startsWith(IPDEColorConstants.P_DEFAULT) ||
147             property.startsWith(IPDEColorConstants.P_PROC_INSTR) ||
148             property.startsWith(IPDEColorConstants.P_STRING) ||
149             property.startsWith(IPDEColorConstants.P_TAG) ||
150             property.startsWith(IPDEColorConstants.P_XML_COMMENT);
151     }
152     
153     public boolean affectsColorPresentation(PropertyChangeEvent event) {
154         String JavaDoc property = event.getProperty();
155         return property.equals(IPDEColorConstants.P_DEFAULT) ||
156             property.equals(IPDEColorConstants.P_PROC_INSTR) ||
157             property.equals(IPDEColorConstants.P_STRING) ||
158             property.equals(IPDEColorConstants.P_TAG) ||
159             property.equals(IPDEColorConstants.P_XML_COMMENT);
160     }
161     
162     public IQuickAssistAssistant getQuickAssistAssistant(ISourceViewer sourceViewer) {
163         if (sourceViewer.isEditable()) {
164             if (fQuickAssistant == null)
165                 fQuickAssistant = new PDEQuickAssistAssistant();
166             return fQuickAssistant;
167         }
168         return null;
169     }
170         
171     public void dispose() {
172         if (fQuickAssistant != null)
173             fQuickAssistant.dispose();
174     }
175     
176     protected int getInfoImplementationType() {
177         return SourceInformationProvider.F_XML_IMP;
178     }
179 }
180
Popular Tags