1 /*******************************************************************************
\r
2 * Copyright (c) 2010, 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.UUID;
\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.ReadGraph;
\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.NameUtils;
\r
26 import org.simantics.db.exception.DatabaseException;
\r
27 import org.simantics.db.layer0.util.Layer0Utils;
\r
28 import org.simantics.layer0.Layer0;
\r
29 import org.simantics.layer0.utils.direct.GraphUtils;
\r
30 import org.simantics.sysdyn.SysdynResource;
\r
31 import org.simantics.sysdyn.ui.browser.nodes.ExperimentsFolder;
\r
32 import org.simantics.ui.SimanticsUI;
\r
33 import org.simantics.utils.ui.AdaptionUtils;
\r
36 * Creates a new normal SysDyn experiment
\r
38 * @author Teemu Lempinen
\r
41 public class NewExperimentNodeHandler extends AbstractHandler {
\r
44 * Assumes that it is called from a node that has a SysDyn model as its resource.
\r
47 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
48 ISelection sel = HandlerUtil.getCurrentSelection(event);
\r
50 ExperimentsFolder node = AdaptionUtils.adaptToSingle(sel, ExperimentsFolder.class);
\r
54 final Resource model = node.data;
\r
56 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
59 public void perform(WriteGraph g) throws DatabaseException {
\r
61 Layer0 l0 = Layer0.getInstance(g);
\r
62 String label = NameUtils.findFreshName(g, getNameSuggestion(), model, l0.ConsistsOf, l0.HasLabel, "%s%d");
\r
63 Resource experiment = GraphUtils.create2(g, getExperimentType(g),
\r
64 l0.HasName, UUID.randomUUID().toString(),
\r
67 configureExperiment(g, experiment);
\r
68 Layer0Utils.addCommentMetadata(g, "Created new experiment " + label + " " + experiment.toString());
\r
75 * Override to do experiment-specific alterations
\r
77 protected void configureExperiment(WriteGraph graph, Resource experiment) throws DatabaseException {
\r
82 * Get the type of this experiment.
\r
84 * @param g ReadGraph
\r
85 * @return The type resource of this experiment
\r
87 protected Resource getExperimentType(ReadGraph g) {
\r
88 return SysdynResource.getInstance(g).BasicExperiment;
\r
92 * Returns the suggested name for this experiment.
\r
93 * If the name has already been taken, appropriate prefix needs to be added.
\r
95 * @return Suggested name for this experiment.
\r
97 protected String getNameSuggestion() {
\r
98 return "Experiment";
\r