]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/function/All.java
Option for exporting tg and pgraph with sharedlibrary
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / function / All.java
1 package org.simantics.modeling.ui.function;
2
3 import java.util.Collection;
4 import java.util.HashSet;
5 import java.util.Set;
6 import java.util.function.Consumer;
7
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;
22
23 public class All {
24
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);
28     }
29
30     @SCLValue(type = "ReadGraph -> Resource -> Variable -> Resource")
31     public static Resource projectSelection(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
32         return Simantics.getProjectResource();
33     }
34     
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);
39     }
40
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();
44
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.");
48             return null;
49         }
50         final Collection<NamedResource> namedTypicals = TypicalUtil.toNamedResources(session, typicals);
51
52         return TypicalUtil.syncExec(procedure ->  {
53             NewTypicalDiagramInstance.asyncQueryFromList(namedTypicals, selectedTypical -> {
54                 try {
55                     if (selectedTypical != null) {
56                         session.syncRequest(TypicalUtil.instantiateTemplate(target, selectedTypical, instance -> {
57                             try {
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);
66                             }
67                         }));
68                     }
69                 } catch (Throwable e) {
70                     procedure.exception(e);
71                 } finally {
72                     procedure.execute(null);
73                 }
74             });
75         });
76     }
77
78 }