1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.browser.actions.newActions;
\r
14 import java.util.Collections;
\r
16 import org.simantics.db.Resource;
\r
17 import org.simantics.db.WriteGraph;
\r
18 import org.simantics.db.common.request.WriteRequest;
\r
19 import org.simantics.db.common.utils.ListUtils;
\r
20 import org.simantics.db.common.utils.NameUtils;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.db.layer0.adapter.ActionFactory;
\r
23 import org.simantics.layer0.Layer0;
\r
24 import org.simantics.layer0.utils.direct.GraphUtils;
\r
25 import org.simantics.modeling.ModelingResources;
\r
26 import org.simantics.structural.stubs.StructuralResource2;
\r
27 import org.simantics.sysdyn.SysdynResource;
\r
28 import org.simantics.ui.SimanticsUI;
\r
31 * Creates a new enumeration to a model or module
\r
32 * @author Teemu Lempinen
\r
35 public class NewEnumerationAction implements ActionFactory{
\r
38 public Runnable create(Object target) {
\r
39 if(!(target instanceof Resource))
\r
41 final Resource resource = (Resource)target;
\r
43 return new Runnable() {
\r
48 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
51 public void perform(WriteGraph g) throws DatabaseException {
\r
52 SysdynResource sr = SysdynResource.getInstance(g);
\r
53 Layer0 l0 = Layer0.getInstance(g);
\r
55 // Find the configuration from...
\r
56 Resource configuration = null;
\r
57 if(g.isInstanceOf(resource, sr.Configuration)) {
\r
58 configuration = resource;
\r
59 } else if(g.isInheritedFrom(resource, sr.ModuleSymbol)) {
\r
61 Resource module = g.getPossibleObject(resource,ModelingResources.getInstance(g).SymbolToComponentType);
\r
62 configuration = g.getPossibleObject(module, StructuralResource2.getInstance(g).IsDefinedBy);
\r
65 Resource instanceOf = g.getSingleObject(resource, l0.InstanceOf);
\r
66 if(g.isInheritedFrom(instanceOf, sr.Module)) {
\r
67 configuration = g.getPossibleObject(instanceOf, StructuralResource2.getInstance(g).IsDefinedBy);
\r
74 String name = NameUtils.findFreshName(g, "Enum", configuration, l0.ConsistsOf, "%s%d");
\r
76 // Create enumeration
\r
77 GraphUtils.create2(g,
\r
80 sr.Enumeration_enumerationIndexList, ListUtils.create(g, Collections.<Resource>emptyList()),
\r
81 l0.PartOf, configuration);
\r