1 package org.simantics.sysdyn.ui.handlers;
\r
3 import org.eclipse.core.commands.AbstractHandler;
\r
4 import org.eclipse.core.commands.ExecutionEvent;
\r
5 import org.eclipse.core.commands.ExecutionException;
\r
6 import org.eclipse.jface.viewers.ISelection;
\r
7 import org.eclipse.ui.handlers.HandlerUtil;
\r
8 import org.simantics.db.Resource;
\r
9 import org.simantics.db.WriteGraph;
\r
10 import org.simantics.db.common.request.WriteRequest;
\r
11 import org.simantics.db.common.utils.NameUtils;
\r
12 import org.simantics.db.common.utils.OrderedSetUtils;
\r
13 import org.simantics.db.exception.DatabaseException;
\r
14 import org.simantics.layer0.Layer0;
\r
15 import org.simantics.layer0.utils.direct.GraphUtils;
\r
16 import org.simantics.modeling.ModelingResources;
\r
17 import org.simantics.structural.stubs.StructuralResource2;
\r
18 import org.simantics.sysdyn.SysdynResource;
\r
19 import org.simantics.ui.SimanticsUI;
\r
20 import org.simantics.ui.utils.AdaptionUtils;
\r
22 public class NewEnumerationNodeHandler extends AbstractHandler {
\r
25 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
27 ISelection sel = HandlerUtil.getCurrentSelection(event);
\r
29 final Resource resource = AdaptionUtils.adaptToSingle(sel, Resource.class);
\r
31 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
34 public void perform(WriteGraph g) throws DatabaseException {
\r
35 SysdynResource sr = SysdynResource.getInstance(g);
\r
36 Layer0 l0 = Layer0.getInstance(g);
\r
38 Resource configuration = null;
\r
39 if(g.isInstanceOf(resource, sr.Configuration)) {
\r
40 configuration = resource;
\r
41 } else if(g.isInheritedFrom(resource, sr.ModuleSymbol)) {
\r
42 Resource module = g.getPossibleObject(resource,ModelingResources.getInstance(g).SymbolToComponentType);
\r
43 configuration = g.getPossibleObject(module, StructuralResource2.getInstance(g).IsDefinedBy);
\r
45 Resource instanceOf = g.getSingleObject(resource, l0.InstanceOf);
\r
46 if(g.isInheritedFrom(instanceOf, sr.Module)) {
\r
47 configuration = g.getPossibleObject(instanceOf, StructuralResource2.getInstance(g).IsDefinedBy);
\r
53 Resource enumerationIndexes = OrderedSetUtils.create(g, sr.EnumerationIndexes);
\r
55 String name = NameUtils.findFreshName(g, "Enumeration", configuration, l0.ConsistsOf, "%s%d");
\r
56 if("Enumeration".equals(name)) name = "Enumeration1";
\r
58 GraphUtils.create2(g,
\r
61 sr.HasEnumerationIndexes, enumerationIndexes,
\r
62 l0.PartOf, configuration);
\r