1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.properties.widgets.expressions;
\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
48 @SuppressWarnings("restriction")
\r
49 public class ExpressionFieldConfiguration extends SourceViewerConfiguration {
\r
51 private final long WAIT_BEFORE_STATUS_CHANGE = 100;
\r
53 Table allowedVariables;
\r
54 boolean allowFunctions;
\r
55 boolean assistSessionActive;
\r
56 CompletionProcessor completionProcessor;
\r
57 ResourceManager resourceManager;
\r
60 public ExpressionFieldConfiguration(ResourceManager resourceManager, Table allowedVariables, boolean allowFunctions) {
\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
69 public boolean isAssistSessionActive() {
\r
70 return assistSessionActive;
\r
74 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
\r
75 return new String[] {
\r
76 IDocument.DEFAULT_CONTENT_TYPE
\r
81 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
\r
82 PresentationReconciler reconciler = new PresentationReconciler();
\r
84 DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getTokenScanner());
\r
86 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
\r
87 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
\r
93 ITokenScanner getTokenScanner() {
\r
94 RuleBasedScanner scanner = new RuleBasedScanner();
\r
96 final Token reserved = new Token(
\r
98 resourceManager.createColor(new RGB(127, 0, 85)),
\r
102 final Token defaultToken = new Token(new TextAttribute(resourceManager.createColor(new RGB(0, 0, 0))));
\r
104 final Token comment = new Token(new TextAttribute(resourceManager.createColor(new RGB(63, 127, 95))));
\r
107 WordRule reservedWord = new WordRule(new IWordDetector() {
\r
109 public boolean isWordStart(char c) {
\r
110 return Character.isLetter(c);
\r
114 public boolean isWordPart(char c) {
\r
115 return Character.isLetter(c);
\r
120 for(String s : VariableNameUtils.keywords) {
\r
121 reservedWord.addWord(s, reserved);
\r
124 IRule[] rules = new IRule[] {
\r
126 new MultiLineRule("/*", "*/", comment),
\r
127 new MultiLineRule("\"", "\"", comment)
\r
129 scanner.setRules(rules);
\r
135 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
\r
136 return new DefaultTextHover(sourceViewer);
\r
140 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
\r
141 return new DefaultAnnotationHover();
\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
160 public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
\r
161 return new IInformationControlCreator() {
\r
163 public IInformationControl createInformationControl(Shell parent) {
\r
164 return new DefaultInformationControl(parent,new HTMLTextPresenter(false));
\r
169 private class CompletionListener implements ICompletionListener {
\r
172 public void assistSessionStarted(ContentAssistEvent event) {
\r
173 if (event.processor.equals(completionProcessor)) {
\r
174 assistSessionActive = true;
\r
179 public void assistSessionEnded(ContentAssistEvent event) {
\r
180 if (event.processor.equals(completionProcessor)) {
\r
181 Thread waitBeforeStateChange = new Thread() {
\r
183 public void run() {
\r
185 sleep(WAIT_BEFORE_STATUS_CHANGE);
\r
186 assistSessionActive = false;
\r
187 } catch (InterruptedException e) {
\r
188 assistSessionActive = false;
\r
192 waitBeforeStateChange.start();
\r
197 public void selectionChanged(ICompletionProposal proposal,
\r
198 boolean smartToggle) {
\r