Merging code from trunk that fixes #774, this work was done by incastrix
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / editors / SQLSourceViewerConfiguration.java
1 package com.quantum.editors;
2
3 import java.util.HashMap;
4
5 import com.quantum.PluginPreferences;
6 import com.quantum.QuantumPlugin;
7
8 import org.eclipse.jface.preference.IPreferenceStore;
9 import org.eclipse.jface.preference.PreferenceConverter;
10 import org.eclipse.jface.text.IDocument;
11 import org.eclipse.jface.text.TextAttribute;
12 import org.eclipse.jface.text.contentassist.ContentAssistant;
13 import org.eclipse.jface.text.contentassist.IContentAssistant;
14 import org.eclipse.jface.text.presentation.IPresentationReconciler;
15 import org.eclipse.jface.text.presentation.PresentationReconciler;
16 import org.eclipse.jface.text.source.ISourceViewer;
17 import org.eclipse.jface.text.source.SourceViewerConfiguration;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.Color;
20 import org.eclipse.swt.graphics.RGB;
21
22 public class SQLSourceViewerConfiguration extends SourceViewerConfiguration {
23         private PresentationReconciler reconciler = new PresentationReconciler();
24         private ColorManager colorManager;
25         private HashMap cache = new HashMap();
26         private boolean textBold = false;
27         private boolean keywordBold = true;
28         private boolean stringBold = false;
29         private boolean commentBold = false;
30         private boolean numericBold = false;
31         public SQLSourceViewerConfiguration(ColorManager colorManager) {
32                 this.colorManager = colorManager;
33         }
34         public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
35                 return new String[] {
36                         IDocument.DEFAULT_CONTENT_TYPE,
37                         SQLPartitionScanner.SQL_COMMENT,
38                         SQLPartitionScanner.SQL_KEYWORD,
39                         SQLPartitionScanner.SQL_IDENTIFIER};
40         }
41
42         public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
43                 initializeColors();
44                 return reconciler;
45         }
46         public void loadPrefs() {
47                 IPreferenceStore store = QuantumPlugin.getDefault().getPreferenceStore();
48                 textBold = store.getBoolean("quantum.text.bold"); //$NON-NLS-1$
49                 keywordBold = store.getBoolean("quantum.keyword.bold"); //$NON-NLS-1$
50                 stringBold = store.getBoolean("quantum.string.bold"); //$NON-NLS-1$
51                 commentBold = store.getBoolean("quantum.comment.bold"); //$NON-NLS-1$
52                 numericBold = store.getBoolean("quantum.numeric.bold"); //$NON-NLS-1$
53                 SQLColorConstants.BACKGROUND = PreferenceConverter.getColor(store, PluginPreferences.BACKGROUND_COLOR); //$NON-NLS-1$
54                 SQLColorConstants.DEFAULT = PreferenceConverter.getColor(store, PluginPreferences.TEXT_COLOR); //$NON-NLS-1$
55                 SQLColorConstants.IDENTIFIER = PreferenceConverter.getColor(store, PluginPreferences.TEXT_COLOR); //$NON-NLS-1$
56                 SQLColorConstants.KEYWORD = PreferenceConverter.getColor(store, PluginPreferences.KEYWORD_COLOR); //$NON-NLS-1$
57                 SQLColorConstants.STRING = PreferenceConverter.getColor(store, PluginPreferences.STRING_COLOR); //$NON-NLS-1$
58                 SQLColorConstants.COMMENT = PreferenceConverter.getColor(store, PluginPreferences.COMMENT_COLOR); //$NON-NLS-1$
59                 SQLColorConstants.NUMERIC = PreferenceConverter.getColor(store, PluginPreferences.NUMERIC_COLOR); //$NON-NLS-1$
60         }
61         public void initializeColors() {
62                 setDamageRepairer(getAttr(SQLColorConstants.KEYWORD, keywordBold), SQLPartitionScanner.SQL_KEYWORD);
63                 setDamageRepairer(getAttr(SQLColorConstants.COMMENT, commentBold), SQLPartitionScanner.SQL_COMMENT);
64                 setDamageRepairer(getAttr(SQLColorConstants.STRING, stringBold), SQLPartitionScanner.SQL_STRING);
65                 setDamageRepairer(getAttr(SQLColorConstants.DEFAULT, textBold), IDocument.DEFAULT_CONTENT_TYPE);
66                 setDamageRepairer(getAttr(SQLColorConstants.DEFAULT, textBold), SQLPartitionScanner.SQL_SYMBOL);
67                 setDamageRepairer(getAttr(SQLColorConstants.DEFAULT, textBold), SQLPartitionScanner.SQL_IDENTIFIER);
68                 setDamageRepairer(getAttr(SQLColorConstants.DEFAULT, textBold), SQLPartitionScanner.SQL_SEPARATOR);
69                 setDamageRepairer(getAttr(SQLColorConstants.NUMERIC, numericBold), SQLPartitionScanner.SQL_NUMERIC);
70         }
71         public TextAttribute getAttr(RGB color, boolean bold) {
72                 colorManager.getColor(SQLColorConstants.BACKGROUND);
73                 Color foreground = colorManager.getColor(color);
74                 TextAttribute attr = new TextAttribute(foreground);
75                 if (bold) {
76                         return new TextAttribute(foreground, attr.getBackground(), SWT.BOLD);
77                 }
78                 return attr;
79         }
80         public void setDamageRepairer(TextAttribute attr, String token) {
81                 NonRuleBasedDamagerRepairer ndr = (NonRuleBasedDamagerRepairer) cache.get(token);
82                 if (ndr == null) {
83                         ndr =
84                                 new NonRuleBasedDamagerRepairer(attr);
85                         reconciler.setDamager(ndr, token);
86                         reconciler.setRepairer(ndr, token);
87                         cache.put(token, ndr);
88                 } else {
89                         ndr.setTextAttribute(attr);
90                 }
91         }
92 //      public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
93 //              ContentAssistant assistant = new ContentAssistant();
94 //              assistant.setContentAssistProcessor(new SQLContentAssistProcessor("default"), 
95 //                              IDocument.DEFAULT_CONTENT_TYPE);
96 ////            assistant.setContentAssistProcessor(new SQLContentAssistProcessor("comment"), 
97 ////                            SQLPartitionScanner.SQL_COMMENT);
98 ////            assistant.setContentAssistProcessor(new SQLContentAssistProcessor("keyword"), 
99 ////                            SQLPartitionScanner.SQL_KEYWORD);
100 ////            assistant.setContentAssistProcessor(new SQLContentAssistProcessor("identifier"), 
101 ////                            SQLPartitionScanner.SQL_IDENTIFIER);
102 //              
103 //              // everybody else is doin' it...
104 //              assistant.enableAutoActivation(true);
105 //              assistant.setAutoActivationDelay(500);
106 //              
107 //              assistant.setProposalPopupOrientation(ContentAssistant.CONTEXT_INFO_BELOW);
108 //              assistant.setContextInformationPopupOrientation(ContentAssistant.CONTEXT_INFO_BELOW);
109 //              return assistant;
110 //      }
111 }