001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.build;
011:
012: import org.eclipse.jdt.ui.PreferenceConstants;
013: import org.eclipse.jface.preference.IPreferenceStore;
014: import org.eclipse.jface.text.BadLocationException;
015: import org.eclipse.jface.text.IDocument;
016: import org.eclipse.jface.text.IRegion;
017: import org.eclipse.jface.text.TextAttribute;
018: import org.eclipse.jface.text.presentation.IPresentationReconciler;
019: import org.eclipse.jface.text.presentation.PresentationReconciler;
020: import org.eclipse.jface.text.quickassist.IQuickAssistAssistant;
021: import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
022: import org.eclipse.jface.text.rules.IRule;
023: import org.eclipse.jface.text.rules.IWhitespaceDetector;
024: import org.eclipse.jface.text.rules.IWordDetector;
025: import org.eclipse.jface.text.rules.Token;
026: import org.eclipse.jface.text.rules.WhitespaceRule;
027: import org.eclipse.jface.text.rules.WordRule;
028: import org.eclipse.jface.text.source.ISourceViewer;
029: import org.eclipse.jface.util.PropertyChangeEvent;
030: import org.eclipse.pde.internal.ui.editor.PDESourcePage;
031: import org.eclipse.pde.internal.ui.editor.text.ArgumentRule;
032: import org.eclipse.pde.internal.ui.editor.text.BasePDEScanner;
033: import org.eclipse.pde.internal.ui.editor.text.ChangeAwareSourceViewerConfiguration;
034: import org.eclipse.pde.internal.ui.editor.text.IColorManager;
035: import org.eclipse.pde.internal.ui.editor.text.PDEQuickAssistAssistant;
036: import org.eclipse.swt.SWT;
037: import org.eclipse.swt.graphics.Color;
038:
039: public class BuildSourceViewerConfiguration extends
040: ChangeAwareSourceViewerConfiguration {
041:
042: protected static String PROPERTIES_FILE_PARTITIONING = "___pf_partitioning"; //$NON-NLS-1$
043: protected static String COMMENT = "__pf_comment"; //$NON-NLS-1$
044: protected static String PROPERTY_VALUE = "__pf_roperty_value"; //$NON-NLS-1$
045: protected static String[] PARTITIONS = new String[] { COMMENT,
046: PROPERTY_VALUE };
047:
048: private BasePDEScanner fPropertyKeyScanner;
049: private BasePDEScanner fCommentScanner;
050: private BasePDEScanner fPropertyValueScanner;
051:
052: private PDEQuickAssistAssistant fQuickAssistant;
053:
054: private abstract class AbstractJavaScanner extends BasePDEScanner {
055:
056: public void adaptToPreferenceChange(PropertyChangeEvent event) {
057: String property = event.getProperty();
058: if (affectsTextPresentation(property)) {
059: Token token = getTokenAffected(event);
060: if (property
061: .endsWith(PreferenceConstants.EDITOR_BOLD_SUFFIX))
062: adaptToStyleChange(event, token, SWT.BOLD);
063: else if (property
064: .endsWith(PreferenceConstants.EDITOR_ITALIC_SUFFIX))
065: adaptToStyleChange(event, token, SWT.ITALIC);
066: else if (property
067: .endsWith(PreferenceConstants.EDITOR_STRIKETHROUGH_SUFFIX))
068: adaptToStyleChange(event, token,
069: TextAttribute.STRIKETHROUGH);
070: else if (property
071: .endsWith(PreferenceConstants.EDITOR_UNDERLINE_SUFFIX))
072: adaptToStyleChange(event, token,
073: TextAttribute.UNDERLINE);
074: else
075: adaptToColorChange(event, token);
076: }
077: }
078:
079: protected TextAttribute createTextAttribute(String property) {
080: Color color = fColorManager.getColor(property);
081: int style = SWT.NORMAL;
082: if (fPreferenceStore.getBoolean(property
083: + PreferenceConstants.EDITOR_BOLD_SUFFIX))
084: style |= SWT.BOLD;
085: if (fPreferenceStore.getBoolean(property
086: + PreferenceConstants.EDITOR_ITALIC_SUFFIX))
087: style |= SWT.ITALIC;
088: if (fPreferenceStore.getBoolean(property
089: + PreferenceConstants.EDITOR_STRIKETHROUGH_SUFFIX))
090: style |= TextAttribute.STRIKETHROUGH;
091: if (fPreferenceStore.getBoolean(property
092: + PreferenceConstants.EDITOR_UNDERLINE_SUFFIX))
093: style |= TextAttribute.UNDERLINE;
094: return new TextAttribute(color, null, style);
095: }
096: }
097:
098: private class SingleTokenJavaScanner extends AbstractJavaScanner {
099:
100: private String fProperty;
101:
102: public SingleTokenJavaScanner(String property) {
103: fProperty = property;
104: setColorManager(fColorManager);
105: initialize();
106: }
107:
108: public boolean affectsTextPresentation(String property) {
109: return property.startsWith(fProperty);
110: }
111:
112: protected Token getTokenAffected(PropertyChangeEvent event) {
113: return (Token) fDefaultReturnToken;
114: }
115:
116: protected void initialize() {
117: setDefaultReturnToken(new Token(
118: createTextAttribute(fProperty)));
119: }
120: }
121:
122: public class PropertyValueScanner extends AbstractJavaScanner {
123:
124: public class AssignmentDetector implements IWordDetector {
125:
126: public boolean isWordStart(char c) {
127: if ('=' != c && ':' != c || fDocument == null)
128: return false;
129:
130: try {
131: // check whether it is the first '='
132: IRegion lineInfo = fDocument
133: .getLineInformationOfOffset(fOffset);
134: int offset = lineInfo.getOffset();
135: String line = fDocument.get(offset, lineInfo
136: .getLength());
137: int i = line.indexOf(c);
138: return i != -1
139: && i + lineInfo.getOffset() + 1 == fOffset;
140: } catch (BadLocationException ex) {
141: return false;
142: }
143: }
144:
145: public boolean isWordPart(char c) {
146: return false;
147: }
148: }
149:
150: private Token fArgumentToken;
151: private Token fAssignmentToken;
152:
153: public PropertyValueScanner() {
154: setColorManager(fColorManager);
155: initialize();
156: }
157:
158: public boolean affectsTextPresentation(String property) {
159: return property
160: .startsWith(PreferenceConstants.PROPERTIES_FILE_COLORING_VALUE)
161: || property
162: .startsWith(PreferenceConstants.PROPERTIES_FILE_COLORING_ARGUMENT)
163: || property
164: .startsWith(PreferenceConstants.PROPERTIES_FILE_COLORING_ASSIGNMENT);
165: }
166:
167: protected Token getTokenAffected(PropertyChangeEvent event) {
168: String property = event.getProperty();
169: if (property
170: .startsWith(PreferenceConstants.PROPERTIES_FILE_COLORING_ARGUMENT))
171: return fArgumentToken;
172: if (property
173: .startsWith(PreferenceConstants.PROPERTIES_FILE_COLORING_ASSIGNMENT))
174: return fAssignmentToken;
175: return (Token) fDefaultReturnToken;
176: }
177:
178: protected void initialize() {
179: IRule[] rules = new IRule[3];
180: fArgumentToken = new Token(
181: createTextAttribute(PreferenceConstants.PROPERTIES_FILE_COLORING_ARGUMENT));
182: rules[0] = new ArgumentRule(fArgumentToken);
183:
184: fAssignmentToken = new Token(
185: createTextAttribute(PreferenceConstants.PROPERTIES_FILE_COLORING_ASSIGNMENT));
186: rules[1] = new WordRule(new AssignmentDetector(),
187: fAssignmentToken);
188:
189: rules[2] = new WhitespaceRule(new IWhitespaceDetector() {
190: public boolean isWhitespace(char c) {
191: return Character.isWhitespace(c);
192: }
193: });
194: setRules(rules);
195: setDefaultReturnToken(new Token(
196: createTextAttribute(PreferenceConstants.PROPERTIES_FILE_COLORING_VALUE)));
197: }
198: }
199:
200: public BuildSourceViewerConfiguration(IColorManager colorManager,
201: IPreferenceStore store, PDESourcePage sourcePage) {
202: super (sourcePage, colorManager, store);
203: initializeScanners();
204: }
205:
206: private void initializeScanners() {
207: fPropertyKeyScanner = new SingleTokenJavaScanner(
208: PreferenceConstants.PROPERTIES_FILE_COLORING_KEY);
209: fPropertyValueScanner = new PropertyValueScanner();
210: fCommentScanner = new SingleTokenJavaScanner(
211: PreferenceConstants.PROPERTIES_FILE_COLORING_COMMENT);
212: }
213:
214: public IPresentationReconciler getPresentationReconciler(
215: ISourceViewer sourceViewer) {
216: PresentationReconciler reconciler = new PresentationReconciler();
217: reconciler
218: .setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
219:
220: DefaultDamagerRepairer dr = new DefaultDamagerRepairer(
221: fPropertyKeyScanner);
222: reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
223: reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
224:
225: dr = new DefaultDamagerRepairer(fCommentScanner);
226: reconciler.setDamager(dr, COMMENT);
227: reconciler.setRepairer(dr, COMMENT);
228:
229: dr = new DefaultDamagerRepairer(fPropertyValueScanner);
230: reconciler.setDamager(dr, PROPERTY_VALUE);
231: reconciler.setRepairer(dr, PROPERTY_VALUE);
232:
233: return reconciler;
234: }
235:
236: public void adaptToPreferenceChange(PropertyChangeEvent event) {
237: if (affectsColorPresentation(event))
238: fColorManager.handlePropertyChangeEvent(event);
239: fPropertyKeyScanner.adaptToPreferenceChange(event);
240: fCommentScanner.adaptToPreferenceChange(event);
241: fPropertyValueScanner.adaptToPreferenceChange(event);
242: }
243:
244: public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
245: int length = PARTITIONS.length;
246: String[] contentTypes = new String[length + 1];
247: contentTypes[0] = IDocument.DEFAULT_CONTENT_TYPE;
248: for (int i = 0; i < length; i++)
249: contentTypes[i + 1] = PARTITIONS[i];
250:
251: return contentTypes;
252: }
253:
254: public String getConfiguredDocumentPartitioning(
255: ISourceViewer sourceViewer) {
256: return PROPERTIES_FILE_PARTITIONING;
257: }
258:
259: public boolean affectsTextPresentation(PropertyChangeEvent event) {
260: String property = event.getProperty();
261: return fCommentScanner.affectsTextPresentation(property)
262: || fPropertyKeyScanner
263: .affectsTextPresentation(property)
264: || fPropertyValueScanner
265: .affectsTextPresentation(property);
266: }
267:
268: public boolean affectsColorPresentation(PropertyChangeEvent event) {
269: String property = event.getProperty();
270: return property
271: .equals(PreferenceConstants.PROPERTIES_FILE_COLORING_VALUE)
272: || property
273: .equals(PreferenceConstants.PROPERTIES_FILE_COLORING_ARGUMENT)
274: || property
275: .equals(PreferenceConstants.PROPERTIES_FILE_COLORING_ASSIGNMENT)
276: || property
277: .equals(PreferenceConstants.PROPERTIES_FILE_COLORING_KEY)
278: || property
279: .equals(PreferenceConstants.PROPERTIES_FILE_COLORING_COMMENT);
280: }
281:
282: public IQuickAssistAssistant getQuickAssistAssistant(
283: ISourceViewer sourceViewer) {
284: if (sourceViewer.isEditable()) {
285: if (fQuickAssistant == null)
286: fQuickAssistant = new PDEQuickAssistAssistant();
287: return fQuickAssistant;
288: }
289: return null;
290: }
291:
292: public void dispose() {
293: if (fQuickAssistant != null)
294: fQuickAssistant.dispose();
295: }
296: }
|