]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenSCLDefinition.java
Fix annoying editor focus jumping with Ctrl+Shift+D/M
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / editor2 / OpenSCLDefinition.java
index bc7f9feac702b7951de422c3a254469eeea78cac..33fb30d4be124956e9b8ed76f3e53f90e9e7c010 100644 (file)
@@ -3,15 +3,20 @@ package org.simantics.scl.ui.editor2;
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
 import org.simantics.scl.compiler.errors.Locations;
 import org.simantics.scl.ui.browser.SCLDefinitionSelectionDialog;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class OpenSCLDefinition extends AbstractHandler {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(OpenSCLDefinition.class);
+
     @Override
     public Object execute(ExecutionEvent event) throws ExecutionException {
         SCLDefinitionSelectionDialog dialog = new SCLDefinitionSelectionDialog(
@@ -19,30 +24,36 @@ public class OpenSCLDefinition extends AbstractHandler {
         if(dialog.open() == SCLDefinitionSelectionDialog.OK) {
             SCLValue value = (SCLValue)dialog.getFirstResult();
             if(value != null)
-                openDefinition(value);
+                scheduleOpenDefinition(value.getName().module, value.definitionLocation);
         }
         return null;
     }
-    
+
     public static void openDefinition(SCLValue value) {
         openDefinition(value.getName().module, value.definitionLocation);
     }
-    
-    public static void openDefinition(String moduleName, long location) {
-        IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
-        if(page == null)
-            return;
-        SCLModuleEditorInput input = new StandardSCLModuleEditorInput(moduleName);
-        try {
-            SCLModuleEditor2 editor = (SCLModuleEditor2)page.openEditor(input, "org.simantics.scl.ui.editor2");
-            if(location != Locations.NO_LOCATION) {
-                int begin = Locations.beginOf(location);
-                int end = Locations.endOf(location);
-                editor.selectAndReveal(begin, end-begin);
+
+    public static Runnable openDefinition(String moduleName, long location) {
+        return () -> {
+            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+            if(page == null)
+                return;
+            SCLModuleEditorInput input = new StandardSCLModuleEditorInput(moduleName);
+            try {
+                SCLModuleEditor2 editor = (SCLModuleEditor2)page.openEditor(input, "org.simantics.scl.ui.editor2");
+                if(location != Locations.NO_LOCATION) {
+                    int begin = Locations.beginOf(location);
+                    int end = Locations.endOf(location);
+                    editor.selectAndReveal(begin, end-begin);
+                }
+            } catch (PartInitException e) {
+                LOGGER.error("", e);
             }
-        } catch (PartInitException e) {
-            e.printStackTrace();
-        }
+        };
+    }
+    
+    public static void scheduleOpenDefinition(String moduleName, long location) {
+        Display.getCurrent().asyncExec(openDefinition(moduleName, location));
     }
 
 }