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.

CheckBox.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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;
  17. import com.vaadin.data.Property;
  18. import com.vaadin.event.FieldEvents.BlurEvent;
  19. import com.vaadin.event.FieldEvents.BlurListener;
  20. import com.vaadin.event.FieldEvents.FocusAndBlurServerRpcImpl;
  21. import com.vaadin.event.FieldEvents.FocusEvent;
  22. import com.vaadin.event.FieldEvents.FocusListener;
  23. import com.vaadin.shared.MouseEventDetails;
  24. import com.vaadin.shared.ui.checkbox.CheckBoxServerRpc;
  25. import com.vaadin.shared.ui.checkbox.CheckBoxState;
  26. public class CheckBox extends AbstractField<Boolean> {
  27. private CheckBoxServerRpc rpc = new CheckBoxServerRpc() {
  28. @Override
  29. public void setChecked(boolean checked,
  30. MouseEventDetails mouseEventDetails) {
  31. if (isReadOnly()) {
  32. return;
  33. }
  34. /*
  35. * Client side updates the state before sending the event so we need
  36. * to make sure the cached state is updated to match the client. If
  37. * we do not do this, a reverting setValue() call in a listener will
  38. * not cause the new state to be sent to the client.
  39. *
  40. * See #11028, #10030.
  41. */
  42. getUI().getConnectorTracker().getDiffState(CheckBox.this)
  43. .put("checked", checked);
  44. final Boolean oldValue = getValue();
  45. final Boolean newValue = checked;
  46. if (!newValue.equals(oldValue)) {
  47. // The event is only sent if the switch state is changed
  48. setValue(newValue);
  49. }
  50. }
  51. };
  52. FocusAndBlurServerRpcImpl focusBlurRpc = new FocusAndBlurServerRpcImpl(this) {
  53. @Override
  54. protected void fireEvent(Event event) {
  55. CheckBox.this.fireEvent(event);
  56. }
  57. };
  58. /**
  59. * Creates a new checkbox.
  60. */
  61. public CheckBox() {
  62. registerRpc(rpc);
  63. registerRpc(focusBlurRpc);
  64. setValue(Boolean.FALSE);
  65. }
  66. /**
  67. * Creates a new checkbox with a set caption.
  68. *
  69. * @param caption
  70. * the Checkbox caption.
  71. */
  72. public CheckBox(String caption) {
  73. this();
  74. setCaption(caption);
  75. }
  76. /**
  77. * Creates a new checkbox with a caption and a set initial state.
  78. *
  79. * @param caption
  80. * the caption of the checkbox
  81. * @param initialState
  82. * the initial state of the checkbox
  83. */
  84. public CheckBox(String caption, boolean initialState) {
  85. this(caption);
  86. setValue(initialState);
  87. }
  88. /**
  89. * Creates a new checkbox that is connected to a boolean property.
  90. *
  91. * @param state
  92. * the Initial state of the switch-button.
  93. * @param dataSource
  94. */
  95. public CheckBox(String caption, Property<?> dataSource) {
  96. this(caption);
  97. setPropertyDataSource(dataSource);
  98. }
  99. @Override
  100. public Class<Boolean> getType() {
  101. return Boolean.class;
  102. }
  103. @Override
  104. protected CheckBoxState getState() {
  105. return (CheckBoxState) super.getState();
  106. }
  107. /*
  108. * Overridden to keep the shared state in sync with the AbstractField
  109. * internal value. Should be removed once AbstractField is refactored to use
  110. * shared state.
  111. *
  112. * See tickets #10921 and #11064.
  113. */
  114. @Override
  115. protected void setInternalValue(Boolean newValue) {
  116. super.setInternalValue(newValue);
  117. if (newValue == null) {
  118. newValue = false;
  119. }
  120. getState().checked = newValue;
  121. }
  122. public void addBlurListener(BlurListener listener) {
  123. addListener(BlurEvent.EVENT_ID, BlurEvent.class, listener,
  124. BlurListener.blurMethod);
  125. }
  126. /**
  127. * @deprecated As of 7.0, replaced by {@link #addBlurListener(BlurListener)}
  128. **/
  129. @Deprecated
  130. public void addListener(BlurListener listener) {
  131. addBlurListener(listener);
  132. }
  133. public void removeBlurListener(BlurListener listener) {
  134. removeListener(BlurEvent.EVENT_ID, BlurEvent.class, listener);
  135. }
  136. /**
  137. * @deprecated As of 7.0, replaced by
  138. * {@link #removeBlurListener(BlurListener)}
  139. **/
  140. @Deprecated
  141. public void removeListener(BlurListener listener) {
  142. removeBlurListener(listener);
  143. }
  144. public void addFocusListener(FocusListener listener) {
  145. addListener(FocusEvent.EVENT_ID, FocusEvent.class, listener,
  146. FocusListener.focusMethod);
  147. }
  148. /**
  149. * @deprecated As of 7.0, replaced by
  150. * {@link #addFocusListener(FocusListener)}
  151. **/
  152. @Deprecated
  153. public void addListener(FocusListener listener) {
  154. addFocusListener(listener);
  155. }
  156. public void removeFocusListener(FocusListener listener) {
  157. removeListener(FocusEvent.EVENT_ID, FocusEvent.class, listener);
  158. }
  159. /**
  160. * @deprecated As of 7.0, replaced by
  161. * {@link #removeFocusListener(FocusListener)}
  162. **/
  163. @Deprecated
  164. public void removeListener(FocusListener listener) {
  165. removeFocusListener(listener);
  166. }
  167. /**
  168. * Get the boolean value of the button state.
  169. *
  170. * @return True iff the button is pressed down or checked.
  171. *
  172. * @deprecated As of 7.0, use {@link #getValue()} instead and, if needed,
  173. * handle null values.
  174. */
  175. @Deprecated
  176. public boolean booleanValue() {
  177. Boolean value = getValue();
  178. return (null == value) ? false : value.booleanValue();
  179. }
  180. }