001: /*******************************************************************************
002: * Copyright (c) 2003, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.text;
011:
012: import org.eclipse.jface.text.IDocument;
013: import org.eclipse.jface.text.ITextDoubleClickStrategy;
014: import org.eclipse.jface.text.TextAttribute;
015: import org.eclipse.jface.text.presentation.IPresentationReconciler;
016: import org.eclipse.jface.text.presentation.PresentationReconciler;
017: import org.eclipse.jface.text.quickassist.IQuickAssistAssistant;
018: import org.eclipse.jface.text.source.IAnnotationHover;
019: import org.eclipse.jface.text.source.ISourceViewer;
020: import org.eclipse.jface.util.PropertyChangeEvent;
021: import org.eclipse.pde.internal.ui.editor.PDESourcePage;
022: import org.eclipse.pde.internal.ui.editor.context.XMLDocumentSetupParticpant;
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.graphics.Color;
025:
026: public class XMLConfiguration extends
027: ChangeAwareSourceViewerConfiguration {
028: private AnnotationHover fAnnotationHover;
029: private XMLDoubleClickStrategy fDoubleClickStrategy;
030: private XMLTagScanner fTagScanner;
031: private XMLScanner fPdeScanner;
032:
033: private TextAttribute fXMLCommentAttr;
034: private MultilineDamagerRepairer fDamagerRepairer;
035: private PDEQuickAssistAssistant fQuickAssistant;
036:
037: public XMLConfiguration(IColorManager colorManager) {
038: this (colorManager, null);
039: }
040:
041: public XMLConfiguration(IColorManager colorManager,
042: PDESourcePage page) {
043: super (page, colorManager);
044: }
045:
046: public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
047: return new String[] { IDocument.DEFAULT_CONTENT_TYPE,
048: XMLPartitionScanner.XML_COMMENT,
049: XMLPartitionScanner.XML_TAG };
050: }
051:
052: public ITextDoubleClickStrategy getDoubleClickStrategy(
053: ISourceViewer sourceViewer, String contentType) {
054: if (fDoubleClickStrategy == null)
055: fDoubleClickStrategy = new XMLDoubleClickStrategy();
056: return fDoubleClickStrategy;
057: }
058:
059: protected XMLScanner getPDEScanner() {
060: if (fPdeScanner == null)
061: fPdeScanner = new XMLScanner(fColorManager);
062: return fPdeScanner;
063: }
064:
065: protected XMLTagScanner getPDETagScanner() {
066: if (fTagScanner == null)
067: fTagScanner = new XMLTagScanner(fColorManager);
068: return fTagScanner;
069: }
070:
071: public IPresentationReconciler getPresentationReconciler(
072: ISourceViewer sourceViewer) {
073: PresentationReconciler reconciler = new PresentationReconciler();
074: reconciler
075: .setDocumentPartitioning(XMLDocumentSetupParticpant.XML_PARTITIONING);
076:
077: MultilineDamagerRepairer dr = new MultilineDamagerRepairer(
078: getPDEScanner());
079: reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
080: reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
081:
082: dr = new MultilineDamagerRepairer(getPDETagScanner());
083: reconciler.setDamager(dr, XMLPartitionScanner.XML_TAG);
084: reconciler.setRepairer(dr, XMLPartitionScanner.XML_TAG);
085:
086: fXMLCommentAttr = BasePDEScanner.createTextAttribute(
087: fColorManager, IPDEColorConstants.P_XML_COMMENT);
088: fDamagerRepairer = new MultilineDamagerRepairer(null,
089: fXMLCommentAttr);
090: reconciler.setDamager(fDamagerRepairer,
091: XMLPartitionScanner.XML_COMMENT);
092: reconciler.setRepairer(fDamagerRepairer,
093: XMLPartitionScanner.XML_COMMENT);
094:
095: return reconciler;
096: }
097:
098: public IAnnotationHover getAnnotationHover(
099: ISourceViewer sourceViewer) {
100: if (fAnnotationHover == null)
101: fAnnotationHover = new AnnotationHover();
102: return fAnnotationHover;
103: }
104:
105: /**
106: * Preference colors or fonts have changed.
107: * Update the default tokens of the scanners.
108: */
109: public void adaptToPreferenceChange(PropertyChangeEvent event) {
110: if (fTagScanner == null)
111: return; //property change before the editor is fully created
112: if (affectsColorPresentation(event))
113: fColorManager.handlePropertyChangeEvent(event);
114: fTagScanner.adaptToPreferenceChange(event);
115: fPdeScanner.adaptToPreferenceChange(event);
116: String property = event.getProperty();
117: if (property.startsWith(IPDEColorConstants.P_XML_COMMENT)) {
118: adaptTextAttribute(event);
119: }
120: }
121:
122: private void adaptTextAttribute(PropertyChangeEvent event) {
123: String property = event.getProperty();
124: if (property.endsWith(IPDEColorConstants.P_BOLD_SUFFIX)) {
125: fXMLCommentAttr = adaptToStyleChange(event, SWT.BOLD,
126: fXMLCommentAttr);
127: } else if (property
128: .endsWith(IPDEColorConstants.P_ITALIC_SUFFIX)) {
129: fXMLCommentAttr = adaptToStyleChange(event, SWT.ITALIC,
130: fXMLCommentAttr);
131: } else {
132: fXMLCommentAttr = new TextAttribute(fColorManager
133: .getColor(event.getProperty()), fXMLCommentAttr
134: .getBackground(), fXMLCommentAttr.getStyle());
135: }
136: fDamagerRepairer.setDefaultTextAttribute(fXMLCommentAttr);
137: }
138:
139: private TextAttribute adaptToStyleChange(PropertyChangeEvent event,
140: int styleAttribute, TextAttribute textAttribute) {
141: boolean eventValue = false;
142: Object value = event.getNewValue();
143: if (value instanceof Boolean)
144: eventValue = ((Boolean) value).booleanValue();
145:
146: boolean activeValue = (textAttribute.getStyle() & styleAttribute) == styleAttribute;
147: if (activeValue != eventValue) {
148: Color foreground = textAttribute.getForeground();
149: Color background = textAttribute.getBackground();
150: int style = eventValue ? textAttribute.getStyle()
151: | styleAttribute : textAttribute.getStyle()
152: & ~styleAttribute;
153: textAttribute = new TextAttribute(foreground, background,
154: style);
155: }
156: return textAttribute;
157: }
158:
159: public boolean affectsTextPresentation(PropertyChangeEvent event) {
160: String property = event.getProperty();
161: return property.startsWith(IPDEColorConstants.P_DEFAULT)
162: || property.startsWith(IPDEColorConstants.P_PROC_INSTR)
163: || property.startsWith(IPDEColorConstants.P_STRING)
164: || property
165: .startsWith(IPDEColorConstants.P_EXTERNALIZED_STRING)
166: || property.startsWith(IPDEColorConstants.P_TAG)
167: || property
168: .startsWith(IPDEColorConstants.P_XML_COMMENT);
169: }
170:
171: public boolean affectsColorPresentation(PropertyChangeEvent event) {
172: String property = event.getProperty();
173: return property.equals(IPDEColorConstants.P_DEFAULT)
174: || property.equals(IPDEColorConstants.P_PROC_INSTR)
175: || property.equals(IPDEColorConstants.P_STRING)
176: || property
177: .equals(IPDEColorConstants.P_EXTERNALIZED_STRING)
178: || property.equals(IPDEColorConstants.P_TAG)
179: || property.equals(IPDEColorConstants.P_XML_COMMENT);
180: }
181:
182: public IQuickAssistAssistant getQuickAssistAssistant(
183: ISourceViewer sourceViewer) {
184: if (sourceViewer.isEditable()) {
185: if (fQuickAssistant == null)
186: fQuickAssistant = new PDEQuickAssistAssistant();
187: return fQuickAssistant;
188: }
189: return null;
190: }
191:
192: public void dispose() {
193: if (fQuickAssistant != null)
194: fQuickAssistant.dispose();
195: }
196:
197: protected int getInfoImplementationType() {
198: return SourceInformationProvider.F_XML_IMP;
199: }
200:
201: public String getConfiguredDocumentPartitioning(
202: ISourceViewer sourceViewer) {
203: return XMLDocumentSetupParticpant.XML_PARTITIONING;
204: }
205:
206: }
|