]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/Reload.java
b49d0c0479f47fc034cc9187071bf35aec0b86af
[simantics/platform.git] / bundles / org.simantics.simulation.ui / src / org / simantics / simulation / ui / handlers / Reload.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.simulation.ui.handlers;
13
14 import org.eclipse.core.commands.AbstractHandler;
15 import org.eclipse.core.commands.ExecutionEvent;
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.core.runtime.jobs.Job;
21 import org.simantics.ObjectIdentitySchedulingRule;
22 import org.simantics.simulation.experiment.IExperiment;
23 import org.simantics.simulation.project.IExperimentManager;
24 import org.simantics.simulation.ui.Activator;
25 import org.simantics.ui.SimanticsUI;
26
27 public class Reload extends AbstractHandler {
28
29     @Override
30     public Object execute(ExecutionEvent event) throws ExecutionException {
31         IExperimentManager manager =
32             SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
33         IExperiment experiment = manager.getActiveExperiment();
34         if (experiment != null) {
35             Job j = new RefreshJob(experiment);
36             j.setRule(new ObjectIdentitySchedulingRule(experiment));
37             j.schedule();
38         }
39         return null;
40     }
41
42     static class RefreshJob extends Job {
43         private final IExperiment experiment;
44
45         public RefreshJob(IExperiment experiment) {
46             super("Refresh Experiment");
47             this.experiment = experiment;
48         }
49
50         @Override
51         protected IStatus run(IProgressMonitor monitor) {
52             try {
53                 monitor.beginTask("", IProgressMonitor.UNKNOWN);
54                 experiment.refresh(SimanticsUI.getSession());
55                 if (monitor.isCanceled())
56                     return Status.CANCEL_STATUS;
57                 return Status.OK_STATUS;
58             } catch (Throwable t) {
59                 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to refresh experiment "
60                         + experiment + " with ID " + experiment.getIdentifier(), t);
61             } finally {
62                 monitor.done();
63             }
64         }
65     }
66
67 }