]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/OpenSCLDefinition.java
33fb30d4be124956e9b8ed76f3e53f90e9e7c010
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / editor2 / OpenSCLDefinition.java
1 package org.simantics.scl.ui.editor2;
2
3 import org.eclipse.core.commands.AbstractHandler;
4 import org.eclipse.core.commands.ExecutionEvent;
5 import org.eclipse.core.commands.ExecutionException;
6 import org.eclipse.swt.widgets.Display;
7 import org.eclipse.ui.IWorkbenchPage;
8 import org.eclipse.ui.PartInitException;
9 import org.eclipse.ui.PlatformUI;
10 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
11 import org.simantics.scl.compiler.errors.Locations;
12 import org.simantics.scl.ui.browser.SCLDefinitionSelectionDialog;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 public class OpenSCLDefinition extends AbstractHandler {
17
18     private static final Logger LOGGER = LoggerFactory.getLogger(OpenSCLDefinition.class);
19
20     @Override
21     public Object execute(ExecutionEvent event) throws ExecutionException {
22         SCLDefinitionSelectionDialog dialog = new SCLDefinitionSelectionDialog(
23                 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
24         if(dialog.open() == SCLDefinitionSelectionDialog.OK) {
25             SCLValue value = (SCLValue)dialog.getFirstResult();
26             if(value != null)
27                 scheduleOpenDefinition(value.getName().module, value.definitionLocation);
28         }
29         return null;
30     }
31
32     public static void openDefinition(SCLValue value) {
33         openDefinition(value.getName().module, value.definitionLocation);
34     }
35
36     public static Runnable openDefinition(String moduleName, long location) {
37         return () -> {
38             IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
39             if(page == null)
40                 return;
41             SCLModuleEditorInput input = new StandardSCLModuleEditorInput(moduleName);
42             try {
43                 SCLModuleEditor2 editor = (SCLModuleEditor2)page.openEditor(input, "org.simantics.scl.ui.editor2");
44                 if(location != Locations.NO_LOCATION) {
45                     int begin = Locations.beginOf(location);
46                     int end = Locations.endOf(location);
47                     editor.selectAndReveal(begin, end-begin);
48                 }
49             } catch (PartInitException e) {
50                 LOGGER.error("", e);
51             }
52         };
53     }
54     
55     public static void scheduleOpenDefinition(String moduleName, long location) {
56         Display.getCurrent().asyncExec(openDefinition(moduleName, location));
57     }
58
59 }