]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/NewDocument.java
(refs #7561) Allow extra parameters to the application of type alias
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / NewDocument.java
1 package org.simantics.modeling.ui.actions;
2
3 import org.simantics.db.Resource;
4 import org.simantics.db.WriteGraph;
5 import org.simantics.db.common.request.WriteRequest;
6 import org.simantics.db.common.utils.NameUtils;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.db.layer0.adapter.ActionFactory;
9 import org.simantics.document.DocumentResource;
10 import org.simantics.layer0.Layer0;
11 import org.simantics.ui.SimanticsUI;
12
13 public class NewDocument implements ActionFactory {
14
15     @Override
16     public Runnable create(Object target) {
17         if(!(target instanceof Resource))
18             return null;
19         final Resource model = (Resource)target;
20         return new Runnable() {
21
22             @Override
23             public void run() {
24                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
25                     @Override
26                     public void perform(WriteGraph graph) throws DatabaseException {
27                         graph.markUndoPoint();
28                         
29                         Layer0 L0 = Layer0.getInstance(graph);
30                         DocumentResource DOC = DocumentResource.getInstance(graph);
31
32                         String name = NameUtils.findFreshEscapedName(graph, "Document", model, L0.ConsistsOf);
33
34                         // Create DOC.WikiDocument instance
35                         Resource wikiDocument = graph.newResource();
36                         graph.claim(wikiDocument, L0.InstanceOf, DOC.WikiDocument);
37                         graph.claim(wikiDocument, L0.PartOf, model);
38                         graph.claimLiteral(wikiDocument, L0.HasName, name);
39
40                         // Bind an empty document scene graph to the new :DOC.WikiDocument
41                         Resource documentType = graph.getSingleObject(DOC.WikiDocument_WikiDocumentBinding, DOC.DocumentTypeBinding_HasDocumentType);
42                         Resource document = graph.newResource();
43                         graph.claim(document, L0.InstanceOf, null, DOC.ScenegraphDocument);
44                         graph.claimLiteral(document, L0.HasName, "Documentation");
45                         graph.claim(wikiDocument, DOC.HasDocumentation, document);
46                         graph.claim(document, L0.PartOf, wikiDocument);
47
48                         Resource scenegraph = graph.newResource();
49                         graph.claim(scenegraph, L0.InstanceOf, null, documentType);
50                         graph.claimLiteral(scenegraph, L0.HasName, "Scenegraph");
51                         graph.claim(scenegraph, L0.PartOf, document);
52                         graph.claim(document, DOC.ScenegraphDocument_scenegraph, scenegraph);
53
54                     }
55                 });
56             }
57             
58         };
59     }
60
61 }