]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/actions/NewDrawingTemplate.java
a187102f61e816d6a2544fb0e9e21162e07c93fb
[simantics/platform.git] / bundles / org.simantics.modeling.template2d.ui / src / org / simantics / modeling / template2d / ui / actions / NewDrawingTemplate.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.template2d.ui.actions;
13
14 import org.simantics.databoard.Bindings;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.common.CommentMetadata;
19 import org.simantics.db.common.request.WriteRequest;
20 import org.simantics.db.common.utils.NameUtils;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.adapter.ActionFactory;
23 import org.simantics.diagram.stubs.G2DResource;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.modeling.template2d.ontology.Template2dResource;
26 import org.simantics.ui.SimanticsUI;
27
28 public class NewDrawingTemplate implements ActionFactory {
29
30     @Override
31     public Runnable create(Object target) {
32         if(!(target instanceof Resource))
33             return null;
34         final Resource parent = (Resource)target;
35
36         return new Runnable() {
37             @Override
38             public void run() {
39                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
40                     @Override
41                     public void perform(WriteGraph g) throws DatabaseException {
42                         g.markUndoPoint();
43                         
44                         createDrawingTemplate(g, parent);
45                     }
46                 });
47             }
48         };
49     }
50     
51     public static Resource createDrawingTemplate (WriteGraph g, Resource parent) throws DatabaseException {
52         
53         Layer0 L0 = Layer0.getInstance(g);
54         Template2dResource TEMPLATE2D = Template2dResource.getInstance(g);
55         G2DResource G2D = G2DResource.getInstance(g);
56         
57         Resource template = g.newResource();
58         String name = NameUtils.findFreshName(g, "Template", parent, L0.ConsistsOf);
59         g.claim(template, L0.InstanceOf, null, TEMPLATE2D.DrawingTemplate);
60         g.claim(template, L0.InstanceOf, null, TEMPLATE2D.DrawingTemplateUI);
61         g.addLiteral(template, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);
62         g.addLiteral(template, L0.HasLabel, L0.HasLabel_Inverse, L0.String, name, Bindings.STRING);
63         g.claim(template, L0.PartOf, parent);
64
65         Resource page = g.newResource();
66         name = NameUtils.findFreshName(g, "Page", template, L0.ConsistsOf);
67         g.claim(page, L0.InstanceOf, null, TEMPLATE2D.Page);
68         g.claim(page, L0.InstanceOf, null, TEMPLATE2D.BrowseNode);
69         g.addLiteral(page, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);
70         g.addLiteral(page, L0.HasLabel, L0.HasLabel_Inverse, L0.String, name, Bindings.STRING);
71         g.claim(page, L0.PartOf, template);
72         //OrderedSetUtils.add(g, template, page);
73         g.claim(template, TEMPLATE2D.HasPage, page);
74         
75         g.claimLiteral(template, TEMPLATE2D.HasMargin, new float[]{10.0f, 10.0f, 10.0f, 10.0f}, Bindings.FLOAT_ARRAY);
76
77         g.claim(template, TEMPLATE2D.HasPageOrientation, TEMPLATE2D.PageOrientation_Landscape);
78
79         // Border is deprecated.
80 //        Resource border = g.newResource();
81 //        name = NameUtils.findFreshName(g, "Border", template, L0.ConsistsOf);
82 //        g.claim(border, L0.InstanceOf, null, TEMPLATE2D.Border);
83 //        g.claim(border, L0.InstanceOf, null, TEMPLATE2D.BorderUI);
84 //        g.addLiteral(border, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);
85 //        g.addLiteral(border, L0.HasLabel, L0.HasLabel_Inverse, L0.String, name, Bindings.STRING);
86 //        g.claim(border, L0.PartOf, template);
87 //        g.claim(template, TEMPLATE2D.HasBorder, border);
88 //        g.claimLiteral(border, TEMPLATE2D.HasSize, new float[]{0.3f, 0.3f, 0.3f, 0.3f}, Bindings.FLOAT_ARRAY);
89 //
90 //        Resource color = G2DUtils.createColor(g, new Color(0.0f, 0.0f, 0.0f, 1.0f));
91 //        g.claim(border, G2D.HasColor, color);//new RGB.Integer(255,255,255), RGB.Integer.BINDING);
92
93 //        ViewsResources VIEW = ViewsResources.getInstance(g);
94 //        Resource sg = g.newResource();
95 //        g.claim(sg, L0.InstanceOf, null, VIEW.Text);
96 //        g.addLiteral(sg, L0.HasName, L0.NameOf, L0.String, "SG", Bindings.STRING);
97 //        g.claimLiteral(sg, VIEW.TextContainer_text, "SG", Bindings.STRING);
98 //        g.claim(template, TEMPLATE2D.HasScenegraph, sg);
99 //        g.claim(template, L0.ConsistsOf, sg);
100         
101         CommentMetadata cm = g.getMetadata(CommentMetadata.class);
102         g.addMetadata(cm.add("Create New Diagram Template."));
103         
104                 return template;
105     }
106     
107     public static Resource getDrawingTemplateLibrary(ReadGraph graph, Resource model) throws DatabaseException {
108                 
109         Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);
110         Resource library = graph.getSingleObject(model, TEMPLATE2D.HasDrawingTemplateRoot);
111         return library;
112     }
113 }