]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram.profile/src/org/simantics/diagram/profile/view/CreateEntryAction.java
Better support for ontological profiles
[simantics/platform.git] / bundles / org.simantics.diagram.profile / src / org / simantics / diagram / profile / view / CreateEntryAction.java
1 package org.simantics.diagram.profile.view;
2
3 import org.eclipse.jface.viewers.ISelection;
4 import org.simantics.Simantics;
5 import org.simantics.browsing.ui.swt.ModelledActionImpl;
6 import org.simantics.db.Resource;
7 import org.simantics.db.WriteGraph;
8 import org.simantics.db.common.request.WriteRequest;
9 import org.simantics.db.common.utils.NameUtils;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.SelectionHints;
12 import org.simantics.diagram.stubs.DiagramResource;
13 import org.simantics.layer0.Layer0;
14 import org.simantics.utils.ui.ISelectionUtils;
15
16 public class CreateEntryAction extends ModelledActionImpl<Resource> {
17
18         public CreateEntryAction(Resource configuration) {
19                 super(configuration);
20         }
21         
22         public void run(final Resource runtimeDiagram) {
23
24                 ISelection groupSelection = getParameter(DiagramResource.URIs.ProfilesView_NewEntryGroup);
25         final Resource selectedGroup = ISelectionUtils.getSinglePossibleKey(groupSelection, SelectionHints.KEY_MAIN, Resource.class);
26         if(selectedGroup == null) return;
27
28                 ISelection styleSelection = getParameter(DiagramResource.URIs.ProfilesView_NewEntryStyle);
29         final Resource selectedStyle = ISelectionUtils.getSinglePossibleKey(styleSelection, SelectionHints.KEY_MAIN, Resource.class);
30         if(selectedStyle == null) return;
31
32         try {
33                 
34                         Simantics.getSession().syncRequest(new WriteRequest() {
35                                 
36                                 @Override
37                                 public void perform(WriteGraph graph) throws DatabaseException {
38         
39                                         Layer0 L0 = Layer0.getInstance(graph);
40                                         DiagramResource DIA = DiagramResource.getInstance(graph);
41                                 String modelURI = graph.getPossibleRelatedValue((Resource)runtimeDiagram, DIA.RuntimeDiagram_HasModelURI);
42                                 if (modelURI == null) return;
43                                 
44                                 Resource model = graph.getPossibleResource(modelURI);
45                                 if(model == null) return;
46                                 
47                                 String name = NameUtils.findFreshEscapedName(graph, "Entry", model, L0.ConsistsOf);
48                                 
49                                 Resource newGroup = graph.newResource();
50                                 graph.claim(newGroup, L0.InstanceOf, DIA.GroupStyleProfileEntry);
51                                 graph.claimLiteral(newGroup, L0.HasName, name);
52                                 graph.claim(newGroup, DIA.ProfileEntry_HasGroup, selectedGroup);
53                                 graph.claim(newGroup, DIA.ProfileEntry_HasStyle, selectedStyle);
54                                 graph.claim(model, L0.ConsistsOf, newGroup);
55         
56                                 }
57         
58                         });
59                 
60         } catch (DatabaseException e) {
61                 e.printStackTrace();
62         }
63
64         }
65
66 }