001: /*******************************************************************************
002: * Copyright (c) 2005 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.preference.IPreferenceStore;
013: import org.eclipse.jface.text.TextAttribute;
014: import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
015: import org.eclipse.jface.text.rules.Token;
016: import org.eclipse.jface.util.PropertyChangeEvent;
017: import org.eclipse.pde.internal.ui.PDEPlugin;
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.graphics.Color;
020:
021: public abstract class BasePDEScanner extends BufferedRuleBasedScanner {
022:
023: private IColorManager fColorManager;
024:
025: protected BasePDEScanner() {
026: }
027:
028: public void setColorManager(IColorManager manager) {
029: fColorManager = manager;
030: }
031:
032: public BasePDEScanner(IColorManager manager) {
033: fColorManager = manager;
034: initialize();
035: }
036:
037: public void adaptToPreferenceChange(PropertyChangeEvent event) {
038: String property = event.getProperty();
039: if (affectsTextPresentation(property)) {
040: Token token = getTokenAffected(event);
041: if (property.endsWith(IPDEColorConstants.P_BOLD_SUFFIX))
042: adaptToStyleChange(event, token, SWT.BOLD);
043: else if (property
044: .endsWith(IPDEColorConstants.P_ITALIC_SUFFIX))
045: adaptToStyleChange(event, token, SWT.ITALIC);
046: else
047: adaptToColorChange(event, token);
048: }
049: }
050:
051: public abstract boolean affectsTextPresentation(String property);
052:
053: protected abstract Token getTokenAffected(PropertyChangeEvent event);
054:
055: protected abstract void initialize();
056:
057: protected void adaptToStyleChange(PropertyChangeEvent event,
058: Token token, int styleAttribute) {
059: if (token == null)
060: return;
061:
062: boolean eventValue = false;
063: Object value = event.getNewValue();
064: if (value instanceof Boolean)
065: eventValue = ((Boolean) value).booleanValue();
066:
067: TextAttribute attr = (TextAttribute) token.getData();
068: boolean activeValue = (attr.getStyle() & styleAttribute) == styleAttribute;
069: if (activeValue != eventValue) {
070: Color foreground = attr.getForeground();
071: Color background = attr.getBackground();
072: int style = eventValue ? attr.getStyle() | styleAttribute
073: : attr.getStyle() & ~styleAttribute;
074: token.setData(new TextAttribute(foreground, background,
075: style));
076: }
077: }
078:
079: protected void adaptToColorChange(PropertyChangeEvent event,
080: Token token) {
081: TextAttribute attr = (TextAttribute) token.getData();
082: token
083: .setData(new TextAttribute(fColorManager.getColor(event
084: .getProperty()), attr.getBackground(), attr
085: .getStyle()));
086: }
087:
088: protected TextAttribute createTextAttribute(String property) {
089: return createTextAttribute(fColorManager, property);
090: }
091:
092: protected static TextAttribute createTextAttribute(
093: IColorManager manager, String property) {
094: Color color = manager.getColor(property);
095: int style = SWT.NORMAL;
096: IPreferenceStore store = PDEPlugin.getDefault()
097: .getPreferenceStore();
098: if (store.getBoolean(property
099: + IPDEColorConstants.P_BOLD_SUFFIX))
100: style |= SWT.BOLD;
101: if (store.getBoolean(property
102: + IPDEColorConstants.P_ITALIC_SUFFIX))
103: style |= SWT.ITALIC;
104: return new TextAttribute(color, null, style);
105: }
106:
107: }
|