]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/ModelingUIUtils.java
Configurable connection crossing styles
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / ModelingUIUtils.java
1 package org.simantics.modeling.ui;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.function.Consumer;
6
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;
33
34 public class ModelingUIUtils {
35
36     public static DiagramViewer tryGetDiagramViewer(ExecutionEvent event) {
37
38         IEditorPart editorPart = HandlerUtil.getActiveEditor(event);
39         if(editorPart == null) return null;
40         if(editorPart instanceof DiagramEditor) {
41                 return (DiagramViewer)editorPart.getAdapter(DiagramViewer.class);
42         }
43         
44         return null;
45         
46     }
47     
48     public static ICanvasContext tryGetCanvasContext(ExecutionEvent event) {
49         
50         DiagramViewer viewer = tryGetDiagramViewer(event);
51         if(viewer != null) return (ICanvasContext)viewer.getAdapter(ICanvasContext.class);
52         else return null;
53         
54     }
55         
56         protected static void findSCLQueryTypes(ReadGraph graph, Resource indexRoot, Map<Resource, Pair<String, ImageDescriptor>> map) throws DatabaseException {
57
58                 Layer0 L0 = Layer0.getInstance(graph);
59                 ModelingResources MOD = ModelingResources.getInstance(graph);
60
61                 Instances query = graph.adapt(MOD.SCLQueryType, Instances.class);
62
63                 String modelURI = graph.getURI(indexRoot);
64                 
65                 for(Resource _res : query.find(graph, indexRoot)) {
66                         
67                         // Don't allow instantiation of abstract query types.
68                         if (graph.hasStatement(_res, L0.Abstract))
69                                 continue;
70
71                         Resource res = graph.getPossibleObject(_res, L0.HasRange_Inverse);
72                         if(res == null) {
73                                 
74                                 // Entry type
75
76                                 String name = graph.getPossibleRelatedValue(_res, L0.HasName, Bindings.STRING);
77                                 if (name == null)
78                                         continue;
79                                 String label = graph.getPossibleRelatedValue2(_res, L0.HasLabel, Bindings.STRING);
80
81                                 if (label != null && !name.equals(label)) {
82                                         name = label + " (" + name + ")"; //$NON-NLS-1$ //$NON-NLS-2$
83                                 }
84
85                                 Resource parent = graph.getPossibleObject(_res, L0.PartOf);
86                                 if(parent == null) continue;
87
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$
92                                 }
93
94                                 name = name + " - " + URIStringUtils.unescape(parentURI); //$NON-NLS-1$
95
96                                 map.put(_res, new Pair<String, ImageDescriptor>(name, null));
97                                 
98                         } else {
99                                 
100                                 // Property type
101                         
102                                 String name = graph.getPossibleRelatedValue(res, L0.HasName, Bindings.STRING);
103                                 if (name == null)
104                                         continue;
105                                 String label = graph.getPossibleRelatedValue2(res, L0.HasLabel, Bindings.STRING);
106
107                                 if (label != null && !name.equals(label)) {
108                                         name = label + " (" + name + ")"; //$NON-NLS-1$ //$NON-NLS-2$
109                                 }
110
111                                 Resource parent = graph.getPossibleObject(_res, L0.PartOf);
112                                 if(parent == null) continue;
113
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$
118                                 }
119
120                                 name = name + " - " + URIStringUtils.unescape(parentURI); //$NON-NLS-1$
121
122                                 map.put(_res, new Pair<String, ImageDescriptor>(name, null));
123                         
124                         }
125                         
126                 }
127
128         }
129         
130         public static void queryUserSelectedQueryType(
131                         final Map<Resource, Pair<String, ImageDescriptor>> map,
132                         final Consumer<Resource> selectionCallback)
133         {
134                 Display.getDefault().asyncExec(new Runnable() {
135                         @Override
136                         public void run() {
137                                 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
138                                 ResourceSelectionDialog3<Resource> dialog = new ResourceSelectionDialog3<Resource>(shell, map, Messages.ModelingUIUtils_SelectQueryType) {
139                                         @Override
140                                         protected IDialogSettings getBaseDialogSettings() {
141                                                 return Activator.getDefault().getDialogSettings();
142                                         }
143                                 };
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);
149                                         }
150                                 }
151                         }
152                 });
153         }
154         
155         public static void newSCLQuery(final Resource parent, final Resource indexRoot) throws DatabaseException {
156         if (indexRoot == null)
157             return;
158         Simantics.getSession().syncRequest(new ReadRequest() {
159             @Override
160             public void run(ReadGraph graph) throws DatabaseException {
161                 
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() {
166                         @Override
167                         public void perform(WriteGraph g) throws DatabaseException {
168                             g.markUndoPoint();
169                             Simantics.applySCL("Simantics/Query", "createSCLQueryDefault", g, parent, selected); //$NON-NLS-1$ //$NON-NLS-2$
170                         }
171                     });
172                 });
173             }
174         });
175     }
176     
177 }