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 org.eclipse.core.commands.AbstractHandler;
\r
15 import org.eclipse.core.commands.ExecutionEvent;
\r
16 import org.eclipse.core.commands.ExecutionException;
\r
17 import org.eclipse.jface.viewers.ISelection;
\r
18 import org.eclipse.ui.handlers.HandlerUtil;
\r
19 import org.simantics.db.ReadGraph;
\r
20 import org.simantics.db.Resource;
\r
21 import org.simantics.db.WriteGraph;
\r
22 import org.simantics.db.common.request.WriteRequest;
\r
23 import org.simantics.db.common.utils.NameUtils;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.document.DocumentResource;
\r
26 import org.simantics.layer0.Layer0;
\r
27 import org.simantics.layer0.utils.direct.GraphUtils;
\r
28 import org.simantics.sysdyn.SysdynResource;
\r
29 import org.simantics.sysdyn.ui.browser.nodes.ExperimentsFolder;
\r
30 import org.simantics.ui.SimanticsUI;
\r
31 import org.simantics.ui.utils.AdaptionUtils;
\r
34 * Creates a new normal SysDyn experiment
\r
36 * @author Teemu Lempinen
\r
39 public class NewExperimentNodeHandler extends AbstractHandler {
\r
42 * Assumes that it is called from a node that has a SysDyn model as its resource.
\r
45 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
46 ISelection sel = HandlerUtil.getCurrentSelection(event);
\r
48 ExperimentsFolder node = AdaptionUtils.adaptToSingle(sel, ExperimentsFolder.class);
\r
52 final Resource model = node.data;
\r
54 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
57 public void perform(WriteGraph g) throws DatabaseException {
\r
58 Layer0 l0 = Layer0.getInstance(g);
\r
59 DocumentResource DOC = DocumentResource.getInstance(g);
\r
60 Resource report = GraphUtils.create2(g, DOC.Report, DOC.HasDocumentation, "===Report===");
\r
62 String name = NameUtils.findFreshName(g, getNameSuggestion(), model, l0.ConsistsOf, "%s%d");
\r
64 Resource experiment = GraphUtils.create2(g, getExperimentType(g),
\r
67 DOC.HasReportFactory, report,
\r
70 configureExperiment(g, experiment);
\r
77 * Override to do experiment-specific alterations
\r
79 protected void configureExperiment(WriteGraph graph, Resource experiment) throws DatabaseException {
\r
84 * Get the type of this experiment.
\r
86 * @param g ReadGraph
\r
87 * @return The type resource of this experiment
\r
89 protected Resource getExperimentType(ReadGraph g) {
\r
90 return SysdynResource.getInstance(g).BasicExperiment;
\r
94 * Returns the suggested name for this experiment.
\r
95 * If the name has already been taken, appropriate prefix needs to be added.
\r
97 * @return Suggested name for this experiment.
\r
99 protected String getNameSuggestion() {
\r
100 return "Experiment";
\r