1 package org.simantics.diagram.profile.view;
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;
16 public class CreateEntryAction extends ModelledActionImpl<Resource> {
18 public CreateEntryAction(Resource configuration) {
22 public void run(final Resource runtimeDiagram) {
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;
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;
34 Simantics.getSession().syncRequest(new WriteRequest() {
37 public void perform(WriteGraph graph) throws DatabaseException {
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;
44 Resource model = graph.getPossibleResource(modelURI);
45 if(model == null) return;
47 String name = NameUtils.findFreshEscapedName(graph, "Entry", model, L0.ConsistsOf);
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);
60 } catch (DatabaseException e) {