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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2011 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. final Boolean oldValue = getValue();
  35. final Boolean newValue = checked;
  36. if (!newValue.equals(oldValue)) {
  37. // The event is only sent if the switch state is changed
  38. setValue(newValue);
  39. }
  40. }
  41. };
  42. FocusAndBlurServerRpcImpl focusBlurRpc = new FocusAndBlurServerRpcImpl(this) {
  43. @Override
  44. protected void fireEvent(Event event) {
  45. CheckBox.this.fireEvent(event);
  46. }
  47. };
  48. /**
  49. * Creates a new checkbox.
  50. */
  51. public CheckBox() {
  52. registerRpc(rpc);
  53. registerRpc(focusBlurRpc);
  54. setValue(Boolean.FALSE);
  55. }
  56. /**
  57. * Creates a new checkbox with a set caption.
  58. *
  59. * @param caption
  60. * the Checkbox caption.
  61. */
  62. public CheckBox(String caption) {
  63. this();
  64. setCaption(caption);
  65. }
  66. /**
  67. * Creates a new checkbox with a caption and a set initial state.
  68. *
  69. * @param caption
  70. * the caption of the checkbox
  71. * @param initialState
  72. * the initial state of the checkbox
  73. */
  74. public CheckBox(String caption, boolean initialState) {
  75. this(caption);
  76. setValue(initialState);
  77. }
  78. /**
  79. * Creates a new checkbox that is connected to a boolean property.
  80. *
  81. * @param state
  82. * the Initial state of the switch-button.
  83. * @param dataSource
  84. */
  85. public CheckBox(String caption, Property<?> dataSource) {
  86. this(caption);
  87. setPropertyDataSource(dataSource);
  88. }
  89. @Override
  90. public Class<Boolean> getType() {
  91. return Boolean.class;
  92. }
  93. @Override
  94. protected CheckBoxState getState() {
  95. return (CheckBoxState) super.getState();
  96. }
  97. @Override
  98. protected void setInternalValue(Boolean newValue) {
  99. super.setInternalValue(newValue);
  100. if (newValue == null) {
  101. newValue = false;
  102. }
  103. getState().checked = newValue;
  104. }
  105. public void addBlurListener(BlurListener listener) {
  106. addListener(BlurEvent.EVENT_ID, BlurEvent.class, listener,
  107. BlurListener.blurMethod);
  108. }
  109. /**
  110. * @deprecated As of 7.0, replaced by {@link #addBlurListener(BlurListener)}
  111. **/
  112. @Deprecated
  113. public void addListener(BlurListener listener) {
  114. addBlurListener(listener);
  115. }
  116. public void removeBlurListener(BlurListener listener) {
  117. removeListener(BlurEvent.EVENT_ID, BlurEvent.class, listener);
  118. }
  119. /**
  120. * @deprecated As of 7.0, replaced by
  121. * {@link #removeBlurListener(BlurListener)}
  122. **/
  123. @Deprecated
  124. public void removeListener(BlurListener listener) {
  125. removeBlurListener(listener);
  126. }
  127. public void addFocusListener(FocusListener listener) {
  128. addListener(FocusEvent.EVENT_ID, FocusEvent.class, listener,
  129. FocusListener.focusMethod);
  130. }
  131. /**
  132. * @deprecated As of 7.0, replaced by
  133. * {@link #addFocusListener(FocusListener)}
  134. **/
  135. @Deprecated
  136. public void addListener(FocusListener listener) {
  137. addFocusListener(listener);
  138. }
  139. public void removeFocusListener(FocusListener listener) {
  140. removeListener(FocusEvent.EVENT_ID, FocusEvent.class, listener);
  141. }
  142. /**
  143. * @deprecated As of 7.0, replaced by
  144. * {@link #removeFocusListener(FocusListener)}
  145. **/
  146. @Deprecated
  147. public void removeListener(FocusListener listener) {
  148. removeFocusListener(listener);
  149. }
  150. /**
  151. * Get the boolean value of the button state.
  152. *
  153. * @return True iff the button is pressed down or checked.
  154. *
  155. * @deprecated As of 7.0, use {@link #getValue()} instead and, if needed,
  156. * handle null values.
  157. */
  158. @Deprecated
  159. public boolean booleanValue() {
  160. Boolean value = getValue();
  161. return (null == value) ? false : value.booleanValue();
  162. }
  163. }