Fixed SCLConsoleView.setCurrentImports to be performed in a Job 29/3729/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Thu, 19 Dec 2019 12:27:14 +0000 (14:27 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Thu, 19 Dec 2019 12:27:14 +0000 (14:27 +0200)
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

bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java
bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/Messages.java
bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsoleView.java
bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/messages.properties

index 00a7714a63640a3f2ae31bd29df0625c7f455ff4..06853c898a7fe30895cfce8d4e3f0ff5abfb0f21 100644 (file)
@@ -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);
index f2ecd0fd638fbc25a13af7e1e9da8ba161dedd6a..ecbb2be227bd4052af8ba3aeb10ec2a4e152d3f0 100644 (file)
@@ -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);
index bab62144b5e514ffbcdd6f6984b650e7875a4e76..516b267f40c5030e829ea810ff0fb45f87ee3cc5 100644 (file)
@@ -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<ArrayList<CommandSessionImportEntry>> assignedImports = new AtomicReference<>();
+    
+    private class SetImportsJob extends Job {
+
+        public SetImportsJob() {
+            super(Messages.SCLConsoleView_SetImports);
+            setUser(true);
+        }
+
+        @Override
+        protected IStatus run(IProgressMonitor monitor) {
+            try {
+                ArrayList<CommandSessionImportEntry> 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<CommandSessionImportEntry> 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 {
index 7db5b8b03cb83547d2fc99bfbd8b28bd7c048869..fad5ea9f24c6570c1303d349c142724b638787fe 100644 (file)
@@ -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