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