]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/ActivateExperimentHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.simulation.ui / src / org / simantics / simulation / ui / handlers / ActivateExperimentHandler.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in 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.simulation.ui.handlers;\r
13 \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.common.request.ReadRequest;\r
22 import org.simantics.db.exception.DatabaseException;\r
23 import org.simantics.project.IProject;\r
24 import org.simantics.simulation.ontology.SimulationResource;\r
25 import org.simantics.simulation.project.IExperimentManager;\r
26 import org.simantics.ui.SimanticsUI;\r
27 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
28 import org.simantics.utils.ui.ErrorLogger;\r
29 \r
30 /**\r
31  * A generic command handler for activating an experiment based on the currently\r
32  * selected experiment in the UI.\r
33  * \r
34  * @author Tuukka Lehtonen\r
35  * \r
36  * @see ActivateExperimentAction\r
37  * @see ExperimentActivator\r
38  */\r
39 public class ActivateExperimentHandler extends AbstractHandler {\r
40 \r
41     @Override\r
42     public Object execute(ExecutionEvent event) throws ExecutionException {\r
43         ISelection selection = HandlerUtil.getCurrentSelection(event);\r
44         final Resource experiment = ResourceAdaptionUtils.toSingleResource(selection);\r
45         if (experiment == null)\r
46             return null;\r
47 \r
48         final IProject project = SimanticsUI.peekProject();\r
49         if (project == null)\r
50             return null;\r
51 \r
52         final IExperimentManager experimentManager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
53         if (experimentManager == null) {\r
54             ErrorLogger.defaultLogWarning("Experiment manager not available.", new Exception());\r
55             return null;\r
56         }\r
57 \r
58         try {\r
59             project.getSession().syncRequest(new ReadRequest() {\r
60                 @Override\r
61                 public void run(ReadGraph graph) throws DatabaseException {\r
62                     if (graph.isInstanceOf(experiment, SimulationResource.getInstance(graph).Experiment)) {\r
63                         ExperimentActivator.scheduleActivation(graph, project, experimentManager, experiment);\r
64                     }\r
65                 }\r
66             });\r
67         } catch (DatabaseException e) {\r
68             ErrorLogger.defaultLogError(e);\r
69         }\r
70 \r
71         return null;\r
72     }\r
73 \r
74 }