/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.itmill.toolkit.terminal.gwt.client.ui;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.HTML;
import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
import com.itmill.toolkit.terminal.gwt.client.Paintable;
import com.itmill.toolkit.terminal.gwt.client.UIDL;

public class IEmbedded extends HTML implements Paintable {
    private static String CLASSNAME = "i-embedded";

    private String heigth;
    private String width;
    private Element browserElement;

    private ApplicationConnection client;

    public IEmbedded() {
        setStyleName(CLASSNAME);
    }

    public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
        if (client.updateComponent(this, uidl, true)) {
            return;
        }
        this.client = client;

        boolean clearBrowserElement = true;

        if (uidl.hasAttribute("type")) {
            final String type = uidl.getStringAttribute("type");
            if (type.equals("image")) {
                String w = uidl.getStringAttribute("width");
                if (w != null) {
                    w = " width=\"" + w + "\" ";
                } else {
                    w = "";
                }
                String h = uidl.getStringAttribute("height");
                if (h != null) {
                    h = " height=\"" + h + "\" ";
                } else {
                    h = "";
                }

                setHTML("<img src=\"" + getSrc(uidl, client) + "\"" + w + h
                        + "/>");

                Element el = DOM.getFirstChild(getElement());
                DOM.sinkEvents(el, Event.ONLOAD);
                client.addPngFix(el);

            } else if (type.equals("browser")) {
                if (browserElement == null) {
                    setHTML("<iframe width=\"100%\" height=\"100%\" frameborder=\"0\" src=\""
                            + getSrc(uidl, client) + "\"></iframe>");
                    browserElement = DOM.getFirstChild(getElement());
                } else {
                    DOM.setElementAttribute(browserElement, "src", getSrc(uidl,
                            client));
                }
                clearBrowserElement = false;
            } else {
                ApplicationConnection.getConsole().log(
                        "Unknown Embedded type '" + type + "'");
            }
        } else if (uidl.hasAttribute("mimetype")) {
            final String mime = uidl.getStringAttribute("mimetype");
            if (mime.equals("application/x-shockwave-flash")) {
                setHTML("<object width=\"" + width + "\" height=\"" + heigth
                        + "\"><param name=\"movie\" value=\""
                        + getSrc(uidl, client) + "\"><embed src=\""
                        + getSrc(uidl, client) + "\" width=\"" + width
                        + "\" height=\"" + heigth + "\"></embed></object>");
            } else {
                ApplicationConnection.getConsole().log(
                        "Unknown Embedded mimetype '" + mime + "'");
            }
        } else {
            ApplicationConnection.getConsole().log(
                    "Unknown Embedded; no type or mimetype attribute");
        }

        if (clearBrowserElement) {
            browserElement = null;
        }

    }

    /**
     * Helper to return translated src-attribute from embedded's UIDL
     * 
     * @param uidl
     * @param client
     * @return
     */
    private String getSrc(UIDL uidl, ApplicationConnection client) {
        String url = client.translateToolkitUri(uidl.getStringAttribute("src"));
        if (url == null) {
            return "";
        }
        return url;
    }

    public void setWidth(String width) {
        if (width == null || width.equals("")) {
            width = "100%";
        }
        this.width = width;
        super.setHeight(width);
    }

    public void setHeight(String height) {
        if (height == null || height.equals("")) {
            height = "100%";
        }
        heigth = height;
        super.setHeight(height);
    }

    protected void onDetach() {
        // Force browser to fire unload event when component is detached from
        // the view (IE doesn't do this automatically)
        if (browserElement != null) {
            DOM.setElementAttribute(browserElement, "src", "javascript:false");
        }
        super.onDetach();
    }

    public void onBrowserEvent(Event event) {
        super.onBrowserEvent(event);
        if (DOM.eventGetType(event) == Event.ONLOAD) {
            client.requestLayoutPhase();
        }
    }
}

@@ -89,16 +89,65 @@
 nt.ui;%0A%0A
+import java.util.HashSet;%0Aimport java.util.Set;%0A%0A
 import c
@@ -177,16 +177,16 @@
 nt.DOM;%0A
-
 import c
@@ -304,16 +304,61 @@
 i.HTML;%0A
+import com.google.gwt.user.client.ui.Widget;%0A
 import c
@@ -475,16 +475,16 @@
 ntable;%0A
-
 import c
@@ -526,16 +526,68 @@
 nt.UIDL;
+%0Aimport com.itmill.toolkit.terminal.gwt.client.Util;
 %0A%0Apublic
@@ -4854,34 +4854,111 @@
     
-client.requestLayoutPhase(
+Set%3CWidget%3E w = new HashSet%3CWidget%3E();%0A            w.add(this);%0A            Util.componentSizeUpdated(w
 );%0A 

Fix for #2042 - Embedded notifies parent of size change when loaded