]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
d2f646baabdb9c66369a048706dee85f7f80285e
[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.modelica.ModelicaSourceViewerConfiguration;\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         private final ExpressionWidgetInput input;\r
60         \r
61         public ExpressionFieldConfiguration(ResourceManager resourceManager, Table allowedVariables, boolean allowFunctions, ExpressionWidgetInput input) {\r
62                 super();\r
63                 this.resourceManager = resourceManager;\r
64                 this.allowedVariables = allowedVariables;\r
65                 this.allowFunctions = allowFunctions;\r
66                 this.assistSessionActive = false;\r
67                 this.completionProcessor = null;\r
68                 this.input = input;\r
69         }\r
70         \r
71         public boolean isAssistSessionActive() {\r
72                 return assistSessionActive;\r
73         }\r
74         \r
75         @Override\r
76     public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {\r
77                 return new String[] {\r
78                                 IDocument.DEFAULT_CONTENT_TYPE\r
79                 };\r
80         }\r
81 \r
82         @Override\r
83     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {\r
84                 PresentationReconciler reconciler = new PresentationReconciler();\r
85 \r
86                 DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getTokenScanner());\r
87 \r
88                 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
89                 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
90 \r
91                 return reconciler;\r
92         }\r
93 \r
94 \r
95         ITokenScanner getTokenScanner() {\r
96                 RuleBasedScanner scanner = new RuleBasedScanner();\r
97 \r
98                 final Token reserved = new Token(\r
99                                 new TextAttribute(\r
100                                         resourceManager.createColor(new RGB(127, 0, 85)),\r
101                                                 null,\r
102                                                 SWT.BOLD\r
103                                 ));\r
104             final Token defaultToken = new Token(new TextAttribute(resourceManager.createColor(new RGB(0, 0, 0))));\r
105 \r
106                  final Token comment = new Token(new TextAttribute(resourceManager.createColor(new RGB(63, 127, 95))));\r
107 \r
108 \r
109                 WordRule reservedWord = new WordRule(new IWordDetector() {   \r
110                         @Override\r
111                         public boolean isWordStart(char c) {\r
112                                 return Character.isLetter(c);\r
113                         }\r
114 \r
115                         @Override\r
116                         public boolean isWordPart(char c) {\r
117                                 return Character.isLetter(c);\r
118                         }\r
119                 }, defaultToken);\r
120                 \r
121 \r
122                 for(String s : ModelicaSourceViewerConfiguration.keywords) {\r
123                         reservedWord.addWord(s, reserved);\r
124                 }\r
125 \r
126                 IRule[] rules = new IRule[] {\r
127                                 reservedWord,\r
128                                 new MultiLineRule("/*", "*/", comment),\r
129                                 new MultiLineRule("\"", "\"", comment)\r
130                 };\r
131                 scanner.setRules(rules);\r
132 \r
133                 return scanner;  \r
134         }\r
135 \r
136         @Override\r
137         public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {\r
138                 return new DefaultTextHover(sourceViewer);\r
139         }\r
140 \r
141         @Override\r
142         public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {\r
143                 return new DefaultAnnotationHover();\r
144         }\r
145 \r
146         @Override\r
147         public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {\r
148                 ContentAssistant assistant = new ContentAssistant();\r
149                 completionProcessor = new CompletionProcessor(allowedVariables, allowFunctions, input);\r
150                 assistant.setContentAssistProcessor(completionProcessor, IDocument.DEFAULT_CONTENT_TYPE);\r
151                 assistant.enableAutoActivation(true);\r
152                 assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));\r
153                 assistant.enableAutoInsert(true);\r
154                 assistant.setAutoActivationDelay(0);\r
155                 assistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);\r
156                 assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);\r
157                 assistant.addCompletionListener(new CompletionListener());\r
158                 return assistant;\r
159         }\r
160         \r
161     @Override\r
162     public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {\r
163         return new IInformationControlCreator() {\r
164             @Override\r
165             public IInformationControl createInformationControl(Shell parent) {\r
166                 return new DefaultInformationControl(parent,new HTMLTextPresenter(false));\r
167             }\r
168         };\r
169     }\r
170         \r
171         private class CompletionListener implements ICompletionListener {\r
172 \r
173                 @Override\r
174                 public void assistSessionStarted(ContentAssistEvent event) {\r
175                         if (event.processor.equals(completionProcessor)) {\r
176                                 assistSessionActive = true;\r
177                         }\r
178                 }\r
179 \r
180                 @Override\r
181                 public void assistSessionEnded(ContentAssistEvent event) {\r
182                         if (event.processor.equals(completionProcessor)) {\r
183                                 Thread waitBeforeStateChange = new Thread() {\r
184                                         @Override\r
185                     public void run() {\r
186                                                 try {\r
187                                                         sleep(WAIT_BEFORE_STATUS_CHANGE);\r
188                                                         assistSessionActive = false;\r
189                                                 } catch (InterruptedException e) {\r
190                                                         assistSessionActive = false;\r
191                                                 }\r
192                                         }\r
193                                 };\r
194                                 waitBeforeStateChange.start();\r
195                         }\r
196                 }\r
197 \r
198                 @Override\r
199                 public void selectionChanged(ICompletionProposal proposal,\r
200                                 boolean smartToggle) {\r
201                 }\r
202                 \r
203         }\r
204         \r
205 }\r