package com.itmill.toolkit.terminal.gwt.client;
+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.google.gwt.user.client.ui.Label;
-import com.google.gwt.user.client.ui.VerticalPanel;
-import com.google.gwt.user.client.ui.Widget;
+import com.google.gwt.user.client.ui.PopupPanel;
+import com.google.gwt.user.client.ui.SimplePanel;
public class Caption extends HTML {
private Paintable owner;
+ private Element errorIndicatorElement;
+
+ private Element captionText;
+
+ private ErrorMessage errorMessage;
+
+ private PopupPanel errorContainer;
+
+ /* Caption must be attached to a Paintable */
+ private Caption(){};
+
public Caption(Paintable component) {
+ super();
owner = component;
setStyleName("i-caption");
}
public void updateCaption(UIDL uidl) {
setVisible(!uidl.getBooleanAttribute("invisible"));
- String c = uidl.getStringAttribute("caption");
- if (c == null) {
- } else {
- setText(c);
+ if(uidl.hasAttribute("error")) {
+ UIDL errorUidl = uidl.getErrors();
+
+ if(errorIndicatorElement == null) {
+ errorIndicatorElement = DOM.createDiv();
+ DOM.setAttribute(errorIndicatorElement, "className", "i-errorindicator");
+ DOM.insertChild(getElement(), errorIndicatorElement, 0);
+ }
+
+ errorMessage = new ErrorMessage();
+ errorMessage.updateFromUIDL(errorUidl);
+
+ } else if( errorIndicatorElement != null) {
+ DOM.setStyleAttribute(errorIndicatorElement, "display", "none");
}
- if(uidl.hasAttribute("description")) {
- setTitle(uidl.getStringAttribute("description"));
+
+ if(uidl.hasAttribute("caption")) {
+ if(captionText == null) {
+ captionText = DOM.createSpan();
+ DOM.appendChild(getElement(), captionText);
+ }
+ DOM.setInnerText(captionText, uidl.getStringAttribute("caption"));
}
- if(uidl.hasAttribute("error")) {
- // TODO error messages
+ if(uidl.hasAttribute("description")) {
+ if(captionText != null) {
+ DOM.setAttribute(captionText, "title", uidl.getStringAttribute("description"));
+ } else {
+ setTitle(uidl.getStringAttribute("description"));
+ }
}
+
}
+ public void onBrowserEvent(Event event) {
+ Element target= DOM.eventGetTarget(event);
+ if(errorIndicatorElement != null && DOM.compare(target, errorIndicatorElement)) {
+ switch (DOM.eventGetType(event)) {
+ case Event.ONMOUSEOVER:
+ showErrorMessage();
+ break;
+ case Event.ONMOUSEOUT:
+ hideErrorMessage();
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ private void hideErrorMessage() {
+ if(errorContainer != null) {
+ errorContainer.hide();
+ }
+ }
+
+ private void showErrorMessage() {
+ if(errorMessage != null) {
+ if(errorContainer == null) {
+ errorContainer = new PopupPanel();
+ errorContainer.setWidget(errorMessage);
+ }
+ errorContainer.setPopupPosition(
+ DOM.getAbsoluteLeft(errorIndicatorElement) +
+ 2*DOM.getIntAttribute(errorIndicatorElement, "offsetHeight"),
+ DOM.getAbsoluteTop(errorIndicatorElement) +
+ 2*DOM.getIntAttribute(errorIndicatorElement, "offsetHeight"));
+ errorContainer.show();
+ }
+ }
+
public static boolean isNeeded(UIDL uidl) {
if (uidl.getStringAttribute("caption") != null) return true;
// TODO Description and error messages
private JsonTag tag;
+ private int errorsOpen;
+
/**
* Creates a new XMLPrintWriter, without automatic line flushing.
*
mTagArgumentListOpen = true;
customLayoutArgumentsOpen = "customlayout".equals(tagName);
+
+ if("error".equals(tagName)) {
+ errorsOpen++;
+ }
}
/**
throw new PaintException("Invalid UIDL: wrong ending tag: '"
+ tagName + "' expected: '" + lastTag + "'.");
- parent.addData(tag.getJSON());
+ // simple hack which writes error uidl structure into attribute
+ if("error".equals(lastTag)) {
+ if(errorsOpen == 1) // ending error section
+ parent.addAttribute("\"error\":[\"error\",{}"+tag.getData() + "]");
+ else // sub error
+ parent.addData(tag.getJSON());
+ errorsOpen--;
+ } else {
+ parent.addData(tag.getJSON());
+ }
+
tag = parent;
} else {