]> gerrit.simantics Code Review - simantics/platform.git/blob
5a64bdc902285b47f8cba62bd0b4dc95583020af
[simantics/platform.git] /
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 java.util.Arrays;
15
16 import org.simantics.databoard.Bindings;
17 import org.simantics.db.Resource;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.request.WriteRequest;
20 import org.simantics.db.common.utils.ListUtils;
21 import org.simantics.db.common.utils.NameUtils;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.adapter.ActionFactory;
24 import org.simantics.diagram.stubs.DiagramResource;
25 import org.simantics.diagram.stubs.G2DResource;
26 import org.simantics.layer0.Layer0;
27 import org.simantics.modeling.template2d.ontology.Template2dResource;
28 import org.simantics.scenegraph.ontology.ScenegraphResources;
29 import org.simantics.ui.SimanticsUI;
30
31 public class NewSVGImage implements ActionFactory {
32
33     @Override
34     public Runnable create(Object target) {
35         if(!(target instanceof Resource))
36             return null;
37         final Resource parent = (Resource)target;
38
39         return new Runnable() {
40             @Override
41             public void run() {
42                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
43                     @Override
44                     public void perform(WriteGraph g) throws DatabaseException {
45                         g.markUndoPoint();
46                         createNewSVGImage(g, parent);
47                     }
48                 });
49             }
50         };
51     }
52     
53     public static Resource createNewSVGImage(WriteGraph g, Resource parent) throws DatabaseException {
54         
55         Layer0 L0 = Layer0.getInstance(g);
56         Template2dResource TEMPLATE2D = Template2dResource.getInstance(g);
57         DiagramResource DIA = DiagramResource.getInstance(g);
58         G2DResource G2D = G2DResource.getInstance(g);
59         ScenegraphResources SG = ScenegraphResources.getInstance(g);
60
61         Resource ref = g.newResource();
62         g.claim(ref, L0.InstanceOf, null, TEMPLATE2D.Profiles_VariableReference);
63         // This works only when experiment is running!
64         g.claimLiteral(ref, TEMPLATE2D.Profiles_VariableReference_path, "", Bindings.STRING);
65
66         Resource svgImage = g.newResource();
67         g.claim(svgImage, L0.InstanceOf, null, DIA.Scenegraph_SVGImage);
68         String name = NameUtils.findFreshName(g, "Image", parent, L0.ConsistsOf);
69         g.addLiteral(svgImage, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);
70         g.claim(svgImage, DIA.Scenegraph_SVGImage_document, ref);
71         g.addLiteral(svgImage, DIA.Scenegraph_SVGImage_transform, DIA.Scenegraph_SVGImage_transform_Inverse, G2D.Transform, new Double[] { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 }, Bindings.getBindingUnchecked(Double[].class));
72         g.claim(svgImage, L0.PartOf, parent);
73         Resource list = g.getPossibleObject(parent, SG.Node_children);
74         if (list != null)
75                 ListUtils.insertBack(g, list, Arrays.asList(svgImage));
76         return svgImage;
77     }
78 }