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.databoard.Bindings;
\r
22 import org.simantics.db.Resource;
\r
23 import org.simantics.db.WriteGraph;
\r
24 import org.simantics.db.common.request.WriteRequest;
\r
25 import org.simantics.db.common.utils.ListUtils;
\r
26 import org.simantics.db.common.utils.NameUtils;
\r
27 import org.simantics.db.exception.DatabaseException;
\r
28 import org.simantics.db.layer0.util.Layer0Utils;
\r
29 import org.simantics.layer0.Layer0;
\r
30 import org.simantics.layer0.utils.direct.GraphUtils;
\r
31 import org.simantics.modeling.ModelingResources;
\r
32 import org.simantics.structural.stubs.StructuralResource2;
\r
33 import org.simantics.sysdyn.SysdynResource;
\r
34 import org.simantics.ui.SimanticsUI;
\r
35 import org.simantics.utils.ui.AdaptionUtils;
\r
38 * Creates a new Enumeration node to a configuration or module
\r
40 * @author Teemu Lempinen
\r
43 public class NewEnumerationNodeHandler extends AbstractHandler {
\r
46 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
48 ISelection sel = HandlerUtil.getCurrentSelection(event);
\r
50 final Resource resource = AdaptionUtils.adaptToSingle(sel, Resource.class);
\r
52 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
55 public void perform(WriteGraph g) throws DatabaseException {
\r
57 SysdynResource sr = SysdynResource.getInstance(g);
\r
58 Layer0 l0 = Layer0.getInstance(g);
\r
60 // Find the actual configuration. Possible cases for resource are sr.Configuration, sr.ModuleSymbol or sr.Module.
\r
61 Resource configuration = null;
\r
62 if(g.isInstanceOf(resource, sr.Configuration)) {
\r
63 configuration = resource;
\r
64 } else if(g.isInheritedFrom(resource, sr.ModuleSymbol)) {
\r
65 Resource module = g.getPossibleObject(resource,ModelingResources.getInstance(g).SymbolToComponentType);
\r
66 configuration = g.getPossibleObject(module, StructuralResource2.getInstance(g).IsDefinedBy);
\r
68 Resource instanceOf = g.getSingleObject(resource, l0.InstanceOf);
\r
69 if(g.isInheritedFrom(instanceOf, sr.Module)) {
\r
70 configuration = g.getPossibleObject(instanceOf, StructuralResource2.getInstance(g).IsDefinedBy);
\r
76 // Create the enumeartion
\r
78 String name = NameUtils.findFreshName(g, "Enum", configuration, l0.ConsistsOf, "%s%d");
\r
80 GraphUtils.create2(g,
\r
83 sr.Enumeration_enumerationIndexList, ListUtils.create(g, new ArrayList<Resource>()),
\r
84 l0.PartOf, configuration);
\r
85 Layer0Utils.addCommentMetadata(g, "Created new Enumeration " + name + " to " + g.getRelatedValue2(configuration, l0.HasLabel, Bindings.STRING));
\r