From 7c6c806356fc43df0a6c048038df30734c066ae9 Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Mon, 14 Jul 2014 05:56:52 +0000 Subject: [PATCH] refs #5096 Add undo support in Sysdyns property view that allows the user to undo last typed text git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29867 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn.ui/plugin.xml | 33 ++++++++++++++- .../sysdyn/ui/ExpressionFieldUndoHandler.java | 40 +++++++++++++++++++ .../widgets/expressions/BasicExpression.java | 7 ++++ .../widgets/expressions/DelayExpression.java | 7 ++++ .../widgets/expressions/EmptyExpression.java | 7 ++++ .../widgets/expressions/IExpression.java | 3 ++ .../widgets/expressions/LookupExpression.java | 7 ++++ .../widgets/expressions/StockExpression.java | 34 +++++++++++----- .../expressions/WithLookupExpression.java | 7 ++++ 9 files changed, 134 insertions(+), 11 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/ExpressionFieldUndoHandler.java diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index ee0afef0..6e8d49e6 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -2504,7 +2504,26 @@ - + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/ExpressionFieldUndoHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/ExpressionFieldUndoHandler.java new file mode 100644 index 00000000..3f33ea9c --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/ExpressionFieldUndoHandler.java @@ -0,0 +1,40 @@ +package org.simantics.sysdyn.ui; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Event; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.Layer0Utils; +import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionField; + +public class ExpressionFieldUndoHandler extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + String id = event.getCommand().getId(); + + Composite c = ((Control)((Event)event.getTrigger()).widget).getParent(); + if (c instanceof ExpressionField) { + ExpressionField f = (ExpressionField)c; + if(id.equals("org.eclipse.ui.edit.undo")) { + f.getSourceViewer().getUndoManager().undo(); + } + else { + f.getSourceViewer().getUndoManager().redo(); + } + } else { + try { + Layer0Utils.undo(); + } catch (DatabaseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return null; + } + +} 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 12ad3c80..a1c65495 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 @@ -19,6 +19,7 @@ import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IUndoManager; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.VerifyKeyListener; import org.eclipse.swt.events.FocusListener; @@ -236,4 +237,10 @@ public class BasicExpression implements IExpression { public void addVerifyKeyListener(VerifyKeyListener listener) { this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener); } + + @Override + public IUndoManager getUndoManager() { + // TODO Auto-generated method stub + return null; + } } 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 e6b19626..5e778c92 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 @@ -19,6 +19,7 @@ import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IUndoManager; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.VerifyKeyListener; import org.eclipse.swt.events.FocusAdapter; @@ -382,4 +383,10 @@ public class DelayExpression implements IExpression { this.initialValue.getSourceViewer().getTextWidget().addVerifyKeyListener(listener); } + @Override + public IUndoManager getUndoManager() { + // TODO Auto-generated method stub + return null; + } + } 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 f36fac96..8a382746 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 @@ -14,6 +14,7 @@ package org.simantics.sysdyn.ui.properties.widgets.expressions; import java.util.List; import java.util.Map; +import org.eclipse.jface.text.IUndoManager; import org.eclipse.swt.custom.VerifyKeyListener; import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.events.KeyListener; @@ -90,4 +91,10 @@ public class EmptyExpression implements IExpression { } + @Override + public IUndoManager getUndoManager() { + // TODO Auto-generated method stub + return null; + } + } 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 c2c6eff0..bd0b0b71 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 @@ -14,6 +14,7 @@ package org.simantics.sysdyn.ui.properties.widgets.expressions; import java.util.List; import java.util.Map; +import org.eclipse.jface.text.IUndoManager; import org.eclipse.swt.custom.VerifyKeyListener; import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.events.KeyListener; @@ -45,4 +46,6 @@ public interface IExpression { public void addVerifyKeyListener(VerifyKeyListener listener); public void addFocusListener(FocusListener listener); + + public IUndoManager getUndoManager(); } 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 cf0f2304..db7311b3 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 @@ -14,6 +14,7 @@ package org.simantics.sysdyn.ui.properties.widgets.expressions; import java.util.List; import java.util.Map; +import org.eclipse.jface.text.IUndoManager; import org.eclipse.swt.custom.VerifyKeyListener; import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.events.KeyListener; @@ -90,4 +91,10 @@ public class LookupExpression implements IExpression { } + @Override + public IUndoManager getUndoManager() { + // TODO Auto-generated method stub + return null; + } + } 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 d92c9a1a..14f87e96 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 @@ -19,6 +19,8 @@ import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IUndoManager; +import org.eclipse.jface.text.TextViewerUndoManager; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.VerifyKeyListener; import org.eclipse.swt.events.FocusAdapter; @@ -33,6 +35,7 @@ import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Table; +import org.simantics.Simantics; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -42,6 +45,7 @@ import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.ListUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.db.layer0.util.RemoverUtil; import org.simantics.db.request.Read; import org.simantics.layer0.Layer0; @@ -50,7 +54,6 @@ import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.manager.SysdynModelManager; import org.simantics.sysdyn.representation.Stock; import org.simantics.sysdyn.representation.Valve; -import org.simantics.ui.SimanticsUI; import org.simantics.utils.datastructures.Pair; public class StockExpression implements IExpression { @@ -58,6 +61,7 @@ public class StockExpression implements IExpression { private final ExpressionWidgetInput input; private ExpressionField integralExpression, initialExpression; private ExpressionField lastSelectedText; + private TextViewerUndoManager undoManager; public StockExpression(ExpressionWidgetInput input) { this.input = input; @@ -88,6 +92,10 @@ public class StockExpression implements IExpression { } }); + undoManager = new TextViewerUndoManager(100); + integralExpression.getSourceViewer().setUndoManager(undoManager); + undoManager.connect(integralExpression.getSourceViewer()); + Button defaultButton = new Button(integralComposite, SWT.PUSH); defaultButton.setText("Use default"); defaultButton.addListener(SWT.Selection, new Listener() { @@ -97,7 +105,7 @@ public class StockExpression implements IExpression { switch (event.type) { case SWT.Selection: StockExpression.this.integralExpression.setExpression(getDefaultIntegral(input.expression)); - SimanticsUI.getSession().asyncRequest(new WriteRequest() { + Simantics.getSession().asyncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) @@ -152,7 +160,7 @@ public class StockExpression implements IExpression { Pair equations = null; if (expression != null && data.get("initialEquation") == null) { try { - equations = SimanticsUI.getSession().syncRequest(new Read>() { + equations = Simantics.getSession().syncRequest(new Read>() { @Override public Pair perform(ReadGraph graph) throws DatabaseException { @@ -176,7 +184,7 @@ public class StockExpression implements IExpression { if (equations.first == null) { try { - SimanticsUI.getSession().syncRequest(new WriteRequest() { + Simantics.getSession().syncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { @@ -197,7 +205,7 @@ public class StockExpression implements IExpression { } else { try { final String integral = equations.first; - SimanticsUI.getSession().syncRequest(new ReadRequest() { + Simantics.getSession().syncRequest(new ReadRequest() { @Override public void run(ReadGraph graph) throws DatabaseException { @@ -242,7 +250,7 @@ public class StockExpression implements IExpression { if(!currentInitial.equals(data.get("initialEquation")) || currentIntegral == null || !currentIntegral.equals(data.get("integral"))) { - SimanticsUI.getSession().asyncRequest(new WriteRequest() { + Simantics.getSession().asyncRequest(new WriteRequest() { @Override public void perform(WriteGraph g) @@ -275,10 +283,11 @@ public class StockExpression implements IExpression { } g.claimLiteral(expression, sr.StockExpression_integralEquation, currentIntegral); + updateData(data); + g.markUndoPoint(); - CommentMetadata cm = g.getMetadata(CommentMetadata.class); - g.addMetadata(cm.add("Save")); - + + Layer0Utils.addCommentMetadata(g, "Saved Stock Expression " + currentIntegral + " " + expression.toString() + " with Initial Value " + currentInitial); } }); @@ -336,7 +345,7 @@ public class StockExpression implements IExpression { if(expression == null) return integral; try { - integral = SimanticsUI.getSession().syncRequest(new Read() { + integral = Simantics.getSession().syncRequest(new Read() { @Override public String perform(ReadGraph graph) throws DatabaseException { @@ -376,4 +385,9 @@ public class StockExpression implements IExpression { this.integralExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener); } + @Override + public IUndoManager getUndoManager() { + return undoManager; + } + } 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 66132ab9..5051f5c1 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 @@ -28,6 +28,7 @@ import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IUndoManager; import org.eclipse.swt.SWT; import org.eclipse.swt.awt.SWT_AWT; import org.eclipse.swt.custom.VerifyKeyListener; @@ -459,4 +460,10 @@ public class WithLookupExpression implements IExpression { smallPanel.getChart().getXYPlot().setDataset(dataset); } + @Override + public IUndoManager getUndoManager() { + // TODO Auto-generated method stub + return null; + } + } -- 2.47.1