]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
05f7914624d9c27047a7f844664c749059d3f9ac
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.browser.actions.newActions;\r
13 \r
14 import org.simantics.db.ReadGraph;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.WriteGraph;\r
17 import org.simantics.db.common.request.WriteRequest;\r
18 import org.simantics.db.common.utils.NameUtils;\r
19 import org.simantics.db.exception.DatabaseException;\r
20 import org.simantics.db.layer0.adapter.ActionFactory;\r
21 import org.simantics.layer0.Layer0;\r
22 import org.simantics.layer0.utils.direct.GraphUtils;\r
23 import org.simantics.sysdyn.SysdynResource;\r
24 import org.simantics.ui.SimanticsUI;\r
25 \r
26 /**\r
27  * Creates a new basic experiment\r
28  * @author Teemu Lempinen\r
29  *\r
30  */\r
31 public class NewExperimentAction implements ActionFactory{\r
32 \r
33     @Override\r
34     public Runnable create(Object target) {\r
35         if(!(target instanceof Resource))\r
36             return null;\r
37         final Resource model = (Resource)target;\r
38 \r
39         return new Runnable() {\r
40             @Override\r
41             public void run() {\r
42 \r
43 \r
44                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
45 \r
46                     @Override\r
47                     public void perform(WriteGraph graph) throws DatabaseException {\r
48                         Layer0 l0 = Layer0.getInstance(graph);\r
49                         String name = NameUtils.findFreshName(graph, getNameSuggestion(), model, l0.ConsistsOf, "%s%d");\r
50 \r
51                         Resource experiment = GraphUtils.create2(graph, getExperimentType(graph),\r
52                                 l0.HasName, name,\r
53                                 l0.HasLabel, name,\r
54                                 l0.PartOf, model);\r
55                         \r
56                         configureExperiment(graph, experiment);\r
57                     }\r
58                 });\r
59             }\r
60         };\r
61     }\r
62 \r
63     /**\r
64      * Override to do experiment-specific alterations\r
65      */\r
66     protected void configureExperiment(WriteGraph graph, Resource experiment) throws DatabaseException {\r
67         \r
68     }\r
69     \r
70     /**\r
71      * Get the type of this experiment.\r
72      * \r
73      * @param g ReadGraph\r
74      * @return The type resource of this experiment\r
75      */\r
76     protected Resource getExperimentType(ReadGraph g) {\r
77         return SysdynResource.getInstance(g).BasicExperiment;\r
78     }\r
79     \r
80     /**\r
81      * Returns the suggested name for this experiment.\r
82      * If the name has already been taken, appropriate prefix needs to be added. \r
83      *  \r
84      * @return Suggested name for this experiment. \r
85      */\r
86     protected String getNameSuggestion() {\r
87         return "Experiment";\r
88     }\r
89 }\r