From 6a8b529bc5ceff5f1455968c03e65d140ffb6e39 Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Thu, 19 Dec 2019 14:27:14 +0200 Subject: [PATCH] Fixed SCLConsoleView.setCurrentImports to be performed in a Job The job is marked as a user job so if it starts taking time or gets blocked due to DB, it should get shown to the user as an ongoing job. gitlab #164 Change-Id: If2dc1389ceb4b3f868f27b17c59b0e59233b9734 --- .../scl/compiler/commands/CommandSession.java | 6 ++- .../simantics/scl/ui/console/Messages.java | 1 + .../scl/ui/console/SCLConsoleView.java | 54 +++++++++++++++++-- .../scl/ui/console/messages.properties | 1 + 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java index 00a7714a6..06853c898 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java @@ -58,6 +58,8 @@ import org.simantics.scl.runtime.reporting.DelegatingSCLReportingHandler; import org.simantics.scl.runtime.reporting.SCLReporting; import org.simantics.scl.runtime.reporting.SCLReportingHandler; import org.simantics.scl.runtime.tuple.Tuple0; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import gnu.trove.map.hash.THashMap; import gnu.trove.procedure.TObjectProcedure; @@ -66,6 +68,8 @@ import gnu.trove.set.hash.THashSet; public class CommandSession { + private static final Logger LOGGER = LoggerFactory.getLogger(CommandSession.class); + ModuleRepository moduleRepository; SCLReportingHandler defaultHandler; @@ -147,7 +151,7 @@ public class CommandSession { } } } catch(RuntimeException e) { - e.printStackTrace(); + LOGGER.error("updateRuntimeEnvironment(clearErrorsFlags={}) failed", clearErrorsFlags, e); throw e; } valueToStringConverter = new ValueToStringConverter(runtimeEnvironment); diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/Messages.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/Messages.java index f2ecd0fd6..ecbb2be22 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/Messages.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/Messages.java @@ -11,6 +11,7 @@ public class Messages extends NLS { public static String SCLConsoleView_RefreshCompleted; public static String SCLConsoleView_RefreshModules; public static String SCLConsoleView_RunTests; + public static String SCLConsoleView_SetImports; static { // initialize resource bundle NLS.initializeMessages(BUNDLE_NAME, Messages.class); diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsoleView.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsoleView.java index bab62144b..516b267f4 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsoleView.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsoleView.java @@ -6,8 +6,13 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; +import org.eclipse.core.runtime.IProgressMonitor; +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.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IAction; @@ -28,6 +33,7 @@ import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.MenuItem; +import org.eclipse.ui.PlatformUI; import org.eclipse.ui.part.ViewPart; import org.eclipse.ui.preferences.ScopedPreferenceStore; import org.simantics.scl.compiler.commands.CommandSession; @@ -110,13 +116,54 @@ public class SCLConsoleView extends ViewPart { console.getSession().setImportEntries(entries); } + AtomicReference> assignedImports = new AtomicReference<>(); + + private class SetImportsJob extends Job { + + public SetImportsJob() { + super(Messages.SCLConsoleView_SetImports); + setUser(true); + } + + @Override + protected IStatus run(IProgressMonitor monitor) { + try { + ArrayList entries = assignedImports.getAndSet(null); + if (entries != null) + setCurrentImports(entries); + return Status.OK_STATUS; + } finally { + monitor.done(); + } + } + + @Override + public boolean shouldSchedule() { + return PlatformUI.isWorkbenchRunning(); + } + + @Override + public boolean shouldRun() { + return PlatformUI.isWorkbenchRunning(); + } + + } + + SetImportsJob setImportsJob = new SetImportsJob(); + + private void scheduleSetCurrentImports(ArrayList entries) { + boolean scheduled = assignedImports.getAndSet(entries) != null; + if (!scheduled) + setImportsJob.schedule(); + } + private void manageImports() { ManageImportsDialog dialog = new ManageImportsDialog( getSite().getShell(), getCurrentImports()); if(dialog.open() == Dialog.OK) { writeImportPreferences(dialog.getImports()); - setCurrentImports(dialog.getImports()); + scheduleSetCurrentImports(dialog.getImports()); } } @@ -263,10 +310,11 @@ public class SCLConsoleView extends ViewPart { toolBarManager.update(true); setRefreshAutomatically(store.getBoolean(REFRESH_AUTOMATICALLY), false); + addScriptDropSupport(console); + // Do this after the actions and SCLConsoleListener are // registered because it can cause output to the console. - setCurrentImports(readImportPreferences()); - addScriptDropSupport(console); + scheduleSetCurrentImports(readImportPreferences()); } private class ScriptRunningDropTarget extends DropTargetAdapter { diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/messages.properties b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/messages.properties index 7db5b8b03..fad5ea9f2 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/messages.properties +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/messages.properties @@ -5,3 +5,4 @@ SCLConsoleView_RefreshAutomatically=Refresh Automatically SCLConsoleView_RefreshCompleted=refresh completed\n SCLConsoleView_RefreshModules=Refresh Modules SCLConsoleView_RunTests=Run Tests +SCLConsoleView_SetImports=Set SCL Console imports -- 2.43.2