-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.simulation.ui.handlers;\r
-\r
-import java.util.concurrent.Semaphore;\r
-import java.util.concurrent.atomic.AtomicInteger;\r
-import java.util.function.Consumer;\r
-\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.Status;\r
-import org.eclipse.core.runtime.SubMonitor;\r
-import org.eclipse.core.runtime.jobs.Job;\r
-import org.eclipse.jface.dialogs.MessageDialog;\r
-import org.eclipse.swt.widgets.Shell;\r
-import org.eclipse.ui.PlatformUI;\r
-import org.eclipse.ui.progress.IProgressConstants2;\r
-import org.simantics.DatabaseJob;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.RequestProcessor;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.request.UniqueRead;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.AdaptionException;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.message.ILogger;\r
-import org.simantics.message.MessageService;\r
-import org.simantics.project.IProject;\r
-import org.simantics.simulation.Activator;\r
-import org.simantics.simulation.experiment.IExperiment;\r
-import org.simantics.simulation.model.ExperimentLoadingCancelled;\r
-import org.simantics.simulation.model.ExperimentLoadingFailed;\r
-import org.simantics.simulation.project.IExperimentActivationListener;\r
-import org.simantics.simulation.project.IExperimentManager;\r
-import org.simantics.simulation.ui.ExperimentManagerListener;\r
-import org.simantics.utils.DataContainer;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
-\r
-/**\r
- * A utility for performing experiment activation as a {@link Job} in the\r
- * background.\r
- * \r
- * @author Tuukka Lehtonen\r
- * \r
- * @see ActivateExperimentAction\r
- * @see ActivateExperimentHandler\r
- */\r
-public class ExperimentActivator {\r
-\r
- /**\r
- * @param project\r
- * @param experimentManager\r
- * @param experiment\r
- */\r
- public static void scheduleActivation(RequestProcessor processor, IProject project, IExperimentManager experimentManager, Resource experiment) {\r
- scheduleActivation(processor, project, experimentManager, experiment, null);\r
- }\r
-\r
- /**\r
- * @param project\r
- * @param experimentManager\r
- * @param experiment\r
- */\r
- public static void scheduleActivation(RequestProcessor processor, IProject project, IExperimentManager experimentManager, Resource experiment, Consumer<IExperiment> callback) {\r
- String jobName = "Activate Experiment";\r
- String experimentName = getName(processor, experiment);\r
- if (experimentName != null)\r
- jobName += " '" + experimentName + "'";\r
-\r
- scheduleActivation(jobName, project, experimentManager, experiment, callback);\r
- }\r
-\r
- static class ExperimentActivationJob extends DatabaseJob {\r
- private IProject project;\r
- private IExperimentManager experimentManager;\r
- private Resource experiment;\r
- private Consumer<IExperiment> callback;\r
-\r
- public ExperimentActivationJob(String name, IProject project, IExperimentManager experimentManager, Resource experiment, Consumer<IExperiment> callback) {\r
- super(name);\r
- this.project = project;\r
- this.experimentManager = experimentManager;\r
- this.experiment = experiment;\r
- this.callback = callback;\r
- }\r
-\r
- @Override\r
- protected IStatus run(IProgressMonitor monitor) {\r
- try {\r
- return ExperimentActivator.activate(monitor, project, experimentManager, experiment, callback);\r
- } finally {\r
- monitor.done();\r
- // Aid GC\r
- project = null;\r
- experimentManager = null;\r
- experiment = null;\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * @param project\r
- * @param experimentManager\r
- * @param experiment\r
- */\r
- public static void scheduleActivation(String jobName, IProject project, IExperimentManager experimentManager, Resource experiment) {\r
- scheduleActivation(jobName, project, experimentManager, experiment, null);\r
- }\r
-\r
- /**\r
- * @param project\r
- * @param experimentManager\r
- * @param experiment\r
- */\r
- public static void scheduleActivation(String jobName, IProject project, IExperimentManager experimentManager, Resource experiment, Consumer<IExperiment> callback) {\r
- Job job = new ExperimentActivationJob(jobName, project, experimentManager, experiment, callback);\r
- job.setProperty(IProgressConstants2.SHOW_IN_TASKBAR_ICON_PROPERTY, Boolean.TRUE);\r
- job.setUser(true);\r
- job.schedule();\r
- }\r
-\r
- public static IStatus activate(IProgressMonitor monitor, IProject project, IExperimentManager experimentManager, Resource experiment) {\r
- return activate(monitor, project, experimentManager, experiment, null);\r
- }\r
-\r
- public static IStatus activate(IProgressMonitor monitor, IProject project, IExperimentManager experimentManager, Resource experiment, Consumer<IExperiment> callback) {\r
- return new ExperimentActivator().activateExperiment(monitor, project, experimentManager, experiment, callback);\r
- }\r
-\r
- private static String getName(RequestProcessor processor, final Resource resource) {\r
- try {\r
- return processor.syncRequest(new UniqueRead<String>() {\r
- @Override\r
- public String perform(ReadGraph graph) throws DatabaseException {\r
- try {\r
- return graph.adapt(resource, String.class);\r
- } catch (AdaptionException e) {\r
- return NameUtils.getSafeName(graph, resource);\r
- }\r
- }\r
- });\r
- } catch (DatabaseException e) {\r
- ErrorLogger.defaultLogWarning(e);\r
- return null;\r
- }\r
- }\r
-\r
- private IStatus activateExperiment(IProgressMonitor monitor, IProject project, IExperimentManager manager, final Resource experimentResource, Consumer<IExperiment> callback) {\r
- final SubMonitor mon = SubMonitor.convert(monitor, "Activating experiment", 100000);\r
-\r
- ExperimentManagerListener.listenManager(manager);\r
- final Semaphore activated = new Semaphore(0);\r
- final DataContainer<IExperiment> activatedExperiment = new DataContainer<>();\r
- final DataContainer<Throwable> problem = new DataContainer<>();\r
- final AtomicInteger worstMessageSeverity = new AtomicInteger(IStatus.OK);\r
- final ILogger messageService = MessageService.getDefault();\r
- manager.startExperiment(experimentResource, new IExperimentActivationListener() {\r
-\r
- @Override\r
- public void onExperimentActivated(final IExperiment experiment) {\r
- MessageService.defaultLog(new org.eclipse.core.runtime.Status(IStatus.INFO, "org.simantics.simulation.ui", 0, "Activated experiment " + experiment.getIdentifier() , null));\r
- activatedExperiment.set(experiment);\r
- activated.release();\r
- }\r
- @Override\r
- public void onFailure(Throwable e) {\r
- problem.set(e);\r
- activated.release();\r
- }\r
- @Override\r
- public void onMessage(IStatus message) {\r
- messageService.log(message);\r
- int s = message.getSeverity();\r
- if (s > worstMessageSeverity.get())\r
- worstMessageSeverity.set(s);\r
- }\r
- @Override\r
- public IProgressMonitor getProgressMonitor() {\r
- return mon;\r
- }\r
- }, true);\r
- try {\r
- activated.acquire();\r
- Throwable t = problem.get();\r
- IStatus status = null;\r
- if (t != null) {\r
- if (t instanceof ExperimentLoadingFailed) {\r
- ExperimentLoadingFailed ex = (ExperimentLoadingFailed) t;\r
- if (t instanceof ExperimentLoadingCancelled) {\r
- status = Status.CANCEL_STATUS;\r
- } else {\r
- // Make sure that the error window gets correct parent\r
- // shell, i.e. the workbench window shell, not the job\r
- // progress window shell.\r
- Activator.logError("Experiment activation failed, see exception for details.", t);\r
- if (PlatformUI.isWorkbenchRunning())\r
- PlatformUI.getWorkbench().getDisplay().asyncExec(showError("Experiment Activation Failed", t.getMessage() + "\n\nSee Error Log for details."));\r
- }\r
- if (ex.getHelperAction() != null && PlatformUI.isWorkbenchRunning())\r
- PlatformUI.getWorkbench().getDisplay().asyncExec(ex.getHelperAction());\r
- } else {\r
- Activator.logError("Experiment activation failed, see exception for details.", t);\r
- if (PlatformUI.isWorkbenchRunning())\r
- PlatformUI.getWorkbench().getDisplay().asyncExec(showError("Experiment Activation Failed", t.getMessage() + "\n\nSee Error Log for details."));\r
- }\r
- }\r
-\r
-// if (worstMessageSeverity.get() > IStatus.OK) {\r
-// SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), new Runnable() {\r
-// @Override\r
-// public void run() {\r
-// try {\r
-// WorkbenchUtils.activateView("org.simantics.message.view");\r
-// } catch (PartInitException ex) {\r
-// ExceptionUtils.logError(ex);\r
-// }\r
-// }\r
-// });\r
-// }\r
-\r
- if (callback != null)\r
- callback.accept(activatedExperiment.get());\r
-\r
- return status != null ? status : Status.OK_STATUS;\r
- } catch (InterruptedException e) {\r
- return Status.CANCEL_STATUS;\r
- }\r
- }\r
-\r
- private Runnable showError(String title, String message) {\r
- return new Runnable() {\r
- @Override\r
- public void run() {\r
- Shell parent = WorkbenchUtils.getActiveWorkbenchWindowShell();\r
- MessageDialog.openError(parent, title, message);\r
- }\r
- };\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.simulation.ui.handlers;
+
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Consumer;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.SubMonitor;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.progress.IProgressConstants2;
+import org.simantics.DatabaseJob;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.RequestProcessor;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.AdaptionException;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.message.ILogger;
+import org.simantics.message.MessageService;
+import org.simantics.project.IProject;
+import org.simantics.simulation.Activator;
+import org.simantics.simulation.experiment.IExperiment;
+import org.simantics.simulation.model.ExperimentLoadingCancelled;
+import org.simantics.simulation.model.ExperimentLoadingFailed;
+import org.simantics.simulation.project.IExperimentActivationListener;
+import org.simantics.simulation.project.IExperimentManager;
+import org.simantics.simulation.ui.ExperimentManagerListener;
+import org.simantics.utils.DataContainer;
+import org.simantics.utils.ui.ErrorLogger;
+import org.simantics.utils.ui.workbench.WorkbenchUtils;
+
+/**
+ * A utility for performing experiment activation as a {@link Job} in the
+ * background.
+ *
+ * @author Tuukka Lehtonen
+ *
+ * @see ActivateExperimentAction
+ * @see ActivateExperimentHandler
+ */
+public class ExperimentActivator {
+
+ /**
+ * @param project
+ * @param experimentManager
+ * @param experiment
+ */
+ public static void scheduleActivation(RequestProcessor processor, IProject project, IExperimentManager experimentManager, Resource experiment) {
+ scheduleActivation(processor, project, experimentManager, experiment, null);
+ }
+
+ /**
+ * @param project
+ * @param experimentManager
+ * @param experiment
+ */
+ public static void scheduleActivation(RequestProcessor processor, IProject project, IExperimentManager experimentManager, Resource experiment, Consumer<IExperiment> callback) {
+ String jobName = "Activate Experiment";
+ String experimentName = getName(processor, experiment);
+ if (experimentName != null)
+ jobName += " '" + experimentName + "'";
+
+ scheduleActivation(jobName, project, experimentManager, experiment, callback);
+ }
+
+ static class ExperimentActivationJob extends DatabaseJob {
+ private IProject project;
+ private IExperimentManager experimentManager;
+ private Resource experiment;
+ private Consumer<IExperiment> callback;
+
+ public ExperimentActivationJob(String name, IProject project, IExperimentManager experimentManager, Resource experiment, Consumer<IExperiment> callback) {
+ super(name);
+ this.project = project;
+ this.experimentManager = experimentManager;
+ this.experiment = experiment;
+ this.callback = callback;
+ }
+
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ try {
+ return ExperimentActivator.activate(monitor, project, experimentManager, experiment, callback);
+ } finally {
+ monitor.done();
+ // Aid GC
+ project = null;
+ experimentManager = null;
+ experiment = null;
+ }
+ }
+ }
+
+ /**
+ * @param project
+ * @param experimentManager
+ * @param experiment
+ */
+ public static void scheduleActivation(String jobName, IProject project, IExperimentManager experimentManager, Resource experiment) {
+ scheduleActivation(jobName, project, experimentManager, experiment, null);
+ }
+
+ /**
+ * @param project
+ * @param experimentManager
+ * @param experiment
+ */
+ public static void scheduleActivation(String jobName, IProject project, IExperimentManager experimentManager, Resource experiment, Consumer<IExperiment> callback) {
+ Job job = new ExperimentActivationJob(jobName, project, experimentManager, experiment, callback);
+ job.setProperty(IProgressConstants2.SHOW_IN_TASKBAR_ICON_PROPERTY, Boolean.TRUE);
+ job.setUser(true);
+ job.schedule();
+ }
+
+ public static IStatus activate(IProgressMonitor monitor, IProject project, IExperimentManager experimentManager, Resource experiment) {
+ return activate(monitor, project, experimentManager, experiment, null);
+ }
+
+ public static IStatus activate(IProgressMonitor monitor, IProject project, IExperimentManager experimentManager, Resource experiment, Consumer<IExperiment> callback) {
+ return new ExperimentActivator().activateExperiment(monitor, project, experimentManager, experiment, callback);
+ }
+
+ private static String getName(RequestProcessor processor, final Resource resource) {
+ try {
+ return processor.syncRequest(new UniqueRead<String>() {
+ @Override
+ public String perform(ReadGraph graph) throws DatabaseException {
+ try {
+ return graph.adapt(resource, String.class);
+ } catch (AdaptionException e) {
+ return NameUtils.getSafeName(graph, resource);
+ }
+ }
+ });
+ } catch (DatabaseException e) {
+ ErrorLogger.defaultLogWarning(e);
+ return null;
+ }
+ }
+
+ private IStatus activateExperiment(IProgressMonitor monitor, IProject project, IExperimentManager manager, final Resource experimentResource, Consumer<IExperiment> callback) {
+ final SubMonitor mon = SubMonitor.convert(monitor, "Activating experiment", 100000);
+
+ ExperimentManagerListener.listenManager(manager);
+ final Semaphore activated = new Semaphore(0);
+ final DataContainer<IExperiment> activatedExperiment = new DataContainer<>();
+ final DataContainer<Throwable> problem = new DataContainer<>();
+ final AtomicInteger worstMessageSeverity = new AtomicInteger(IStatus.OK);
+ final ILogger messageService = MessageService.getDefault();
+ manager.startExperiment(experimentResource, new IExperimentActivationListener() {
+
+ @Override
+ public void onExperimentActivated(final IExperiment experiment) {
+ MessageService.defaultLog(new org.eclipse.core.runtime.Status(IStatus.INFO, "org.simantics.simulation.ui", 0, "Activated experiment " + experiment.getIdentifier() , null));
+ activatedExperiment.set(experiment);
+ activated.release();
+ }
+ @Override
+ public void onFailure(Throwable e) {
+ problem.set(e);
+ activated.release();
+ }
+ @Override
+ public void onMessage(IStatus message) {
+ messageService.log(message);
+ int s = message.getSeverity();
+ if (s > worstMessageSeverity.get())
+ worstMessageSeverity.set(s);
+ }
+ @Override
+ public IProgressMonitor getProgressMonitor() {
+ return mon;
+ }
+ }, true);
+ try {
+ activated.acquire();
+ Throwable t = problem.get();
+ IStatus status = null;
+ if (t != null) {
+ if (t instanceof ExperimentLoadingFailed) {
+ ExperimentLoadingFailed ex = (ExperimentLoadingFailed) t;
+ if (t instanceof ExperimentLoadingCancelled) {
+ status = Status.CANCEL_STATUS;
+ } else {
+ // Make sure that the error window gets correct parent
+ // shell, i.e. the workbench window shell, not the job
+ // progress window shell.
+ Activator.logError("Experiment activation failed, see exception for details.", t);
+ if (PlatformUI.isWorkbenchRunning())
+ PlatformUI.getWorkbench().getDisplay().asyncExec(showError("Experiment Activation Failed", t.getMessage() + "\n\nSee Error Log for details."));
+ }
+ if (ex.getHelperAction() != null && PlatformUI.isWorkbenchRunning())
+ PlatformUI.getWorkbench().getDisplay().asyncExec(ex.getHelperAction());
+ } else {
+ Activator.logError("Experiment activation failed, see exception for details.", t);
+ if (PlatformUI.isWorkbenchRunning())
+ PlatformUI.getWorkbench().getDisplay().asyncExec(showError("Experiment Activation Failed", t.getMessage() + "\n\nSee Error Log for details."));
+ }
+ }
+
+// if (worstMessageSeverity.get() > IStatus.OK) {
+// SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), new Runnable() {
+// @Override
+// public void run() {
+// try {
+// WorkbenchUtils.activateView("org.simantics.message.view");
+// } catch (PartInitException ex) {
+// ExceptionUtils.logError(ex);
+// }
+// }
+// });
+// }
+
+ if (callback != null)
+ callback.accept(activatedExperiment.get());
+
+ return status != null ? status : Status.OK_STATUS;
+ } catch (InterruptedException e) {
+ return Status.CANCEL_STATUS;
+ }
+ }
+
+ private Runnable showError(String title, String message) {
+ return new Runnable() {
+ @Override
+ public void run() {
+ Shell parent = WorkbenchUtils.getActiveWorkbenchWindowShell();
+ MessageDialog.openError(parent, title, message);
+ }
+ };
+ }
+
+}