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
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 String oldExpression;
\r
50 ColorManager cManager = new ColorManager();
\r
52 IAnnotationAccess annotationAccess = new DefaultMarkerAnnotationAccess();
\r
54 public ExpressionField(Composite parent, int style) {
\r
55 super(parent, style);
\r
57 GridLayoutFactory.fillDefaults().applyTo(this);
\r
59 int styles = SWT.V_SCROLL
\r
61 | SWT.FULL_SELECTION
\r
64 _document = new Document();
\r
67 _annotationModel = new AnnotationModel();
\r
68 _annotationModel.connect(_document);
\r
70 _sourceViewer = new SourceViewer(this,
\r
76 _sourceViewer.configure(new ExpressionFieldConfiguration(cManager));
\r
78 AnnotationPainter painter = new AnnotationPainter(_sourceViewer, annotationAccess);
\r
79 _sourceViewer.addPainter(painter);
\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
86 _sourceViewer.setDocument(_document, _annotationModel);
\r
88 GridDataFactory.fillDefaults().grab(true, true).applyTo(_sourceViewer.getControl());
\r
89 // _sourceViewer.getControl().setLayoutData(new GridData(SWT.FILL,
\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
101 _sourceViewer.getTextWidget().addKeyListener(new KeyListener() {
\r
104 public void keyReleased(KeyEvent e) {
\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
118 public SourceViewer getSourceViewer() {
\r
119 return this._sourceViewer;
\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
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
140 public void setSyntaxError(Token token){
\r
142 int offset = this._document.getLength();
\r
143 if(token.image != null && this._document.getLength() > 0) {
\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
151 setSyntaxError(start, offset, "MissingLink", "Syntax error");
\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
162 public void resetAnnotations() {
\r
163 _annotationModel.removeAllAnnotations();
\r
165 public void setExpression(String expression) {
\r
166 _document.set(expression);
\r
167 this.oldExpression = expression;
\r
170 public String getExpression() {
\r
171 return this._document.get();
\r
174 public Point getSelection() {
\r
175 return _sourceViewer.getSelectedRange();
\r
178 public void setSelection(int selection) {
\r
179 this._sourceViewer.setSelectedRange(selection, 0);
\r
182 public IDocument getDocument() {
\r
186 public void focus() {
\r
187 this._sourceViewer.getTextWidget().forceFocus();
\r