From dfba6909ac98bc7142202b2d8992ec2db749f0b0 Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 29 Jan 2010 12:32:46 +0000 Subject: [PATCH] SourceViewer not working git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13767 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn.ui/plugin.xml | 7 +- .../sysdyn/ui/equation/EquationView.java | 114 +++++++++++++- .../ui/equation/expressions/ColorManager.java | 32 ++++ .../equation/expressions/ExpressionField.java | 144 ++++++++++++++++++ .../ExpressionFieldConfiguration.java | 122 +++++++++++++++ .../expressionParser/ExpressionParser.jj | 21 ++- .../TestExpressionParser.java | 5 +- 7 files changed, 431 insertions(+), 14 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ColorManager.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionField.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldConfiguration.java diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index f2be79e6..7a7185bf 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -97,5 +97,10 @@ commandId="org.simantics.sysdyn.ui.trend.view.pin"> - +- + + +- + + 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 f741b600..9ece38b0 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 @@ -2,7 +2,12 @@ package org.simantics.sysdyn.ui.equation; import java.io.StringReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Set; import org.eclipse.jface.layout.GridDataFactory; @@ -10,6 +15,8 @@ import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyleRange; +import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.events.FocusEvent; import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.events.KeyEvent; @@ -23,9 +30,7 @@ import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.Point; -import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.widgets.Text; @@ -41,6 +46,8 @@ import org.simantics.db.procedure.Listener; import org.simantics.db.request.Read; import org.simantics.sysdyn.expressionParser.ExpressionParser; import org.simantics.sysdyn.expressionParser.ParseException; +import org.simantics.sysdyn.expressionParser.Token; +import org.simantics.sysdyn.ui.equation.expressions.ExpressionField; import org.simantics.ui.SimanticsUI; public class EquationView extends ViewPart implements ISelectionListener { @@ -64,6 +71,20 @@ public class EquationView extends ViewPart implements ISelectionListener { // Dummies Composite unitsAndRange; Composite emptyComposite; + + ExpressionField ef; + + + StyledText styledText; + + public static final String ANNO_TYPE = "org.eclipse.ui.workbench.texteditor.error"; + public static final String ANNO_KEY_HIGHLIGHT = "annotateElemHighlight"; + public static final String ANNO_KEY_OVERVIEW = "annotateElemOverviewRuler"; + public static final String ANNO_KEY_VERTICAL = "annotateElemVertialRuler"; + public static final String ANNO_KEY_TEXT = "annotateElemText"; + public static final String ANNO_KEY_COLOR = "annotateElemColor"; + + @Override public void createPartControl(Composite parent) { @@ -88,7 +109,7 @@ public class EquationView extends ViewPart implements ISelectionListener { if(unitSelector != null) { unitSelector.save(); } - + if(nameComposite != null) { nameComposite.save(variable); } @@ -130,6 +151,9 @@ public class EquationView extends ViewPart implements ISelectionListener { emptyComposite = new Composite(parent, SWT.NONE); GridDataFactory.fillDefaults().span(1,4).grab(true, true).applyTo(emptyComposite); GridLayoutFactory.fillDefaults().applyTo(emptyComposite); + + ef = new ExpressionField(emptyComposite, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, true).applyTo(ef); // Below variable name: Equation expressionComposite = new ExpressionComposite(parent, SWT.NONE); @@ -138,7 +162,42 @@ public class EquationView extends ViewPart implements ISelectionListener { // Lower part of the view: Units and Range unitsAndRange = new Composite(parent, SWT.NONE); - GridDataFactory.fillDefaults().span(3, 1).applyTo(unitsAndRange); + GridDataFactory.fillDefaults().span(3, 1).grab(true, true).applyTo(unitsAndRange); + + GridLayoutFactory.fillDefaults().applyTo(unitsAndRange); + + styledText = new StyledText(unitsAndRange, SWT.BORDER | SWT.H_SCROLL + | SWT.V_SCROLL); + + + GridDataFactory.fillDefaults().grab(true, true).applyTo(styledText); + + /* + styledText.addLineStyleListener(new LineStyleListener() { + + public void lineGetStyle(LineStyleEvent event) { + // Create a collection to hold the StyleRanges + java.util.List styles = new java.util.ArrayList(); + // Iterate through the text + for (int i = 0, n = event.lineText.length(); i < n; i++) { + // Check for 'e' + if (event.lineText.charAt(i) == 'e') { + // Found an 'e'; combine all subsequent e's into the same StyleRange + int start = i; + for (; i < n && event.lineText.charAt(i) == 'e'; i++); + + // Create the StyleRange and add it to the collection + styles.add(new StyleRange(event.lineOffset + start, i - start, styledText.getDisplay().getSystemColor(SWT.COLOR_RED), + null)); + } + } + // Set the styles for the line + event.styles = (StyleRange[]) styles.toArray(new StyleRange[0]); + } + }); + */ + + /* RowLayout layout = new RowLayout(); layout.spacing = 3; unitsAndRange.setLayout(layout); @@ -155,6 +214,7 @@ public class EquationView extends ViewPart implements ISelectionListener { text1.setToolTipText("Stop"); text1 = new Text(c, SWT.SINGLE | SWT.BORDER); text1.setToolTipText("Step"); + */ } static class Auxiliary { @@ -330,11 +390,11 @@ public class EquationView extends ViewPart implements ISelectionListener { } }); text.addKeyListener(new KeyListener() { - + @Override public void keyReleased(KeyEvent e) { } - + @Override public void keyPressed(KeyEvent e) { if(e.keyCode == SWT.ESC && expression != null) { @@ -349,14 +409,24 @@ public class EquationView extends ViewPart implements ISelectionListener { private void validateExpressionFields() { ExpressionParser parser = new ExpressionParser(new StringReader("")); Set variables = new HashSet(); + HashMap> references = null; + + if(expressionComposite.getExpressionViewFactor() != null) for(Text text : expressionComposite.getExpressionViewFactor().getExpressionFields()) { + + + styledText.setText(text.getText()); + String textString = text.getText(); parser.ReInit(new StringReader(textString)); try { parser.expr(); - variables.addAll(parser.getReferences()); + System.out.println(parser.getReferences()); + references = parser.getReferences(); + for(String t : references.keySet()) + variables.add(t); text.setBackground(new Color(text.getDisplay(), 255, 255, 255)); } catch (ParseException e1) { text.setBackground(new Color(text.getDisplay(), 255, 230, 230)); @@ -376,11 +446,39 @@ public class EquationView extends ViewPart implements ISelectionListener { } if(!variables.isEmpty()) { + ArrayList ranges = new ArrayList(); String no_connections = "No connections for: "; for(String s : variables) { - no_connections = no_connections + s + " "; + List tlist = references.get(s); + for(Token t : tlist) { + no_connections = no_connections + s + "[" + t.beginLine + "," + + t.beginColumn + "," + t.endLine + "," + t.endColumn + "]"; + int start = styledText.getOffsetAtLine(t.beginLine - 1) + t.beginColumn - 1; + int length = t.endColumn - t.beginColumn + 1; + System.out.println(start + ", " + length); + ranges.add(new StyleRange( + start, + length, + new Color(styledText.getDisplay(), 0, 0, 0), + new Color(styledText.getDisplay(), 255, 255, 255), + SWT.BOLD)); + } } + System.out.println(no_connections); + Collections.sort(ranges, new StyleRangeSorter()); + styledText.setStyleRanges(ranges.toArray(new StyleRange[0])); } } + + class StyleRangeSorter implements Comparator { + public int compare(StyleRange a, StyleRange b) { + Integer ai, bi; + + ai = a.start; + bi = b.start; + + return ai.compareTo(bi); + } + } } \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ColorManager.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ColorManager.java new file mode 100644 index 00000000..9a4e8c18 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ColorManager.java @@ -0,0 +1,32 @@ +package org.simantics.sysdyn.ui.equation.expressions; + +import java.util.HashMap; + +import org.eclipse.jface.text.source.ISharedTextColors; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.widgets.Display; + +public class ColorManager implements ISharedTextColors { + + protected HashMap colorTable = new HashMap(); + + @Override + public void dispose() { + for(Color c : colorTable.values()) + c.dispose(); + colorTable.clear(); + } + + @Override + public Color getColor(RGB rgb) { + Color color = colorTable.get(rgb); + if (color == null) { + color = new Color(Display.getCurrent(), rgb); + colorTable.put(rgb, color); + } + return color; + + + } +} 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 new file mode 100644 index 00000000..3c97db7b --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionField.java @@ -0,0 +1,144 @@ +package org.simantics.sysdyn.ui.equation.expressions; + +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.PaintManager; +import org.eclipse.jface.text.Position; +import org.eclipse.jface.text.source.Annotation; +import org.eclipse.jface.text.source.AnnotationModel; +import org.eclipse.jface.text.source.AnnotationPainter; +import org.eclipse.jface.text.source.CompositeRuler; +import org.eclipse.jface.text.source.DefaultCharacterPairMatcher; +import org.eclipse.jface.text.source.IAnnotationAccess; +import org.eclipse.jface.text.source.IOverviewRuler; +import org.eclipse.jface.text.source.LineNumberRulerColumn; +import org.eclipse.jface.text.source.MatchingCharacterPainter; +import org.eclipse.jface.text.source.OverviewRuler; +import org.eclipse.jface.text.source.SourceViewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.texteditor.AnnotationPreference; +import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess; +import org.eclipse.ui.texteditor.SourceViewerDecorationSupport; + +public class ExpressionField extends Composite { + + public static final String ANNO_TYPE = "com.mycompany.element"; + public static final String ANNO_KEY_HIGHLIGHT = "annotateElemHighlight"; + public static final String ANNO_KEY_OVERVIEW = "annotateElemOverviewRuler"; + public static final String ANNO_KEY_VERTICAL = "annotateElemVertialRuler"; + public static final String ANNO_KEY_TEXT = "annotateElemText"; + public static final String ANNO_KEY_COLOR = "annotateElemColor"; + + protected SourceViewer _sourceViewer; + protected SourceViewerDecorationSupport _sds; + protected IDocument _document; + protected AnnotationModel _annotationModel; + + protected String _docString = "this\nis\na\ntest\ndocument"; + + ColorManager cManager = new ColorManager(); + + IAnnotationAccess annotationAccess = new DefaultMarkerAnnotationAccess(); + + public ExpressionField(Composite parent, int style) { + super(parent, style); + + int VERTICAL_RULER_WIDTH = 12; + + int styles = SWT.V_SCROLL + | SWT.H_SCROLL + | SWT.MULTI + | SWT.BORDER + | SWT.FULL_SELECTION; + + IOverviewRuler overviewRuler = new OverviewRuler(null, VERTICAL_RULER_WIDTH, + cManager); + CompositeRuler ruler = new CompositeRuler(VERTICAL_RULER_WIDTH); + + _document = new Document(); + _document.set(_docString); + + _annotationModel = new AnnotationModel(); + _annotationModel.connect(_document); + + _sourceViewer = new SourceViewer(parent, + ruler, + overviewRuler, + true, + styles); + _sourceViewer.configure(new ExpressionFieldConfiguration(cManager)); + + + // Tämä ei esimerkissä + AnnotationPainter painter = new AnnotationPainter(_sourceViewer, annotationAccess); + _sourceViewer.addPainter(painter); + + _sds = new SourceViewerDecorationSupport(_sourceViewer, + overviewRuler, + annotationAccess, + cManager); + // ^^^^^^^ + + /* Esimerkistä: + _sds = new SourceViewerDecorationSupport(_sourceViewer, + overviewRuler, + null, + cManager); + */ + + AnnotationPreference ap = new AnnotationPreference(); + ap.setColorPreferenceKey(ANNO_KEY_COLOR); + ap.setHighlightPreferenceKey(ANNO_KEY_HIGHLIGHT); + ap.setVerticalRulerPreferenceKey(ANNO_KEY_VERTICAL); + ap.setOverviewRulerPreferenceKey(ANNO_KEY_OVERVIEW); + ap.setTextPreferenceKey(ANNO_KEY_TEXT); + ap.setAnnotationType(ANNO_TYPE); + _sds.setAnnotationPreference(ap); + + _sds.install(PlatformUI.getPreferenceStore()); + + _sourceViewer.setDocument(_document, _annotationModel); + + _sourceViewer.getControl().setLayoutData(new GridData(SWT.FILL, + SWT.FILL, + true, + true)); + + ruler.addDecorator(0, new LineNumberRulerColumn()); + + + Annotation annotation = new Annotation(false); + annotation.setType(ANNO_TYPE); + annotation.setText("moro!"); + Position position = new Position(0, 4); + _annotationModel.addAnnotation(annotation, position); + + + + 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); + + + + annotation = new Annotation(false); + annotation.setType("org.eclipse.ui.workbench.texteditor.error"); + annotation.setText("Tekstiä!"); + position = new Position(0, 4); + _sourceViewer.getAnnotationModel().addAnnotation(annotation, position); + + } + + public SourceViewer getSourceViewer() { + return this._sourceViewer; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldConfiguration.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldConfiguration.java new file mode 100644 index 00000000..6c274f30 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldConfiguration.java @@ -0,0 +1,122 @@ +package org.simantics.sysdyn.ui.equation.expressions; + +import org.eclipse.jface.text.DefaultTextHover; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextHover; +import org.eclipse.jface.text.TextAttribute; +import org.eclipse.jface.text.presentation.IPresentationReconciler; +import org.eclipse.jface.text.presentation.PresentationReconciler; +import org.eclipse.jface.text.rules.DefaultDamagerRepairer; +import org.eclipse.jface.text.rules.IRule; +import org.eclipse.jface.text.rules.ITokenScanner; +import org.eclipse.jface.text.rules.IWordDetector; +import org.eclipse.jface.text.rules.PatternRule; +import org.eclipse.jface.text.rules.RuleBasedScanner; +import org.eclipse.jface.text.rules.Token; +import org.eclipse.jface.text.rules.WordRule; +import org.eclipse.jface.text.source.DefaultAnnotationHover; +import org.eclipse.jface.text.source.IAnnotationHover; +import org.eclipse.jface.text.source.ISourceViewer; +import org.eclipse.jface.text.source.SourceViewerConfiguration; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.RGB; + +public class ExpressionFieldConfiguration extends SourceViewerConfiguration { + + + ColorManager colorManager; + + public ExpressionFieldConfiguration(ColorManager colorManager) { + super(); + this.colorManager = colorManager; + } + + public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) { + return new String[] { + IDocument.DEFAULT_CONTENT_TYPE + }; + } + + /* + @Override + public IAutoEditStrategy[] getAutoEditStrategies( + ISourceViewer sourceViewer, String contentType) { + return new IAutoEditStrategy[] { + new ReplaceTabsBySpaces(), + new IndentLineEditStrategy(), + new MatchingBracketsEditStrategy() + }; + } + */ + + public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) { + PresentationReconciler reconciler = new PresentationReconciler(); + + DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner()); + + reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE); + reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); + + return reconciler; + } + + /* + * + */ + ITokenScanner getSclTokenScanner() { + RuleBasedScanner scanner = new RuleBasedScanner(); + + final Token string = new Token(new TextAttribute(colorManager.getColor(new RGB(42, 0, 255)))); + final Token reserved = new Token( + new TextAttribute( + colorManager.getColor(new RGB(127, 0, 85)), + null, + SWT.BOLD + )); + final Token comment = new Token(new TextAttribute(colorManager.getColor(new RGB(63, 127, 95)))); + + WordRule reservedWord = new WordRule(new IWordDetector() { + @Override + public boolean isWordStart(char c) { + return c=='@'; + } + + @Override + public boolean isWordPart(char c) { + return Character.isJavaIdentifierPart(c); + } + }); + + reservedWord.addWord("@a", reserved); + reservedWord.addWord("@keywords", reserved); + reservedWord.addWord("@forAll", reserved); + reservedWord.addWord("@forSome", reserved); + reservedWord.addWord("@true", reserved); + reservedWord.addWord("@false", reserved); + reservedWord.addWord("@base", reserved); + reservedWord.addWord("@prefix", reserved); + + IRule[] rules = new IRule[] { + //new MultiLineRule("\"\"\"", "\"\"\"", string), + new PatternRule("\"", "\"", string, '\\', true), + new PatternRule("\"\"\"", "\"\"\"", string, '\\', false), + new PatternRule("<", ">", string, '\\', false), + new PatternRule("#", null, comment, '\0', true), + reservedWord + }; + scanner.setRules(rules); + + return scanner; + } + + @Override + public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) { + return new DefaultTextHover(sourceViewer); + } + + @Override + public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) { + return new DefaultAnnotationHover(); + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj index b9fd2b45..2dbca6f5 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj @@ -8,13 +8,18 @@ package org.simantics.sysdyn.expressionParser; import java.util.Set; import java.util.HashSet; +import java.util.List; +import java.util.ArrayList; +import java.util.HashMap; public class ExpressionParser { - Set references = new HashSet(); + HashMap> references = new HashMap>(); - public Set getReferences() { + public HashMap> getReferences() { return references; } + + } PARSER_END(ExpressionParser) @@ -56,7 +61,7 @@ TOKEN: // { add_op term } -> ( add_op() term() )* void expr() : { - references = new HashSet(); + references = new HashMap>(); } { simple_expression() | "if" expression() "then" expression() ( "elseif" expression() "then" expression() )* @@ -148,7 +153,15 @@ void name() : { void component_reference() : { } { //IDENT [ array_subscripts ] [ "." component_reference ] - { references.add(token.image); } + { + + if(references.get(token.image) == null) { + references.put(token.image, new ArrayList()); + } + List list = references.get(token.image); + list.add(token); + + } ( "." component_reference() )? } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/TestExpressionParser.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/TestExpressionParser.java index bed43adb..ec74402b 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/TestExpressionParser.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/TestExpressionParser.java @@ -1,6 +1,8 @@ package org.simantics.sysdyn.expressionParser; import java.io.StringReader; +import java.util.HashMap; +import java.util.List; public class TestExpressionParser { @@ -10,7 +12,8 @@ public class TestExpressionParser { parser.ReInit(new StringReader(string)); try { parser.expr(); - for(String ref : parser.getReferences()) { + HashMap> references = parser.getReferences(); + for(String ref : references.keySet()) { System.out.println(ref); } } catch (ParseException e) { -- 2.47.1