]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsoleView.java
Fixed SCLConsoleView.setCurrentImports to be performed in a Job
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / console / SCLConsoleView.java
index 67de0b2ffe9e02571d75ec114c22416498e51274..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;
@@ -43,11 +49,11 @@ import org.simantics.scl.ui.tests.SCLTestsDialog;
 
 public class SCLConsoleView extends ViewPart {
 
-    public static final String PLUGIN_ID = "org.simantics.scl.ui";
-    public static final String IMPORTS = "imports";
-    public static final String REFRESH_AUTOMATICALLY = "refresh-automatically";
-    public static final String SEPARATOR = ";";
-    public static final String DISABLED_TAG = "[DISABLED]";
+    public static final String PLUGIN_ID = "org.simantics.scl.ui"; //$NON-NLS-1$
+    public static final String IMPORTS = "imports"; //$NON-NLS-1$
+    public static final String REFRESH_AUTOMATICALLY = "refresh-automatically"; //$NON-NLS-1$
+    public static final String SEPARATOR = ";"; //$NON-NLS-1$
+    public static final String DISABLED_TAG = "[DISABLED]"; //$NON-NLS-1$
     
     IPersistentPreferenceStore store;
     SCLConsole console;
@@ -67,10 +73,10 @@ public class SCLConsoleView extends ViewPart {
                 disabled = true;
                 entryString = entryString.substring(DISABLED_TAG.length());
             }
-            String[] parts = entryString.split("=");
+            String[] parts = entryString.split("="); //$NON-NLS-1$
             CommandSessionImportEntry entry;
             if(parts.length == 1)
-                entry = new CommandSessionImportEntry(parts[0], "", true);
+                entry = new CommandSessionImportEntry(parts[0], "", true); //$NON-NLS-1$
             else
                 entry = new CommandSessionImportEntry(parts[1], parts[0], true);
             entry.disabled = disabled;
@@ -93,7 +99,7 @@ public class SCLConsoleView extends ViewPart {
                     b.append(DISABLED_TAG);
                 if(!entry.localName.isEmpty()) {
                     b.append(entry.localName);
-                    b.append("=");
+                    b.append("="); //$NON-NLS-1$
                 }
                 b.append(entry.moduleName);
             }
@@ -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());
         }
     }
     
@@ -131,8 +178,8 @@ public class SCLConsoleView extends ViewPart {
                 try {
                     // Bit of a haxx solution to get around a deadlock caused by simply
                     // running the test with test.run()
-                    console.execute("import \"Commands/Tests\"");
-                    console.execute("runByName \"" + test.getName() + "\"");
+                    console.execute("import \"Commands/Tests\""); //$NON-NLS-1$
+                    console.execute("runByName \"" + test.getName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
 //                    test.run();
                 } catch (Exception e) {
                     e.printStackTrace();
@@ -200,9 +247,9 @@ public class SCLConsoleView extends ViewPart {
         });
         
         // Refresh action
-        toolBarManager.add(new Action("Refresh modules", IAction.AS_DROP_DOWN_MENU) {
+        toolBarManager.add(new Action(Messages.SCLConsoleView_RefreshModules, IAction.AS_DROP_DOWN_MENU) {
             {
-                setImageDescriptor(Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/arrow_refresh.png"));
+                setImageDescriptor(Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/arrow_refresh.png")); //$NON-NLS-1$ //$NON-NLS-2$
                 setMenuCreator(new IMenuCreator() {
                     Menu menu;
                     @Override
@@ -215,7 +262,7 @@ public class SCLConsoleView extends ViewPart {
                         if(menu == null) {
                             menu = new Menu(parent);
                             refreshAutomaticallyItem = new MenuItem(menu, SWT.CHECK);
-                            refreshAutomaticallyItem.setText("Refresh automatically");
+                            refreshAutomaticallyItem.setText(Messages.SCLConsoleView_RefreshAutomatically);
                             refreshAutomaticallyItem.setSelection(refreshAutomatically);
                             refreshAutomaticallyItem.addSelectionListener(new SelectionAdapter() {
                                 @Override
@@ -238,11 +285,11 @@ public class SCLConsoleView extends ViewPart {
             public void run() {
                 console.getSession().getModuleRepository().getSourceRepository().checkUpdates();
                 console.getSession().updateRuntimeEnvironment(true);
-                console.appendOutput("refresh completed\n", console.greenColor, null);
+                console.appendOutput(Messages.SCLConsoleView_RefreshCompleted, console.greenColor, null);
             }
         });
-        toolBarManager.add(new Action("Manage imports",
-                Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/configure_imports.png")) {
+        toolBarManager.add(new Action(Messages.SCLConsoleView_ManageImports,
+                Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/configure_imports.png")) { //$NON-NLS-1$ //$NON-NLS-2$
             @Override
             public void run() {
                 manageImports();
@@ -251,8 +298,8 @@ public class SCLConsoleView extends ViewPart {
 
         // Show action for running SCL tests if in development mode
         if (Platform.inDevelopmentMode()) {
-            toolBarManager.add(new Action("Run tests",
-                    Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/run_tests.png")) {
+            toolBarManager.add(new Action(Messages.SCLConsoleView_RunTests,
+                    Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/run_tests.png")) { //$NON-NLS-1$ //$NON-NLS-2$
                 @Override
                 public void run() {
                     sclTestDialog();
@@ -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 {
@@ -292,7 +340,7 @@ public class SCLConsoleView extends ViewPart {
                 for (String file : files) {
                     Path p = Paths.get(file).toAbsolutePath();
                     if (isScriptFile(p)) {
-                        console.execute("runFromFile \"" + p.toString().replace('\\', '/') + "\"");
+                        console.execute("runFromFile \"" + p.toString().replace('\\', '/') + "\""); //$NON-NLS-1$ //$NON-NLS-2$
                     }
                 }
             }