1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.simulation.ui.handlers;
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;
31 * A generic command handler for activating an experiment based on the currently
32 * selected experiment in the UI.
34 * @author Tuukka Lehtonen
36 * @see ActivateExperimentAction
37 * @see ExperimentActivator
39 public class ActivateExperimentHandler extends AbstractHandler {
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)
48 final IProject project = SimanticsUI.peekProject();
52 final IExperimentManager experimentManager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
53 if (experimentManager == null) {
54 ErrorLogger.defaultLogWarning("Experiment manager not available.", new Exception());
59 project.getSession().syncRequest(new ReadRequest() {
61 public void run(ReadGraph graph) throws DatabaseException {
62 if (graph.isInstanceOf(experiment, SimulationResource.getInstance(graph).Experiment)) {
63 ExperimentActivator.scheduleActivation(graph, project, experimentManager, experiment);
67 } catch (DatabaseException e) {
68 ErrorLogger.defaultLogError(e);