1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\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.sysdyn.ui.handlers;
\r
14 import java.util.concurrent.Semaphore;
\r
16 import org.eclipse.core.runtime.IProgressMonitor;
\r
17 import org.eclipse.core.runtime.IStatus;
\r
18 import org.eclipse.core.runtime.Status;
\r
19 import org.eclipse.core.runtime.jobs.Job;
\r
20 import org.simantics.db.ReadGraph;
\r
21 import org.simantics.db.RequestProcessor;
\r
22 import org.simantics.db.Resource;
\r
23 import org.simantics.db.common.utils.NameUtils;
\r
24 import org.simantics.db.exception.AdaptionException;
\r
25 import org.simantics.db.exception.DatabaseException;
\r
26 import org.simantics.db.request.Read;
\r
27 import org.simantics.message.MessageService;
\r
28 import org.simantics.project.IProject;
\r
29 import org.simantics.simulation.experiment.IExperiment;
\r
30 import org.simantics.simulation.model.ExperimentLoadingFailed;
\r
31 import org.simantics.simulation.project.IExperimentActivationListener;
\r
32 import org.simantics.simulation.project.IExperimentManager;
\r
33 import org.simantics.sysdyn.ui.listeners.SysdynExperimentManagerListener;
\r
34 import org.simantics.utils.DataContainer;
\r
35 import org.simantics.utils.ui.ErrorLogger;
\r
36 import org.simantics.utils.ui.ExceptionUtils;
\r
37 import org.simantics.utils.ui.dialogs.ShowMessage;
\r
39 public class SysdynExperimentActivator {
\r
42 * @param experimentManager
\r
45 public static void scheduleActivation(RequestProcessor processor, final IProject project, final IExperimentManager experimentManager, final Resource experiment) {
\r
46 String jobName = "Activate Experiment";
\r
47 String experimentName = getName(processor, experiment);
\r
48 if (experimentName != null)
\r
49 jobName += " '" + experimentName + "'";
\r
51 // Shut down the previous active experiment
\r
52 IExperiment activeExperiment = experimentManager.getActiveExperiment();
\r
53 if (experiment != null)
\r
54 activeExperiment.shutdown();
\r
56 // Activate a new experiment
\r
57 scheduleActivation(jobName, project, experimentManager, experiment);
\r
62 * @param experimentManager
\r
65 public static void scheduleActivation(String jobName, final IProject project, final IExperimentManager experimentManager, final Resource experiment) {
\r
68 protected IStatus run(final IProgressMonitor monitor) {
\r
69 return SysdynExperimentActivator.activate(monitor, project, experimentManager, experiment);
\r
74 public static IStatus activate(IProgressMonitor monitor, IProject project, IExperimentManager experimentManager, Resource experiment) {
\r
75 return new SysdynExperimentActivator().activateExperiment(monitor, project, experimentManager, experiment);
\r
78 private static String getName(RequestProcessor processor, final Resource resource) {
\r
80 return processor.syncRequest(new Read<String>() {
\r
82 public String perform(ReadGraph graph) throws DatabaseException {
\r
84 return graph.adapt(resource, String.class);
\r
85 } catch (AdaptionException e) {
\r
86 return NameUtils.getSafeName(graph, resource);
\r
90 } catch (DatabaseException e) {
\r
91 ErrorLogger.defaultLogWarning(e);
\r
96 private IStatus activateExperiment(final IProgressMonitor monitor, final IProject project, final IExperimentManager manager, final Resource experimentResource) {
\r
97 monitor.beginTask("Activating experiment", IProgressMonitor.UNKNOWN);
\r
99 SysdynExperimentManagerListener.listenManager(manager);
\r
100 IExperiment experiment = manager.getActiveExperiment();
\r
101 if(experiment != null) {
\r
102 experiment.shutdown();
\r
104 final Semaphore activated = new Semaphore(0);
\r
105 final DataContainer<Throwable> problem = new DataContainer<Throwable>();
\r
106 manager.startExperiment(experimentResource, new IExperimentActivationListener() {
\r
109 public void onExperimentActivated(final IExperiment experiment) {
\r
110 MessageService.defaultLog(new org.eclipse.core.runtime.Status(IStatus.INFO, "org.simantics.simulation.ui", 0, "Activated experiment " + experiment.getIdentifier() , null));
\r
111 activated.release();
\r
114 public void onFailure(Throwable e) {
\r
116 activated.release();
\r
119 public void onMessage(IStatus message) {
\r
120 MessageService.getDefault().log(message);
\r
121 /*ILogger logger = MessageService.getDefault();
\r
122 MultiStatus init = new MultiStatus(Activator.PLUGIN_ID, 0, "Activating experiment", null);
\r
123 for (String msg : messages) {
\r
124 init.add(new Status(IStatus.INFO, Activator.PLUGIN_ID, msg));
\r
126 logger.log(init);*/
\r
130 activated.acquire();
\r
131 Throwable t = problem.get();
\r
133 if (t instanceof ExperimentLoadingFailed) {
\r
134 ErrorLogger.defaultLogError(t);
\r
135 ShowMessage.showError("Experiment Activation Failed", t.getMessage());
\r
137 ExceptionUtils.logAndShowError(t);
\r
141 return Status.OK_STATUS;
\r
142 //return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Experiment activation failed, see exception for details.", problem.get());
\r
143 } catch (InterruptedException e) {
\r
144 return Status.CANCEL_STATUS;
\r