1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.template2d.ui.actions;
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;
28 public class NewDrawingTemplate implements ActionFactory {
31 public Runnable create(Object target) {
32 if(!(target instanceof Resource))
34 final Resource parent = (Resource)target;
36 return new Runnable() {
39 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
41 public void perform(WriteGraph g) throws DatabaseException {
44 createDrawingTemplate(g, parent);
51 public static Resource createDrawingTemplate (WriteGraph g, Resource parent) throws DatabaseException {
53 Layer0 L0 = Layer0.getInstance(g);
54 Template2dResource TEMPLATE2D = Template2dResource.getInstance(g);
55 G2DResource G2D = G2DResource.getInstance(g);
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);
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);
75 g.claimLiteral(template, TEMPLATE2D.HasMargin, new float[]{10.0f, 10.0f, 10.0f, 10.0f}, Bindings.FLOAT_ARRAY);
77 g.claim(template, TEMPLATE2D.HasPageOrientation, TEMPLATE2D.PageOrientation_Landscape);
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);
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);
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);
101 CommentMetadata cm = g.getMetadata(CommentMetadata.class);
102 g.addMetadata(cm.add("Create New Diagram Template."));
107 public static Resource getDrawingTemplateLibrary(ReadGraph graph, Resource model) throws DatabaseException {
109 Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);
110 Resource library = graph.getSingleObject(model, TEMPLATE2D.HasDrawingTemplateRoot);