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 java.util.Arrays;
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;
29 public class NewFlagTypeVisualFilter implements ActionFactory {
32 public Runnable create(Object target) {
33 if(!(target instanceof Resource))
35 final Resource parent = (Resource)target;
37 return new Runnable() {
40 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
42 public void perform(WriteGraph g) throws DatabaseException {
44 createNewFilter(g, parent);
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);
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);
63 Resource list = g.getPossibleObject(parent, SG.Node_children);
65 ListUtils.insertBack(g, list, Arrays.asList(newRes));