]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/Dispose.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.simulation.ui / src / org / simantics / simulation / ui / handlers / Dispose.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.simulation.ui.handlers;\r
13 \r
14 import java.lang.reflect.InvocationTargetException;\r
15 \r
16 import org.eclipse.core.commands.AbstractHandler;\r
17 import org.eclipse.core.commands.ExecutionEvent;\r
18 import org.eclipse.core.commands.ExecutionException;\r
19 import org.eclipse.core.runtime.IProgressMonitor;\r
20 import org.eclipse.core.runtime.SubMonitor;\r
21 import org.eclipse.jface.dialogs.ProgressMonitorDialog;\r
22 import org.eclipse.jface.operation.IRunnableWithProgress;\r
23 import org.eclipse.ui.handlers.HandlerUtil;\r
24 import org.simantics.project.IProject;\r
25 import org.simantics.simulation.experiment.IExperiment;\r
26 import org.simantics.simulation.project.IExperimentManager;\r
27 import org.simantics.ui.SimanticsUI;\r
28 import org.simantics.utils.ui.ExceptionUtils;\r
29 \r
30 public class Dispose extends AbstractHandler {\r
31 \r
32     @Override\r
33     public Object execute(ExecutionEvent event) throws ExecutionException {\r
34         IProject project = SimanticsUI.getProject();\r
35         IExperimentManager manager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
36         final IExperiment experiment = manager.getActiveExperiment();\r
37         if (experiment == null)\r
38             return null;\r
39 \r
40         try {\r
41             new ProgressMonitorDialog(HandlerUtil.getActiveShell(event)).run(true, false, new IRunnableWithProgress() {\r
42                 @Override\r
43                 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
44                     SubMonitor mon = SubMonitor.convert(monitor, "Shutdown experiment...", 10000);\r
45                     try {\r
46                         experiment.shutdown(mon.newChild(10000));\r
47                     } finally {\r
48                         monitor.done();\r
49                     }\r
50                 }\r
51             });\r
52         } catch (InvocationTargetException e) {\r
53             ExceptionUtils.logAndShowError(e.getCause());\r
54         } catch (InterruptedException e) {\r
55             ExceptionUtils.logAndShowError(e.getCause());\r
56         }\r
57 \r
58         return null;\r
59     }\r
60 \r
61 }\r