]> source.dussan.org Git - vaadin-framework.git/commitdiff
Error, icon, description for Panel
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Thu, 29 Nov 2007 07:02:04 +0000 (07:02 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Thu, 29 Nov 2007 07:02:04 +0000 (07:02 +0000)
svn changeset:3027/svn branch:trunk

src/com/itmill/toolkit/terminal/gwt/client/ui/IPanel.java

index 84a34992a4c2c0775fc2ed1083d8b2c660c8970b..82db2235b4c6da094c91d38e8597bd075e8f47ac 100644 (file)
@@ -2,10 +2,12 @@ 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.SimplePanel;
 import com.google.gwt.user.client.ui.Widget;
 import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
 import com.itmill.toolkit.terminal.gwt.client.ContainerResizedListener;
+import com.itmill.toolkit.terminal.gwt.client.ErrorMessage;
 import com.itmill.toolkit.terminal.gwt.client.Paintable;
 import com.itmill.toolkit.terminal.gwt.client.UIDL;
 import com.itmill.toolkit.terminal.gwt.client.Util;
@@ -21,10 +23,18 @@ public class IPanel extends SimplePanel implements Paintable,
 
     private Element captionNode = DOM.createDiv();
 
+    private Element captionText = DOM.createSpan();
+
+    private Icon icon;
+
     private Element bottomDecoration = DOM.createDiv();
 
     private Element contentNode = DOM.createDiv();
 
+    private Element errorIndicatorElement;
+
+    private ErrorMessage errorMessage;
+
     private String height;
 
     private Widget layout;
@@ -32,6 +42,7 @@ public class IPanel extends SimplePanel implements Paintable,
     public IPanel() {
         super();
         DOM.appendChild(getElement(), captionNode);
+        DOM.appendChild(captionNode, captionText);
         DOM.appendChild(getElement(), contentNode);
         DOM.appendChild(getElement(), bottomDecoration);
         setStyleName(CLASSNAME);
@@ -49,6 +60,10 @@ public class IPanel extends SimplePanel implements Paintable,
         return contentNode;
     }
 
+    private void setCaption(String text) {
+        DOM.setInnerText(captionText, text);
+    }
+
     public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
         // Ensure correct implementation
         if (client.updateComponent(this, uidl, false)) {
@@ -79,14 +94,20 @@ public class IPanel extends SimplePanel implements Paintable,
         boolean hasCaption = false;
         if (uidl.hasAttribute("caption")
                 && !uidl.getStringAttribute("caption").equals("")) {
-            DOM.setInnerText(captionNode, uidl.getStringAttribute("caption"));
+            setCaption(uidl.getStringAttribute("caption"));
             hasCaption = true;
         } else {
-            DOM.setInnerText(captionNode, "");
+            setCaption("");
             DOM.setElementProperty(captionNode, "className", CLASSNAME
                     + "-nocaption");
         }
 
+        setIconUri(uidl, client);
+
+        handleDescription(uidl);
+
+        handleError(uidl);
+
         // Add proper stylenames for all elements. This way we can prevent
         // unwanted CSS selector inheritance.
         if (uidl.hasAttribute("style")) {
@@ -125,6 +146,51 @@ public class IPanel extends SimplePanel implements Paintable,
 
     }
 
+    private void handleError(UIDL uidl) {
+        if (uidl.hasAttribute("error")) {
+            UIDL errorUidl = uidl.getErrors();
+            if (errorIndicatorElement == null) {
+                errorIndicatorElement = DOM.createDiv();
+                DOM.setElementProperty(errorIndicatorElement, "className",
+                        "i-errorindicator");
+                DOM.sinkEvents(errorIndicatorElement, Event.MOUSEEVENTS);
+                sinkEvents(Event.MOUSEEVENTS);
+            }
+            DOM.insertBefore(captionNode, errorIndicatorElement, captionText);
+            if (errorMessage == null) {
+                errorMessage = new ErrorMessage();
+            }
+            errorMessage.updateFromUIDL(errorUidl);
+
+        } else if (errorIndicatorElement != null) {
+            DOM.removeChild(captionNode, errorIndicatorElement);
+            errorIndicatorElement = null;
+        }
+    }
+
+    private void handleDescription(UIDL uidl) {
+        DOM.setElementProperty(captionText, "title", uidl
+                .hasAttribute("description") ? uidl
+                .getStringAttribute("description") : "");
+    }
+
+    private void setIconUri(UIDL uidl, ApplicationConnection client) {
+        String iconUri = uidl.hasAttribute("icon") ? uidl
+                .getStringAttribute("icon") : null;
+        if (iconUri == null) {
+            if (icon != null) {
+                DOM.removeChild(captionNode, icon.getElement());
+                icon = null;
+            }
+        } else {
+            if (icon == null) {
+                icon = new Icon(client);
+                DOM.insertChild(captionNode, icon.getElement(), 0);
+            }
+            icon.setUri(iconUri);
+        }
+    }
+
     public void iLayout() {
         if (height != null && height != "") {
             boolean hasChildren = getWidget() != null;
@@ -175,4 +241,29 @@ public class IPanel extends SimplePanel implements Paintable,
         Util.runDescendentsLayout(this);
     }
 
+    public void onBrowserEvent(Event event) {
+        Element target = DOM.eventGetTarget(event);
+        if (errorIndicatorElement != null
+                && DOM.compare(target, errorIndicatorElement)) {
+            switch (DOM.eventGetType(event)) {
+            case Event.ONMOUSEOVER:
+                if (errorMessage != null) {
+                    errorMessage.showAt(errorIndicatorElement);
+                }
+                break;
+            case Event.ONMOUSEOUT:
+                if (errorMessage != null) {
+                    errorMessage.hide();
+                }
+                break;
+            case Event.ONCLICK:
+                ApplicationConnection.getConsole().log(
+                        DOM.getInnerHTML(errorMessage.getElement()));
+                return;
+            default:
+                break;
+            }
+        }
+    }
+
 }