]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/CSSSourceViewerConfiguration.java
Externalize strings in org.simantics.document.ui
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / CSSSourceViewerConfiguration.java
1 package org.simantics.document.ui;
2
3 import org.eclipse.jface.preference.IPreferenceStore;
4 import org.eclipse.jface.text.DefaultInformationControl;
5 import org.eclipse.jface.text.IDocument;
6 import org.eclipse.jface.text.IInformationControl;
7 import org.eclipse.jface.text.IInformationControlCreator;
8 import org.eclipse.jface.text.ITextHover;
9 import org.eclipse.jface.text.TextAttribute;
10 import org.eclipse.jface.text.contentassist.ContentAssistant;
11 import org.eclipse.jface.text.contentassist.IContentAssistant;
12 import org.eclipse.jface.text.presentation.IPresentationReconciler;
13 import org.eclipse.jface.text.presentation.PresentationReconciler;
14 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
15 import org.eclipse.jface.text.rules.EndOfLineRule;
16 import org.eclipse.jface.text.rules.IRule;
17 import org.eclipse.jface.text.rules.ITokenScanner;
18 import org.eclipse.jface.text.rules.IWordDetector;
19 import org.eclipse.jface.text.rules.MultiLineRule;
20 import org.eclipse.jface.text.rules.PatternRule;
21 import org.eclipse.jface.text.rules.RuleBasedScanner;
22 import org.eclipse.jface.text.rules.Token;
23 import org.eclipse.jface.text.rules.WordRule;
24 import org.eclipse.jface.text.source.DefaultAnnotationHover;
25 import org.eclipse.jface.text.source.IAnnotationHover;
26 import org.eclipse.jface.text.source.ISharedTextColors;
27 import org.eclipse.jface.text.source.ISourceViewer;
28 import org.eclipse.jface.text.source.SourceViewerConfiguration;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.graphics.Device;
31 import org.eclipse.swt.graphics.Font;
32 import org.eclipse.swt.graphics.RGB;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.ui.editors.text.EditorsUI;
35 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
36
37 public class CSSSourceViewerConfiguration extends SourceViewerConfiguration {
38
39     public static final char[] CONTENT_ASSIST_AUTO_CHARS = new char[] { '.' };
40     Device device;
41     
42     ISharedTextColors sharedTextColors;
43     IPreferenceStore preferenceStore;
44     
45     private CSSTextEditorEnvironment editorEnvironment;
46     
47     public CSSSourceViewerConfiguration(Device device,
48             ISharedTextColors sharedTextColors) {
49         this.device = device;
50         this.sharedTextColors = sharedTextColors;
51         this.preferenceStore = EditorsUI.getPreferenceStore();
52         
53         this.editorEnvironment = new CSSTextEditorEnvironment();
54     }
55
56     public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
57         return new String[] {
58             IDocument.DEFAULT_CONTENT_TYPE
59         };
60     }
61     
62         @Override
63     public int getTabWidth(ISourceViewer sourceViewer) {
64                 if (preferenceStore == null)
65                         return super.getTabWidth(sourceViewer);
66                 return preferenceStore.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
67         }
68
69         @Override
70         public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
71                 String[] indentPrefixes = getIndentPrefixesForTab(getTabWidth(sourceViewer));
72                 if (indentPrefixes == null)
73                         return null;
74
75                 int length = indentPrefixes.length;
76                 if (length > 2) {
77                         // Swap first with second last
78                         String first = indentPrefixes[0];
79                         indentPrefixes[0] = indentPrefixes[length - 2];
80                         indentPrefixes[length - 2] = first;
81                 }
82
83                 return indentPrefixes;
84         }
85     
86     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
87         PresentationReconciler reconciler = new PresentationReconciler();
88         
89         DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner());
90         
91         reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
92         reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
93         
94         return reconciler;
95     }
96     
97     ITokenScanner getSclTokenScanner() {
98         RuleBasedScanner scanner = new RuleBasedScanner();
99         
100         Font font = new Font(device, "Courier New", 10, SWT.NORMAL); //$NON-NLS-1$
101         Font boldFont = new Font(device, "Courier New", 10, SWT.BOLD); //$NON-NLS-1$
102
103         Token defaultToken = new Token(
104                 new TextAttribute(
105                         sharedTextColors.getColor(new RGB(0, 0, 0)),
106                         null,
107                         0,
108                         font
109                 ));
110         Token string = new Token(new TextAttribute(
111                 sharedTextColors.getColor(new RGB(42, 0, 255)),
112                 null,
113                 0,
114                 font
115                 ));
116         Token reserved = new Token(
117                 new TextAttribute(
118                         sharedTextColors.getColor(new RGB(127, 0, 85)),
119                         null,
120                         SWT.BOLD,
121                         boldFont
122                 ));
123         Token comment = new Token(new TextAttribute(
124                 sharedTextColors.getColor(new RGB(63, 127, 95)),
125                 null,
126                 0,
127                 font
128                 ));
129
130         WordRule reservedWord = new WordRule(new IWordDetector() {          
131             @Override
132             public boolean isWordStart(char c) {
133                 return Character.isJavaIdentifierStart(c);
134             }
135             
136             @Override
137             public boolean isWordPart(char c) {
138                 return Character.isJavaIdentifierPart(c) || c=='.';
139             }
140         });
141
142         reservedWord.addWord("if", reserved); //$NON-NLS-1$
143         reservedWord.addWord("then", reserved); //$NON-NLS-1$
144         reservedWord.addWord("else", reserved); //$NON-NLS-1$
145         reservedWord.addWord("match", reserved); //$NON-NLS-1$
146         reservedWord.addWord("with", reserved); //$NON-NLS-1$
147         reservedWord.addWord("data", reserved); //$NON-NLS-1$
148         reservedWord.addWord("type", reserved); //$NON-NLS-1$
149         reservedWord.addWord("class", reserved); //$NON-NLS-1$
150         
151         IRule[] rules = new IRule[] {
152             //new MultiLineRule("\"\"\"", "\"\"\"", string),
153             new PatternRule("\"", "\"", string, '\\', true), //$NON-NLS-1$ //$NON-NLS-2$
154             new MultiLineRule("/*", "*/", comment), //$NON-NLS-1$ //$NON-NLS-2$
155             new EndOfLineRule("//", comment), //$NON-NLS-1$
156             reservedWord
157         };
158         scanner.setRules(rules);
159         scanner.setDefaultReturnToken(defaultToken);
160         
161         return scanner;
162     }
163     
164     @Override
165     public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
166         return new CSSEditorTextHover(sourceViewer, editorEnvironment);
167     }
168     
169     @Override
170     public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
171         return new DefaultAnnotationHover();
172     }
173     
174     @Override
175     public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
176         ContentAssistant assistant = new ContentAssistant();
177         assistant.enableColoredLabels(true);
178         assistant.setStatusLineVisible(true);
179
180         assistant.enableAutoActivation(true);
181         assistant.enableAutoInsert(true);
182         assistant.setContentAssistProcessor(new CSSCompletionAssistProcessor(editorEnvironment), IDocument.DEFAULT_CONTENT_TYPE);
183         assistant.setInformationControlCreator(CREATOR);
184         assistant.setRestoreCompletionProposalSize(Activator.getDefault().getDialogSettings()); 
185         return assistant;
186     }
187     
188     private static final IInformationControlCreator CREATOR = new IInformationControlCreator() {
189         
190         @Override
191         public IInformationControl createInformationControl(Shell parent) {
192             return new DefaultInformationControl(parent);
193         }
194     };
195
196 //    @Override
197 //    public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
198 //        return new ContentFormatter();
199 //    }
200 }