]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
f2a4e7a9aa53d45807384fcb2bbc2ff631908151
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in 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 java.util.List;\r
15 \r
16 import org.eclipse.jface.layout.GridDataFactory;\r
17 import org.eclipse.jface.layout.GridLayoutFactory;\r
18 import org.eclipse.jface.text.BadLocationException;\r
19 import org.eclipse.jface.text.Document;\r
20 import org.eclipse.jface.text.IDocument;\r
21 import org.eclipse.jface.text.PaintManager;\r
22 import org.eclipse.jface.text.Position;\r
23 import org.eclipse.jface.text.source.Annotation;\r
24 import org.eclipse.jface.text.source.AnnotationModel;\r
25 import org.eclipse.jface.text.source.AnnotationPainter;\r
26 import org.eclipse.jface.text.source.DefaultCharacterPairMatcher;\r
27 import org.eclipse.jface.text.source.IAnnotationAccess;\r
28 import org.eclipse.jface.text.source.MatchingCharacterPainter;\r
29 import org.eclipse.jface.text.source.SourceViewer;\r
30 import org.eclipse.swt.SWT;\r
31 import org.eclipse.swt.custom.StyledText;\r
32 import org.eclipse.swt.events.KeyEvent;\r
33 import org.eclipse.swt.events.KeyListener;\r
34 import org.eclipse.swt.graphics.Color;\r
35 import org.eclipse.swt.graphics.RGB;\r
36 import org.eclipse.swt.widgets.Composite;\r
37 import org.eclipse.swt.widgets.Display;\r
38 import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess;\r
39 import org.eclipse.swt.graphics.Point;\r
40 import org.simantics.sysdyn.expressionParser.Token;\r
41 \r
42 public class ExpressionField extends Composite {\r
43 \r
44     protected SourceViewer                  _sourceViewer;\r
45     protected IDocument                     _document;\r
46     protected AnnotationModel               _annotationModel;\r
47     \r
48     String oldExpression;\r
49 \r
50     ColorManager cManager = new ColorManager();\r
51 \r
52     IAnnotationAccess annotationAccess = new DefaultMarkerAnnotationAccess();\r
53 \r
54     public ExpressionField(Composite parent, int style) {\r
55         super(parent, style);\r
56         \r
57         GridLayoutFactory.fillDefaults().applyTo(this);\r
58         \r
59         int styles = SWT.V_SCROLL\r
60         | SWT.MULTI\r
61         | SWT.FULL_SELECTION\r
62         | SWT.WRAP;\r
63 \r
64         _document = new Document();\r
65         _document.set("");\r
66 \r
67         _annotationModel = new AnnotationModel();\r
68         _annotationModel.connect(_document);\r
69 \r
70         _sourceViewer = new SourceViewer(this,\r
71 \r
72                 null,\r
73                 null,\r
74                 true,\r
75                 styles);\r
76         _sourceViewer.configure(new ExpressionFieldConfiguration(cManager));\r
77 \r
78         AnnotationPainter painter = new AnnotationPainter(_sourceViewer, annotationAccess);\r
79         _sourceViewer.addPainter(painter);\r
80 \r
81         painter.addAnnotationType("MissingLink");\r
82         painter.setAnnotationTypeColor("MissingLink", new Color(this.getDisplay(), 255,0,0));\r
83         painter.addAnnotationType("NoSuchVariable");\r
84         painter.setAnnotationTypeColor("NoSuchVariable", new Color(this.getDisplay(), 255,0,0));\r
85         \r
86         _sourceViewer.setDocument(_document, _annotationModel);\r
87 \r
88         GridDataFactory.fillDefaults().grab(true, true).applyTo(_sourceViewer.getControl());\r
89 //        _sourceViewer.getControl().setLayoutData(new GridData(SWT.FILL,\r
90 //                SWT.FILL,\r
91 //                true,\r
92 //                true)); \r
93 \r
94         PaintManager paintManager = new PaintManager(_sourceViewer);\r
95         MatchingCharacterPainter matchingCharacterPainter = new MatchingCharacterPainter(_sourceViewer,\r
96                 new DefaultCharacterPairMatcher( new char[] {'(', ')', '{', '}', '[', ']'} ));\r
97         matchingCharacterPainter.setColor(new Color(Display.getCurrent(), new RGB(160, 160, 160)));\r
98         paintManager.addPainter(matchingCharacterPainter);\r
99         \r
100         \r
101         _sourceViewer.getTextWidget().addKeyListener(new KeyListener() {\r
102 \r
103             @Override\r
104             public void keyReleased(KeyEvent e) {\r
105             }\r
106 \r
107             @Override\r
108             public void keyPressed(KeyEvent e) {\r
109                 if(e.keyCode == SWT.ESC && getExpression() != null) {\r
110                     ((StyledText)e.widget).setText(oldExpression);\r
111                     ((StyledText)e.widget).setSelection(getExpression().length());\r
112                 }   \r
113             }\r
114         });\r
115        \r
116     }\r
117 \r
118     public SourceViewer getSourceViewer() {\r
119         return this._sourceViewer;\r
120     }\r
121 \r
122     public void setMissingLinkAnnotations(List<Position> positions){\r
123         for(Position p : positions) {\r
124             Annotation annotation = new Annotation(false);\r
125             annotation.setType("MissingLink");\r
126             annotation.setText("No link to this variable");\r
127             _annotationModel.addAnnotation(annotation, p);        \r
128         }\r
129     }\r
130     \r
131     public void setNoSuchVariableAnnotations(List<Position> positions){\r
132         for(Position p : positions) {\r
133             Annotation annotation = new Annotation(false);\r
134             annotation.setType("NoSuchVariable");\r
135             annotation.setText("No such variable in model");\r
136             _annotationModel.addAnnotation(annotation, p);        \r
137         }\r
138     }\r
139 \r
140     public void setSyntaxError(Token token){\r
141         int start = 0;\r
142         int offset = this._document.getLength();\r
143         if(token.image != null && this._document.getLength() > 0) {\r
144             try {\r
145                 start = this._document.getLineOffset(token.beginLine - 1) + token.beginColumn - 1;\r
146                 offset = this._document.getLineOffset(token.endLine - 1) + token.endColumn - start;\r
147             } catch (BadLocationException e) {\r
148                 e.printStackTrace();\r
149             }\r
150         }\r
151         setSyntaxError(start, offset, "MissingLink", "Syntax error");\r
152     }\r
153     \r
154     public void setSyntaxError(int start, int offset, String type, String text) {\r
155         Annotation annotation = new Annotation(false);\r
156         annotation.setType(type);\r
157         annotation.setText(text);\r
158         Position p = new Position(start, offset);\r
159         _annotationModel.addAnnotation(annotation, p);      \r
160     }\r
161 \r
162     public void resetAnnotations() {\r
163         _annotationModel.removeAllAnnotations();\r
164     }\r
165     public void setExpression(String expression) {\r
166         _document.set(expression);\r
167         this.oldExpression = expression;\r
168     }\r
169 \r
170     public String getExpression() {\r
171         return this._document.get();\r
172     }\r
173 \r
174     public Point getSelection() {\r
175         return _sourceViewer.getSelectedRange();\r
176     }\r
177 \r
178     public void setSelection(int selection) {\r
179         this._sourceViewer.setSelectedRange(selection, 0);\r
180     }\r
181 \r
182     public IDocument getDocument() {\r
183         return _document;\r
184     }\r
185 \r
186     public void focus() {\r
187         this._sourceViewer.getTextWidget().forceFocus();\r
188     }\r
189 \r
190 }\r