1 package org.simantics.modeling.ui.function;
3 import java.util.Collection;
4 import java.util.HashSet;
6 import java.util.function.Consumer;
8 import org.simantics.Simantics;
9 import org.simantics.browsing.ui.StatePersistor;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.Session;
13 import org.simantics.db.common.NamedResource;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.variable.Variable;
16 import org.simantics.modeling.typicals.TypicalUtil;
17 import org.simantics.modeling.ui.typicals.NewTypicalDiagramInstance;
18 import org.simantics.scenegraph.loader.ScenegraphLoaderUtils;
19 import org.simantics.scl.reflection.annotations.SCLValue;
20 import org.simantics.ui.workbench.action.DefaultActions;
21 import org.simantics.utils.ui.dialogs.ShowMessage;
25 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
26 public static Variable singleVariableSelection(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
27 return ScenegraphLoaderUtils.getVariableSelection(graph, context);
30 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Resource")
31 public static Resource projectSelection(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
32 return Simantics.getProjectResource();
35 @SCLValue(type = "ReadGraph -> Resource -> Variable -> StatePersistor")
36 public static StatePersistor standardPersistor(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
37 String configurationId = context.getParent(graph).getPossiblePropertyValue(graph, "browseContext");
38 return new StandardPersistor(configurationId);
41 @SCLValue(type = "Resource -> Resource -> Resource")
42 public static Resource standardTypicalInstantiator(final Resource model, final Resource target) throws DatabaseException {
43 final Session session = Simantics.getSession();
45 Collection<Resource> typicals = TypicalUtil.findModelTypicals(session, model);
46 if (typicals.isEmpty()) {
47 ShowMessage.showInformation("No Typical Diagrams", "There are no typical diagrams in the selected model. Cannot instantiate.");
50 final Collection<NamedResource> namedTypicals = TypicalUtil.toNamedResources(session, typicals);
52 return TypicalUtil.syncExec(procedure -> {
53 NewTypicalDiagramInstance.asyncQueryFromList(namedTypicals, selectedTypical -> {
55 if (selectedTypical != null) {
56 session.syncRequest(TypicalUtil.instantiateTemplate(target, selectedTypical, instance -> {
58 Set<Resource> customlyNamed = new HashSet<Resource>();
59 TypicalUtil.applyTypicalModuleNames(instance.first, instance.second, customlyNamed);
60 //TypicalUtil.generateFreshModuleNames(instance.first, instance.second, customlyNamed);
61 procedure.execute(instance.second);
62 // Perform default action on the newly created diagram
63 DefaultActions.asyncPerformDefaultAction(session, instance.second, false, false, true);
64 } catch (DatabaseException e) {
65 procedure.exception(e);
69 } catch (Throwable e) {
70 procedure.exception(e);
72 procedure.execute(null);