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 2.8KB

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