]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/ActivateExperimentHandler.java
19da34f3b8a003a047d0aa45bd907a871255f8e5
[simantics/platform.git] / bundles / org.simantics.simulation.ui / src / org / simantics / simulation / ui / handlers / ActivateExperimentHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.simulation.ui.handlers;
13
14 import org.eclipse.core.commands.AbstractHandler;
15 import org.eclipse.core.commands.ExecutionEvent;
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.ui.handlers.HandlerUtil;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.common.request.ReadRequest;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.project.IProject;
24 import org.simantics.simulation.ontology.SimulationResource;
25 import org.simantics.simulation.project.IExperimentManager;
26 import org.simantics.ui.SimanticsUI;
27 import org.simantics.ui.utils.ResourceAdaptionUtils;
28 import org.simantics.utils.ui.ErrorLogger;
29
30 /**
31  * A generic command handler for activating an experiment based on the currently
32  * selected experiment in the UI.
33  * 
34  * @author Tuukka Lehtonen
35  * 
36  * @see ActivateExperimentAction
37  * @see ExperimentActivator
38  */
39 public class ActivateExperimentHandler extends AbstractHandler {
40
41     @Override
42     public Object execute(ExecutionEvent event) throws ExecutionException {
43         ISelection selection = HandlerUtil.getCurrentSelection(event);
44         final Resource experiment = ResourceAdaptionUtils.toSingleResource(selection);
45         if (experiment == null)
46             return null;
47
48         final IProject project = SimanticsUI.peekProject();
49         if (project == null)
50             return null;
51
52         final IExperimentManager experimentManager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
53         if (experimentManager == null) {
54             ErrorLogger.defaultLogWarning("Experiment manager not available.", new Exception());
55             return null;
56         }
57
58         try {
59             project.getSession().syncRequest(new ReadRequest() {
60                 @Override
61                 public void run(ReadGraph graph) throws DatabaseException {
62                     if (graph.isInstanceOf(experiment, SimulationResource.getInstance(graph).Experiment)) {
63                         ExperimentActivator.scheduleActivation(graph, project, experimentManager, experiment);
64                     }
65                 }
66             });
67         } catch (DatabaseException e) {
68             ErrorLogger.defaultLogError(e);
69         }
70
71         return null;
72     }
73
74 }