<commit_before>/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2013 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */

package org.geomajas.puregwt.widget.client.dialog;

import org.geomajas.puregwt.widget.client.i18n.MessageBoxMessages;

import com.google.gwt.core.client.Callback;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * General MessageBox for showing a message.
 * <p>
 * Includes extra buttons so it can also be used to ask a question.
 * 
 * <p>
 * Use the different static methods to conveniently create a MessageBox.
 * 
 * @author <NAME>
 */
public class MessageBox extends Composite {

	// can't directly inherit from CloseableDialogbox because it has ui:fields in its binder (which we don't need/want).

	private static final MessageBoxMessages MSG = GWT.create(MessageBoxMessages.class);

	/**
	 * Style of the MessageBox.
	 * @author <NAME>
	 */
	public enum MessageStyleType {
		INFO, HELP, WARN, ERROR
	}

	/**
	 * Type of the MessageBox.
	 * @author <NAME>
	 */
	public enum MessageBoxType {
		MESSAGE, YESNO, YESNOCANCEL
	}

	/**
	 * UI binder interface.
	 */
	interface MyUiBinder extends UiBinder<Widget, MessageBox> {
	}

	private static final MyUiBinder UIBINDER = GWT.create(MyUiBinder.class);

	@UiField
	protected SimplePanel icon;

	@UiField
	protected HTML message;

	@UiField
	protected Button btnYes;

	@UiField
	protected Button btnNo;

	@UiField
	protected Button btnCancel;

	private CloseableDialogBox dialog;

	/**
	 * Create a MessageBox.
	 * <p>You might want to use one of the static convenience methods instead of creating your own though.
	 * @param title
	 * @param messageText
	 */
	public MessageBox(String title, String messageText) {
		this(title, SafeHtmlUtils.fromString(messageText), null);
	}

	/**
	 * Create a MessageBox.
	 * <p>You might want to use one of the static convenience methods instead of creating your own though.
	 * @param title
	 * @param messageText
	 */
	public MessageBox(String title, SafeHtml messageText) {
		this(title, messageText, null);
	}

	/**
	 * Create a MessageBox.
	 * <p>You might want to use one of the static convenience methods instead of creating your own though.
	 * @param title
	 * @param messageText
	 * @param onFinished
	 */
	public MessageBox(String title, String messageText, Callback<Boolean, Void> onFinished) {
		this(title, SafeHtmlUtils.fromString(messageText), onFinished);
	}

	/**
	 * Create a MessageBox with a callback.
	 * <p>You might want to use one of the static convenience methods instead of creating your own though.
	 * @param title
	 * @param messageText
	 * @param onFinished
	 */
	public MessageBox(String title, SafeHtml messageText, final Callback<Boolean, Void> onFinished) {
		initWidget(UIBINDER.createAndBindUi(this));
		dialog = new CloseableDialogBox();
		dialog.setWidget(this);
		MessageBoxResource.INSTANCE.css().ensureInjected();
		dialog.setAnimationEnabled(true);
		dialog.setGlassEnabled(true);
		dialog.setModal(true);
		dialog.setHTML(title);
		message.setHTML(messageText);

		btnYes.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (onFinished != null) {
					onFinished.onSuccess(true);
				}
				dialog.hide();
			}
		});

		btnNo.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (onFinished != null) {
					onFinished.onSuccess(false);
				}
				dialog.hide();
			}
		});

		btnCancel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (onFinished != null) {
					onFinished.onFailure(null);
				}
				dialog.hide();
			}
		});

		if (onFinished != null) {
			dialog.setOnCloseHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					onFinished.onFailure(null);
				}
			});
		}
	}

	/***
	 * Set width of all buttons in pixels.
	 * 
	 * @param width
	 */
	public void setButtonWidth(int width) {
		btnNo.setWidth(width + "px");
		btnYes.setWidth(width + "px");
		btnCancel.setWidth(width + "px");
	}

	public void center() {
		dialog.center();
	}

	public void hide() {
		dialog.hide();
	}

	public void show() {
		dialog.show();
	}

	// ---------------------------------------------------

	public void setMessageStyleType(MessageStyleType messageStyleType) {
		if (messageStyleType == null) {
			icon.getElement().getStyle().setDisplay(Display.NONE);
		} else {
			icon.getElement().getStyle().setDisplay(Display.BLOCK);
			icon.setStyleName(MessageBoxResource.INSTANCE.css().messageBoxIcon());	
			switch (messageStyleType) {
				case HELP:
					icon.addStyleName(MessageBoxResource.INSTANCE.css().messageBoxIconHelp());
					break;
				case WARN:
					icon.addStyleName(MessageBoxResource.INSTANCE.css().messageBoxIconWarn());
					break;
				case ERROR:<commit_msg>PUREWIDGET-14: Changed from composite to isWidget, this is clearer.<commit_after>/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2013 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */

package org.geomajas.puregwt.widget.client.dialog;

import org.geomajas.puregwt.widget.client.i18n.MessageBoxMessages;

import com.google.gwt.core.client.Callback;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * General MessageBox for showing a message.
 * <p>
 * Includes extra buttons so it can also be used to ask a question.
 * 
 * <p>
 * Use the different static methods to conveniently create a MessageBox.
 * 
 * @author <NAME>
 */
public class MessageBox implements IsWidget {

	// can't directly inherit from CloseableDialogbox because it has ui:fields in its binder (which we don't need/want).

	private static final MessageBoxMessages MSG = GWT.create(MessageBoxMessages.class);

	/**
	 * Style of the MessageBox.
	 * @author <NAME>
	 */
	public enum MessageStyleType {
		INFO, HELP, WARN, ERROR
	}

	/**
	 * Type of the MessageBox.
	 * @author <NAME>
	 */
	public enum MessageBoxType {
		MESSAGE, YESNO, YESNOCANCEL
	}

	/**
	 * UI binder interface.
	 */
	interface MyUiBinder extends UiBinder<Widget, MessageBox> {
	}

	private static final MyUiBinder UIBINDER = GWT.create(MyUiBinder.class);

	@UiField
	protected SimplePanel icon;

	@UiField
	protected HTML message;

	@UiField
	protected Button btnYes;

	@UiField
	protected Button btnNo;

	@UiField
	protected Button btnCancel;

	private CloseableDialogBox dialog;

	/**
	 * Create a MessageBox.
	 * <p>You might want to use one of the static convenience methods instead of creating your own though.
	 * @param title
	 * @param messageText
	 */
	public MessageBox(String title, String messageText) {
		this(title, SafeHtmlUtils.fromString(messageText), null);
	}

	/**
	 * Create a MessageBox.
	 * <p>You might want to use one of the static convenience methods instead of creating your own though.
	 * @param title
	 * @param messageText
	 */
	public MessageBox(String title, SafeHtml messageText) {
		this(title, messageText, null);
	}

	/**
	 * Create a MessageBox.
	 * <p>You might want to use one of the static convenience methods instead of creating your own though.
	 * @param title
	 * @param messageText
	 * @param onFinished
	 */
	public MessageBox(String title, String messageText, Callback<Boolean, Void> onFinished) {
		this(title, SafeHtmlUtils.fromString(messageText), onFinished);
	}

	/**
	 * Create a MessageBox with a callback.
	 * <p>You might want to use one of the static convenience methods instead of creating your own though.
	 * @param title
	 * @param messageText
	 * @param onFinished
	 */
	public MessageBox(String title, SafeHtml messageText, final Callback<Boolean, Void> onFinished) {
		MessageBoxResource.INSTANCE.css().ensureInjected();
		Widget inner = UIBINDER.createAndBindUi(this);
		dialog = new CloseableDialogBox();
		dialog.setWidget(inner);
		dialog.setAnimationEnabled(true);
		dialog.setGlassEnabled(true);
		dialog.setModal(true);
		dialog.setHTML(title);
		message.setHTML(messageText);

		btnYes.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (onFinished != null) {
					onFinished.onSuccess(true);
				}
				dialog.hide();
			}
		});

		btnNo.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (onFinished != null) {
					onFinished.onSuccess(false);
				}
				dialog.hide();
			}
		});

		btnCancel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (onFinished != null) {
					onFinished.onFailure(null);
				}
				dialog.hide();
			}
		});

		if (onFinished != null) {
			dialog.setOnCloseHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					onFinished.onFailure(null);
				}
			});
		}
	}

	/***
	 * Set width of all buttons in pixels.
	 * 
	 * @param width
	 */
	public void setButtonWidth(int width) {
		btnNo.setWidth(width + "px");
		btnYes.setWidth(width + "px");
		btnCancel.setWidth(width + "px");
	}

	public void center() {
		dialog.center();
	}

	public void hide() {
		dialog.hide();
	}

	public void show() {
		dialog.show();
	}

	@Override
	public Widget asWidget() {
		return dialog;
	}

	// ---------------------------------------------------

	public void setMessageStyleType(MessageStyleType messageStyleType) {
		if (messageStyleType == null) {
			icon.getElement().getStyle().setDisplay(Display.NONE);
		} else {
			icon.getElement().getStyle().setDisplay(Display.BLOCK);
			icon.setStyleName(MessageBoxResource.INSTANCE.css().messageBoxIcon());	
			switch (messageStyleType) {
				case HELP:
					icon.addStyleName(MessageBoxResource.INSTANCE.css().messageBoxIconHelp());
					break;
				case WARN:
					icon.addStyleName(MessageBoxResource.INSTANCE.css().messageBoxIconWarn());
					break;
				case ERROR:
{'fraction_non_alphanumeric': 0.12065797532592527, 'fraction_numerical': 0.0019249278152069298, 'mean_word_length': 3.727047146401985, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '382802', 'n_tokens_mistral': 3940, 'n_tokens_neox': 3501, 'n_words': 927}