package to.etc.domui.component.delayed;

import to.etc.domui.component.misc.MsgBox;
import to.etc.domui.dom.html.Div;
import to.etc.domui.dom.html.HTag;
import to.etc.domui.util.Msgs;
import to.etc.parallelrunner.IAsyncRunnable;
import to.etc.util.CancelledException;
import to.etc.util.StringTool;

/**
 * @author <a href="mailto:jal@etc.to">Frits Jalvingh</a>
 * Created on 14-12-21.
 */
abstract public class AsyncDiv<T extends IAsyncRunnable> extends Div {
	final private T m_runnable;

	private final String m_what;

	public AsyncDiv(T runnable, String what) {
		m_runnable = runnable;
		m_what = what;
	}

	@Override
	final public void createContent() throws Exception {
		String what = m_what;
		if(null != what) {
			add(new HTag(1, what));
		}
		add(new AsyncContainer(m_runnable, (cancelled, errorException) -> {
			//-- If we've got an exception replace the contents with the exception message.
			if(errorException != null && !(errorException instanceof CancelledException)) {
				//errorException.printStackTrace();
				StringBuilder sb = new StringBuilder(8192);
				StringTool.strStacktrace(sb, errorException);
				String s = sb.toString();
				s = s.replace("\n", "<br/>\n");

				MsgBox.error(this, Msgs.BUNDLE.getString(Msgs.ASYNC_ERROR) + "<br/>" + s);
			} else if(cancelled) {
				MsgBox.info(this, Msgs.BUNDLE.getString(Msgs.ASYNC_CONTAINER_CANCELLED));
			} else {
				removeAllChildren();
				createContent(m_runnable);
			}
		}));
	}

	abstract public void createContent(T task) throws Exception;
}

@@ -68,19 +68,109 @@
 ent.
-misc.MsgBox
+layout.MessageLine;%0Aimport to.etc.domui.component.misc.MsgBox;%0Aimport to.etc.domui.dom.errors.MsgType
 ;%0Aim
@@ -236,16 +236,51 @@
 l.HTag;%0A
+import to.etc.domui.dom.html.Para;%0A
 import t
@@ -386,16 +386,53 @@
 eption;%0A
+import to.etc.util.MessageException;%0A
 import t
@@ -767,16 +767,93 @@
 at;%0A%09%7D%0A%0A
+%09public AsyncDiv(T runnable) %7B%0A%09%09m_runnable = runnable;%0A%09%09m_what = null;%0A%09%7D%0A%0A
 %09@Overri
@@ -1229,230 +1229,397 @@
 %09%09%09%09
-//errorException.printStackTrace();%0A%09%09%09%09StringBuilder sb = new StringBuilder(8192);%0A%09%09%09%09StringTool.strStacktrace(sb, errorException);%0A%09%09%09%09String s = sb.toString();%0A%09%09%09%09s = s.replace(%22%5Cn%22, %22%3Cbr/%3E%5Cn%22);%0A%0A%09%09%09%09MsgBox.error(this
+createError(m_runnable, errorException, false);%0A%09%09%09%7D else if(cancelled) %7B%0A%09%09%09%09MsgBox.info(this, Msgs.BUNDLE.getString(Msgs.ASYNC_CONTAINER_CANCELLED));%0A%09%09%09%7D else %7B%0A%09%09%09%09removeAllChildren();%0A%09%09%09%09createContent(m_runnable);%0A%09%09%09%7D%0A%09%09%7D));%0A%09%7D%0A%0A%09protected void createError(T task, Exception errorException, boolean cancelled) throws Exception %7B%0A%09%09if(cancelled) %7B%0A%09%09%09add(new MessageLine(MsgType.INFO
 , Ms
@@ -1653,32 +1653,32 @@
 YNC_
-ERROR) + %22%3Cbr/%3E%22 + s
+CONTAINER_CANCELLED))
 );%0A
-%09
 %09%09%7D 
@@ -1689,41 +1689,170 @@
  if(
-cancelled) %7B%0A%09%09%09%09MsgBox.info(this
+errorException instanceof MessageException) %7B%0A%09%09%09add(new MessageLine(MsgType.ERROR, errorException.getMessage()));%0A%09%09%7D else %7B%0A%09%09%09add(new MessageLine(MsgType.ERROR
 , Ms
@@ -1886,110 +1886,235 @@
 YNC_
-CONTAINER_CANCELLED
+ERROR)
 ));%0A
+%0A
 %09%09%09
-%7D else %7B%0A%09%09%09%09removeAllChildren();%0A%09%09%09%09createContent(m_runnable);%0A%09%09%09%7D%0A%09%09%7D));
+StringBuilder sb = new StringBuilder(8192);%0A%09%09%09StringTool.strStacktrace(sb, errorException);%0A%09%09%09String s = sb.toString();%0A%09%09%09s = s.replace(%22%5Cn%22, %22%3Cbr/%3E%5Cn%22);%0A%0A%09%09%09Para p = new Para();%0A%09%09%09add(p);%0A%09%09%09p.setText(s);%0A%09%09%7D
 %0A%09%7D%0A

Implement the single test part including UI.