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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal.gwt.client.ui;
  5. import com.google.gwt.user.client.DOM;
  6. import com.google.gwt.user.client.Element;
  7. import com.google.gwt.user.client.Event;
  8. import com.google.gwt.user.client.ui.Button;
  9. import com.google.gwt.user.client.ui.ClickListener;
  10. import com.google.gwt.user.client.ui.Widget;
  11. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  12. import com.itmill.toolkit.terminal.gwt.client.BrowserInfo;
  13. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  14. import com.itmill.toolkit.terminal.gwt.client.ITooltip;
  15. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  16. public class IButton extends Button implements Paintable {
  17. public static final String CLASSNAME = "i-button";
  18. String id;
  19. ApplicationConnection client;
  20. private Element errorIndicatorElement;
  21. private final Element captionElement = DOM.createSpan();
  22. private Icon icon;
  23. public IButton() {
  24. setStyleName(CLASSNAME);
  25. DOM.appendChild(getElement(), captionElement);
  26. addClickListener(new ClickListener() {
  27. public void onClick(Widget sender) {
  28. if (id == null || client == null) {
  29. return;
  30. }
  31. /*
  32. * TODO isolata workaround. Safari don't always seem to fire
  33. * onblur previously focused component before button is clicked.
  34. */
  35. IButton.this.setFocus(true);
  36. client.updateVariable(id, "state", true, true);
  37. }
  38. });
  39. sinkEvents(ITooltip.TOOLTIP_EVENTS);
  40. }
  41. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  42. // Ensure correct implementation,
  43. // but don't let container manage caption etc.
  44. if (client.updateComponent(this, uidl, false)) {
  45. return;
  46. }
  47. // Save details
  48. this.client = client;
  49. id = uidl.getId();
  50. // Set text
  51. setText(uidl.getStringAttribute("caption"));
  52. // handle error
  53. if (uidl.hasAttribute("error")) {
  54. if (errorIndicatorElement == null) {
  55. errorIndicatorElement = DOM.createDiv();
  56. DOM.setElementProperty(errorIndicatorElement, "className",
  57. "i-errorindicator");
  58. }
  59. DOM.insertChild(getElement(), errorIndicatorElement, 0);
  60. // Fix for IE6, IE7
  61. if (BrowserInfo.get().isIE()) {
  62. DOM.setInnerText(errorIndicatorElement, " ");
  63. }
  64. } else if (errorIndicatorElement != null) {
  65. DOM.removeChild(getElement(), errorIndicatorElement);
  66. errorIndicatorElement = null;
  67. }
  68. if (uidl.hasAttribute("readonly")) {
  69. setEnabled(false);
  70. }
  71. if (uidl.hasAttribute("icon")) {
  72. if (icon == null) {
  73. icon = new Icon(client);
  74. DOM.insertChild(getElement(), icon.getElement(), 0);
  75. }
  76. icon.setUri(uidl.getStringAttribute("icon"));
  77. } else {
  78. if (icon != null) {
  79. DOM.removeChild(getElement(), icon.getElement());
  80. icon = null;
  81. }
  82. }
  83. }
  84. public void setStyleName(String style) {
  85. super.setStyleName(style);
  86. if (BrowserInfo.get().isIE7()) {
  87. /*
  88. * Workaround for IE7 bug (#2014) where button width is growing when
  89. * changing styles
  90. */
  91. Element e = getElement();
  92. String w = DOM.getStyleAttribute(e, "width");
  93. DOM.setStyleAttribute(e, "width", "1px");
  94. DOM.setStyleAttribute(e, "width", w);
  95. }
  96. }
  97. public void setText(String text) {
  98. DOM.setInnerText(captionElement, text);
  99. }
  100. public void onBrowserEvent(Event event) {
  101. super.onBrowserEvent(event);
  102. if (client != null) {
  103. client.handleTooltipEvent(event, this);
  104. }
  105. }
  106. }