From ad607f688662b19259247974c2433202f7e8e6b0 Mon Sep 17 00:00:00 2001 From: miettinen Date: Mon, 10 Sep 2012 11:40:31 +0000 Subject: [PATCH] Assister in equation writing: While the user is typing an equation, the text field anticipates the input and presents the allowed variables and functions in a popup box. Ctrl+space opens the popup box with all allowed functions and variables. (refs #2969) ESC and ENTER do not yet work properly, and the completion fails also if the equation is written without whitespaces between tokens. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@25702 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../properties/widgets/ExpressionWidget.java | 18 +- .../expressions/AuxiliaryExpression.java | 21 ++ .../widgets/expressions/BasicExpression.java | 6 +- .../expressions/CompletionProcessor.java | 182 ++++++++++++++++++ .../expressions/ConstantExpression.java | 23 +++ .../widgets/expressions/DelayExpression.java | 10 +- .../widgets/expressions/EmptyExpression.java | 3 +- .../widgets/expressions/ExpressionField.java | 25 ++- .../ExpressionFieldConfiguration.java | 25 ++- .../widgets/expressions/IExpression.java | 3 +- .../widgets/expressions/LookupExpression.java | 3 +- .../expressions/ParameterExpression.java | 5 +- .../widgets/expressions/StockExpression.java | 5 +- .../expressions/WithLookupExpression.java | 8 +- .../widgets/functions/FunctionCodeWidget.java | 3 +- 15 files changed, 313 insertions(+), 27 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/CompletionProcessor.java diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionWidget.java index 5b9b66e0..0b1c36cf 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionWidget.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionWidget.java @@ -16,23 +16,31 @@ import java.awt.event.ActionListener; import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.CopyOnWriteArrayList; import javax.swing.Timer; +import org.eclipse.jface.fieldassist.SimpleContentProposalProvider; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.swt.SWT; import org.eclipse.swt.custom.VerifyKeyListener; import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.graphics.Color; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.TableItem; import org.simantics.browsing.ui.swt.widgets.impl.Widget; import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.db.AsyncReadGraph; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.management.ISessionContext; +import org.simantics.db.procedure.AsyncListener; import org.simantics.db.request.Read; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; @@ -47,9 +55,12 @@ import org.simantics.sysdyn.ui.properties.widgets.expressions.LookupExpression; import org.simantics.sysdyn.ui.properties.widgets.expressions.ParameterExpression; import org.simantics.sysdyn.ui.properties.widgets.expressions.StockExpression; import org.simantics.sysdyn.ui.properties.widgets.expressions.WithLookupExpression; +import org.simantics.sysdyn.ui.trend.chart.properties.VariableProposalProvider; import org.simantics.sysdyn.ui.utils.ExpressionUtils; +import org.simantics.sysdyn.ui.validation.ValidationUtils; import org.simantics.ui.SimanticsUI; import org.simantics.ui.utils.AdaptionUtils; +import org.simantics.utils.ui.ISelectionUtils; /** * Widget for displaying an expression. Widget creates the IExpression for displaying @@ -57,6 +68,7 @@ import org.simantics.ui.utils.AdaptionUtils; * to the active IExpression. * * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class ExpressionWidget implements Widget { @@ -105,7 +117,7 @@ public class ExpressionWidget implements Widget { }); validationTimer.setRepeats(false); } - + @Override public void setInput(ISessionContext context, Object input) { // Update IExpression based on the newly selected expression @@ -163,7 +175,7 @@ public class ExpressionWidget implements Widget { exp.readData(expr, data); // Create the visual representation of the expression type - exp.createExpressionFields(parent, data); + exp.createExpressionFields(parent, data, variableTable); // Add listeners if(modifyListener != null) @@ -253,7 +265,7 @@ public class ExpressionWidget implements Widget { public void addVerifyKeyListener(VerifyKeyListener listener) { this.verifyKeyListener = listener; } - + public void addFocusListener(FocusListener listener) { this.focusListener = listener; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/AuxiliaryExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/AuxiliaryExpression.java index 85c7ec14..abfa3e4a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/AuxiliaryExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/AuxiliaryExpression.java @@ -11,6 +11,14 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.properties.widgets.expressions; +import java.util.Map; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Table; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; @@ -33,5 +41,18 @@ public class AuxiliaryExpression extends BasicExpression { e.printStackTrace(); } } + + @Override + public void createExpressionFields(Composite parent, Map data, Table allowedVariables) { + // Create the single field + GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent); + String equation = data.get("equation") != null ? (String)data.get("equation") : ""; + + Label l = new Label(parent, SWT.NONE); + l.setText("="); + expression = new ExpressionField(parent, SWT.BORDER, allowedVariables, true); + expression.setExpression(equation); + GridDataFactory.fillDefaults().grab(true, true).applyTo(expression); + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java index cc66c5dc..062d452a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java @@ -28,6 +28,7 @@ import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Table; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -46,6 +47,7 @@ import org.simantics.ui.SimanticsUI; /** * Basic expression that is used with parameter, auxiliary and constant * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class BasicExpression implements IExpression { @@ -54,7 +56,7 @@ public class BasicExpression implements IExpression { protected Resource expressionType; @Override - public void createExpressionFields(Composite parent, Map data) { + public void createExpressionFields(Composite parent, Map data, Table allowedVariables) { // Create the single field GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent); String equation = data.get("equation") != null ? (String)data.get("equation") : ""; @@ -62,7 +64,7 @@ public class BasicExpression implements IExpression { Label l = new Label(parent, SWT.NONE); l.setText("="); - expression = new ExpressionField(parent, SWT.BORDER); + expression = new ExpressionField(parent, SWT.BORDER, null, false); expression.setExpression(equation); GridDataFactory.fillDefaults().grab(true, true).applyTo(expression); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/CompletionProcessor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/CompletionProcessor.java new file mode 100644 index 00000000..d0966676 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/CompletionProcessor.java @@ -0,0 +1,182 @@ +/******************************************************************************* + * Copyright (c) 2010 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.properties.widgets.expressions; + +import java.util.ArrayList; +import java.util.Collections; + +import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.contentassist.CompletionProposal; +import org.eclipse.jface.text.contentassist.ICompletionProposal; +import org.eclipse.jface.text.contentassist.IContentAssistProcessor; +import org.eclipse.jface.text.contentassist.IContextInformation; +import org.eclipse.jface.text.contentassist.IContextInformationValidator; +import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.TableItem; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.ui.SimanticsUI; + + +/** + * IContentAssistProcessor to determine which options (the functions and + * variables available) are shown for ContentAssistant; this assist of + * text field allows long variable names to be selected from a popup menu. + * @author Tuomas Miettinen + * + */ +public class CompletionProcessor implements IContentAssistProcessor { + + private Table allowedVariables; + private ArrayList functions; + private ArrayList variables = null; + + private char[] allowedCharacters = { + 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','å','ä','ö', + 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Å','Ä','Ö', + '1','2','3','4','5','6','7','8','9','0','.', + '(',')'}; + + public CompletionProcessor(Table allowedVariables, boolean allowFunctions){ + this.allowedVariables = allowedVariables; + + //Finding functions + functions = new ArrayList(); + if (allowFunctions) { + try { + functions = SimanticsUI.getSession().syncRequest(new Read>() { + @Override + public ArrayList perform(ReadGraph graph) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + ArrayList functions = new ArrayList(); + + Resource funktionlibrary = graph.getPossibleResource(SysdynResource.URIs.Built$in_Functions); + for(Resource r : graph.syncRequest(new ObjectsWithType(funktionlibrary, l0.ConsistsOf, sr.SysdynModelicaFunction))) { + String name = NameUtils.getSafeName(graph, r); + functions.add(name); + } + + Resource subfunktionlibrary = graph.getPossibleResource(SysdynResource.URIs.Built$in_Functions_Vensim_Functions); + for(Resource r : graph.syncRequest(new ObjectsWithType(subfunktionlibrary, l0.ConsistsOf, sr.SysdynModelicaFunction))) { + String name = NameUtils.getSafeName(graph, r); + functions.add(name); + } + + return functions; + } + }); + } + catch (DatabaseException e) { + e.printStackTrace(); + } + } + Collections.sort(functions); + } + + private ICompletionProposal[] collectProposals(String token, int offset) { + //Finding variables + if (variables == null) { + variables = new ArrayList(); + if(allowedVariables != null && !allowedVariables.isDisposed()) { + TableItem[] connectedVariables = allowedVariables.getItems(); + for(TableItem ti : connectedVariables) { + this.variables.add(ti.getText()); + } + } + Collections.sort(variables); + } + + ArrayList resultArray = new ArrayList(); + for (String function : functions) { + if (token.length() == 0 || function.toUpperCase().startsWith(token.toUpperCase())) { + resultArray.add(new CompletionProposal(function, + offset - token.length(), + token.length(), + function.length())); + } + } + for (String variable : variables) { + if (token.length() == 0 || variable.toUpperCase().startsWith(token.toUpperCase())) { + resultArray.add(new CompletionProposal(variable, + offset - token.length(), + token.length(), + variable.length())); + } + } + ICompletionProposal[] result = new ICompletionProposal[resultArray.size()]; + for (int i = 0; i < result.length; ++i) { + result[i] = resultArray.get(i); + } + return result; + } + + @Override + public ICompletionProposal[] computeCompletionProposals( + ITextViewer viewer, int offset) { + String equation = viewer.getDocument().get(); +// System.out.println(equation + "\noffset = " + offset); + + if (equation.length() == 0 + || offset == 0 + || Character.isWhitespace(equation.charAt(offset - 1))) { + return collectProposals("", offset); + } + + equation = equation.substring(0, offset); +// System.out.println(equation + "\noffset = " + offset); + + // Split into tokens on whitespace characters + String[] tokens = equation.split("\\s+"); + if (tokens.length == 0) { + return collectProposals("", offset); + } + String token = tokens[tokens.length - 1]; +// System.out.println(token + "\noffset = " + offset); + + return collectProposals(token, offset); + } + + @Override + public IContextInformation[] computeContextInformation( + ITextViewer viewer, int offset) { + return null; + } + + @Override + public char[] getCompletionProposalAutoActivationCharacters() { + return allowedCharacters; + } + + @Override + public char[] getContextInformationAutoActivationCharacters() { + return null; + } + + @Override + public String getErrorMessage() { + return "Error in CompletionProcessor"; + } + + @Override + public IContextInformationValidator getContextInformationValidator() { + return null; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ConstantExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ConstantExpression.java index d1c1fbd9..8ca2a5fa 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ConstantExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ConstantExpression.java @@ -11,6 +11,14 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.properties.widgets.expressions; +import java.util.Map; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Table; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; @@ -33,4 +41,19 @@ public class ConstantExpression extends BasicExpression { e.printStackTrace(); } } + + @Override + public void createExpressionFields(Composite parent, Map data, Table allowedVariables) { + // Create the single field + GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent); + String equation = data.get("equation") != null ? (String)data.get("equation") : ""; + + Label l = new Label(parent, SWT.NONE); + l.setText("="); +System.out.println("ConstantExpression"); + expression = new ExpressionField(parent, SWT.BORDER, null, false); + expression.setExpression(equation); + GridDataFactory.fillDefaults().grab(true, true).applyTo(expression); + + } } \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/DelayExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/DelayExpression.java index d9401442..ab5f6749 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/DelayExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/DelayExpression.java @@ -32,6 +32,7 @@ import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Spinner; +import org.eclipse.swt.widgets.Table; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -49,6 +50,7 @@ import org.simantics.ui.SimanticsUI; /** * IExpression for displaying fields of DelayExpression * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class DelayExpression implements IExpression { @@ -70,7 +72,7 @@ public class DelayExpression implements IExpression { * Displays the fields for delayExpression */ @Override - public void createExpressionFields(Composite parent, final Map data) { + public void createExpressionFields(Composite parent, final Map data, Table allowedVariables) { // Get possible existing data GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent); String eq = data.get("equation") != null ? (String)data.get("equation") : ""; @@ -82,7 +84,7 @@ public class DelayExpression implements IExpression { l.setText("expression"); // Equation that is delayed - equation = new ExpressionField(parent, SWT.BORDER); + equation = new ExpressionField(parent, SWT.BORDER, allowedVariables, true); equation.setExpression(eq); GridDataFactory.fillDefaults().grab(true, true).applyTo(equation); equation.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() { @@ -97,7 +99,7 @@ public class DelayExpression implements IExpression { l.setText("delay time"); // How much the equation is delayed - delayTime = new ExpressionField(parent, SWT.BORDER); + delayTime = new ExpressionField(parent, SWT.BORDER, allowedVariables, true); delayTime.setExpression(dt); GridDataFactory.fillDefaults().grab(true, true).applyTo(delayTime); @@ -113,7 +115,7 @@ public class DelayExpression implements IExpression { l.setText("initial value"); // What is the initial value of the delay (empty == same as equation) - initialValue = new ExpressionField(parent, SWT.BORDER); + initialValue = new ExpressionField(parent, SWT.BORDER, allowedVariables, true); initialValue.setExpression(iv); GridDataFactory.fillDefaults().grab(true, true).applyTo(initialValue); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/EmptyExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/EmptyExpression.java index 6de6e82a..f36fac96 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/EmptyExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/EmptyExpression.java @@ -19,12 +19,13 @@ import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.events.KeyListener; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Table; import org.simantics.db.Resource; public class EmptyExpression implements IExpression { @Override - public void createExpressionFields(Composite parent, Map data) { + public void createExpressionFields(Composite parent, Map data, Table allowedVariables) { // TODO Auto-generated method stub } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionField.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionField.java index 2904c0d0..696c960a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionField.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionField.java @@ -29,14 +29,17 @@ import org.eclipse.jface.text.source.MatchingCharacterPainter; import org.eclipse.jface.text.source.SourceViewer; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.custom.VerifyKeyListener; import org.eclipse.swt.events.FocusEvent; import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.events.KeyListener; +import org.eclipse.swt.events.VerifyEvent; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Table; import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess; import org.eclipse.swt.graphics.Point; import org.simantics.sysdyn.expressionParser.Token; @@ -47,6 +50,7 @@ import org.simantics.sysdyn.expressionParser.Token; * source viewer in eclipse. * * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class ExpressionField extends Composite { @@ -70,7 +74,7 @@ public class ExpressionField extends Composite { * @param parent * @param style */ - public ExpressionField(Composite parent, int style) { + public ExpressionField(Composite parent, int style, Table allowedVariables, boolean allowFunctions) { super(parent, style); GridLayoutFactory.fillDefaults().applyTo(this); @@ -94,7 +98,7 @@ public class ExpressionField extends Composite { styles); // Configuration for color management - _sourceViewer.configure(new ExpressionFieldConfiguration(cManager)); + _sourceViewer.configure(new ExpressionFieldConfiguration(cManager, allowedVariables, allowFunctions)); AnnotationPainter painter = new AnnotationPainter(_sourceViewer, annotationAccess); _sourceViewer.addPainter(painter); @@ -153,6 +157,23 @@ public class ExpressionField extends Composite { ((StyledText)e.widget).setSelection(selection); } }); + + _sourceViewer.appendVerifyKeyListener(new VerifyKeyListener() { + @Override + public void verifyKey(VerifyEvent event) { + // Check for Ctrl+Spacebar + if (event.stateMask == SWT.CTRL && event.character == ' ') { + // Check if source viewer is able to perform operation + if (_sourceViewer.canDoOperation(SourceViewer.CONTENTASSIST_PROPOSALS)) { + // Perform operation + _sourceViewer.doOperation(SourceViewer.CONTENTASSIST_PROPOSALS); + } + // Veto this key press to avoid further processing + event.doit = false; + } + } + + }); } /** diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionFieldConfiguration.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionFieldConfiguration.java index ba5d08be..11c82ee1 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionFieldConfiguration.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionFieldConfiguration.java @@ -15,6 +15,8 @@ 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.contentassist.ContentAssistant; +import org.eclipse.jface.text.contentassist.IContentAssistant; import org.eclipse.jface.text.presentation.IPresentationReconciler; import org.eclipse.jface.text.presentation.PresentationReconciler; import org.eclipse.jface.text.rules.DefaultDamagerRepairer; @@ -31,18 +33,23 @@ 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; +import org.eclipse.swt.widgets.Table; import org.simantics.sysdyn.ui.utils.VariableNameUtils; public class ExpressionFieldConfiguration extends SourceViewerConfiguration { ColorManager colorManager; - - public ExpressionFieldConfiguration(ColorManager colorManager) { + Table allowedVariables; + Boolean allowFunctions; + + public ExpressionFieldConfiguration(ColorManager colorManager, Table allowedVariables, boolean allowFunctions) { super(); this.colorManager = colorManager; + this.allowedVariables = allowedVariables; + this.allowFunctions = allowFunctions; } - + public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) { return new String[] { IDocument.DEFAULT_CONTENT_TYPE @@ -112,4 +119,16 @@ public class ExpressionFieldConfiguration extends SourceViewerConfiguration { return new DefaultAnnotationHover(); } + @Override + public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { + ContentAssistant assistant = new ContentAssistant(); + assistant.setContentAssistProcessor(new CompletionProcessor(allowedVariables, allowFunctions), "__dftl_partition_content_type"); + assistant.enableAutoActivation(true); + assistant.enableAutoInsert(true); + assistant.setAutoActivationDelay(0); + assistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY); + assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE); + return assistant; + } + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/IExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/IExpression.java index 1491cd32..c2c6eff0 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/IExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/IExpression.java @@ -19,11 +19,12 @@ import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.events.KeyListener; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Table; import org.simantics.db.Resource; public interface IExpression { - public void createExpressionFields(Composite parent, Map data); + public void createExpressionFields(Composite parent, Map data, Table allowedVaribles); public void readData(final Resource expression, Map data); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupExpression.java index 3cebc8c0..cf0f2304 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupExpression.java @@ -19,6 +19,7 @@ import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.events.KeyListener; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Table; import org.simantics.db.Resource; public class LookupExpression implements IExpression { @@ -60,7 +61,7 @@ public class LookupExpression implements IExpression { } @Override - public void createExpressionFields(Composite parent, Map data) { + public void createExpressionFields(Composite parent, Map data, Table allowedVariables) { // TODO Auto-generated method stub } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ParameterExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ParameterExpression.java index c181f5fc..6ccda38a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ParameterExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ParameterExpression.java @@ -19,6 +19,7 @@ import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Table; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; @@ -56,7 +57,7 @@ public class ParameterExpression extends BasicExpression { @Override - public void createExpressionFields(Composite parent, Map data) { + public void createExpressionFields(Composite parent, Map data, Table allowedVariables) { // Create the single field GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent); String equation = data.get("equation") != null ? (String)data.get("equation") : ""; @@ -64,7 +65,7 @@ public class ParameterExpression extends BasicExpression { Label l = new Label(parent, SWT.NONE); l.setText("="); - expression = new ExpressionField(parent, SWT.BORDER); + expression = new ExpressionField(parent, SWT.BORDER, null, false); expression.setExpression(equation); GridDataFactory.fillDefaults().grab(true, true).applyTo(expression); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java index ec2caa18..0e3e7f5d 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java @@ -27,6 +27,7 @@ import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.Text; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -49,7 +50,7 @@ public class StockExpression implements IExpression { private ExpressionField expression; @Override - public void createExpressionFields(Composite parent, Map data) { + public void createExpressionFields(Composite parent, Map data, Table allowedVariables) { GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent); String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : ""; String integralEquation = data.get("integral") != null ? (String)data.get("integral") : ""; @@ -67,7 +68,7 @@ public class StockExpression implements IExpression { label = new Label(parent, SWT.NONE); label.setText("Initial\nValue"); - expression = new ExpressionField(parent, SWT.BORDER); + expression = new ExpressionField(parent, SWT.BORDER, allowedVariables, true); expression.setExpression(initialEquation); GridDataFactory.fillDefaults().grab(true, true).applyTo(expression); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java index c8e10962..5d2fa12e 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java @@ -40,6 +40,7 @@ import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Table; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; @@ -82,7 +83,7 @@ public class WithLookupExpression implements IExpression { } @Override - public void createExpressionFields(Composite parent, final Map data) { + public void createExpressionFields(Composite parent, final Map data, Table allowedVariables) { GridLayoutFactory.fillDefaults().numColumns(3).applyTo(parent); updateChartTimer = new Timer(1000, new ActionListener() { @@ -100,7 +101,7 @@ public class WithLookupExpression implements IExpression { Label l = new Label(parent, SWT.NONE); l.setText("With\nLookup"); - expression = new ExpressionField(parent, SWT.BORDER); + expression = new ExpressionField(parent, SWT.BORDER, allowedVariables, true); expression.setExpression(equation); GridDataFactory.fillDefaults().grab(true, true).applyTo(expression); @@ -119,7 +120,7 @@ public class WithLookupExpression implements IExpression { l = new Label(parent, SWT.NONE); l.setText("Lookup\ntable"); - lookup = new ExpressionField(parent, SWT.BORDER); + lookup = new ExpressionField(parent, SWT.BORDER, null, false); lookup.setExpression(lookupTable); GridDataFactory.fillDefaults().grab(true, true).applyTo(lookup); @@ -404,5 +405,4 @@ public class WithLookupExpression implements IExpression { smallPanel.getChart().getXYPlot().setDataset(dataset); } - } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/functions/FunctionCodeWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/functions/FunctionCodeWidget.java index 9bbd115f..8fd7bdb8 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/functions/FunctionCodeWidget.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/functions/FunctionCodeWidget.java @@ -56,8 +56,7 @@ public class FunctionCodeWidget implements Widget { public FunctionCodeWidget(Composite parent, WidgetSupport support, int style) { support.register(this); - - modelicaCode = new ExpressionField(parent, SWT.BORDER); + modelicaCode = new ExpressionField(parent, SWT.BORDER, null, false); GridDataFactory.fillDefaults().grab(true, true).applyTo(modelicaCode); -- 2.47.1