Removed unnecessary dependencies on org.apache.log4j
[simantics/platform.git] / bundles / org.simantics.diagram.profile / src / org / simantics / diagram / profile / view / CreateStyleAction.java
1 package org.simantics.diagram.profile.view;
2
3 import org.simantics.Simantics;
4 import org.simantics.browsing.ui.swt.ModelledActionImpl;
5 import org.simantics.db.Resource;
6 import org.simantics.db.WriteGraph;
7 import org.simantics.db.common.request.WriteRequest;
8 import org.simantics.db.common.utils.NameUtils;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.diagram.stubs.DiagramResource;
11 import org.simantics.layer0.Layer0;
12 import org.simantics.operation.Layer0X;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 public class CreateStyleAction extends ModelledActionImpl<Resource> {
17
18         private static final Logger LOGGER = LoggerFactory.getLogger(CreateStyleAction.class);
19
20         public CreateStyleAction(Resource configuration) {
21                 super(configuration);
22         }
23         
24         public void run(final Resource runtimeDiagram) {
25
26                 try {
27                 
28                         Simantics.getSession().syncRequest(new WriteRequest() {
29         
30                                 @Override
31                                 public void perform(WriteGraph graph) throws DatabaseException {
32         
33                                         Layer0 L0 = Layer0.getInstance(graph);
34                                         Layer0X L0X = Layer0X.getInstance(graph);
35                                         DiagramResource DIA = DiagramResource.getInstance(graph);
36                                 String modelURI = graph.getPossibleRelatedValue((Resource)runtimeDiagram, DIA.RuntimeDiagram_HasModelURI);
37                                 if (modelURI == null) return;
38                                 
39                                 Resource model = graph.getPossibleResource(modelURI);
40                                 if(model == null) return;
41                                 
42                                 String name = NameUtils.findFreshEscapedName(graph, "Style", model, L0.ConsistsOf);
43                                 
44                                 Resource newStyle = graph.newResource();
45                                 graph.claim(newStyle, L0.InstanceOf, DIA.SCLTextStyle);
46                                 graph.claimLiteral(newStyle, L0X.HasExpression, "");
47                                 graph.claimLiteral(newStyle, L0.HasName, name);
48                                 graph.claim(model, L0.ConsistsOf, newStyle);
49         
50                                 }
51         
52                         });
53                 
54                 } catch (DatabaseException e) {
55                         LOGGER.error("CreateStyleAction failed", e);
56                 }
57
58         }
59
60 }