Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CheckBox.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.ui.components.fields;
  17. import java.util.Collection;
  18. import java.util.LinkedHashSet;
  19. import java.util.Set;
  20. import org.jsoup.nodes.Attributes;
  21. import org.jsoup.nodes.Element;
  22. import com.vaadin.event.FieldEvents.FocusAndBlurServerRpcImpl;
  23. import com.vaadin.event.handler.Handler;
  24. import com.vaadin.event.handler.Registration;
  25. import com.vaadin.shared.MouseEventDetails;
  26. import com.vaadin.shared.ui.checkbox.CheckBoxServerRpc;
  27. import com.vaadin.shared.ui.checkbox.CheckBoxState;
  28. import com.vaadin.ui.AbstractComponent;
  29. import com.vaadin.ui.components.HasValue;
  30. import com.vaadin.ui.declarative.DesignAttributeHandler;
  31. import com.vaadin.ui.declarative.DesignContext;
  32. public class CheckBox extends AbstractComponent implements HasValue<Boolean> {
  33. private final LinkedHashSet<Handler<Boolean>> handlers = new LinkedHashSet<>();
  34. private CheckBoxServerRpc rpc = new CheckBoxServerRpc() {
  35. @Override
  36. public void setChecked(boolean checked,
  37. MouseEventDetails mouseEventDetails) {
  38. if (isReadOnly()) {
  39. return;
  40. }
  41. /*
  42. * Client side updates the state before sending the event so we need
  43. * to make sure the cached state is updated to match the client. If
  44. * we do not do this, a reverting setValue() call in a listener will
  45. * not cause the new state to be sent to the client.
  46. *
  47. * See #11028, #10030.
  48. */
  49. getUI().getConnectorTracker().getDiffState(CheckBox.this)
  50. .put("checked", checked);
  51. final Boolean oldValue = getValue();
  52. final Boolean newValue = checked;
  53. if (!newValue.equals(oldValue)) {
  54. // The event is only sent if the switch state is changed
  55. setValue(newValue, true);
  56. }
  57. }
  58. };
  59. FocusAndBlurServerRpcImpl focusBlurRpc = new FocusAndBlurServerRpcImpl(this) {
  60. @Override
  61. protected void fireEvent(Event event) {
  62. CheckBox.this.fireEvent(event);
  63. }
  64. };
  65. /**
  66. * Creates a new checkbox.
  67. */
  68. public CheckBox() {
  69. registerRpc(rpc);
  70. registerRpc(focusBlurRpc);
  71. setValue(Boolean.FALSE);
  72. }
  73. /**
  74. * Creates a new checkbox with a set caption.
  75. *
  76. * @param caption
  77. * the Checkbox caption.
  78. */
  79. public CheckBox(String caption) {
  80. this();
  81. setCaption(caption);
  82. }
  83. /**
  84. * Creates a new checkbox with a caption and a set initial state.
  85. *
  86. * @param caption
  87. * the caption of the checkbox
  88. * @param initialState
  89. * the initial state of the checkbox
  90. */
  91. public CheckBox(String caption, boolean initialState) {
  92. this(caption);
  93. setValue(initialState);
  94. }
  95. @Override
  96. public void setValue(Boolean value) {
  97. setValue(value, false);
  98. }
  99. protected void setValue(Boolean value, Boolean userOriginated) {
  100. getState().checked = value;
  101. if (!userOriginated)
  102. markAsDirty();
  103. if (!handlers.isEmpty()) {
  104. // name conflict
  105. com.vaadin.event.handler.Event<Boolean> event = new com.vaadin.event.handler.Event<Boolean>(
  106. this, new Boolean(value), userOriginated);
  107. Set<Handler<Boolean>> copy = new LinkedHashSet<>(handlers);
  108. for (Handler<Boolean> handler : copy) {
  109. handler.handleEvent(event);
  110. }
  111. }
  112. }
  113. @Override
  114. public Registration onChange(Handler<Boolean> handler) {
  115. if (handler == null)
  116. throw new IllegalArgumentException("Handler can't be null");
  117. handlers.add(handler);
  118. return () -> handlers.remove(handler);
  119. }
  120. @Override
  121. public Boolean getValue() {
  122. return getState().checked;
  123. }
  124. @Override
  125. protected CheckBoxState getState() {
  126. return (CheckBoxState) super.getState();
  127. }
  128. @Override
  129. protected CheckBoxState getState(boolean markAsDirty) {
  130. return (CheckBoxState) super.getState(markAsDirty);
  131. }
  132. /*
  133. * (non-Javadoc)
  134. *
  135. * @see com.vaadin.ui.AbstractField#readDesign(org.jsoup.nodes.Element,
  136. * com.vaadin.ui.declarative.DesignContext)
  137. */
  138. @Override
  139. public void readDesign(Element design, DesignContext designContext) {
  140. super.readDesign(design, designContext);
  141. if (design.hasAttr("checked")) {
  142. this.setValue(
  143. DesignAttributeHandler.readAttribute("checked",
  144. design.attributes(), Boolean.class), false);
  145. }
  146. }
  147. /*
  148. * (non-Javadoc)
  149. *
  150. * @see com.vaadin.ui.AbstractField#writeDesign(org.jsoup.nodes.Element,
  151. * com.vaadin.ui.declarative.DesignContext)
  152. */
  153. @Override
  154. public void writeDesign(Element design, DesignContext designContext) {
  155. super.writeDesign(design, designContext);
  156. CheckBox def = (CheckBox) designContext.getDefaultInstance(this);
  157. Attributes attr = design.attributes();
  158. DesignAttributeHandler.writeAttribute("checked", attr, getValue(),
  159. def.getValue(), Boolean.class);
  160. }
  161. /*
  162. * (non-Javadoc)
  163. *
  164. * @see com.vaadin.ui.AbstractField#getCustomAttributes()
  165. */
  166. @Override
  167. protected Collection<String> getCustomAttributes() {
  168. Collection<String> attributes = super.getCustomAttributes();
  169. attributes.add("checked");
  170. return attributes;
  171. }
  172. }