]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/e4/SaveState.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.simulation.ui / src / org / simantics / simulation / ui / handlers / e4 / SaveState.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.commands.ExecutionException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.core.runtime.jobs.Job;
19 import org.eclipse.e4.core.di.annotations.CanExecute;
20 import org.eclipse.e4.core.di.annotations.Execute;
21 import org.simantics.ObjectIdentitySchedulingRule;
22 import org.simantics.Simantics;
23 import org.simantics.simulation.experiment.IDynamicExperiment;
24 import org.simantics.simulation.experiment.IExperiment;
25 import org.simantics.simulation.project.IExperimentManager;
26 import org.simantics.simulation.ui.Activator;
27
28 /**
29  * @author Tuukka Lehtonen
30  */
31 public class SaveState {
32
33     @CanExecute
34     public boolean canExecute() {
35         IExperimentManager manager = Simantics.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
36         if (manager == null)
37             return false;
38         IExperiment experiment = manager.getActiveExperiment();
39         if (experiment == null)
40             return false;
41         return true;
42     }
43     
44     @Execute
45     public void execute() throws ExecutionException {
46         IExperimentManager manager = Simantics.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
47         IExperiment experiment = manager.getActiveExperiment();
48         if (experiment instanceof IDynamicExperiment) {
49             Job j = new SaveStateJob((IDynamicExperiment) experiment);
50             j.setRule(new ObjectIdentitySchedulingRule(experiment));
51             j.schedule();
52         }
53     }
54
55     static class SaveStateJob extends Job {
56         private final IDynamicExperiment experiment;
57
58         public SaveStateJob(IDynamicExperiment experiment) {
59             super("Save Experiment State");
60             this.experiment = experiment;
61         }
62
63         @Override
64         protected IStatus run(IProgressMonitor monitor) {
65             try {
66                 monitor.beginTask("", IProgressMonitor.UNKNOWN);
67                 experiment.saveState();
68                 if (monitor.isCanceled())
69                     return Status.CANCEL_STATUS;
70                 return Status.OK_STATUS;
71             } catch (Throwable t) {
72                 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to save state of dynamic experiment "
73                         + experiment + " with ID " + experiment.getIdentifier(), t);
74             } finally {
75                 //System.out.println("DONE");
76                 monitor.done();
77             }
78         }
79     }
80
81 }