commandId="org.simantics.sysdyn.ui.trend.view.pin">\r
</handler>\r
</extension>\r
-\r
+- <extension point="org.eclipse.ui.editors.annotationTypes">\r
+ <type markerSeverity="0" name="com.mycompany.element" /> \r
+ </extension>\r
+- <extension point="org.eclipse.ui.editors.markerAnnotationSpecification">\r
+ <specification annotationType="com.mycompany.element" colorPreferenceKey="annotateElemColor" colorPreferenceValue="255,255,0" highlightPreferenceKey="annotateElemHighlight" highlightPreferenceValue="true" includeOnPreferencePage="true" label="Sample Annotation" overviewRulerPreferenceKey="annotateElemOverviewRuler" textPreferenceKey="annotateElemText" verticalRulerPreferenceKey="annotateElemVerticalRuler" verticalRulerPreferenceValue="true" /> \r
+ </extension>\r
</plugin>\r
\r
\r
import java.io.StringReader;\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+import java.util.HashMap;\r
import java.util.HashSet;\r
+import java.util.List;\r
import java.util.Set;\r
\r
import org.eclipse.jface.layout.GridDataFactory;\r
import org.eclipse.jface.viewers.ISelection;\r
import org.eclipse.jface.viewers.IStructuredSelection;\r
import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.custom.StyleRange;\r
+import org.eclipse.swt.custom.StyledText;\r
import org.eclipse.swt.events.FocusEvent;\r
import org.eclipse.swt.events.FocusListener;\r
import org.eclipse.swt.events.KeyEvent;\r
import org.eclipse.swt.graphics.Color;\r
import org.eclipse.swt.graphics.Font;\r
import org.eclipse.swt.graphics.Point;\r
-import org.eclipse.swt.layout.RowLayout;\r
import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Label;\r
import org.eclipse.swt.widgets.Table;\r
import org.eclipse.swt.widgets.TableItem;\r
import org.eclipse.swt.widgets.Text;\r
import org.simantics.db.request.Read;\r
import org.simantics.sysdyn.expressionParser.ExpressionParser;\r
import org.simantics.sysdyn.expressionParser.ParseException;\r
+import org.simantics.sysdyn.expressionParser.Token;\r
+import org.simantics.sysdyn.ui.equation.expressions.ExpressionField;\r
import org.simantics.ui.SimanticsUI;\r
\r
public class EquationView extends ViewPart implements ISelectionListener {\r
// Dummies\r
Composite unitsAndRange; \r
Composite emptyComposite;\r
+ \r
+ ExpressionField ef;\r
+\r
+\r
+ StyledText styledText;\r
+ \r
+ public static final String ANNO_TYPE = "org.eclipse.ui.workbench.texteditor.error";\r
+ public static final String ANNO_KEY_HIGHLIGHT = "annotateElemHighlight";\r
+ public static final String ANNO_KEY_OVERVIEW = "annotateElemOverviewRuler";\r
+ public static final String ANNO_KEY_VERTICAL = "annotateElemVertialRuler";\r
+ public static final String ANNO_KEY_TEXT = "annotateElemText";\r
+ public static final String ANNO_KEY_COLOR = "annotateElemColor";\r
+ \r
+\r
\r
@Override \r
public void createPartControl(Composite parent) {\r
if(unitSelector != null) {\r
unitSelector.save(); \r
}\r
- \r
+\r
if(nameComposite != null) {\r
nameComposite.save(variable);\r
}\r
emptyComposite = new Composite(parent, SWT.NONE);\r
GridDataFactory.fillDefaults().span(1,4).grab(true, true).applyTo(emptyComposite);\r
GridLayoutFactory.fillDefaults().applyTo(emptyComposite);\r
+ \r
+ ef = new ExpressionField(emptyComposite, SWT.NONE);\r
+ GridDataFactory.fillDefaults().grab(true, true).applyTo(ef);\r
\r
// Below variable name: Equation\r
expressionComposite = new ExpressionComposite(parent, SWT.NONE);\r
\r
// Lower part of the view: Units and Range\r
unitsAndRange = new Composite(parent, SWT.NONE); \r
- GridDataFactory.fillDefaults().span(3, 1).applyTo(unitsAndRange);\r
+ GridDataFactory.fillDefaults().span(3, 1).grab(true, true).applyTo(unitsAndRange);\r
+\r
+ GridLayoutFactory.fillDefaults().applyTo(unitsAndRange);\r
+\r
+ styledText = new StyledText(unitsAndRange, SWT.BORDER | SWT.H_SCROLL\r
+ | SWT.V_SCROLL);\r
+\r
+\r
+ GridDataFactory.fillDefaults().grab(true, true).applyTo(styledText);\r
+\r
+ /*\r
+ styledText.addLineStyleListener(new LineStyleListener() {\r
+\r
+ public void lineGetStyle(LineStyleEvent event) {\r
+ // Create a collection to hold the StyleRanges\r
+ java.util.List styles = new java.util.ArrayList();\r
+ // Iterate through the text\r
+ for (int i = 0, n = event.lineText.length(); i < n; i++) {\r
+ // Check for 'e'\r
+ if (event.lineText.charAt(i) == 'e') {\r
+ // Found an 'e'; combine all subsequent e's into the same StyleRange\r
+ int start = i;\r
+ for (; i < n && event.lineText.charAt(i) == 'e'; i++);\r
+\r
+ // Create the StyleRange and add it to the collection\r
+ styles.add(new StyleRange(event.lineOffset + start, i - start, styledText.getDisplay().getSystemColor(SWT.COLOR_RED),\r
+ null));\r
+ }\r
+ }\r
+ // Set the styles for the line\r
+ event.styles = (StyleRange[]) styles.toArray(new StyleRange[0]);\r
+ }\r
+ });\r
+ */\r
+\r
+ /*\r
RowLayout layout = new RowLayout();\r
layout.spacing = 3;\r
unitsAndRange.setLayout(layout);\r
text1.setToolTipText("Stop");\r
text1 = new Text(c, SWT.SINGLE | SWT.BORDER);\r
text1.setToolTipText("Step");\r
+ */\r
} \r
\r
static class Auxiliary {\r
}\r
});\r
text.addKeyListener(new KeyListener() {\r
- \r
+\r
@Override\r
public void keyReleased(KeyEvent e) {\r
}\r
- \r
+\r
@Override\r
public void keyPressed(KeyEvent e) {\r
if(e.keyCode == SWT.ESC && expression != null) {\r
private void validateExpressionFields() {\r
ExpressionParser parser = new ExpressionParser(new StringReader(""));\r
Set<String> variables = new HashSet<String>();\r
+ HashMap<String, List<Token>> references = null;\r
+\r
+\r
\r
if(expressionComposite.getExpressionViewFactor() != null)\r
for(Text text : expressionComposite.getExpressionViewFactor().getExpressionFields()) {\r
+\r
+ \r
+ styledText.setText(text.getText());\r
+\r
String textString = text.getText();\r
parser.ReInit(new StringReader(textString));\r
try {\r
parser.expr();\r
- variables.addAll(parser.getReferences());\r
+ System.out.println(parser.getReferences());\r
+ references = parser.getReferences();\r
+ for(String t : references.keySet())\r
+ variables.add(t);\r
text.setBackground(new Color(text.getDisplay(), 255, 255, 255));\r
} catch (ParseException e1) {\r
text.setBackground(new Color(text.getDisplay(), 255, 230, 230));\r
}\r
\r
if(!variables.isEmpty()) {\r
+ ArrayList<StyleRange> ranges = new ArrayList<StyleRange>();\r
String no_connections = "No connections for: ";\r
for(String s : variables) {\r
- no_connections = no_connections + s + " ";\r
+ List<Token> tlist = references.get(s);\r
+ for(Token t : tlist) {\r
+ no_connections = no_connections + s + "[" + t.beginLine + "," + \r
+ t.beginColumn + "," + t.endLine + "," + t.endColumn + "]";\r
+ int start = styledText.getOffsetAtLine(t.beginLine - 1) + t.beginColumn - 1;\r
+ int length = t.endColumn - t.beginColumn + 1;\r
+ System.out.println(start + ", " + length);\r
+ ranges.add(new StyleRange( \r
+ start,\r
+ length, \r
+ new Color(styledText.getDisplay(), 0, 0, 0), \r
+ new Color(styledText.getDisplay(), 255, 255, 255), \r
+ SWT.BOLD));\r
+ }\r
}\r
+ System.out.println(no_connections);\r
+ Collections.sort(ranges, new StyleRangeSorter());\r
+ styledText.setStyleRanges(ranges.toArray(new StyleRange[0]));\r
}\r
}\r
\r
+\r
+ class StyleRangeSorter implements Comparator<StyleRange> {\r
+ public int compare(StyleRange a, StyleRange b) {\r
+ Integer ai, bi;\r
+\r
+ ai = a.start;\r
+ bi = b.start;\r
+\r
+ return ai.compareTo(bi);\r
+ }\r
+ }\r
}
\ No newline at end of file
--- /dev/null
+package org.simantics.sysdyn.ui.equation.expressions;\r
+\r
+import java.util.HashMap;\r
+\r
+import org.eclipse.jface.text.source.ISharedTextColors;\r
+import org.eclipse.swt.graphics.Color;\r
+import org.eclipse.swt.graphics.RGB;\r
+import org.eclipse.swt.widgets.Display;\r
+\r
+public class ColorManager implements ISharedTextColors {\r
+\r
+ protected HashMap<RGB, Color> colorTable = new HashMap<RGB, Color>();\r
+\r
+ @Override\r
+ public void dispose() {\r
+ for(Color c : colorTable.values())\r
+ c.dispose();\r
+ colorTable.clear();\r
+ }\r
+\r
+ @Override\r
+ public Color getColor(RGB rgb) {\r
+ Color color = colorTable.get(rgb);\r
+ if (color == null) {\r
+ color = new Color(Display.getCurrent(), rgb);\r
+ colorTable.put(rgb, color);\r
+ }\r
+ return color;\r
+\r
+\r
+ }\r
+}\r
--- /dev/null
+package org.simantics.sysdyn.ui.equation.expressions;\r
+\r
+import org.eclipse.jface.text.Document;\r
+import org.eclipse.jface.text.IDocument;\r
+import org.eclipse.jface.text.PaintManager;\r
+import org.eclipse.jface.text.Position;\r
+import org.eclipse.jface.text.source.Annotation;\r
+import org.eclipse.jface.text.source.AnnotationModel;\r
+import org.eclipse.jface.text.source.AnnotationPainter;\r
+import org.eclipse.jface.text.source.CompositeRuler;\r
+import org.eclipse.jface.text.source.DefaultCharacterPairMatcher;\r
+import org.eclipse.jface.text.source.IAnnotationAccess;\r
+import org.eclipse.jface.text.source.IOverviewRuler;\r
+import org.eclipse.jface.text.source.LineNumberRulerColumn;\r
+import org.eclipse.jface.text.source.MatchingCharacterPainter;\r
+import org.eclipse.jface.text.source.OverviewRuler;\r
+import org.eclipse.jface.text.source.SourceViewer;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.graphics.Color;\r
+import org.eclipse.swt.graphics.RGB;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.eclipse.ui.texteditor.AnnotationPreference;\r
+import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess;\r
+import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;\r
+\r
+public class ExpressionField extends Composite {\r
+ \r
+ public static final String ANNO_TYPE = "com.mycompany.element";\r
+ public static final String ANNO_KEY_HIGHLIGHT = "annotateElemHighlight";\r
+ public static final String ANNO_KEY_OVERVIEW = "annotateElemOverviewRuler";\r
+ public static final String ANNO_KEY_VERTICAL = "annotateElemVertialRuler";\r
+ public static final String ANNO_KEY_TEXT = "annotateElemText";\r
+ public static final String ANNO_KEY_COLOR = "annotateElemColor";\r
+ \r
+ protected SourceViewer _sourceViewer;\r
+ protected SourceViewerDecorationSupport _sds;\r
+ protected IDocument _document;\r
+ protected AnnotationModel _annotationModel;\r
+ \r
+ protected String _docString = "this\nis\na\ntest\ndocument";\r
+\r
+ ColorManager cManager = new ColorManager();\r
+ \r
+ IAnnotationAccess annotationAccess = new DefaultMarkerAnnotationAccess();\r
+\r
+ public ExpressionField(Composite parent, int style) {\r
+ super(parent, style);\r
+\r
+ int VERTICAL_RULER_WIDTH = 12;\r
+ \r
+ int styles = SWT.V_SCROLL\r
+ | SWT.H_SCROLL\r
+ | SWT.MULTI\r
+ | SWT.BORDER\r
+ | SWT.FULL_SELECTION;\r
+\r
+ IOverviewRuler overviewRuler = new OverviewRuler(null, VERTICAL_RULER_WIDTH,\r
+ cManager);\r
+ CompositeRuler ruler = new CompositeRuler(VERTICAL_RULER_WIDTH);\r
+ \r
+ _document = new Document();\r
+ _document.set(_docString);\r
+ \r
+ _annotationModel = new AnnotationModel();\r
+ _annotationModel.connect(_document);\r
+ \r
+ _sourceViewer = new SourceViewer(parent,\r
+ ruler,\r
+ overviewRuler,\r
+ true,\r
+ styles);\r
+ _sourceViewer.configure(new ExpressionFieldConfiguration(cManager));\r
+ \r
+ \r
+ // Tämä ei esimerkissä \r
+ AnnotationPainter painter = new AnnotationPainter(_sourceViewer, annotationAccess);\r
+ _sourceViewer.addPainter(painter);\r
+ \r
+ _sds = new SourceViewerDecorationSupport(_sourceViewer,\r
+ overviewRuler,\r
+ annotationAccess,\r
+ cManager);\r
+ // ^^^^^^^\r
+ \r
+ /* Esimerkistä:\r
+ _sds = new SourceViewerDecorationSupport(_sourceViewer,\r
+ overviewRuler,\r
+ null,\r
+ cManager);\r
+ */\r
+ \r
+ AnnotationPreference ap = new AnnotationPreference();\r
+ ap.setColorPreferenceKey(ANNO_KEY_COLOR);\r
+ ap.setHighlightPreferenceKey(ANNO_KEY_HIGHLIGHT);\r
+ ap.setVerticalRulerPreferenceKey(ANNO_KEY_VERTICAL);\r
+ ap.setOverviewRulerPreferenceKey(ANNO_KEY_OVERVIEW);\r
+ ap.setTextPreferenceKey(ANNO_KEY_TEXT);\r
+ ap.setAnnotationType(ANNO_TYPE);\r
+ _sds.setAnnotationPreference(ap);\r
+ \r
+ _sds.install(PlatformUI.getPreferenceStore());\r
+ \r
+ _sourceViewer.setDocument(_document, _annotationModel);\r
+ \r
+ _sourceViewer.getControl().setLayoutData(new GridData(SWT.FILL,\r
+ SWT.FILL,\r
+ true,\r
+ true));\r
+ \r
+ ruler.addDecorator(0, new LineNumberRulerColumn());\r
+ \r
+ \r
+ Annotation annotation = new Annotation(false);\r
+ annotation.setType(ANNO_TYPE);\r
+ annotation.setText("moro!");\r
+ Position position = new Position(0, 4);\r
+ _annotationModel.addAnnotation(annotation, position); \r
+\r
+\r
+ \r
+ PaintManager paintManager = new PaintManager(_sourceViewer);\r
+ MatchingCharacterPainter matchingCharacterPainter = new MatchingCharacterPainter(_sourceViewer,\r
+ new DefaultCharacterPairMatcher( new char[] {'(', ')', '{', '}', '[', ']'} ));\r
+ matchingCharacterPainter.setColor(new Color(Display.getCurrent(), new RGB(160, 160, 160)));\r
+ paintManager.addPainter(matchingCharacterPainter);\r
+ \r
+ \r
+ \r
+ annotation = new Annotation(false);\r
+ annotation.setType("org.eclipse.ui.workbench.texteditor.error");\r
+ annotation.setText("Tekstiä!");\r
+ position = new Position(0, 4);\r
+ _sourceViewer.getAnnotationModel().addAnnotation(annotation, position);\r
+\r
+ }\r
+ \r
+ public SourceViewer getSourceViewer() {\r
+ return this._sourceViewer;\r
+ }\r
+\r
+}\r
--- /dev/null
+package org.simantics.sysdyn.ui.equation.expressions;\r
+\r
+import org.eclipse.jface.text.DefaultTextHover;\r
+import org.eclipse.jface.text.IDocument;\r
+import org.eclipse.jface.text.ITextHover;\r
+import org.eclipse.jface.text.TextAttribute;\r
+import org.eclipse.jface.text.presentation.IPresentationReconciler;\r
+import org.eclipse.jface.text.presentation.PresentationReconciler;\r
+import org.eclipse.jface.text.rules.DefaultDamagerRepairer;\r
+import org.eclipse.jface.text.rules.IRule;\r
+import org.eclipse.jface.text.rules.ITokenScanner;\r
+import org.eclipse.jface.text.rules.IWordDetector;\r
+import org.eclipse.jface.text.rules.PatternRule;\r
+import org.eclipse.jface.text.rules.RuleBasedScanner;\r
+import org.eclipse.jface.text.rules.Token;\r
+import org.eclipse.jface.text.rules.WordRule;\r
+import org.eclipse.jface.text.source.DefaultAnnotationHover;\r
+import org.eclipse.jface.text.source.IAnnotationHover;\r
+import org.eclipse.jface.text.source.ISourceViewer;\r
+import org.eclipse.jface.text.source.SourceViewerConfiguration;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.graphics.RGB;\r
+\r
+public class ExpressionFieldConfiguration extends SourceViewerConfiguration {\r
+\r
+\r
+ ColorManager colorManager;\r
+\r
+ public ExpressionFieldConfiguration(ColorManager colorManager) {\r
+ super();\r
+ this.colorManager = colorManager;\r
+ }\r
+\r
+ public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {\r
+ return new String[] {\r
+ IDocument.DEFAULT_CONTENT_TYPE\r
+ };\r
+ }\r
+\r
+ /*\r
+ @Override\r
+ public IAutoEditStrategy[] getAutoEditStrategies(\r
+ ISourceViewer sourceViewer, String contentType) {\r
+ return new IAutoEditStrategy[] {\r
+ new ReplaceTabsBySpaces(),\r
+ new IndentLineEditStrategy(),\r
+ new MatchingBracketsEditStrategy()\r
+ };\r
+ }\r
+ */\r
+\r
+ public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {\r
+ PresentationReconciler reconciler = new PresentationReconciler();\r
+\r
+ DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner());\r
+\r
+ reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
+ reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
+\r
+ return reconciler;\r
+ }\r
+\r
+ /*\r
+ * \r
+ */\r
+ ITokenScanner getSclTokenScanner() {\r
+ RuleBasedScanner scanner = new RuleBasedScanner();\r
+\r
+ final Token string = new Token(new TextAttribute(colorManager.getColor(new RGB(42, 0, 255)))); \r
+ final Token reserved = new Token(\r
+ new TextAttribute(\r
+ colorManager.getColor(new RGB(127, 0, 85)),\r
+ null,\r
+ SWT.BOLD\r
+ ));\r
+ final Token comment = new Token(new TextAttribute(colorManager.getColor(new RGB(63, 127, 95))));\r
+\r
+ WordRule reservedWord = new WordRule(new IWordDetector() { \r
+ @Override\r
+ public boolean isWordStart(char c) {\r
+ return c=='@';\r
+ }\r
+\r
+ @Override\r
+ public boolean isWordPart(char c) {\r
+ return Character.isJavaIdentifierPart(c);\r
+ }\r
+ });\r
+\r
+ reservedWord.addWord("@a", reserved);\r
+ reservedWord.addWord("@keywords", reserved);\r
+ reservedWord.addWord("@forAll", reserved);\r
+ reservedWord.addWord("@forSome", reserved);\r
+ reservedWord.addWord("@true", reserved);\r
+ reservedWord.addWord("@false", reserved);\r
+ reservedWord.addWord("@base", reserved);\r
+ reservedWord.addWord("@prefix", reserved);\r
+\r
+ IRule[] rules = new IRule[] {\r
+ //new MultiLineRule("\"\"\"", "\"\"\"", string),\r
+ new PatternRule("\"", "\"", string, '\\', true), \r
+ new PatternRule("\"\"\"", "\"\"\"", string, '\\', false),\r
+ new PatternRule("<", ">", string, '\\', false),\r
+ new PatternRule("#", null, comment, '\0', true),\r
+ reservedWord\r
+ };\r
+ scanner.setRules(rules);\r
+\r
+ return scanner; \r
+ }\r
+\r
+ @Override\r
+ public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {\r
+ return new DefaultTextHover(sourceViewer);\r
+ }\r
+\r
+ @Override\r
+ public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {\r
+ return new DefaultAnnotationHover();\r
+ }\r
+\r
+}\r
\r
import java.util.Set;\r
import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
\r
public class ExpressionParser {\r
- Set<String> references = new HashSet<String>();\r
+ HashMap<String, List<Token>> references = new HashMap<String, List<Token>>();\r
\r
- public Set<String> getReferences() {\r
+ public HashMap<String, List<Token>> getReferences() {\r
return references;\r
}\r
+ \r
+ \r
}\r
PARSER_END(ExpressionParser)\r
\r
// { add_op term } -> ( add_op() term() )*\r
\r
void expr() : {\r
- references = new HashSet<String>();\r
+ references = new HashMap<String, List<Token>>();\r
} {\r
simple_expression() <EOF> \r
| "if" expression() "then" expression() ( "elseif" expression() "then" expression() )*\r
void component_reference() : {\r
} {\r
//IDENT [ array_subscripts ] [ "." component_reference ]\r
- <IDENT> { references.add(token.image); } \r
+ <IDENT> { \r
+ \r
+ if(references.get(token.image) == null) {\r
+ references.put(token.image, new ArrayList<Token>());\r
+ }\r
+ List list = references.get(token.image);\r
+ list.add(token); \r
+ \r
+ } \r
( "." component_reference() )?\r
}\r
\r
package org.simantics.sysdyn.expressionParser;\r
\r
import java.io.StringReader;\r
+import java.util.HashMap;\r
+import java.util.List;\r
\r
public class TestExpressionParser {\r
\r
parser.ReInit(new StringReader(string));\r
try {\r
parser.expr();\r
- for(String ref : parser.getReferences()) {\r
+ HashMap<String, List<Token>> references = parser.getReferences();\r
+ for(String ref : references.keySet()) {\r
System.out.println(ref);\r
}\r
} catch (ParseException e) {\r