/*
 * DomUI Java User Interface library
 * Copyright (c) 2010 by Frits Jalvingh, Itris B.V.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * See the "sponsors" file for a list of supporters.
 *
 * The latest version of DomUI and related code, support and documentation
 * can be found at http://www.domui.org/
 * The contact for the project is Frits Jalvingh <jal@etc.to>.
 */
package to.etc.domui.component.layout;

import java.util.*;

import to.etc.domui.component.buttons.*;
import to.etc.domui.dom.html.*;
import to.etc.domui.util.*;

public class Caption extends Div {
	private String m_caption;

	private TD m_buttonpart;

	private List<SmallImgButton> m_btns = Collections.EMPTY_LIST;

	private Table m_table;

	public Caption() {}

	public Caption(String ttl) {
		m_caption = ttl;
	}

	public String getCaption() {
		return m_caption;
	}

	public void setCaption(String caption) {
		if(DomUtil.isEqual(m_caption, caption))
			return;
		m_caption = caption;
		forceRebuild();
	}

	@Override
	public void createContent() throws Exception {
		setCssClass("ui-cptn");
		m_table = new Table();
		add(m_table);
		m_table.setCellPadding("0");
		m_table.setCellSpacing("0");
		m_table.setTableWidth("100%");
		TBody b = new TBody();
		m_table.add(b);
		TD ttltd = b.addRowAndCell();
		ttltd.setCssClass("ui-cptn-ttl");
		ttltd.setNowrap(true);
		Div ttl = new Div();
		ttltd.add(ttl);
		//		ttl.setCssClass("ui-cptn-ttl");
		ttl.setText(m_caption);
		TD right = b.addCell();
		right.setCssClass("ui-cptn-btn");
		m_buttonpart = right;
		right.setAlign(TDAlignType.RIGHT);
		for(SmallImgButton btn : m_btns) {
			m_buttonpart.add(btn);
		}
	}

	public void addButton(String image, String hint, IClicked<NodeBase> handler) {
		SmallImgButton ib = new SmallImgButton(image);
		ib.setClicked(handler);
		internallyAddButton(ib, hint);
	}

	public void addButton(String image, String hint, String onClickJs) {
		SmallImgButton ib = new SmallImgButton(image);
		ib.setOnClickJS(onClickJs);
		internallyAddButton(ib, hint);
	}

	private void internallyAddButton(SmallImgButton ib, String hint) {
		if(m_btns == Collections.EMPTY_LIST) {
			m_btns = new ArrayList<SmallImgButton>();
		}
		ib.setTitle(hint);
		m_btns.add(ib);
		if(isBuilt() && m_buttonpart != null) {
			m_buttonpart.add(ib);
		}
	}
}

@@ -1413,16 +1413,84 @@
 table;%0A%0A
+%09private TD m_ttltd;%0A%0A%09private Img m_icon;%0A%0A%09private Div m_ttldiv;%0A%0A
 %09public 
@@ -1502,16 +1502,16 @@
 on() %7B%7D%0A
-
 %0A%09public
@@ -2016,19 +2016,18 @@
 d(b);%0A%09%09
-TD 
+m_
 ttltd = 
@@ -2043,24 +2043,26 @@
 ndCell();%0A%09%09
+m_
 ttltd.setCss
@@ -2081,24 +2081,26 @@
 tn-ttl%22);%0A%09%09
+m_
 ttltd.setNow
@@ -2116,15 +2116,16 @@
 ;%0A%09%09
-Div ttl
+m_ttldiv
  = n
@@ -2136,16 +2136,18 @@
 iv();%0A%09%09
+m_
 ttltd.ad
@@ -2152,14 +2152,20 @@
 add(
+m_
 ttl
+div
 );%0A
+%0A
 %09%09//
@@ -2204,19 +2204,20 @@
 ;%0A%09%09
+m_
 ttl
-.setText
+div.add
 (m_c
@@ -2348,16 +2348,68 @@
 RIGHT);%0A
+%0A%09%09if(m_icon != null)%0A%09%09%09m_buttonpart.add(m_icon);%0A%0A
 %09%09for(Sm
@@ -2467,16 +2467,16 @@
 n);%0A
-
 %09%09%7D%0A%09%7D%0A%0A
 %09pub
@@ -2467,24 +2467,229 @@
 n);%0A%09%09%7D%0A%09%7D%0A%0A
+%09public void addIcon(String src) %7B%0A%09%09if(m_icon == null) %7B%0A%09%09%09m_icon = new Img(src);%0A%09%09%09if(isBuilt())%0A%09%09%09%09m_buttonpart.add(0, m_icon);%0A%09%09%7D else%0A%09%09%09m_icon.setSrc(src);%0A%09%09m_icon.setAlign(ImgAlign.RIGHT);%0A%09%7D%0A%0A
 %09public void

Allow icon on caption.