From d7edfffac0876116707c3bb4c96b6269d430fb7a Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 12 Feb 2010 08:08:48 +0000 Subject: [PATCH] Syntax error highlights the erroneus syntax. Focus fix for expression view with multiple expression fields. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@14033 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/equation/EquationView.java | 4 +- .../equation/expressions/ExpressionField.java | 73 +++++++++++-------- .../WithLookupExpressionViewFactor.java | 17 +++++ 3 files changed, 62 insertions(+), 32 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java index 77c71bfe..b924cb35 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java @@ -301,6 +301,7 @@ public class EquationView extends ViewPart implements ISelectionListener { @Override public void run() { expressionComposite.replaceSelection(var); + validateExpressionFields(); } }); } @@ -381,8 +382,7 @@ public class EquationView extends ViewPart implements ISelectionListener { for(String t : cr.keySet()) variables.add(t); } catch (ParseException e1) { - ef.setSyntaxError(); - return; + ef.setSyntaxError(e1.currentToken); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionField.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionField.java index 4c615fca..3c26c4f5 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionField.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionField.java @@ -3,6 +3,7 @@ package org.simantics.sysdyn.ui.equation.expressions; import java.util.List; import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.PaintManager; @@ -22,66 +23,67 @@ import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess; import org.eclipse.swt.graphics.Point; +import org.simantics.sysdyn.expressionParser.Token; public class ExpressionField extends Composite { - + protected SourceViewer _sourceViewer; protected IDocument _document; protected AnnotationModel _annotationModel; - + ColorManager cManager = new ColorManager(); - + IAnnotationAccess annotationAccess = new DefaultMarkerAnnotationAccess(); public ExpressionField(Composite parent, int style) { super(parent, style); - + GridLayoutFactory.fillDefaults().applyTo(this); int styles = SWT.V_SCROLL - | SWT.MULTI - | SWT.FULL_SELECTION; - + | SWT.MULTI + | SWT.FULL_SELECTION; + _document = new Document(); _document.set(""); - + _annotationModel = new AnnotationModel(); _annotationModel.connect(_document); - + _sourceViewer = new SourceViewer(this, - + null, null, true, styles); _sourceViewer.configure(new ExpressionFieldConfiguration(cManager)); - + AnnotationPainter painter = new AnnotationPainter(_sourceViewer, annotationAccess); _sourceViewer.addPainter(painter); - + painter.addAnnotationType("MissingLink"); painter.setAnnotationTypeColor("MissingLink", new Color(this.getDisplay(), 255,0,0)); - + _sourceViewer.setDocument(_document, _annotationModel); - + _sourceViewer.getControl().setLayoutData(new GridData(SWT.FILL, - SWT.FILL, - true, - true)); + SWT.FILL, + true, + true)); PaintManager paintManager = new PaintManager(_sourceViewer); MatchingCharacterPainter matchingCharacterPainter = new MatchingCharacterPainter(_sourceViewer, new DefaultCharacterPairMatcher( new char[] {'(', ')', '{', '}', '[', ']'} )); matchingCharacterPainter.setColor(new Color(Display.getCurrent(), new RGB(160, 160, 160))); paintManager.addPainter(matchingCharacterPainter); - + } - + public SourceViewer getSourceViewer() { return this._sourceViewer; } - + public void setMissingLinkAnnotations(List positions){ for(Position p : positions) { Annotation annotation = new Annotation(false); @@ -90,39 +92,50 @@ public class ExpressionField extends Composite { _annotationModel.addAnnotation(annotation, p); } } - - public void setSyntaxError(){ - + + public void setSyntaxError(Token token){ + int start = 0; + int offset = this._document.getLength(); + + if(this._document.getLength() > 0) { + try { + start = this._document.getLineOffset(token.beginLine - 1) + token.beginColumn - 1; + offset = this._document.getLineOffset(token.endLine - 1) + token.endColumn - start; + } catch (BadLocationException e) { + e.printStackTrace(); + } + } + Annotation annotation = new Annotation(false); annotation.setType("MissingLink"); annotation.setText("Syntax error"); - Position p = new Position(0, _document.getLength()); + Position p = new Position(start, offset); _annotationModel.addAnnotation(annotation, p); } - + public void resetAnnotations() { _annotationModel.removeAllAnnotations(); } public void setExpression(String expression) { _document.set(expression); } - + public String getExpression() { return this._document.get(); } - + public Point getSelection() { return _sourceViewer.getSelectedRange(); } - + public void setSelection(int selection) { this._sourceViewer.setSelectedRange(selection, 0); } - + public IDocument getDocument() { return _document; } - + public void focus() { this._sourceViewer.getTextWidget().forceFocus(); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/WithLookupExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/WithLookupExpressionViewFactor.java index 94c5d1aa..d40afcd2 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/WithLookupExpressionViewFactor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/WithLookupExpressionViewFactor.java @@ -9,6 +9,8 @@ import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.swt.SWT; +import org.eclipse.swt.events.FocusAdapter; +import org.eclipse.swt.events.FocusEvent; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; @@ -49,6 +51,14 @@ public class WithLookupExpressionViewFactor implements IExpressionViewFactor { expression.setExpression(equation); GridDataFactory.fillDefaults().grab(true, true).applyTo(expression); + expression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() { + + @Override + public void focusLost(FocusEvent e) { + lastSelectedText = expression; + } + }); + lookupLabel = new Label(parent, SWT.NONE); lookupLabel.setFont(FONT); lookupLabel.setText("Lookup\ntable"); @@ -59,6 +69,13 @@ public class WithLookupExpressionViewFactor implements IExpressionViewFactor { lookup.setExpression(lookupTable); GridDataFactory.fillDefaults().grab(true, true).applyTo(lookup); + lookup.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() { + + @Override + public void focusLost(FocusEvent e) { + lastSelectedText = lookup; + } + }); } @Override -- 2.47.1