Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

RadioButtonGroupConnector.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2000-2018 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. void updateWidgetReadOnly() {
  76. getWidget().setReadonly(isReadOnly());
  77. }
  78. @OnStateChange("enabled")
  79. void updateWidgetEnabled() {
  80. getWidget().setEnabled(isEnabled());
  81. }
  82. @OnStateChange("selectedItemKey")
  83. void updateSelectedItem() {
  84. getWidget().selectItemKey(getState().selectedItemKey);
  85. }
  86. @Override
  87. public RadioButtonGroupState getState() {
  88. return (RadioButtonGroupState) super.getState();
  89. }
  90. /**
  91. * A data change handler registered to the data source. Updates the data
  92. * items and selection status when the data source notifies of new changes
  93. * from the server side.
  94. *
  95. * @param range
  96. * the new range of data items
  97. */
  98. private void onDataChange(Range range) {
  99. assert range.getStart() == 0 && range.getEnd() == getDataSource()
  100. .size() : "RadioButtonGroup only supports full updates, but "
  101. + "got range " + range;
  102. final VRadioButtonGroup select = getWidget();
  103. DataSource<JsonObject> dataSource = getDataSource();
  104. int size = dataSource.size();
  105. List<JsonObject> options = new ArrayList<>();
  106. for (int i = 0; i < size; i++) {
  107. options.add(dataSource.getRow(i));
  108. }
  109. select.buildOptions(options);
  110. getLayoutManager().setNeedsMeasure(this);
  111. updateSelectedItem();
  112. }
  113. @Override
  114. public TooltipInfo getTooltipInfo(Element element) {
  115. JsonObject item = getWidget().getItem(element);
  116. if (item != null
  117. && item.hasKey(ListingJsonConstants.JSONKEY_ITEM_DESCRIPTION)) {
  118. return new TooltipInfo(item
  119. .getString(ListingJsonConstants.JSONKEY_ITEM_DESCRIPTION));
  120. }
  121. return super.getTooltipInfo(element);
  122. }
  123. @Override
  124. public boolean hasTooltip() {
  125. return true;
  126. }
  127. }