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.

SingleSelection.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2000-2014 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.server.communication.data.typed;
  17. import java.util.Collection;
  18. import java.util.Collections;
  19. import java.util.LinkedHashSet;
  20. import java.util.Set;
  21. import com.vaadin.event.handler.Handler;
  22. import com.vaadin.event.handler.Registration;
  23. import com.vaadin.server.communication.data.typed.SelectionModel.Single;
  24. import com.vaadin.shared.data.selection.SelectionServerRpc;
  25. /**
  26. * {@link SelectionModel} for selecting a single value.
  27. *
  28. * @param <T>
  29. * type of selected data
  30. */
  31. public class SingleSelection<T> extends AbstractSelectionModel<T> implements
  32. Single<T> {
  33. private final LinkedHashSet<Handler<T>> handlers = new LinkedHashSet<>();
  34. public SingleSelection() {
  35. registerRpc(new SelectionServerRpc() {
  36. @Override
  37. public void select(String key) {
  38. setValue(getData(key), true);
  39. }
  40. @Override
  41. public void deselect(String key) {
  42. if (getData(key).equals(value)) {
  43. setValue(null, true);
  44. }
  45. }
  46. });
  47. }
  48. private T value = null;
  49. @Override
  50. public Collection<T> getSelected() {
  51. if (value != null) {
  52. return Collections.singleton(value);
  53. } else {
  54. return Collections.emptySet();
  55. }
  56. }
  57. @Override
  58. public void setValue(T value) {
  59. setValue(value, false);
  60. }
  61. protected void setValue(T value, boolean userOriginated) {
  62. if (this.value != value) {
  63. if (this.value != null) {
  64. refresh(this.value);
  65. }
  66. this.value = value;
  67. if (value != null) {
  68. refresh(value);
  69. }
  70. Set<Handler<T>> copy = new LinkedHashSet<>(handlers);
  71. for (Handler<T> handler : copy) {
  72. handler.handleEvent(new com.vaadin.event.handler.Event<T>(this
  73. .getParent(), value, userOriginated));
  74. }
  75. }
  76. }
  77. @Override
  78. public T getValue() {
  79. return value;
  80. }
  81. @Override
  82. public Registration onChange(Handler<T> handler) {
  83. if (handler == null) {
  84. throw new IllegalArgumentException("Handler can't be null");
  85. }
  86. handlers.add(handler);
  87. return () -> handlers.remove(handler);
  88. }
  89. @Override
  90. public void select(T value) {
  91. if (this.value != value) {
  92. setValue(value);
  93. }
  94. }
  95. @Override
  96. public void deselect(T value) {
  97. if (this.value == value) {
  98. setValue(null);
  99. }
  100. }
  101. @Override
  102. public void remove() {
  103. if (getValue() != null) {
  104. refresh(getValue());
  105. }
  106. super.remove();
  107. }
  108. }