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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.simulation.ui.handlers;
\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
31 * A generic command handler for activating an experiment based on the currently
\r
32 * selected experiment in the UI.
\r
34 * @author Tuukka Lehtonen
\r
36 * @see ActivateExperimentAction
\r
37 * @see ExperimentActivator
\r
39 public class ActivateExperimentHandler extends AbstractHandler {
\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
48 final IProject project = SimanticsUI.peekProject();
\r
49 if (project == null)
\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
59 project.getSession().syncRequest(new ReadRequest() {
\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
67 } catch (DatabaseException e) {
\r
68 ErrorLogger.defaultLogError(e);
\r