package com.smartgwt.sample.client.tinymce;

import com.google.gwt.user.client.Timer;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;

public class TinyMCECanvas extends Canvas {
	private String elementID;
	private String cssWidth = "700px";
	private String cssHeight = "200px";
	private HTMLFlow textArea;

	@Override
	protected void onUnload() {
		TinyMCE.unload(getElementID());
	};
	/**
	 * Return an HTML editor widget using Tiny MCE.
	 * @param cssWidth The Width for the editor window in pixels.
	 * @param cssHeight The Height for the editor window in pixels.
	 * @param tinyMCECanvasItem The item into which this canvas will be put
	 */
	public TinyMCECanvas() {
		TinyMCE.initMCE();
		setWidth(cssWidth);
		setHeight(cssHeight);
		setElementID(SC.generateID());
		textArea = new HTMLFlow("<div id=\""+getElementID()+"\"></div>");
		textArea.setWidth(cssWidth);
		textArea.setHeight(cssHeight);
		addDrawHandler(new DrawHandler() {
			
			@Override
			public void onDraw(DrawEvent event) {
	            Timer timer = new Timer() {
	                public void run() {
	                	textArea.hide();
	                	SC.logDebug("drawing element "+getElementID());
						TinyMCE.initInstance(getElementID());
						textArea.show();
	                }
	            };
	            timer.schedule(100);
			}
		});
		addChild(textArea);
	}

	public String getElementID() {
		return elementID;
	}

	public void setElementID(String elementID) {
		this.elementID = elementID;
	}
}
