You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IButton.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.itmill.toolkit.terminal.gwt.client.ui;
  2. import com.google.gwt.user.client.DOM;
  3. import com.google.gwt.user.client.Element;
  4. import com.google.gwt.user.client.Event;
  5. import com.google.gwt.user.client.ui.ClickListener;
  6. import com.google.gwt.user.client.ui.PopupPanel;
  7. import com.google.gwt.user.client.ui.Widget;
  8. import com.google.gwt.user.client.ui.Button;
  9. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  10. import com.itmill.toolkit.terminal.gwt.client.ErrorMessage;
  11. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  12. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  13. public class IButton extends Button implements Paintable {
  14. public static final String CLASSNAME = "i-button";
  15. String id;
  16. ApplicationConnection client;
  17. private Element errorIndicatorElement;
  18. private ErrorMessage errorMessage;
  19. private PopupPanel errorContainer;
  20. public IButton() {
  21. setStyleName(CLASSNAME);
  22. addClickListener(new ClickListener() {
  23. public void onClick(Widget sender) {
  24. if (id == null || client == null)
  25. return;
  26. client.updateVariable(id, "state", true, true);
  27. }
  28. });
  29. }
  30. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  31. // Ensure correct implementation,
  32. // but don't let container manage caption etc.
  33. if (client.updateComponent(this, uidl, false))
  34. return;
  35. // Save details
  36. this.client = client;
  37. id = uidl.getId();
  38. // Set text
  39. setText(uidl.getStringAttribute("caption"));
  40. if (uidl.hasAttribute("error")) {
  41. UIDL errorUidl = uidl.getErrors();
  42. if (errorIndicatorElement == null) {
  43. errorIndicatorElement = DOM.createDiv();
  44. DOM.setElementProperty(errorIndicatorElement, "className",
  45. "i-errorindicator");
  46. DOM.sinkEvents(errorIndicatorElement, Event.MOUSEEVENTS);
  47. }
  48. DOM.insertChild(getElement(), errorIndicatorElement, 0);
  49. if (errorMessage == null)
  50. errorMessage = new ErrorMessage();
  51. errorMessage.updateFromUIDL(errorUidl);
  52. } else if (errorIndicatorElement != null) {
  53. DOM.setStyleAttribute(errorIndicatorElement, "display", "none");
  54. }
  55. if (uidl.hasAttribute("description")) {
  56. setTitle(uidl.getStringAttribute("description"));
  57. }
  58. }
  59. public void onBrowserEvent(Event event) {
  60. Element target = DOM.eventGetTarget(event);
  61. if (errorIndicatorElement != null
  62. && DOM.compare(target, errorIndicatorElement)) {
  63. switch (DOM.eventGetType(event)) {
  64. case Event.ONMOUSEOVER:
  65. showErrorMessage();
  66. break;
  67. case Event.ONMOUSEOUT:
  68. hideErrorMessage();
  69. break;
  70. case Event.ONCLICK:
  71. ApplicationConnection.getConsole().log(
  72. DOM.getInnerHTML(errorMessage.getElement()));
  73. return;
  74. default:
  75. break;
  76. }
  77. }
  78. super.onBrowserEvent(event);
  79. }
  80. private void hideErrorMessage() {
  81. if (errorContainer != null) {
  82. errorContainer.hide();
  83. }
  84. }
  85. private void showErrorMessage() {
  86. if (errorMessage != null) {
  87. if (errorContainer == null) {
  88. errorContainer = new PopupPanel();
  89. errorContainer.setWidget(errorMessage);
  90. }
  91. errorContainer.setPopupPosition(DOM
  92. .getAbsoluteLeft(errorIndicatorElement)
  93. + 2
  94. * DOM.getElementPropertyInt(errorIndicatorElement,
  95. "offsetHeight"), DOM
  96. .getAbsoluteTop(errorIndicatorElement)
  97. + 2
  98. * DOM.getElementPropertyInt(errorIndicatorElement,
  99. "offsetHeight"));
  100. errorContainer.show();
  101. }
  102. }
  103. }