From 45ce7075167afcd5a1a13a976caa5797d1db5f6a Mon Sep 17 00:00:00 2001 From: miettinen Date: Tue, 22 Jan 2013 07:11:22 +0000 Subject: [PATCH] Simulate buttons save changes made in the model (refs #3960). When pressing certain simulation buttons in toolbar, they acquire the focus which in turn causes focus listeners to save changes, e.g. in EquationTab. All buttons do not cause this effect. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26661 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/handlers/DisposeExperiment.java | 3 + .../ui/handlers/RunBasicExperiment.java | 3 + .../ui/handlers/SaveResultsHandler.java | 3 + .../sysdyn/ui/handlers/ToggleSimulation.java | 4 +- .../sysdyn/ui/handlers/game/StepHandler.java | 4 +- .../PlaybackReloadHandler.java | 3 + .../sysdyn/ui/properties/EquationTab.java | 57 ++++++------------- .../expressions/ParameterExpression.java | 2 - .../sysdyn/ui/utils/HandlerUtils.java | 28 +++++++++ 9 files changed, 64 insertions(+), 43 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/HandlerUtils.java diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/DisposeExperiment.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/DisposeExperiment.java index 207cadda..175edc7b 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/DisposeExperiment.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/DisposeExperiment.java @@ -17,12 +17,15 @@ import org.eclipse.core.commands.ExecutionException; import org.simantics.project.IProject; import org.simantics.simulation.experiment.IExperiment; import org.simantics.simulation.project.IExperimentManager; +import org.simantics.sysdyn.ui.utils.HandlerUtils; import org.simantics.ui.SimanticsUI; public class DisposeExperiment extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { + HandlerUtils.acquireFocus(event); + IProject project = SimanticsUI.getProject(); IExperimentManager manager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/RunBasicExperiment.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/RunBasicExperiment.java index 2b4ef65f..b0f3e756 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/RunBasicExperiment.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/RunBasicExperiment.java @@ -24,6 +24,7 @@ import org.simantics.simulation.experiment.IDynamicExperiment; import org.simantics.simulation.experiment.IExperiment; import org.simantics.simulation.project.IExperimentManager; import org.simantics.sysdyn.manager.SysdynExperiment; +import org.simantics.sysdyn.ui.utils.HandlerUtils; import org.simantics.ui.SimanticsUI; public class RunBasicExperiment extends AbstractHandler implements IElementUpdater { @@ -32,6 +33,8 @@ public class RunBasicExperiment extends AbstractHandler implements IElementUpdat @Override public Object execute(ExecutionEvent event) throws ExecutionException { + HandlerUtils.acquireFocus(event); + IExperimentManager manager = SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER); IExperiment experiment = manager.getActiveExperiment(); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/SaveResultsHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/SaveResultsHandler.java index 84e679a9..e6c9968b 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/SaveResultsHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/SaveResultsHandler.java @@ -17,12 +17,15 @@ import org.eclipse.core.commands.ExecutionException; import org.simantics.simulation.experiment.IDynamicExperiment; import org.simantics.simulation.experiment.IExperiment; import org.simantics.simulation.project.IExperimentManager; +import org.simantics.sysdyn.ui.utils.HandlerUtils; import org.simantics.ui.SimanticsUI; public class SaveResultsHandler extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { + HandlerUtils.acquireFocus(event); + IExperimentManager manager = SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER); IExperiment experiment = manager.getActiveExperiment(); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ToggleSimulation.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ToggleSimulation.java index bcd17050..5dd71a50 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ToggleSimulation.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ToggleSimulation.java @@ -26,6 +26,7 @@ import org.eclipse.ui.menus.UIElement; import org.simantics.simulation.experiment.IExperiment; import org.simantics.simulation.project.IExperimentManager; import org.simantics.sysdyn.manager.SysdynExperiment; +import org.simantics.sysdyn.ui.utils.HandlerUtils; import org.simantics.ui.SimanticsUI; public class ToggleSimulation extends AbstractHandler implements IElementUpdater { @@ -54,7 +55,8 @@ public class ToggleSimulation extends AbstractHandler implements IElementUpdater } } - + HandlerUtils.acquireFocus(event); + return null; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/game/StepHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/game/StepHandler.java index 7435b4cf..09caf0e5 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/game/StepHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/game/StepHandler.java @@ -23,6 +23,7 @@ import org.simantics.simulation.experiment.ExperimentState; import org.simantics.simulation.experiment.IExperiment; import org.simantics.simulation.project.IExperimentManager; import org.simantics.sysdyn.manager.SysdynGameExperiment; +import org.simantics.sysdyn.ui.utils.HandlerUtils; import org.simantics.ui.SimanticsUI; /** @@ -55,7 +56,8 @@ public class StepHandler extends AbstractHandler implements IElementUpdater { @Override public Object execute(ExecutionEvent event) throws ExecutionException { - + HandlerUtils.acquireFocus(event); + SysdynGameExperiment game = getGameExperiment(); if(game != null) game.simulateDuration(game.getStepDuration()); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/PlaybackReloadHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/PlaybackReloadHandler.java index e7d97445..a76fe7a2 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/PlaybackReloadHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/PlaybackReloadHandler.java @@ -6,12 +6,15 @@ import org.eclipse.core.commands.ExecutionException; import org.simantics.simulation.experiment.IExperiment; import org.simantics.simulation.project.IExperimentManager; import org.simantics.sysdyn.manager.SysdynPlaybackExperiment; +import org.simantics.sysdyn.ui.utils.HandlerUtils; import org.simantics.ui.SimanticsUI; public class PlaybackReloadHandler extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { + HandlerUtils.acquireFocus(event); + IExperimentManager manager = SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER); IExperiment experiment = manager.getActiveExperiment(); if(experiment == null || !(experiment instanceof SysdynPlaybackExperiment)) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java index d5761aa8..9a9487e6 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java @@ -36,9 +36,7 @@ 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.ui.IWorkbenchPartReference; import org.eclipse.ui.IWorkbenchSite; -import org.simantics.browsing.ui.platform.PropertyPageView; import org.simantics.browsing.ui.swt.widgets.Button; import org.simantics.browsing.ui.swt.widgets.TrackedCombo; import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl; @@ -82,6 +80,7 @@ import org.simantics.utils.ui.AdaptionUtils; /** * Tab for displaying equation information of a variable * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class EquationTab extends LabelPropertyTabContributor implements Widget { @@ -617,6 +616,20 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { } }); + expressionWidget.addFocusListener(new FocusListener() { + + @Override + public void focusGained(FocusEvent e) { + } + + @Override + public void focusLost(FocusEvent e) { + if (expressionWidget != null) { + expressionWidget.save(); + } + } + }); + // Pressing return without shift key triggers saving the expression expressionWidget.addVerifyKeyListener(new VerifyKeyListener() { @@ -635,7 +648,8 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { if((event.stateMask & SWT.SHIFT) == 0) { event.doit = false; ((StyledText)event.widget).getParent().forceFocus(); - expressionWidget.save(); + // No need to save equations here since they are saved in focusListener + //expressionWidget.save(); } } else { // When a proposed expression is selected with enter, fields are validated. @@ -644,42 +658,7 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { } } }); - - // Triggers save when equation tab loses focus - if(focusLostListener == null) { - focusLostListener = new org.eclipse.ui.IPartListener2() - { - @Override - public void partInputChanged(IWorkbenchPartReference partRef) {} - @Override - public void partVisible(IWorkbenchPartReference partRef) {} - @Override - public void partHidden(IWorkbenchPartReference partRef) {} - @Override - public void partOpened(IWorkbenchPartReference partRef) {} - @Override - public void partDeactivated(IWorkbenchPartReference partRef) - { - if(partRef.getPart(false) instanceof PropertyPageView) { - PropertyPageView ppv = (PropertyPageView)partRef.getPart(false); - if(ppv.getCurrentPage() instanceof SysdynPropertyPage) { - // Save expressions - if(expressionWidget != null) { - expressionWidget.save(); - } -// site.getPage().removePartListener(this); - } - } - } - @Override - public void partClosed(IWorkbenchPartReference partRef) {} - @Override - public void partBroughtToTop(IWorkbenchPartReference partRef) {} - @Override - public void partActivated(IWorkbenchPartReference partRef) {} - }; - site.getPage().addPartListener(focusLostListener); - } + } @Override 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 6ccda38a..aab9b0ba 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 @@ -147,8 +147,6 @@ public class ParameterExpression extends BasicExpression { if(savedIntoFMU) { data.put("equation", currentText); return; - } else { - super.save(expression, data); } } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/HandlerUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/HandlerUtils.java new file mode 100644 index 00000000..4482d14a --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/HandlerUtils.java @@ -0,0 +1,28 @@ +package org.simantics.sysdyn.ui.utils; + +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Event; + +/** + * Utility methods for handlers. + * @author Tuomas Miettinen + * + */ +public class HandlerUtils { + + /** + * Get keyboard focus. This allows focus based model saving prior to running experiment functions. + * + * @param event ExecutionEvent of which triggering event is used to acquire focus. The intended + * use is to acquire (keyboard) focus so that various listeners elsewhere that listen to losing + * focus will activate. + */ + public static void acquireFocus(ExecutionEvent event) { + if (event.getTrigger() instanceof Event) { + Event trigger = (Event)event.getTrigger(); + Control focusControl = trigger.display.getFocusControl(); + focusControl.getParent().forceFocus(); + } + } +} -- 2.47.1