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