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.

SingleSelectionConnector.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.connectors.selection;
  17. import java.util.Optional;
  18. import com.vaadin.client.ServerConnector;
  19. import com.vaadin.client.connectors.AbstractListingConnector;
  20. import com.vaadin.shared.data.selection.SelectionModel;
  21. import com.vaadin.shared.data.selection.SelectionServerRpc;
  22. import com.vaadin.shared.ui.Connect;
  23. /**
  24. * A connector for single selection extensions.
  25. *
  26. * @author Vaadin Ltd.
  27. */
  28. @Connect(com.vaadin.data.selection.SingleSelection.class)
  29. public class SingleSelectionConnector extends AbstractSelectionConnector {
  30. private static class SingleSelection implements
  31. SelectionModel.Single<String> {
  32. private String value;
  33. private SelectionServerRpc rpc;
  34. SingleSelection(SelectionServerRpc rpc) {
  35. this.rpc = rpc;
  36. }
  37. @Override
  38. public void select(String item) {
  39. if (item != null && !item.equals(value)) {
  40. rpc.select(item);
  41. value = item;
  42. }
  43. }
  44. @Override
  45. public void deselect(String item) {
  46. if (item != null && item.equals(value)) {
  47. rpc.deselect(item);
  48. value = null;
  49. }
  50. }
  51. @Override
  52. public boolean isSelected(String item) {
  53. return value != null && value.equals(item);
  54. }
  55. @Override
  56. public Optional<String> getSelectedItem() {
  57. return Optional.ofNullable(value);
  58. }
  59. }
  60. private AbstractListingConnector parent;
  61. @Override
  62. public void onUnregister() {
  63. super.onUnregister();
  64. if (parent.getSelectionModel() == getSelectionModel()) {
  65. parent.setSelectionModel(null);
  66. }
  67. }
  68. @Override
  69. protected void extend(ServerConnector target) {
  70. super.extend(target);
  71. parent = getParent();
  72. }
  73. @Override
  74. protected SelectionModel<String> createSelectionModel() {
  75. return new SingleSelection(getRpcProxy(SelectionServerRpc.class));
  76. }
  77. }