/*
 * Copyright (c) 2011, Patrik Dufresne. All rights reserved.
 * Patrik Dufresne PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.planimod.ui;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.MessageDialogWithToggle;
import org.eclipse.swt.widgets.Shell;

import com.patrikdufresne.jface.dialogs.DetailMessageDialog;
import com.patrikdufresne.util.Localized;

/**
 * This utility class may be used to display message box.
 * 
 * @author Patrik Dufresne
 * 
 */
public class MessageUtils {

	/**
	 * Load the localized strings
	 */
	public static String APPLICATION_NAME = Localized.load(MessageUtils.class)
			.get("applicationName"); //$NON-NLS-1$

	public static boolean openQuestion(Shell shell, String dialogMessage) {
		return MessageDialog.openQuestion(shell, APPLICATION_NAME,
				dialogMessage);
	}

	public static void openWarning(Shell shell, String dialogMessage,
			String shortDetail, Throwable explanation) {
		explanation.printStackTrace();
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		explanation.printStackTrace(pw);
		String stackTrace = sw.toString();

		openWarning(shell, dialogMessage, shortDetail, stackTrace);
	}

	public static void openWarning(Shell shell, String message,
			String shortDetail, String longDetail) {
		DetailMessageDialog.openDetailWarning(shell, APPLICATION_NAME, message,
				shortDetail, longDetail);
	}

	/**
	 * Show a message dialog with two buttons : Yes, no.
	 * 
	 * @param shell
	 *            the parent shell
	 * @param message
	 *            the message
	 * @param shortDetail
	 *            the detailed message
	 * @return The message dialog after being closed. User selection may be
	 *         retreived using getReturnCode().
	 */
	public static DetailMessageDialog openDetailYesNoQuestion(Shell shell,
			String message, String shortDetail) {
		return DetailMessageDialog.openDetailYesNoQuestion(shell,
				APPLICATION_NAME, message, shortDetail, null);
	}

	public static void openInfo(Shell shell, String message,
			String shortDetail, String longDetail) {
		DetailMessageDialog.openDetailInformation(shell, APPLICATION_NAME,
				message, shortDetail, longDetail);
	}

	/**
	 * Open a message dialog without long detail.
	 * 
	 * @param shell
	 *            the parent shell
	 * @param message
	 *            the warning message
	 * 
	 * @param shortDetail
	 *            a short detailed message
	 */
	public static void openDetailWarning(Shell shell, String message,
			String shortDetail) {
		DetailMessageDialog.openDetailWarning(shell, APPLICATION_NAME, message,
				shortDetail, null);
	}
}
