blob: 63ba62165a8d06fd22c83da76edfd9037c542eb6 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2007, 2019 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.nico.ui.util;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.statushandlers.StatusManager;
import org.eclipse.statet.jcommons.status.ProgressMonitor;
import org.eclipse.statet.jcommons.status.Status;
import org.eclipse.statet.jcommons.status.StatusException;
import org.eclipse.statet.jcommons.status.Statuses;
import org.eclipse.statet.jcommons.ts.core.Tool;
import org.eclipse.statet.jcommons.ts.core.ToolRunnable;
import org.eclipse.statet.jcommons.ts.core.ToolService;
import org.eclipse.statet.ecommons.runtime.core.util.StatusUtils;
import org.eclipse.statet.ecommons.ui.util.UIAccess;
import org.eclipse.statet.internal.nico.ui.Messages;
import org.eclipse.statet.nico.core.runtime.ConsoleService;
import org.eclipse.statet.nico.core.runtime.IRemoteEngineController;
import org.eclipse.statet.nico.core.runtime.IToolEventHandler;
import org.eclipse.statet.nico.core.runtime.ToolController;
import org.eclipse.statet.nico.core.runtime.ToolProcess;
import org.eclipse.statet.nico.ui.NicoUI;
/**
* @see IToolEventHandler#SCHEDULE_QUIT_EVENT_ID
*/
public class QuitHandler implements IToolEventHandler {
private static class UIRunnable implements Runnable {
private ToolController controller;
private String dialogTitle;
private String dialogMessage;
private String[] dialogOptions;
private volatile int result;
@Override
public void run() {
final IWorkbenchWindow window= UIAccess.getActiveWorkbenchWindow(true);
final MessageDialog dialog= new MessageDialog(window.getShell(), this.dialogTitle, null,
this.dialogMessage, MessageDialog.QUESTION, this.dialogOptions, 0 );
this.result= dialog.open();
if (this.result == 1) {
try {
window.run(true, true, new IRunnableWithProgress() {
@Override
public void run(final IProgressMonitor monitor) throws InvocationTargetException {
final ProgressMonitor m= StatusUtils.convert(monitor, 1);
try {
UIRunnable.this.controller.kill(m);
}
catch (final StatusException e) {
throw new InvocationTargetException(e);
}
}
});
}
catch (final InvocationTargetException e) {
StatusManager.getManager().handle(new org.eclipse.core.runtime.Status(IStatus.ERROR, NicoUI.BUNDLE_ID,
Messages.TerminatingMonitor_Force_error_message,
e.getTargetException() ),
StatusManager.LOG | StatusManager.SHOW);
}
catch (final InterruptedException e) {
}
}
}
}
@Override
public Status execute(final String id, final ToolService service, final Map<String, Object> data,
final ProgressMonitor m) {
final ConsoleService console= (ConsoleService) service;
if (PlatformUI.getWorkbench().isClosing()) {
final ToolController controller= console.getController();
if (controller != null) {
if (console.getTool().isProvidingFeatureSet(IRemoteEngineController.FEATURE_SET_ID)) {
try {
((IRemoteEngineController) controller).disconnect(m);
return Statuses.CANCEL_STATUS;
}
catch (final StatusException e) {}
}
try {
controller.kill(m);
return Statuses.CANCEL_STATUS;
}
catch (final StatusException e) {}
}
return Statuses.CANCEL_STATUS;
}
final List<ToolRunnable> quitRunnables= (List<ToolRunnable>) data.get("scheduledQuitRunnables");
if (quitRunnables.isEmpty()) {
return Statuses.OK_STATUS; // run default= schedule quit
}
final UIRunnable runner= new UIRunnable();
runner.controller= console.getController();
final ToolProcess process= runner.controller.getTool();
runner.dialogTitle= NLS.bind(Messages.TerminatingMonitor_title, process.getLabel(Tool.DEFAULT_LABEL));
runner.dialogMessage= NLS.bind(Messages.TerminatingMonitor_message, process.getLabel(Tool.LONG_LABEL));
runner.dialogOptions= new String[] {
Messages.TerminatingMonitor_WaitButton_label,
Messages.TerminatingMonitor_ForceButton_label,
Messages.TerminatingMonitor_CancelButton_label
};
UIAccess.getDisplay().syncExec(runner);
if (runner.result == 2) {
runner.controller.cancelQuit();
}
return Statuses.CANCEL_STATUS; // do nothing
}
}