]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
d2fa797dae084218023966574f94e9810f49c26f
[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.internal.text.html.HTMLTextPresenter;\r
15 import org.eclipse.jface.resource.ResourceManager;\r
16 import org.eclipse.jface.text.DefaultInformationControl;\r
17 import org.eclipse.jface.text.DefaultTextHover;\r
18 import org.eclipse.jface.text.IDocument;\r
19 import org.eclipse.jface.text.IInformationControl;\r
20 import org.eclipse.jface.text.IInformationControlCreator;\r
21 import org.eclipse.jface.text.ITextHover;\r
22 import org.eclipse.jface.text.TextAttribute;\r
23 import org.eclipse.jface.text.contentassist.ContentAssistEvent;\r
24 import org.eclipse.jface.text.contentassist.ContentAssistant;\r
25 import org.eclipse.jface.text.contentassist.ICompletionListener;\r
26 import org.eclipse.jface.text.contentassist.ICompletionProposal;\r
27 import org.eclipse.jface.text.contentassist.IContentAssistant;\r
28 import org.eclipse.jface.text.presentation.IPresentationReconciler;\r
29 import org.eclipse.jface.text.presentation.PresentationReconciler;\r
30 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;\r
31 import org.eclipse.jface.text.rules.IRule;\r
32 import org.eclipse.jface.text.rules.ITokenScanner;\r
33 import org.eclipse.jface.text.rules.IWordDetector;\r
34 import org.eclipse.jface.text.rules.MultiLineRule;\r
35 import org.eclipse.jface.text.rules.RuleBasedScanner;\r
36 import org.eclipse.jface.text.rules.Token;\r
37 import org.eclipse.jface.text.rules.WordRule;\r
38 import org.eclipse.jface.text.source.DefaultAnnotationHover;\r
39 import org.eclipse.jface.text.source.IAnnotationHover;\r
40 import org.eclipse.jface.text.source.ISourceViewer;\r
41 import org.eclipse.jface.text.source.SourceViewerConfiguration;\r
42 import org.eclipse.swt.SWT;\r
43 import org.eclipse.swt.graphics.RGB;\r
44 import org.eclipse.swt.widgets.Shell;\r
45 import org.eclipse.swt.widgets.Table;\r
46 import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
47 \r
48 @SuppressWarnings("restriction")\r
49 public class ExpressionFieldConfiguration extends SourceViewerConfiguration {\r
50 \r
51         private final long WAIT_BEFORE_STATUS_CHANGE = 100;\r
52         \r
53         Table allowedVariables;\r
54         boolean allowFunctions;\r
55         boolean assistSessionActive; \r
56         CompletionProcessor completionProcessor;\r
57         ResourceManager resourceManager;\r
58 \r
59         \r
60         public ExpressionFieldConfiguration(ResourceManager resourceManager, Table allowedVariables, boolean allowFunctions) {\r
61                 super();\r
62                 this.resourceManager = resourceManager;\r
63                 this.allowedVariables = allowedVariables;\r
64                 this.allowFunctions = allowFunctions;\r
65                 this.assistSessionActive = false;\r
66                 this.completionProcessor = null;\r
67         }\r
68         \r
69         public boolean isAssistSessionActive() {\r
70                 return assistSessionActive;\r
71         }\r
72         \r
73         @Override\r
74     public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {\r
75                 return new String[] {\r
76                                 IDocument.DEFAULT_CONTENT_TYPE\r
77                 };\r
78         }\r
79 \r
80         @Override\r
81     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {\r
82                 PresentationReconciler reconciler = new PresentationReconciler();\r
83 \r
84                 DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getTokenScanner());\r
85 \r
86                 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
87                 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
88 \r
89                 return reconciler;\r
90         }\r
91 \r
92 \r
93         ITokenScanner getTokenScanner() {\r
94                 RuleBasedScanner scanner = new RuleBasedScanner();\r
95 \r
96                 final Token reserved = new Token(\r
97                                 new TextAttribute(\r
98                                         resourceManager.createColor(new RGB(127, 0, 85)),\r
99                                                 null,\r
100                                                 SWT.BOLD\r
101                                 ));\r
102             final Token defaultToken = new Token(new TextAttribute(resourceManager.createColor(new RGB(0, 0, 0))));\r
103 \r
104                  final Token comment = new Token(new TextAttribute(resourceManager.createColor(new RGB(63, 127, 95))));\r
105 \r
106 \r
107                 WordRule reservedWord = new WordRule(new IWordDetector() {   \r
108                         @Override\r
109                         public boolean isWordStart(char c) {\r
110                                 return Character.isLetter(c);\r
111                         }\r
112 \r
113                         @Override\r
114                         public boolean isWordPart(char c) {\r
115                                 return Character.isLetter(c);\r
116                         }\r
117                 }, defaultToken);\r
118                 \r
119 \r
120                 for(String s : VariableNameUtils.keywords) {\r
121                         reservedWord.addWord(s, reserved);\r
122                 }\r
123 \r
124                 IRule[] rules = new IRule[] {\r
125                                 reservedWord,\r
126                                 new MultiLineRule("/*", "*/", comment),\r
127                                 new MultiLineRule("\"", "\"", comment)\r
128                 };\r
129                 scanner.setRules(rules);\r
130 \r
131                 return scanner;  \r
132         }\r
133 \r
134         @Override\r
135         public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {\r
136                 return new DefaultTextHover(sourceViewer);\r
137         }\r
138 \r
139         @Override\r
140         public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {\r
141                 return new DefaultAnnotationHover();\r
142         }\r
143 \r
144         @Override\r
145         public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {\r
146                 ContentAssistant assistant = new ContentAssistant();\r
147                 completionProcessor = new CompletionProcessor(allowedVariables, allowFunctions);\r
148                 assistant.setContentAssistProcessor(completionProcessor, IDocument.DEFAULT_CONTENT_TYPE);\r
149                 assistant.enableAutoActivation(true);\r
150                 assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));\r
151                 assistant.enableAutoInsert(true);\r
152                 assistant.setAutoActivationDelay(0);\r
153                 assistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);\r
154                 assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);\r
155                 assistant.addCompletionListener(new CompletionListener());\r
156                 return assistant;\r
157         }\r
158         \r
159     @Override\r
160     public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {\r
161         return new IInformationControlCreator() {\r
162             @Override\r
163             public IInformationControl createInformationControl(Shell parent) {\r
164                 return new DefaultInformationControl(parent,new HTMLTextPresenter(false));\r
165             }\r
166         };\r
167     }\r
168         \r
169         private class CompletionListener implements ICompletionListener {\r
170 \r
171                 @Override\r
172                 public void assistSessionStarted(ContentAssistEvent event) {\r
173                         if (event.processor.equals(completionProcessor)) {\r
174                                 assistSessionActive = true;\r
175                         }\r
176                 }\r
177 \r
178                 @Override\r
179                 public void assistSessionEnded(ContentAssistEvent event) {\r
180                         if (event.processor.equals(completionProcessor)) {\r
181                                 Thread waitBeforeStateChange = new Thread() {\r
182                                         @Override\r
183                     public void run() {\r
184                                                 try {\r
185                                                         sleep(WAIT_BEFORE_STATUS_CHANGE);\r
186                                                         assistSessionActive = false;\r
187                                                 } catch (InterruptedException e) {\r
188                                                         assistSessionActive = false;\r
189                                                 }\r
190                                         }\r
191                                 };\r
192                                 waitBeforeStateChange.start();\r
193                         }\r
194                 }\r
195 \r
196                 @Override\r
197                 public void selectionChanged(ICompletionProposal proposal,\r
198                                 boolean smartToggle) {\r
199                 }\r
200                 \r
201         }\r
202         \r
203 }\r