/*******************************************************************************
 * Copyright (c) 2014, 2014 Bruno Medeiros and other Contributors.
 * 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:
 *     Bruno Medeiros - initial API and implementation
 *******************************************************************************/
package melnorme.lang.ide.ui.utils;


import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;
import melnorme.lang.ide.core.LangCore;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;

/**
 * Utility for handling exceptions during UI operations, by presenting an information dialog to the user.
 */
public class UIOperationExceptionHandler {
	
	public static void handleError(String message, Throwable exception) {
		handleError(true, message, exception);
	}
	
	public static void handleError(boolean logError, String message, Throwable exception) {
		assertNotNull(message);
		
		if(logError) {
			LangCore.logError(message, exception);
		}
		
		Shell shell = WorkbenchUtils.getActiveWorkbenchShell();
		
		if(exception == null) {
			MessageDialog.open(SWT.ERROR, shell, "Error: ", message, SWT.SHEET);
		} else {
			String exceptionText = getExceptionText(exception);
			MessageDialog.open(SWT.ERROR, shell, "Error: " + message, exceptionText, SWT.SHEET);
		}
	}
	
	protected static String getExceptionText(Throwable exception) {
		if(exception == null)
			return null;
		
		String exceptionText = exception.getClass().getName();
		if(exception.getMessage() != null) {
			exceptionText += ": " + exception.getMessage();
		}
		return exceptionText;
	}
	
	public static void handleWithErrorDialog(boolean logError, String title, String message, Throwable exception) {
		if(logError) {
			LangCore.logError(message, exception);
		}
		
		if(message == null) {
			message = "Error";
		}
		
		Shell shell = WorkbenchUtils.getActiveWorkbenchShell();
		
		Status status = LangCore.createErrorStatus(getExceptionText(exception), exception);
		ErrorDialog.openError(shell, title, message, status);
	}
	
	/* -----------------  ----------------- */
	
	public static void handleOperationStatus(String dialogTitle, CoreException ce) {
		IStatus status = ce.getStatus();
		if(status.isOK() || status.matches(IStatus.CANCEL)) {
			return;
		}
		
		boolean logError = status.matches(IStatus.ERROR);
		
		handleWithErrorDialog(logError, dialogTitle, ce.getMessage(), ce.getCause());
	}
	
}
@@ -2417,24 +2417,198 @@
 ell();%0D%0A%09%09%0D%0A
+%09%09if(exception == null) %7B%0D%0A%09%09%09// No point in using ErrorDialog, use simpler dialog%0D%0A%09%09%09MessageDialog.open(SWT.ERROR, shell, title, message, SWT.SHEET);%0D%0A%09%09%09return;%0D%0A%09%09%7D%0D%0A%09%09%0D%0A
 %09%09Status sta

Fix dialog UI when error without exception happens.