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 java.util.List;
\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
42 public class ExpressionField extends Composite {
\r
44 protected SourceViewer _sourceViewer;
\r
45 protected IDocument _document;
\r
46 protected AnnotationModel _annotationModel;
\r
48 public static final String MISSING_LINK = "MissingLink";
\r
49 public static final String NO_SUCH_VARIABLE = "NoSuchVariable";
\r
50 public static final String SYNTAX_ERROR = "SyntaxError";
\r
52 String oldExpression;
\r
54 ColorManager cManager = new ColorManager();
\r
56 IAnnotationAccess annotationAccess = new DefaultMarkerAnnotationAccess();
\r
58 public ExpressionField(Composite parent, int style) {
\r
59 super(parent, style);
\r
61 GridLayoutFactory.fillDefaults().applyTo(this);
\r
63 int styles = SWT.V_SCROLL
\r
65 | SWT.FULL_SELECTION
\r
68 _document = new Document();
\r
71 _annotationModel = new AnnotationModel();
\r
72 _annotationModel.connect(_document);
\r
74 _sourceViewer = new SourceViewer(this,
\r
80 _sourceViewer.configure(new ExpressionFieldConfiguration(cManager));
\r
82 AnnotationPainter painter = new AnnotationPainter(_sourceViewer, annotationAccess);
\r
83 _sourceViewer.addPainter(painter);
\r
85 painter.addAnnotationType(MISSING_LINK);
\r
86 painter.setAnnotationTypeColor(MISSING_LINK, new Color(this.getDisplay(), 255,215,0));
\r
87 painter.addAnnotationType(NO_SUCH_VARIABLE);
\r
88 painter.setAnnotationTypeColor(NO_SUCH_VARIABLE, new Color(this.getDisplay(), 255,0,0));
\r
89 painter.addAnnotationType(SYNTAX_ERROR);
\r
90 painter.setAnnotationTypeColor(SYNTAX_ERROR, new Color(this.getDisplay(), 255,0,0));
\r
92 _sourceViewer.setDocument(_document, _annotationModel);
\r
94 GridDataFactory.fillDefaults().grab(true, true).applyTo(_sourceViewer.getControl());
\r
95 // _sourceViewer.getControl().setLayoutData(new GridData(SWT.FILL,
\r
100 PaintManager paintManager = new PaintManager(_sourceViewer);
\r
101 MatchingCharacterPainter matchingCharacterPainter = new MatchingCharacterPainter(_sourceViewer,
\r
102 new DefaultCharacterPairMatcher( new char[] {'(', ')', '{', '}', '[', ']'} ));
\r
103 matchingCharacterPainter.setColor(new Color(Display.getCurrent(), new RGB(160, 160, 160)));
\r
104 paintManager.addPainter(matchingCharacterPainter);
\r
107 _sourceViewer.getTextWidget().addKeyListener(new KeyListener() {
\r
110 public void keyReleased(KeyEvent e) {
\r
114 public void keyPressed(KeyEvent e) {
\r
115 if(e.keyCode == SWT.ESC && getExpression() != null) {
\r
116 ((StyledText)e.widget).setText(oldExpression);
\r
117 ((StyledText)e.widget).setSelection(getExpression().length());
\r
124 public SourceViewer getSourceViewer() {
\r
125 return this._sourceViewer;
\r
128 public void setMissingLinkAnnotations(List<Position> positions){
\r
129 for(Position p : positions) {
\r
130 Annotation annotation = new Annotation(false);
\r
131 annotation.setType(MISSING_LINK);
\r
132 annotation.setText("No link to this variable");
\r
133 _annotationModel.addAnnotation(annotation, p);
\r
137 public void setNoSuchVariableAnnotations(List<Position> positions){
\r
138 for(Position p : positions) {
\r
139 Annotation annotation = new Annotation(false);
\r
140 annotation.setType(NO_SUCH_VARIABLE);
\r
141 annotation.setText("No such variable in model");
\r
142 _annotationModel.addAnnotation(annotation, p);
\r
146 public void setSyntaxError(Token token, String message){
\r
147 setSyntaxError(token.image, message, token.beginLine, token.beginColumn, token.endLine, token.endColumn);
\r
150 public void setSyntaxError(org.simantics.sysdyn.tableParser.Token token, String message){
\r
151 setSyntaxError(token.image, message, token.beginLine, token.beginColumn, token.endLine, token.endColumn);
\r
154 public void setSyntaxError(String image, String message, int beginLine, int beginColumn, int endLine, int endColumn) {
\r
156 int offset = this._document.getLength();
\r
157 if(image != null && this._document.getLength() > 0) {
\r
159 start = this._document.getLineOffset(beginLine - 1) + beginColumn - 1;
\r
160 offset = this._document.getLineOffset(endLine - 1) + endColumn - start;
\r
161 } catch (BadLocationException e) {
\r
162 e.printStackTrace();
\r
165 setSyntaxError(start, offset, SYNTAX_ERROR, message == null ? "Syntax Error" : message);
\r
168 public void setSyntaxError(int start, int offset, String type, String text) {
\r
169 Annotation annotation = new Annotation(false);
\r
170 annotation.setType(type);
\r
171 annotation.setText(text);
\r
172 Position p = new Position(start, offset);
\r
173 _annotationModel.addAnnotation(annotation, p);
\r
176 public void resetAnnotations() {
\r
177 _annotationModel.removeAllAnnotations();
\r
179 public void setExpression(String expression) {
\r
180 _document.set(expression);
\r
181 this.oldExpression = expression;
\r
184 public String getExpression() {
\r
185 return this._document.get();
\r
188 public Point getSelection() {
\r
189 return _sourceViewer.getSelectedRange();
\r
192 public void setSelection(int selection) {
\r
193 this._sourceViewer.setSelectedRange(selection, 0);
\r
196 public IDocument getDocument() {
\r
200 public void focus() {
\r
201 this._sourceViewer.getTextWidget().forceFocus();
\r