]> gerrit.simantics Code Review - simantics/platform.git/blob
4b4edd7c46dbedf90d12d8c9adbfc3c4af17bb89
[simantics/platform.git] /
1 package org.simantics.structural.synchronization;
2
3 import java.util.concurrent.ConcurrentMap;
4
5 import org.eclipse.core.runtime.IProgressMonitor;
6 import org.eclipse.core.runtime.IStatus;
7 import org.eclipse.core.runtime.Status;
8 import org.eclipse.core.runtime.jobs.Job;
9 import org.simantics.DatabaseJob;
10 import org.simantics.Simantics;
11 import org.simantics.simulation.experiment.ExperimentState;
12 import org.simantics.simulation.experiment.IExperiment;
13
14 public class ContinuousSynchronizationJob extends DatabaseJob {
15         
16         public static final String FAMILY = "ContinuousSynchronizationJob";
17
18         final private ConcurrentMap<IExperiment, Job> currentSyncJobs;
19         
20     IExperiment                         experiment;
21
22     public ContinuousSynchronizationJob(IExperiment experiment, ConcurrentMap<IExperiment, Job> currentSyncJobs) {
23         super("Synchronization");
24         this.experiment = experiment;
25         this.currentSyncJobs = currentSyncJobs;
26         setPriority(SHORT);
27         setSystem(false);
28     }
29     
30     @Override
31     public boolean belongsTo(Object family) {
32         if (FAMILY == family) return true;
33         return super.belongsTo(family);
34     }
35
36     @Override
37     public boolean shouldRun() {
38         return currentSyncJobs.remove(experiment, this);
39     }
40
41     @Override
42     protected IStatus run(IProgressMonitor monitor) {
43         try {
44             //System.out.println("SYNC #" + (++syncCounter));
45             monitor.beginTask("", IProgressMonitor.UNKNOWN);
46             if (experiment.getState() != ExperimentState.DISPOSED)
47                 experiment.refresh(Simantics.getSession());
48             return Status.OK_STATUS;
49         } catch (Throwable t) {
50             return new Status(IStatus.ERROR, "org.simantics.structural.synchronization.client", "Failed to refresh experiment "
51                     + experiment + " with ID " + experiment.getIdentifier(), t);
52         } finally {
53             monitor.done();
54         }
55     }
56
57 }