]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/NewSubscription.java
Allow simulation stop handling in Simantics/Sequences
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / NewSubscription.java
1 package org.simantics.modeling.ui.actions;
2
3 import java.util.UUID;
4
5 import org.simantics.Simantics;
6 import org.simantics.db.Resource;
7 import org.simantics.db.WriteGraph;
8 import org.simantics.db.common.CommentMetadata;
9 import org.simantics.db.common.request.WriteRequest;
10 import org.simantics.db.common.utils.NameUtils;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.adapter.ActionFactory;
13 import org.simantics.layer0.Layer0;
14 import org.simantics.layer0.utils.direct.GraphUtils;
15 import org.simantics.modeling.ModelingResources;
16
17 /**
18  * @author Tuukka Lehtonen
19  */
20 public class NewSubscription implements ActionFactory {
21
22     @Override
23     public Runnable create(Object target) {
24         if (!(target instanceof Resource))
25             return null;
26         final Resource model = (Resource)target;
27
28         return new Runnable() {
29             @Override
30             public void run() {
31                 Simantics.getSession().asyncRequest(new WriteRequest() {
32                     @Override
33                     public void perform(WriteGraph g) throws DatabaseException {
34                         g.markUndoPoint();
35                         
36                         Layer0 l0 = Layer0.getInstance(g);
37                         ModelingResources wr = ModelingResources.getInstance(g);
38                         String freshLabel = NameUtils.findFreshLabel(g, Messages.NewSubscription_Subscription, model);
39                         @SuppressWarnings("unused")
40                         Resource subscription = GraphUtils.create2(g, wr.Subscription,
41                                 l0.HasName, UUID.randomUUID().toString(),
42                                 l0.HasLabel, freshLabel,
43                                 l0.PartOf, model);
44                         
45                         CommentMetadata cm = g.getMetadata(CommentMetadata.class);
46                         g.addMetadata(cm.add("Created subscription folder " + freshLabel)); //$NON-NLS-1$
47                     }
48                 });
49             }
50         };
51     }
52
53 }