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