blob: 7ac4c06a0844d69eed78c0019184036206e3c234 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency
* 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:
* Pierre Allard,
* Regent L'Archeveque,
* Olivier L. Larouche - initial API and implementation
*
* SPDX-License-Identifier: EPL-1.0
*
*******************************************************************************/
package org.eclipse.apogy.core.invocator.ui.utils;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.progress.UIJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class LongExecutionButtonManager {
private static final Logger Logger = LoggerFactory.getLogger(LongExecutionButtonManager.class);
protected Button managedButton = null;
protected boolean notifyOnCompletion = true;
protected String operationDescription = null;
public LongExecutionButtonManager(Button managedButton, String operationDescription, boolean notifyOnCompletion) {
this.managedButton = managedButton;
this.operationDescription = operationDescription;
this.notifyOnCompletion = notifyOnCompletion;
managedButton.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
managedButton.setEnabled(false);
Logger.info("Launching execution of " + operationDescription);
Job job = new Job(operationDescription) {
@Override
protected IStatus run(IProgressMonitor monitor) {
try {
execute();
postExecuteWithSuccess();
if (notifyOnCompletion) {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
try {
String message = getExecutionSuccessMessage();
Logger.info(message);
MessageDialog.openInformation(managedButton.getDisplay().getActiveShell(),
operationDescription + " Success", message);
} catch (Exception e) {
Logger.error(e.getMessage(), e);
}
}
});
}
} catch (final Exception e) {
postExecuteWithError(e);
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
String message = getExecutionErrorMessage(e);
postExecuteWithError(e);
Logger.info(message);
MessageDialog.openError(managedButton.getDisplay().getActiveShell(), "Error",
message);
}
});
}
return Status.OK_STATUS;
}
};
job.setPriority(Job.LONG);
job.schedule();
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
/**
* Method that return the message to display and log for successful execution.
*
* @return
*/
public String getExecutionSuccessMessage() {
String message = this.operationDescription + " : Completed sucessfully.";
return message;
}
/**
* Method that return the message to display and log for failed execution.
*
* @return
*/
public String getExecutionErrorMessage(Throwable t) {
String message = "The following error occured during " + this.operationDescription + " : " + t.toString();
return message;
}
/**
* Method called after the execute() has completed with success. It is called
* before the Message Dialog is shown.
*/
public void postExecuteWithSuccess() {
// Starts UI job that wait for the execution to complete.
UIJob uiJob = new UIJob(this.operationDescription + "postExecuteWithSuccess()") {
@Override
public IStatus runInUIThread(IProgressMonitor monitor) {
try {
if (!LongExecutionButtonManager.this.managedButton.isDisposed()) {
LongExecutionButtonManager.this.managedButton.setEnabled(true);
}
} catch (Throwable t) {
Logger.error(t.getMessage(), t);
}
return Status.OK_STATUS;
}
};
uiJob.schedule();
}
/**
* Method called after the execute() has completed with error. It is called
* before the Error Message Dialog is shown.
*/
public void postExecuteWithError(Throwable t) {
// Starts UI job that wait for the execution to complete.
UIJob uiJob = new UIJob(this.operationDescription + "postExecuteWithError(t)") {
@Override
public IStatus runInUIThread(IProgressMonitor monitor) {
try {
if (!LongExecutionButtonManager.this.managedButton.isDisposed()) {
LongExecutionButtonManager.this.managedButton.setEnabled(true);
}
} catch (Throwable t) {
Logger.error(t.getMessage(), t);
}
return Status.OK_STATUS;
}
};
uiJob.schedule();
}
public abstract void execute() throws Exception;
}