]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/Reload.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.simulation.ui / src / org / simantics / simulation / ui / handlers / Reload.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 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.core.runtime.IProgressMonitor;\r
18 import org.eclipse.core.runtime.IStatus;\r
19 import org.eclipse.core.runtime.Status;\r
20 import org.eclipse.core.runtime.jobs.Job;\r
21 import org.simantics.ObjectIdentitySchedulingRule;\r
22 import org.simantics.simulation.experiment.IExperiment;\r
23 import org.simantics.simulation.project.IExperimentManager;\r
24 import org.simantics.simulation.ui.Activator;\r
25 import org.simantics.ui.SimanticsUI;\r
26 \r
27 public class Reload extends AbstractHandler {\r
28 \r
29     @Override\r
30     public Object execute(ExecutionEvent event) throws ExecutionException {\r
31         IExperimentManager manager =\r
32             SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
33         IExperiment experiment = manager.getActiveExperiment();\r
34         if (experiment != null) {\r
35             Job j = new RefreshJob(experiment);\r
36             j.setRule(new ObjectIdentitySchedulingRule(experiment));\r
37             j.schedule();\r
38         }\r
39         return null;\r
40     }\r
41 \r
42     static class RefreshJob extends Job {\r
43         private final IExperiment experiment;\r
44 \r
45         public RefreshJob(IExperiment experiment) {\r
46             super("Refresh Experiment");\r
47             this.experiment = experiment;\r
48         }\r
49 \r
50         @Override\r
51         protected IStatus run(IProgressMonitor monitor) {\r
52             try {\r
53                 monitor.beginTask("", IProgressMonitor.UNKNOWN);\r
54                 experiment.refresh(SimanticsUI.getSession());\r
55                 if (monitor.isCanceled())\r
56                     return Status.CANCEL_STATUS;\r
57                 return Status.OK_STATUS;\r
58             } catch (Throwable t) {\r
59                 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to refresh experiment "\r
60                         + experiment + " with ID " + experiment.getIdentifier(), t);\r
61             } finally {\r
62                 monitor.done();\r
63             }\r
64         }\r
65     }\r
66 \r
67 }\r