new build.xml
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / editors / SQLConfiguration.java
1 package com.quantum.editors;
2
3 import java.util.HashMap;
4
5 import com.quantum.QuantumPlugin;
6
7 import org.eclipse.jface.preference.IPreferenceStore;
8 import org.eclipse.jface.preference.PreferenceConverter;
9 import org.eclipse.jface.text.IDocument;
10 import org.eclipse.jface.text.TextAttribute;
11 import org.eclipse.jface.text.presentation.IPresentationReconciler;
12 import org.eclipse.jface.text.presentation.PresentationReconciler;
13 import org.eclipse.jface.text.source.ISourceViewer;
14 import org.eclipse.jface.text.source.SourceViewerConfiguration;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.swt.graphics.RGB;
18
19 public class SQLConfiguration extends SourceViewerConfiguration {
20         private PresentationReconciler reconciler = new PresentationReconciler();
21         private ColorManager colorManager;
22         private HashMap cache = new HashMap();
23         private boolean textBold = false;
24         private boolean keywordBold = true;
25         private boolean stringBold = false;
26         private boolean commentBold = false;
27         private boolean numericBold = false;
28         public SQLConfiguration(ColorManager colorManager) {
29                 this.colorManager = colorManager;
30         }
31         public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
32                 return new String[] {
33                         IDocument.DEFAULT_CONTENT_TYPE,
34                         SQLPartitionScanner.SQL_COMMENT,
35                         SQLPartitionScanner.SQL_KEYWORD,
36                         SQLPartitionScanner.SQL_IDENTIFIER};
37         }
38
39         public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
40                 initializeColors();
41                 return reconciler;
42         }
43         public void loadPrefs() {
44                 IPreferenceStore store = QuantumPlugin.getDefault().getPreferenceStore();
45                 textBold = store.getBoolean("quantum.text.bold"); //$NON-NLS-1$
46                 keywordBold = store.getBoolean("quantum.keyword.bold"); //$NON-NLS-1$
47                 stringBold = store.getBoolean("quantum.string.bold"); //$NON-NLS-1$
48                 commentBold = store.getBoolean("quantum.comment.bold"); //$NON-NLS-1$
49                 numericBold = store.getBoolean("quantum.numeric.bold"); //$NON-NLS-1$
50                 SQLColorConstants.BACKGROUND = PreferenceConverter.getColor(store, "quantum.background.color"); //$NON-NLS-1$
51                 SQLColorConstants.DEFAULT = PreferenceConverter.getColor(store, "quantum.text.color"); //$NON-NLS-1$
52                 SQLColorConstants.IDENTIFIER = PreferenceConverter.getColor(store, "quantum.text.color"); //$NON-NLS-1$
53                 SQLColorConstants.KEYWORD = PreferenceConverter.getColor(store, "quantum.keyword.color"); //$NON-NLS-1$
54                 SQLColorConstants.STRING = PreferenceConverter.getColor(store, "quantum.string.color"); //$NON-NLS-1$
55                 SQLColorConstants.COMMENT = PreferenceConverter.getColor(store, "quantum.comment.color"); //$NON-NLS-1$
56                 SQLColorConstants.NUMERIC = PreferenceConverter.getColor(store, "quantum.numeric.color"); //$NON-NLS-1$
57         }
58         public void initializeColors() {
59                 setDamageRepairer(getAttr(SQLColorConstants.KEYWORD, keywordBold), SQLPartitionScanner.SQL_KEYWORD);
60                 setDamageRepairer(getAttr(SQLColorConstants.COMMENT, commentBold), SQLPartitionScanner.SQL_COMMENT);
61                 setDamageRepairer(getAttr(SQLColorConstants.STRING, stringBold), SQLPartitionScanner.SQL_STRING);
62                 setDamageRepairer(getAttr(SQLColorConstants.DEFAULT, textBold), IDocument.DEFAULT_CONTENT_TYPE);
63                 setDamageRepairer(getAttr(SQLColorConstants.DEFAULT, textBold), SQLPartitionScanner.SQL_SYMBOL);
64                 setDamageRepairer(getAttr(SQLColorConstants.DEFAULT, textBold), SQLPartitionScanner.SQL_IDENTIFIER);
65                 setDamageRepairer(getAttr(SQLColorConstants.DEFAULT, textBold), SQLPartitionScanner.SQL_SEPARATOR);
66                 setDamageRepairer(getAttr(SQLColorConstants.NUMERIC, numericBold), SQLPartitionScanner.SQL_NUMERIC);
67         }
68         public TextAttribute getAttr(RGB color, boolean bold) {
69                 colorManager.getColor(SQLColorConstants.BACKGROUND);
70                 Color foreground = colorManager.getColor(color);
71                 TextAttribute attr = new TextAttribute(foreground);
72                 if (bold) {
73                         return new TextAttribute(foreground, attr.getBackground(), SWT.BOLD);
74                 }
75                 return attr;
76         }
77         public void setDamageRepairer(TextAttribute attr, String token) {
78                 NonRuleBasedDamagerRepairer ndr = (NonRuleBasedDamagerRepairer) cache.get(token);
79                 if (ndr == null) {
80                         ndr =
81                                 new NonRuleBasedDamagerRepairer(attr);
82                         reconciler.setDamager(ndr, token);
83                         reconciler.setRepairer(ndr, token);
84                         cache.put(token, ndr);
85                 } else {
86                         ndr.setTextAttribute(attr);
87                 }
88         }
89 }