]> 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 a7eb4332333cc0cba1a4325c36e69b829c7a7a75..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,24 +33,27 @@ 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;
 import org.simantics.scl.compiler.commands.CommandSessionImportEntry;
 import org.simantics.scl.compiler.commands.SCLConsoleListener;
 import org.simantics.scl.compiler.module.repository.UpdateListener;
 import org.simantics.scl.compiler.testing.TestRunnable;
 import org.simantics.scl.osgi.internal.TestUtils;
+import org.simantics.scl.runtime.reporting.SCLReportingHandler;
 import org.simantics.scl.ui.Activator;
 import org.simantics.scl.ui.imports.internal.ManageImportsDialog;
 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;
@@ -65,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;
@@ -91,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);
             }
@@ -108,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());
         }
     }
     
@@ -129,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();
@@ -168,41 +217,20 @@ public class SCLConsoleView extends ViewPart {
     @Override
     public void createPartControl(Composite parent) {
         store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PLUGIN_ID);
-        this.console = new SCLConsole(parent, SWT.NONE);
+        store.setDefault(REFRESH_AUTOMATICALLY, true);
         
-        setRefreshAutomatically(store.getBoolean(REFRESH_AUTOMATICALLY), false);
-        setCurrentImports(readImportPreferences());
-
-        addScriptDropSupport(console);
+        this.console = new SCLConsole(parent, SWT.NONE);
 
         IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
         
         // Interrupt action
-        final Action interruptAction = new Action("Interrupt current command",
-                Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/stop.png")) {
-            @Override
-            public void run() {
-                console.interruptCurrentCommands();
-            }
-        };
-        interruptAction.setDisabledImageDescriptor(
-                Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/stop_disabled.png"));
-        interruptAction.setEnabled(false);
+        Action interruptAction = ConsoleActions.createInterruptAction(console);
         toolBarManager.add(interruptAction);
         
         // Clear console action
-        final Action clearAction = new Action("Clear console",
-                Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/clear_console.png")) {
-            @Override
-            public void run() {
-                setEnabled(false);
-                console.clear();
-            }
-        };
-        clearAction.setDisabledImageDescriptor(
-                Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/clear_console_disabled.png"));
-        clearAction.setEnabled(false);
+        Action clearAction = ConsoleActions.createClearAction(console);
         toolBarManager.add(clearAction);
+        
         console.addListener(new SCLConsoleListener() {
             @Override
             public void startedExecution() {
@@ -219,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
@@ -234,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
@@ -257,21 +285,21 @@ 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();
             }
         });
-        
+
         // 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();
@@ -280,6 +308,13 @@ 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.
+        scheduleSetCurrentImports(readImportPreferences());
     }
 
     private class ScriptRunningDropTarget extends DropTargetAdapter {
@@ -305,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$
                     }
                 }
             }
@@ -335,4 +370,14 @@ public class SCLConsoleView extends ViewPart {
         console.dispose();
     }
 
+    @SuppressWarnings("unchecked")
+    @Override
+    public <T> T getAdapter(Class<T> adapter) {
+        if (adapter == CommandSession.class)
+            return (T) console.getSession();
+        if (adapter == SCLReportingHandler.class)
+            return (T) console.getHandler();
+        return super.getAdapter(adapter);
+    }
+
 }