1 package org.simantics.modeling.ui;
3 import java.util.HashMap;
5 import java.util.function.Consumer;
7 import org.eclipse.core.commands.ExecutionEvent;
8 import org.eclipse.jface.dialogs.IDialogSettings;
9 import org.eclipse.jface.resource.ImageDescriptor;
10 import org.eclipse.jface.window.Window;
11 import org.eclipse.swt.widgets.Display;
12 import org.eclipse.swt.widgets.Shell;
13 import org.eclipse.ui.IEditorPart;
14 import org.eclipse.ui.PlatformUI;
15 import org.eclipse.ui.handlers.HandlerUtil;
16 import org.simantics.Simantics;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.databoard.util.URIStringUtils;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.request.ReadRequest;
23 import org.simantics.db.common.request.WriteRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.adapter.Instances;
26 import org.simantics.g2d.canvas.ICanvasContext;
27 import org.simantics.layer0.Layer0;
28 import org.simantics.modeling.ModelingResources;
29 import org.simantics.modeling.ui.diagramEditor.DiagramEditor;
30 import org.simantics.modeling.ui.diagramEditor.DiagramViewer;
31 import org.simantics.ui.workbench.dialogs.ResourceSelectionDialog3;
32 import org.simantics.utils.datastructures.Pair;
34 public class ModelingUIUtils {
36 public static DiagramViewer tryGetDiagramViewer(ExecutionEvent event) {
38 IEditorPart editorPart = HandlerUtil.getActiveEditor(event);
39 if(editorPart == null) return null;
40 if(editorPart instanceof DiagramEditor) {
41 return (DiagramViewer)editorPart.getAdapter(DiagramViewer.class);
48 public static ICanvasContext tryGetCanvasContext(ExecutionEvent event) {
50 DiagramViewer viewer = tryGetDiagramViewer(event);
51 if(viewer != null) return (ICanvasContext)viewer.getAdapter(ICanvasContext.class);
56 protected static void findSCLQueryTypes(ReadGraph graph, Resource indexRoot, Map<Resource, Pair<String, ImageDescriptor>> map) throws DatabaseException {
58 Layer0 L0 = Layer0.getInstance(graph);
59 ModelingResources MOD = ModelingResources.getInstance(graph);
61 Instances query = graph.adapt(MOD.SCLQueryType, Instances.class);
63 String modelURI = graph.getURI(indexRoot);
65 for(Resource _res : query.find(graph, indexRoot)) {
67 // Don't allow instantiation of abstract query types.
68 if (graph.hasStatement(_res, L0.Abstract))
71 Resource res = graph.getPossibleObject(_res, L0.HasRange_Inverse);
76 String name = graph.getPossibleRelatedValue(_res, L0.HasName, Bindings.STRING);
79 String label = graph.getPossibleRelatedValue2(_res, L0.HasLabel, Bindings.STRING);
81 if (label != null && !name.equals(label)) {
82 name = label + " (" + name + ")"; //$NON-NLS-1$ //$NON-NLS-2$
85 Resource parent = graph.getPossibleObject(_res, L0.PartOf);
86 if(parent == null) continue;
88 String parentURI = graph.getURI(parent);
89 if(parentURI.startsWith(modelURI)) {
90 parentURI = parentURI.substring(modelURI.length());
91 if(parentURI.startsWith("/")) parentURI = parentURI.substring(1); //$NON-NLS-1$
94 name = name + " - " + URIStringUtils.unescape(parentURI); //$NON-NLS-1$
96 map.put(_res, new Pair<String, ImageDescriptor>(name, null));
102 String name = graph.getPossibleRelatedValue(res, L0.HasName, Bindings.STRING);
105 String label = graph.getPossibleRelatedValue2(res, L0.HasLabel, Bindings.STRING);
107 if (label != null && !name.equals(label)) {
108 name = label + " (" + name + ")"; //$NON-NLS-1$ //$NON-NLS-2$
111 Resource parent = graph.getPossibleObject(_res, L0.PartOf);
112 if(parent == null) continue;
114 String parentURI = graph.getURI(parent);
115 if(parentURI.startsWith(modelURI)) {
116 parentURI = parentURI.substring(modelURI.length());
117 if(parentURI.startsWith("/")) parentURI = parentURI.substring(1); //$NON-NLS-1$
120 name = name + " - " + URIStringUtils.unescape(parentURI); //$NON-NLS-1$
122 map.put(_res, new Pair<String, ImageDescriptor>(name, null));
130 public static void queryUserSelectedQueryType(
131 final Map<Resource, Pair<String, ImageDescriptor>> map,
132 final Consumer<Resource> selectionCallback)
134 Display.getDefault().asyncExec(new Runnable() {
137 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
138 ResourceSelectionDialog3<Resource> dialog = new ResourceSelectionDialog3<Resource>(shell, map, Messages.ModelingUIUtils_SelectQueryType) {
140 protected IDialogSettings getBaseDialogSettings() {
141 return Activator.getDefault().getDialogSettings();
144 if (dialog.open() == Window.OK) {
145 Object[] result = dialog.getResult();
146 if (result != null && result.length == 1) {
147 final Resource res = (Resource)result[0];
148 selectionCallback.accept(res);
155 public static void newSCLQuery(final Resource parent, final Resource indexRoot) throws DatabaseException {
156 if (indexRoot == null)
158 Simantics.getSession().syncRequest(new ReadRequest() {
160 public void run(ReadGraph graph) throws DatabaseException {
162 final Map<Resource, Pair<String, ImageDescriptor>> map = new HashMap<Resource, Pair<String,ImageDescriptor>>();
163 findSCLQueryTypes(graph, indexRoot, map);
164 queryUserSelectedQueryType(map, selected -> {
165 Simantics.getSession().async(new WriteRequest() {
167 public void perform(WriteGraph g) throws DatabaseException {
169 Simantics.applySCL("Simantics/Query", "createSCLQueryDefault", g, parent, selected); //$NON-NLS-1$ //$NON-NLS-2$