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