]> gerrit.simantics Code Review - simantics/platform.git/blob
758a89da1fc001408fe240cc5a25a82fc40b8355
[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.layer0.Layer0;
26 import org.simantics.modeling.template2d.ontology.Template2dResource;
27 import org.simantics.scenegraph.ontology.ScenegraphResources;
28 import org.simantics.ui.SimanticsUI;
29
30 /**
31  * @author Teemu Matasniemi
32  */
33 public class NewFlagTypeVisual implements ActionFactory {
34
35     @Override
36     public Runnable create(Object target) {
37         if(!(target instanceof Resource))
38             return null;
39         final Resource parent = (Resource)target;
40
41         return new Runnable() {
42             @Override
43             public void run() {
44                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
45                     @Override
46                     public void perform(WriteGraph g) throws DatabaseException {
47                         g.markUndoPoint();
48                         createNewFlagTypeVisual(g, parent);
49                     }
50                 });
51             }
52         };
53     }
54     
55     public static Resource createNewFlagTypeVisual(WriteGraph g, Resource parent) throws DatabaseException {
56         Layer0 L0 = Layer0.getInstance(g);
57         Template2dResource TEMPLATE2D = Template2dResource.getInstance(g);
58         ScenegraphResources SG = ScenegraphResources.getInstance(g);
59         
60         Resource newRes = g.newResource();
61         g.claim(newRes, L0.InstanceOf, null, TEMPLATE2D.FlagTypeVisual);
62         g.claim(newRes, L0.InstanceOf, null, TEMPLATE2D.FlagTypeVisualUI);
63         String name = NameUtils.findFreshName(g, "FlagTypeVisual", parent, L0.ConsistsOf);
64         g.addLiteral(newRes, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);
65         g.claim(newRes, SG.Node_children, ListUtils.create(g, new ArrayList<Resource>()));
66         g.claim(newRes, L0.PartOf, parent);
67
68         Resource list = g.getPossibleObject(parent, SG.Node_children);
69         if (list != null)
70                 ListUtils.insertBack(g, list, Arrays.asList(newRes));
71         
72         
73         NewScenegraph actionFactory = new NewScenegraph();
74         Runnable runnable = actionFactory.create(newRes);
75         runnable.run();
76         
77         return newRes;
78     }
79 }