1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.utils;
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.SubMonitor;
18 import org.eclipse.core.runtime.jobs.Job;
19 import org.simantics.DatabaseJob;
20 import org.simantics.Simantics;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.Session;
24 import org.simantics.db.common.procedure.adapter.ListenerAdapter;
25 import org.simantics.db.common.request.ParametrizedRead;
26 import org.simantics.db.common.request.UnaryRead;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.request.Read;
29 import org.simantics.simulation.experiment.ExperimentState;
30 import org.simantics.simulation.experiment.IExperiment;
31 import org.simantics.simulation.experiment.IExperimentListener;
32 import org.simantics.ui.workbench.editor.input.InputValidationCombinators;
33 import org.simantics.utils.ui.ExceptionUtils;
36 * @author Tuukka Lehtonen
38 public class ExperimentExistenceWatchdog {
40 public static ExperimentExistenceWatchdog activate(IExperiment experiment, Resource experimentResource) {
41 ExperimentExistenceWatchdog validator = new ExperimentExistenceWatchdog(experiment, experimentResource,
42 InputValidationCombinators.hasURI());
43 validator.attachExperimentListener();
44 validator.activateValidation();
48 private Session session;
49 private final Resource experimentResource;
50 private IExperiment experiment;
51 private final ParametrizedRead<Resource, Boolean> inputValidator;
52 private InputListener inputListener;
54 private ExperimentExistenceWatchdog(IExperiment experiment, Resource experimentResource, ParametrizedRead<Resource, Boolean> inputValidator) {
55 this.session = Simantics.getSession();
56 this.experiment = experiment;
57 this.experimentResource = experimentResource;
58 this.inputValidator = inputValidator;
61 private void attachExperimentListener() {
62 experiment.addListener(new IExperimentListener() {
64 public void stateChanged(ExperimentState state) {
65 if (state == ExperimentState.DISPOSED) {
73 public void dispose() {
74 deactivateValidation();
79 protected boolean isDisposed() {
80 return session == null || experimentResource == null || experiment == null;
83 public synchronized void activateValidation() {
85 throw new IllegalStateException(this + " is disposed");
86 if (inputListener != null)
89 inputListener = new InputListener();
90 session.asyncRequest(validationRequest(), inputListener);
93 public synchronized void deactivateValidation() {
95 throw new IllegalStateException(this + " is disposed");
96 if (inputListener == null)
98 inputListener.dispose();
104 * @return a read request that returns <code>true</code> for valid inputs
105 * and <code>false</code> for non-existent or invalid inputs.
107 private Read<Boolean> validationRequest() {
108 return new UnaryRead<Resource, Boolean>(experimentResource) {
110 public Boolean perform(ReadGraph graph) throws DatabaseException {
111 return graph.syncRequest(inputValidator.get(experimentResource));
116 private class InputListener extends ListenerAdapter<Boolean> {
118 private boolean disposed = false;
120 public void dispose() {
125 public void execute(Boolean evaluation) {
126 if (!Boolean.TRUE.equals(evaluation)) {
127 scheduleExperimentShutdown(experiment);
132 public void exception(Throwable t) {
133 ExceptionUtils.logError("ResourceEditorSupport.InputListener received an unexpected exception.", t);
137 public boolean isDisposed() {
138 return disposed || ExperimentExistenceWatchdog.this.isDisposed();
142 private static void scheduleExperimentShutdown(final IExperiment experiment) {
143 Job job = new DatabaseJob("Experiment Shutdown Watchdog") {
145 protected IStatus run(IProgressMonitor monitor) {
146 SubMonitor mon = SubMonitor.convert(monitor, "Shutdown", 100000);
148 experiment.shutdown(mon.newChild(100000));
149 return Status.OK_STATUS;