1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 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.handlers.newComponents;
\r
14 import java.util.ArrayList;
\r
16 import org.eclipse.core.commands.AbstractHandler;
\r
17 import org.eclipse.core.commands.ExecutionEvent;
\r
18 import org.eclipse.core.commands.ExecutionException;
\r
19 import org.eclipse.jface.viewers.ISelection;
\r
20 import org.eclipse.ui.handlers.HandlerUtil;
\r
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.WriteGraph;
\r
23 import org.simantics.db.common.request.WriteRequest;
\r
24 import org.simantics.db.common.utils.ListUtils;
\r
25 import org.simantics.db.common.utils.NameUtils;
\r
26 import org.simantics.db.exception.DatabaseException;
\r
27 import org.simantics.layer0.Layer0;
\r
28 import org.simantics.layer0.utils.direct.GraphUtils;
\r
29 import org.simantics.modeling.ModelingResources;
\r
30 import org.simantics.structural.stubs.StructuralResource2;
\r
31 import org.simantics.sysdyn.SysdynResource;
\r
32 import org.simantics.ui.SimanticsUI;
\r
33 import org.simantics.utils.ui.AdaptionUtils;
\r
36 * Creates a new Enumeration node to a configuration or module
\r
38 * @author Teemu Lempinen
\r
41 public class NewEnumerationNodeHandler extends AbstractHandler {
\r
44 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
46 ISelection sel = HandlerUtil.getCurrentSelection(event);
\r
48 final Resource resource = AdaptionUtils.adaptToSingle(sel, Resource.class);
\r
50 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
53 public void perform(WriteGraph g) throws DatabaseException {
\r
54 SysdynResource sr = SysdynResource.getInstance(g);
\r
55 Layer0 l0 = Layer0.getInstance(g);
\r
57 // Find the actual configuration. Possible cases for resource are sr.Configuration, sr.ModuleSymbol or sr.Module.
\r
58 Resource configuration = null;
\r
59 if(g.isInstanceOf(resource, sr.Configuration)) {
\r
60 configuration = resource;
\r
61 } else if(g.isInheritedFrom(resource, sr.ModuleSymbol)) {
\r
62 Resource module = g.getPossibleObject(resource,ModelingResources.getInstance(g).SymbolToComponentType);
\r
63 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
73 // Create the enumeartion
\r
75 String name = NameUtils.findFreshName(g, "Enum", configuration, l0.ConsistsOf, "%s%d");
\r
77 GraphUtils.create2(g,
\r
80 sr.Enumeration_enumerationIndexList, ListUtils.create(g, new ArrayList<Resource>()),
\r
81 l0.PartOf, configuration);
\r