--- /dev/null
+package org.simantics.sysdyn.ui;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.swt.custom.StyledText;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Event;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
+import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionField;\r
+\r
+public class ExpressionFieldUndoHandler extends AbstractHandler {\r
+\r
+ @Override\r
+ public Object execute(ExecutionEvent event) throws ExecutionException {\r
+ String id = event.getCommand().getId();\r
+ \r
+ Composite c = ((Control)((Event)event.getTrigger()).widget).getParent();\r
+ if (c instanceof ExpressionField) {\r
+ ExpressionField f = (ExpressionField)c;\r
+ if(id.equals("org.eclipse.ui.edit.undo")) {\r
+ f.getSourceViewer().getUndoManager().undo();\r
+ }\r
+ else {\r
+ f.getSourceViewer().getUndoManager().redo();\r
+ }\r
+ } else {\r
+ try {\r
+ Layer0Utils.undo();\r
+ } catch (DatabaseException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+\r
+}\r
import org.eclipse.jface.layout.GridLayoutFactory;\r
import org.eclipse.jface.text.BadLocationException;\r
import org.eclipse.jface.text.IDocument;\r
+import org.eclipse.jface.text.IUndoManager;\r
+import org.eclipse.jface.text.TextViewerUndoManager;\r
import org.eclipse.swt.SWT;\r
import org.eclipse.swt.custom.VerifyKeyListener;\r
import org.eclipse.swt.events.FocusAdapter;\r
import org.eclipse.swt.widgets.Label;\r
import org.eclipse.swt.widgets.Listener;\r
import org.eclipse.swt.widgets.Table;\r
+import org.simantics.Simantics;\r
import org.simantics.databoard.Bindings;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.common.request.WriteRequest;\r
import org.simantics.db.common.utils.ListUtils;\r
import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
import org.simantics.db.layer0.util.RemoverUtil;\r
import org.simantics.db.request.Read;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.sysdyn.manager.SysdynModelManager;\r
import org.simantics.sysdyn.representation.Stock;\r
import org.simantics.sysdyn.representation.Valve;\r
-import org.simantics.ui.SimanticsUI;\r
import org.simantics.utils.datastructures.Pair;\r
\r
public class StockExpression implements IExpression {\r
private final ExpressionWidgetInput input;\r
private ExpressionField integralExpression, initialExpression;\r
private ExpressionField lastSelectedText;\r
+ private TextViewerUndoManager undoManager;\r
\r
public StockExpression(ExpressionWidgetInput input) {\r
this.input = input;\r
}\r
});\r
\r
+ undoManager = new TextViewerUndoManager(100);\r
+ integralExpression.getSourceViewer().setUndoManager(undoManager);\r
+ undoManager.connect(integralExpression.getSourceViewer());\r
+ \r
Button defaultButton = new Button(integralComposite, SWT.PUSH);\r
defaultButton.setText("Use default");\r
defaultButton.addListener(SWT.Selection, new Listener() {\r
switch (event.type) {\r
case SWT.Selection:\r
StockExpression.this.integralExpression.setExpression(getDefaultIntegral(input.expression));\r
- SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+ Simantics.getSession().asyncRequest(new WriteRequest() {\r
\r
@Override\r
public void perform(WriteGraph graph)\r
Pair<String, String> equations = null;\r
if (expression != null && data.get("initialEquation") == null) {\r
try {\r
- equations = SimanticsUI.getSession().syncRequest(new Read<Pair<String, String>>() {\r
+ equations = Simantics.getSession().syncRequest(new Read<Pair<String, String>>() {\r
\r
@Override\r
public Pair<String, String> perform(ReadGraph graph) throws DatabaseException {\r
\r
if (equations.first == null) {\r
try {\r
- SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
+ Simantics.getSession().syncRequest(new WriteRequest() {\r
\r
@Override\r
public void perform(WriteGraph graph) throws DatabaseException {\r
} else {\r
try {\r
final String integral = equations.first;\r
- SimanticsUI.getSession().syncRequest(new ReadRequest() {\r
+ Simantics.getSession().syncRequest(new ReadRequest() {\r
\r
@Override\r
public void run(ReadGraph graph) throws DatabaseException {\r
if(!currentInitial.equals(data.get("initialEquation"))\r
|| currentIntegral == null\r
|| !currentIntegral.equals(data.get("integral"))) {\r
- SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+ Simantics.getSession().asyncRequest(new WriteRequest() {\r
\r
@Override\r
public void perform(WriteGraph g)\r
}\r
g.claimLiteral(expression, sr.StockExpression_integralEquation, currentIntegral);\r
\r
+ updateData(data);\r
+ \r
g.markUndoPoint();\r
- CommentMetadata cm = g.getMetadata(CommentMetadata.class);\r
- g.addMetadata(cm.add("Save"));\r
-\r
+ \r
+ Layer0Utils.addCommentMetadata(g, "Saved Stock Expression " + currentIntegral + " " + expression.toString() + " with Initial Value " + currentInitial);\r
}\r
\r
});\r
if(expression == null)\r
return integral;\r
try {\r
- integral = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
+ integral = Simantics.getSession().syncRequest(new Read<String>() {\r
\r
@Override\r
public String perform(ReadGraph graph) throws DatabaseException {\r
this.integralExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
}\r
\r
+ @Override\r
+ public IUndoManager getUndoManager() {\r
+ return undoManager;\r
+ }\r
+\r
}\r