package net.sourceforge.phpdt.sql.editors; import java.util.HashMap; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.preference.PreferenceConverter; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.TextAttribute; import org.eclipse.jface.text.presentation.IPresentationReconciler; import org.eclipse.jface.text.presentation.PresentationReconciler; import org.eclipse.jface.text.source.ISourceViewer; import org.eclipse.jface.text.source.SourceViewerConfiguration; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.RGB; import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin; public class SQLConfiguration extends SourceViewerConfiguration { private PresentationReconciler reconciler = new PresentationReconciler(); private ColorManager colorManager; private HashMap cache = new HashMap(); private boolean textBold = false; private boolean keywordBold = true; private boolean stringBold = false; private boolean commentBold = false; private boolean numericBold = false; public SQLConfiguration(ColorManager colorManager) { this.colorManager = colorManager; } public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) { return new String[] { IDocument.DEFAULT_CONTENT_TYPE, SQLPartitionScanner.SQL_COMMENT, SQLPartitionScanner.SQL_KEYWORD, SQLPartitionScanner.SQL_IDENTIFIER}; } public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) { initializeColors(); return reconciler; } public void loadPrefs() { IPreferenceStore store = PHPEclipseSQLPlugin.getDefault().getPreferenceStore(); textBold = store.getBoolean("phpeclipse.sql.text.bold"); keywordBold = store.getBoolean("phpeclipse.sql.keyword.bold"); stringBold = store.getBoolean("phpeclipse.sql.string.bold"); commentBold = store.getBoolean("phpeclipse.sql.comment.bold"); numericBold = store.getBoolean("phpeclipse.sql.numeric.bold"); SQLColorConstants.BACKGROUND = PreferenceConverter.getColor(store, "phpeclipse.sql.background.color"); SQLColorConstants.DEFAULT = PreferenceConverter.getColor(store, "phpeclipse.sql.text.color"); SQLColorConstants.IDENTIFIER = PreferenceConverter.getColor(store, "phpeclipse.sql.text.color"); SQLColorConstants.KEYWORD = PreferenceConverter.getColor(store, "phpeclipse.sql.keyword.color"); SQLColorConstants.STRING = PreferenceConverter.getColor(store, "phpeclipse.sql.string.color"); SQLColorConstants.COMMENT = PreferenceConverter.getColor(store, "phpeclipse.sql.comment.color"); SQLColorConstants.NUMERIC = PreferenceConverter.getColor(store, "phpeclipse.sql.numeric.color"); } public void initializeColors() { setDamageRepairer(getAttr(SQLColorConstants.KEYWORD, keywordBold), SQLPartitionScanner.SQL_KEYWORD); setDamageRepairer(getAttr(SQLColorConstants.COMMENT, commentBold), SQLPartitionScanner.SQL_COMMENT); setDamageRepairer(getAttr(SQLColorConstants.STRING, stringBold), SQLPartitionScanner.SQL_STRING); setDamageRepairer(getAttr(SQLColorConstants.DEFAULT, textBold), IDocument.DEFAULT_CONTENT_TYPE); setDamageRepairer(getAttr(SQLColorConstants.DEFAULT, textBold), SQLPartitionScanner.SQL_SYMBOL); setDamageRepairer(getAttr(SQLColorConstants.DEFAULT, textBold), SQLPartitionScanner.SQL_IDENTIFIER); setDamageRepairer(getAttr(SQLColorConstants.DEFAULT, textBold), SQLPartitionScanner.SQL_SEPARATOR); setDamageRepairer(getAttr(SQLColorConstants.NUMERIC, numericBold), SQLPartitionScanner.SQL_NUMERIC); } public TextAttribute getAttr(RGB color, boolean bold) { colorManager.getColor(SQLColorConstants.BACKGROUND); Color foreground = colorManager.getColor(color); TextAttribute attr = new TextAttribute(foreground); if (bold) { return new TextAttribute(foreground, attr.getBackground(), SWT.BOLD); } return attr; } public void setDamageRepairer(TextAttribute attr, String token) { NonRuleBasedDamagerRepairer ndr = (NonRuleBasedDamagerRepairer) cache.get(token); if (ndr == null) { ndr = new NonRuleBasedDamagerRepairer(attr); reconciler.setDamager(ndr, token); reconciler.setRepairer(ndr, token); cache.put(token, ndr); } else { ndr.setTextAttribute(attr); } } }