]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
09d7beb002a89a40c992181ac8bd5fbbfd1d57f9
[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.handlers.newComponents;\r
13 \r
14 import java.util.UUID;\r
15 \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.NameUtils;\r
25 import org.simantics.db.exception.DatabaseException;\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.ui.SimanticsUI;\r
30 import org.simantics.ui.utils.AdaptionUtils;\r
31 \r
32 /**\r
33  * Handler for creating new history dataset\r
34  * @author Teemu Lempinen\r
35  *\r
36  */\r
37 public class NewHistoryDataHandler extends AbstractHandler {\r
38 \r
39     @Override\r
40     public Object execute(ExecutionEvent event) throws ExecutionException {\r
41 \r
42         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
43 \r
44         final Resource experiment = AdaptionUtils.adaptToSingle(sel, Resource.class);\r
45 \r
46         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
47 \r
48             @Override\r
49             public void perform(WriteGraph g) throws DatabaseException {\r
50                 SysdynResource sr = SysdynResource.getInstance(g);\r
51                 Layer0 l0 = Layer0.getInstance(g);\r
52                 if(!g.isInstanceOf(experiment, sr.Experiment))\r
53                     return; // Not called from an experiment\r
54 \r
55                 Resource model = g.getPossibleObject(experiment, l0.PartOf);\r
56                 // Create the history dataset\r
57                 GraphUtils.create2(g, \r
58                         sr.HistoryDataset,\r
59                         l0.HasName, "HistoryDataset" + UUID.randomUUID().toString(),\r
60                         l0.HasLabel, NameUtils.findFreshLabel(g, "History Dataset", experiment),\r
61                         sr.Experiment_result_Inverse, experiment, \r
62                         sr.HistoryDataset_columns, Boolean.TRUE,\r
63                         l0.PartOf, model);\r
64             }\r
65         });\r
66         return null;\r
67     }\r
68 \r
69 }