]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
ffa300d6d345a1b2ba7000fe5cff142aabc9366a
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.properties.widgets.expressions;\r
13 \r
14 import org.eclipse.jface.text.DefaultTextHover;\r
15 import org.eclipse.jface.text.IDocument;\r
16 import org.eclipse.jface.text.ITextHover;\r
17 import org.eclipse.jface.text.TextAttribute;\r
18 import org.eclipse.jface.text.presentation.IPresentationReconciler;\r
19 import org.eclipse.jface.text.presentation.PresentationReconciler;\r
20 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;\r
21 import org.eclipse.jface.text.rules.IRule;\r
22 import org.eclipse.jface.text.rules.ITokenScanner;\r
23 import org.eclipse.jface.text.rules.IWordDetector;\r
24 import org.eclipse.jface.text.rules.MultiLineRule;\r
25 import org.eclipse.jface.text.rules.RuleBasedScanner;\r
26 import org.eclipse.jface.text.rules.Token;\r
27 import org.eclipse.jface.text.rules.WordRule;\r
28 import org.eclipse.jface.text.source.DefaultAnnotationHover;\r
29 import org.eclipse.jface.text.source.IAnnotationHover;\r
30 import org.eclipse.jface.text.source.ISourceViewer;\r
31 import org.eclipse.jface.text.source.SourceViewerConfiguration;\r
32 import org.eclipse.swt.SWT;\r
33 import org.eclipse.swt.graphics.RGB;\r
34 import org.simantics.sysdyn.ui.properties.VariableNameUtils;\r
35 \r
36 public class ExpressionFieldConfiguration extends SourceViewerConfiguration {\r
37 \r
38 \r
39         ColorManager colorManager;\r
40 \r
41         public ExpressionFieldConfiguration(ColorManager colorManager) {\r
42                 super();\r
43                 this.colorManager = colorManager;\r
44         }\r
45 \r
46         public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {\r
47                 return new String[] {\r
48                                 IDocument.DEFAULT_CONTENT_TYPE\r
49                 };\r
50         }\r
51 \r
52         public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {\r
53                 PresentationReconciler reconciler = new PresentationReconciler();\r
54 \r
55                 DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner());\r
56 \r
57                 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
58                 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
59 \r
60                 return reconciler;\r
61         }\r
62 \r
63 \r
64         ITokenScanner getSclTokenScanner() {\r
65                 RuleBasedScanner scanner = new RuleBasedScanner();\r
66 \r
67                 final Token reserved = new Token(\r
68                                 new TextAttribute(\r
69                                                 colorManager.getColor(new RGB(127, 0, 85)),\r
70                                                 null,\r
71                                                 SWT.BOLD\r
72                                 ));\r
73             final Token defaultToken = new Token(new TextAttribute(colorManager.getColor(new RGB(0, 0, 0))));\r
74 \r
75                  final Token comment = new Token(new TextAttribute(colorManager.getColor(new RGB(63, 127, 95))));\r
76 \r
77 \r
78                 WordRule reservedWord = new WordRule(new IWordDetector() {   \r
79                         @Override\r
80                         public boolean isWordStart(char c) {\r
81                                 return Character.isLetter(c);\r
82                         }\r
83 \r
84                         @Override\r
85                         public boolean isWordPart(char c) {\r
86                                 return Character.isLetter(c);\r
87                         }\r
88                 }, defaultToken);\r
89                 \r
90 \r
91                 for(String s : VariableNameUtils.keywords) {\r
92                         reservedWord.addWord(s, reserved);\r
93                 }\r
94 \r
95                 IRule[] rules = new IRule[] {\r
96                                 reservedWord,\r
97                                 new MultiLineRule("/*", "*/", comment)\r
98                 };\r
99                 scanner.setRules(rules);\r
100 \r
101                 return scanner;  \r
102         }\r
103 \r
104         @Override\r
105         public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {\r
106                 return new DefaultTextHover(sourceViewer);\r
107         }\r
108 \r
109         @Override\r
110         public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {\r
111                 return new DefaultAnnotationHover();\r
112         }\r
113 \r
114 }\r