]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
8fa80b3b70b63a93fa7860b8ef866eef2f784e2a
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 Association for Decentralized Information Management in\r
3  * 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.sysdyn.ui.handlers;\r
13 \r
14 import java.util.concurrent.Semaphore;\r
15 \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
38 \r
39 public class SysdynExperimentActivator {\r
40     /**\r
41      * @param project\r
42      * @param experimentManager\r
43      * @param experiment\r
44      */\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
50         /*\r
51         // Shut down the previous active experiment\r
52         IExperiment activeExperiment = experimentManager.getActiveExperiment();\r
53         if (experiment != null)\r
54             activeExperiment.shutdown();\r
55          */\r
56         // Activate a new experiment\r
57         scheduleActivation(jobName, project, experimentManager, experiment);\r
58     }\r
59 \r
60     /**\r
61      * @param project\r
62      * @param experimentManager\r
63      * @param experiment\r
64      */\r
65     public static void scheduleActivation(String jobName, final IProject project, final IExperimentManager experimentManager, final Resource experiment) {\r
66         new Job(jobName) {\r
67             @Override\r
68             protected IStatus run(final IProgressMonitor monitor) {\r
69                 return SysdynExperimentActivator.activate(monitor, project, experimentManager, experiment);\r
70             }\r
71         }.schedule();\r
72     }\r
73 \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
76     }\r
77 \r
78     private static String getName(RequestProcessor processor, final Resource resource) {\r
79         try {\r
80             return processor.syncRequest(new Read<String>() {\r
81                 @Override\r
82                 public String perform(ReadGraph graph) throws DatabaseException {\r
83                     try {\r
84                         return graph.adapt(resource, String.class);\r
85                     } catch (AdaptionException e) {\r
86                         return NameUtils.getSafeName(graph, resource);\r
87                     }\r
88                 }\r
89             });\r
90         } catch (DatabaseException e) {\r
91             ErrorLogger.defaultLogWarning(e);\r
92             return null;\r
93         }\r
94     }\r
95 \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
98         try {\r
99             SysdynExperimentManagerListener.listenManager(manager);\r
100             IExperiment experiment = manager.getActiveExperiment();\r
101             if(experiment != null) {\r
102                 experiment.shutdown();\r
103             }\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
107 \r
108                 @Override\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
112                 }\r
113                 @Override\r
114                 public void onFailure(Throwable e) {\r
115                     problem.set(e);\r
116                     activated.release();\r
117                 }\r
118                 @Override\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
125                     }\r
126                     logger.log(init);*/\r
127                 }\r
128             }, true);\r
129             try {\r
130                 activated.acquire();\r
131                 Throwable t = problem.get();\r
132                 if (t != null) {\r
133                     if (t instanceof ExperimentLoadingFailed) {\r
134                         ErrorLogger.defaultLogError(t);\r
135                         ShowMessage.showError("Experiment Activation Failed", t.getMessage());\r
136                     } else {\r
137                         ExceptionUtils.logAndShowError(t);\r
138                     }\r
139                 }\r
140 \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
145             }\r
146         } finally {\r
147             monitor.done();\r
148         }\r
149     }\r
150 \r
151 }\r