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.

RadioButtonGroupConnector.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.optiongroup;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import com.google.gwt.dom.client.Element;
  20. import com.vaadin.client.TooltipInfo;
  21. import com.vaadin.client.annotations.OnStateChange;
  22. import com.vaadin.client.communication.StateChangeEvent;
  23. import com.vaadin.client.connectors.AbstractSingleSelectConnector;
  24. import com.vaadin.client.data.DataSource;
  25. import com.vaadin.client.ui.VRadioButtonGroup;
  26. import com.vaadin.shared.Range;
  27. import com.vaadin.shared.Registration;
  28. import com.vaadin.shared.data.selection.SelectionServerRpc;
  29. import com.vaadin.shared.ui.Connect;
  30. import com.vaadin.shared.ui.ListingJsonConstants;
  31. import com.vaadin.shared.ui.optiongroup.RadioButtonGroupState;
  32. import com.vaadin.ui.RadioButtonGroup;
  33. import elemental.json.JsonObject;
  34. /**
  35. * CheckBoxGroup client side connector.
  36. *
  37. * @author Vaadin Ltd
  38. * @since 8.0
  39. */
  40. @Connect(RadioButtonGroup.class)
  41. public class RadioButtonGroupConnector
  42. extends AbstractSingleSelectConnector<VRadioButtonGroup> {
  43. private Registration selectionChangeRegistration;
  44. private Registration dataChangeRegistration;
  45. private final SelectionServerRpc selectionRpc = getRpcProxy(
  46. SelectionServerRpc.class);
  47. @Override
  48. protected void init() {
  49. super.init();
  50. selectionChangeRegistration = getWidget().addSelectionChangeHandler(
  51. event -> selectionRpc.select(getRowKey(event)));
  52. }
  53. @Override
  54. public void onUnregister() {
  55. super.onUnregister();
  56. selectionChangeRegistration.remove();
  57. selectionChangeRegistration = null;
  58. }
  59. @Override
  60. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  61. super.onStateChanged(stateChangeEvent);
  62. getWidget().setTabIndex(getState().tabIndex);
  63. getWidget().client = getConnection();
  64. }
  65. @Override
  66. public void setDataSource(DataSource<JsonObject> dataSource) {
  67. if (dataChangeRegistration != null) {
  68. dataChangeRegistration.remove();
  69. }
  70. dataChangeRegistration = dataSource
  71. .addDataChangeHandler(this::onDataChange);
  72. super.setDataSource(dataSource);
  73. }
  74. @OnStateChange("readOnly")
  75. @SuppressWarnings("deprecation")
  76. void updateWidgetReadOnly() {
  77. getWidget().setEnabled(isEnabled() && !isReadOnly());
  78. }
  79. @OnStateChange("selectedItemKey")
  80. void updateSelectedItem() {
  81. getWidget().selectItemKey(getState().selectedItemKey);
  82. }
  83. @Override
  84. public RadioButtonGroupState getState() {
  85. return (RadioButtonGroupState) super.getState();
  86. }
  87. /**
  88. * A data change handler registered to the data source. Updates the data
  89. * items and selection status when the data source notifies of new changes
  90. * from the server side.
  91. *
  92. * @param range
  93. * the new range of data items
  94. */
  95. private void onDataChange(Range range) {
  96. assert range.getStart() == 0 && range.getEnd() == getDataSource()
  97. .size() : "RadioButtonGroup only supports full updates, but "
  98. + "got range " + range;
  99. final VRadioButtonGroup select = getWidget();
  100. DataSource<JsonObject> dataSource = getDataSource();
  101. int size = dataSource.size();
  102. List<JsonObject> options = new ArrayList<>();
  103. for (int i = 0; i < size; i++) {
  104. options.add(dataSource.getRow(i));
  105. }
  106. select.buildOptions(options);
  107. getLayoutManager().setNeedsMeasure(this);
  108. updateSelectedItem();
  109. }
  110. @Override
  111. public TooltipInfo getTooltipInfo(Element element) {
  112. JsonObject item = getWidget().getItem(element);
  113. if (item != null
  114. && item.hasKey(ListingJsonConstants.JSONKEY_ITEM_DESCRIPTION)) {
  115. return new TooltipInfo(item
  116. .getString(ListingJsonConstants.JSONKEY_ITEM_DESCRIPTION));
  117. }
  118. return super.getTooltipInfo(element);
  119. }
  120. @Override
  121. public boolean hasTooltip() {
  122. return true;
  123. }
  124. }