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