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.

CheckBoxGroupConnector.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.Collections;
  19. import java.util.HashSet;
  20. import java.util.List;
  21. import com.vaadin.client.communication.StateChangeEvent;
  22. import com.vaadin.client.connectors.AbstractListingConnector;
  23. import com.vaadin.client.data.DataSource;
  24. import com.vaadin.client.ui.ConnectorFocusAndBlurHandler;
  25. import com.vaadin.client.ui.VCheckBoxGroup;
  26. import com.vaadin.shared.data.selection.MultiSelectServerRpc;
  27. import com.vaadin.shared.data.selection.SelectionModel;
  28. import com.vaadin.shared.ui.Connect;
  29. import com.vaadin.shared.ui.optiongroup.CheckBoxGroupState;
  30. import com.vaadin.ui.CheckBoxGroup;
  31. import elemental.json.JsonObject;
  32. @Connect(CheckBoxGroup.class)
  33. // We don't care about the framework-provided selection model at this point
  34. public class CheckBoxGroupConnector
  35. extends AbstractListingConnector<SelectionModel<?>> {
  36. private ConnectorFocusAndBlurHandler handler;
  37. @Override
  38. protected void init() {
  39. super.init();
  40. getWidget().addSelectionChangeHandler(this::selectionChanged);
  41. handler = ConnectorFocusAndBlurHandler.addHandlers(this);
  42. }
  43. @Override
  44. public void onUnregister() {
  45. super.onUnregister();
  46. handler.removeHandlers();
  47. handler = null;
  48. }
  49. private void selectionChanged(JsonObject changedItem, Boolean selected) {
  50. MultiSelectServerRpc rpc = getRpcProxy(MultiSelectServerRpc.class);
  51. String key = getRowKey(changedItem);
  52. HashSet<String> change = new HashSet<>();
  53. change.add(key);
  54. if (Boolean.TRUE.equals(selected)) {
  55. rpc.updateSelection(change, Collections.emptySet());
  56. } else {
  57. rpc.updateSelection(Collections.emptySet(), change);
  58. }
  59. }
  60. @Override
  61. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  62. super.onStateChanged(stateChangeEvent);
  63. getWidget().setTabIndex(getState().tabIndex);
  64. getWidget().setReadonly(isReadOnly());
  65. getWidget().client = getConnection();
  66. }
  67. @Override
  68. public void setDataSource(DataSource<JsonObject> dataSource) {
  69. dataSource.addDataChangeHandler(range -> updateOptionGroup());
  70. super.setDataSource(dataSource);
  71. }
  72. private void updateOptionGroup() {
  73. List<JsonObject> items = new ArrayList<>(getDataSource().size());
  74. for (int i = 0; i < getDataSource().size(); ++i) {
  75. JsonObject item = getDataSource().getRow(i);
  76. items.add(item);
  77. }
  78. getWidget().buildOptions(items);
  79. }
  80. @Override
  81. public VCheckBoxGroup getWidget() {
  82. return (VCheckBoxGroup) super.getWidget();
  83. }
  84. @Override
  85. public CheckBoxGroupState getState() {
  86. return (CheckBoxGroupState) super.getState();
  87. }
  88. }