From 2fab8b2d4e1cf0b39b0cc925e5fc7f3454ce67bc Mon Sep 17 00:00:00 2001 From: miettinen Date: Tue, 22 Jan 2013 14:13:17 +0000 Subject: [PATCH] * Add the saved simulation results under the experiment _after_ the respective RecordAccessors have been saved to file (refs #3958). * Catching NPEs that are thrown if any "repeat" button is pressed consecutively (refs #4007). git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26663 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/utils/HandlerUtils.java | 15 ++++++++-- .../sysdyn/manager/SaveResultJob.java | 30 ++++++++++++++----- 2 files changed, 34 insertions(+), 11 deletions(-) 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 index 4482d14a..6d5497ed 100644 --- 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 @@ -1,6 +1,7 @@ package org.simantics.sysdyn.ui.utils; import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Event; @@ -20,9 +21,17 @@ public class HandlerUtils { */ public static void acquireFocus(ExecutionEvent event) { if (event.getTrigger() instanceof Event) { - Event trigger = (Event)event.getTrigger(); - Control focusControl = trigger.display.getFocusControl(); - focusControl.getParent().forceFocus(); + try { + Event trigger = (Event)event.getTrigger(); + Control focusControl = trigger.display.getFocusControl(); + Composite parent = focusControl.getParent(); + parent.forceFocus(); + } catch (NullPointerException e) { + // Consecutive getParent.forceFocus()'s cause NullPointerException, + // if there has been no action on UI between. I don't see many cases + // where this is an issue and the NPEs are not critical and are thus + // ignored. + } } } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SaveResultJob.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SaveResultJob.java index 859bbc7a..04beb102 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SaveResultJob.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SaveResultJob.java @@ -19,12 +19,14 @@ import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.WriteResultRequest; +import org.simantics.db.common.request.WriteRequest; 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.layer0.utils.direct.GraphUtils; import org.simantics.sysdyn.SysdynResource; @@ -40,6 +42,7 @@ public class SaveResultJob extends Job { private final SysdynExperiment experiment; private final Session session; private IProgressMonitor monitor; + private File file; public SaveResultJob(final SysdynExperiment experiment, Session session, final SysdynResult result) { super("Save Result"); @@ -54,10 +57,11 @@ public class SaveResultJob extends Job { int resultItemsNumber = sysdynResult.numberOfVariables(); monitor.beginTask("Save result", resultItemsNumber * 2); try { - File file = session.syncRequest(new WriteResultRequest() { + // Create result file + file = session.syncRequest(new Read() { @Override - public File perform(WriteGraph graph) throws DatabaseException { + public File perform(ReadGraph graph) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); Resource model = graph.getSingleObject(experiment.getResource(), l0.PartOf); Resource project = graph.getSingleObject(model, l0.PartOf); @@ -67,7 +71,21 @@ public class SaveResultJob extends Job { File projectRoot = new File(root, projectName); if(!projectRoot.isDirectory()) projectRoot.mkdir(); File file = new File( projectRoot, UUID.randomUUID().toString() + ".dbb"); + return file; + } + }); + // Start the saving operation + Thread saveThread = experiment.getSaveThread(sysdynResult, file, SaveResultJob.this.monitor); + saveThread.run(); + + // Add the result under the experiment within the model browser. + session.syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + Resource model = graph.getSingleObject(experiment.getResource(), l0.PartOf); String name = NameUtils.findFreshName(graph, "Result", model, l0.ConsistsOf, "%s%d"); SysdynResource sr = SysdynResource.getInstance(graph); @@ -77,13 +95,9 @@ public class SaveResultJob extends Job { l0.PartOf, model, sr.Result_resultFile, file.getAbsolutePath()); graph.claim(experiment.getResource(), sr.Experiment_result, res); - - return file; } }); - // Start the saving operation - Thread saveThread = experiment.getSaveThread(sysdynResult, file, SaveResultJob.this.monitor); - saveThread.run(); + } catch (DatabaseException e) { e.printStackTrace(); } catch (Exception e) { -- 2.47.1