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.

ButtonConnector.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui.button;
  17. import com.google.gwt.event.dom.client.ClickEvent;
  18. import com.google.gwt.event.dom.client.ClickHandler;
  19. import com.google.gwt.user.client.DOM;
  20. import com.vaadin.client.MouseEventDetailsBuilder;
  21. import com.vaadin.client.StyleConstants;
  22. import com.vaadin.client.VCaption;
  23. import com.vaadin.client.WidgetUtil.ErrorUtil;
  24. import com.vaadin.client.annotations.OnStateChange;
  25. import com.vaadin.client.ui.AbstractComponentConnector;
  26. import com.vaadin.client.ui.ConnectorFocusAndBlurHandler;
  27. import com.vaadin.client.ui.Icon;
  28. import com.vaadin.client.ui.VButton;
  29. import com.vaadin.shared.MouseEventDetails;
  30. import com.vaadin.shared.ui.Connect;
  31. import com.vaadin.shared.ui.Connect.LoadStyle;
  32. import com.vaadin.shared.ui.button.ButtonServerRpc;
  33. import com.vaadin.shared.ui.button.ButtonState;
  34. import com.vaadin.ui.Button;
  35. @Connect(value = Button.class, loadStyle = LoadStyle.EAGER)
  36. public class ButtonConnector extends AbstractComponentConnector
  37. implements ClickHandler {
  38. @Override
  39. public boolean delegateCaptionHandling() {
  40. return false;
  41. }
  42. @Override
  43. public void init() {
  44. super.init();
  45. getWidget().addClickHandler(this);
  46. getWidget().client = getConnection();
  47. ConnectorFocusAndBlurHandler.addHandlers(this);
  48. }
  49. @OnStateChange("resources")
  50. void onResourceChange() {
  51. if (getWidget().icon != null) {
  52. getWidget().wrapper.removeChild(getWidget().icon.getElement());
  53. getWidget().icon = null;
  54. }
  55. Icon icon = getIcon();
  56. if (icon != null) {
  57. getWidget().icon = icon;
  58. icon.setAlternateText(getState().iconAltText);
  59. getWidget().wrapper.insertBefore(icon.getElement(),
  60. getWidget().captionElement);
  61. }
  62. }
  63. @OnStateChange({ "caption", "captionAsHtml" })
  64. void setCaption() {
  65. VCaption.setCaptionText(getWidget().captionElement, getState());
  66. }
  67. @OnStateChange("iconAltText")
  68. void setIconAltText() {
  69. if (getWidget().icon != null) {
  70. getWidget().icon.setAlternateText(getState().iconAltText);
  71. }
  72. }
  73. @OnStateChange("clickShortcutKeyCode")
  74. void setClickShortcut() {
  75. getWidget().clickShortcut = getState().clickShortcutKeyCode;
  76. }
  77. @Override
  78. public VButton getWidget() {
  79. return (VButton) super.getWidget();
  80. }
  81. @Override
  82. public ButtonState getState() {
  83. return (ButtonState) super.getState();
  84. }
  85. @Override
  86. public void onClick(ClickEvent event) {
  87. if (getState().disableOnClick) {
  88. // Simulate getting disabled from the server without waiting for the
  89. // round trip. The server-side RPC handler takes care of updating
  90. // the server-side state in a similar way to ensure subsequent
  91. // changes are properly propagated. Changing state on client is not
  92. // generally supported.
  93. getState().enabled = false;
  94. super.updateEnabledState(false);
  95. getRpcProxy(ButtonServerRpc.class).disableOnClick();
  96. }
  97. // Add mouse details
  98. MouseEventDetails details = MouseEventDetailsBuilder
  99. .buildMouseEventDetails(event.getNativeEvent(),
  100. getWidget().getElement());
  101. getRpcProxy(ButtonServerRpc.class).click(details);
  102. }
  103. }