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.simulation.ui.handlers;
14 import java.util.concurrent.Semaphore;
15 import java.util.concurrent.atomic.AtomicInteger;
16 import java.util.function.Consumer;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.core.runtime.SubMonitor;
22 import org.eclipse.core.runtime.jobs.Job;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.progress.IProgressConstants2;
27 import org.simantics.DatabaseJob;
28 import org.simantics.db.ReadGraph;
29 import org.simantics.db.RequestProcessor;
30 import org.simantics.db.Resource;
31 import org.simantics.db.common.request.UniqueRead;
32 import org.simantics.db.common.utils.NameUtils;
33 import org.simantics.db.exception.AdaptionException;
34 import org.simantics.db.exception.DatabaseException;
35 import org.simantics.message.ILogger;
36 import org.simantics.message.MessageService;
37 import org.simantics.project.IProject;
38 import org.simantics.simulation.Activator;
39 import org.simantics.simulation.experiment.IExperiment;
40 import org.simantics.simulation.model.ExperimentLoadingCancelled;
41 import org.simantics.simulation.model.ExperimentLoadingFailed;
42 import org.simantics.simulation.project.IExperimentActivationListener;
43 import org.simantics.simulation.project.IExperimentManager;
44 import org.simantics.simulation.ui.ExperimentManagerListener;
45 import org.simantics.utils.DataContainer;
46 import org.simantics.utils.ui.ErrorLogger;
47 import org.simantics.utils.ui.workbench.WorkbenchUtils;
50 * A utility for performing experiment activation as a {@link Job} in the
53 * @author Tuukka Lehtonen
55 * @see ActivateExperimentAction
56 * @see ActivateExperimentHandler
58 public class ExperimentActivator {
62 * @param experimentManager
65 public static void scheduleActivation(RequestProcessor processor, IProject project, IExperimentManager experimentManager, Resource experiment) {
66 scheduleActivation(processor, project, experimentManager, experiment, null);
71 * @param experimentManager
74 public static void scheduleActivation(RequestProcessor processor, IProject project, IExperimentManager experimentManager, Resource experiment, Consumer<IExperiment> callback) {
75 String jobName = "Activate Experiment";
76 String experimentName = getName(processor, experiment);
77 if (experimentName != null)
78 jobName += " '" + experimentName + "'";
80 scheduleActivation(jobName, project, experimentManager, experiment, callback);
83 static class ExperimentActivationJob extends DatabaseJob {
84 private IProject project;
85 private IExperimentManager experimentManager;
86 private Resource experiment;
87 private Consumer<IExperiment> callback;
89 public ExperimentActivationJob(String name, IProject project, IExperimentManager experimentManager, Resource experiment, Consumer<IExperiment> callback) {
91 this.project = project;
92 this.experimentManager = experimentManager;
93 this.experiment = experiment;
94 this.callback = callback;
98 protected IStatus run(IProgressMonitor monitor) {
100 return ExperimentActivator.activate(monitor, project, experimentManager, experiment, callback);
105 experimentManager = null;
113 * @param experimentManager
116 public static void scheduleActivation(String jobName, IProject project, IExperimentManager experimentManager, Resource experiment) {
117 scheduleActivation(jobName, project, experimentManager, experiment, null);
122 * @param experimentManager
125 public static void scheduleActivation(String jobName, IProject project, IExperimentManager experimentManager, Resource experiment, Consumer<IExperiment> callback) {
126 Job job = new ExperimentActivationJob(jobName, project, experimentManager, experiment, callback);
127 job.setProperty(IProgressConstants2.SHOW_IN_TASKBAR_ICON_PROPERTY, Boolean.TRUE);
132 public static IStatus activate(IProgressMonitor monitor, IProject project, IExperimentManager experimentManager, Resource experiment) {
133 return activate(monitor, project, experimentManager, experiment, null);
136 public static IStatus activate(IProgressMonitor monitor, IProject project, IExperimentManager experimentManager, Resource experiment, Consumer<IExperiment> callback) {
137 return new ExperimentActivator().activateExperiment(monitor, project, experimentManager, experiment, callback);
140 private static String getName(RequestProcessor processor, final Resource resource) {
142 return processor.syncRequest(new UniqueRead<String>() {
144 public String perform(ReadGraph graph) throws DatabaseException {
146 return graph.adapt(resource, String.class);
147 } catch (AdaptionException e) {
148 return NameUtils.getSafeName(graph, resource);
152 } catch (DatabaseException e) {
153 ErrorLogger.defaultLogWarning(e);
158 private IStatus activateExperiment(IProgressMonitor monitor, IProject project, IExperimentManager manager, final Resource experimentResource, Consumer<IExperiment> callback) {
159 final SubMonitor mon = SubMonitor.convert(monitor, "Activating experiment", 100000);
161 ExperimentManagerListener.listenManager(manager);
162 final Semaphore activated = new Semaphore(0);
163 final DataContainer<IExperiment> activatedExperiment = new DataContainer<>();
164 final DataContainer<Throwable> problem = new DataContainer<>();
165 final AtomicInteger worstMessageSeverity = new AtomicInteger(IStatus.OK);
166 final ILogger messageService = MessageService.getDefault();
167 manager.startExperiment(experimentResource, new IExperimentActivationListener() {
170 public void onExperimentActivated(final IExperiment experiment) {
171 MessageService.defaultLog(new org.eclipse.core.runtime.Status(IStatus.INFO, "org.simantics.simulation.ui", 0, "Activated experiment " + experiment.getIdentifier() , null));
172 activatedExperiment.set(experiment);
176 public void onFailure(Throwable e) {
181 public void onMessage(IStatus message) {
182 messageService.log(message);
183 int s = message.getSeverity();
184 if (s > worstMessageSeverity.get())
185 worstMessageSeverity.set(s);
188 public IProgressMonitor getProgressMonitor() {
194 Throwable t = problem.get();
195 IStatus status = null;
197 if (t instanceof ExperimentLoadingFailed) {
198 ExperimentLoadingFailed ex = (ExperimentLoadingFailed) t;
199 if (t instanceof ExperimentLoadingCancelled) {
200 status = Status.CANCEL_STATUS;
202 // Make sure that the error window gets correct parent
203 // shell, i.e. the workbench window shell, not the job
204 // progress window shell.
205 Activator.logError("Experiment activation failed, see exception for details.", t);
206 if (PlatformUI.isWorkbenchRunning())
207 PlatformUI.getWorkbench().getDisplay().asyncExec(showError("Experiment Activation Failed", t.getMessage() + "\n\nSee Error Log for details."));
209 if (ex.getHelperAction() != null && PlatformUI.isWorkbenchRunning())
210 PlatformUI.getWorkbench().getDisplay().asyncExec(ex.getHelperAction());
212 Activator.logError("Experiment activation failed, see exception for details.", t);
213 if (PlatformUI.isWorkbenchRunning())
214 PlatformUI.getWorkbench().getDisplay().asyncExec(showError("Experiment Activation Failed", t.getMessage() + "\n\nSee Error Log for details."));
218 // if (worstMessageSeverity.get() > IStatus.OK) {
219 // SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), new Runnable() {
221 // public void run() {
223 // WorkbenchUtils.activateView("org.simantics.message.view");
224 // } catch (PartInitException ex) {
225 // ExceptionUtils.logError(ex);
231 if (callback != null)
232 callback.accept(activatedExperiment.get());
234 return status != null ? status : Status.OK_STATUS;
235 } catch (InterruptedException e) {
236 return Status.CANCEL_STATUS;
240 private Runnable showError(String title, String message) {
241 return new Runnable() {
244 Shell parent = WorkbenchUtils.getActiveWorkbenchWindowShell();
245 MessageDialog.openError(parent, title, message);