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.

OptionGroup.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 java.util.Collection;
  18. import java.util.HashSet;
  19. import java.util.Map;
  20. import java.util.Set;
  21. import com.vaadin.data.Container;
  22. import com.vaadin.event.FieldEvents;
  23. import com.vaadin.event.FieldEvents.BlurEvent;
  24. import com.vaadin.event.FieldEvents.BlurListener;
  25. import com.vaadin.event.FieldEvents.FocusEvent;
  26. import com.vaadin.event.FieldEvents.FocusListener;
  27. import com.vaadin.server.PaintException;
  28. import com.vaadin.server.PaintTarget;
  29. import com.vaadin.shared.ui.optiongroup.OptionGroupConstants;
  30. /**
  31. * Configures select to be used as an option group.
  32. */
  33. @SuppressWarnings("serial")
  34. public class OptionGroup extends AbstractSelect implements
  35. FieldEvents.BlurNotifier, FieldEvents.FocusNotifier {
  36. private Set<Object> disabledItemIds = new HashSet<Object>();
  37. private boolean htmlContentAllowed = false;
  38. public OptionGroup() {
  39. super();
  40. }
  41. public OptionGroup(String caption, Collection<?> options) {
  42. super(caption, options);
  43. }
  44. public OptionGroup(String caption, Container dataSource) {
  45. super(caption, dataSource);
  46. }
  47. public OptionGroup(String caption) {
  48. super(caption);
  49. }
  50. @Override
  51. public void paintContent(PaintTarget target) throws PaintException {
  52. target.addAttribute("type", "optiongroup");
  53. if (isHtmlContentAllowed()) {
  54. target.addAttribute(OptionGroupConstants.HTML_CONTENT_ALLOWED, true);
  55. }
  56. super.paintContent(target);
  57. }
  58. @Override
  59. protected void paintItem(PaintTarget target, Object itemId)
  60. throws PaintException {
  61. super.paintItem(target, itemId);
  62. if (!isItemEnabled(itemId)) {
  63. target.addAttribute(OptionGroupConstants.ATTRIBUTE_OPTION_DISABLED,
  64. true);
  65. }
  66. }
  67. @Override
  68. public void changeVariables(Object source, Map<String, Object> variables) {
  69. super.changeVariables(source, variables);
  70. if (variables.containsKey(FocusEvent.EVENT_ID)) {
  71. fireEvent(new FocusEvent(this));
  72. }
  73. if (variables.containsKey(BlurEvent.EVENT_ID)) {
  74. fireEvent(new BlurEvent(this));
  75. }
  76. }
  77. @Override
  78. public void addBlurListener(BlurListener listener) {
  79. addListener(BlurEvent.EVENT_ID, BlurEvent.class, listener,
  80. BlurListener.blurMethod);
  81. }
  82. /**
  83. * @deprecated As of 7.0, replaced by {@link #addBlurListener(BlurListener)}
  84. **/
  85. @Override
  86. @Deprecated
  87. public void addListener(BlurListener listener) {
  88. addBlurListener(listener);
  89. }
  90. @Override
  91. public void removeBlurListener(BlurListener listener) {
  92. removeListener(BlurEvent.EVENT_ID, BlurEvent.class, listener);
  93. }
  94. /**
  95. * @deprecated As of 7.0, replaced by
  96. * {@link #removeBlurListener(BlurListener)}
  97. **/
  98. @Override
  99. @Deprecated
  100. public void removeListener(BlurListener listener) {
  101. removeBlurListener(listener);
  102. }
  103. @Override
  104. public void addFocusListener(FocusListener listener) {
  105. addListener(FocusEvent.EVENT_ID, FocusEvent.class, listener,
  106. FocusListener.focusMethod);
  107. }
  108. /**
  109. * @deprecated As of 7.0, replaced by
  110. * {@link #addFocusListener(FocusListener)}
  111. **/
  112. @Override
  113. @Deprecated
  114. public void addListener(FocusListener listener) {
  115. addFocusListener(listener);
  116. }
  117. @Override
  118. public void removeFocusListener(FocusListener listener) {
  119. removeListener(FocusEvent.EVENT_ID, FocusEvent.class, listener);
  120. }
  121. /**
  122. * @deprecated As of 7.0, replaced by
  123. * {@link #removeFocusListener(FocusListener)}
  124. **/
  125. @Override
  126. @Deprecated
  127. public void removeListener(FocusListener listener) {
  128. removeFocusListener(listener);
  129. }
  130. @Override
  131. protected void setValue(Object newValue, boolean repaintIsNotNeeded) {
  132. if (repaintIsNotNeeded) {
  133. /*
  134. * Check that value from changeVariables() doesn't contain unallowed
  135. * selections: In the multi select mode, the user has selected or
  136. * deselected a disabled item. In the single select mode, the user
  137. * has selected a disabled item.
  138. */
  139. if (isMultiSelect()) {
  140. Set<?> currentValueSet = (Set<?>) getValue();
  141. Set<?> newValueSet = (Set<?>) newValue;
  142. for (Object itemId : currentValueSet) {
  143. if (!isItemEnabled(itemId) && !newValueSet.contains(itemId)) {
  144. markAsDirty();
  145. return;
  146. }
  147. }
  148. for (Object itemId : newValueSet) {
  149. if (!isItemEnabled(itemId)
  150. && !currentValueSet.contains(itemId)) {
  151. markAsDirty();
  152. return;
  153. }
  154. }
  155. } else {
  156. if (newValue == null) {
  157. newValue = getNullSelectionItemId();
  158. }
  159. if (!isItemEnabled(newValue)) {
  160. markAsDirty();
  161. return;
  162. }
  163. }
  164. }
  165. super.setValue(newValue, repaintIsNotNeeded);
  166. }
  167. /**
  168. * Sets an item disabled or enabled. In the multiselect mode, a disabled
  169. * item cannot be selected or deselected by the user. In the single
  170. * selection mode, a disable item cannot be selected.
  171. *
  172. * However, programmatical selection or deselection of an disable item is
  173. * possible. By default, items are enabled.
  174. *
  175. * @param itemId
  176. * the id of the item to be disabled or enabled
  177. * @param enabled
  178. * if true the item is enabled, otherwise the item is disabled
  179. */
  180. public void setItemEnabled(Object itemId, boolean enabled) {
  181. if (itemId != null) {
  182. if (enabled) {
  183. disabledItemIds.remove(itemId);
  184. } else {
  185. disabledItemIds.add(itemId);
  186. }
  187. markAsDirty();
  188. }
  189. }
  190. /**
  191. * Returns true if the item is enabled.
  192. *
  193. * @param itemId
  194. * the id of the item to be checked
  195. * @return true if the item is enabled, false otherwise
  196. * @see #setItemEnabled(Object, boolean)
  197. */
  198. public boolean isItemEnabled(Object itemId) {
  199. if (itemId != null) {
  200. return !disabledItemIds.contains(itemId);
  201. }
  202. return true;
  203. }
  204. /**
  205. * Sets whether html is allowed in the item captions. If set to true, the
  206. * captions are passed to the browser as html and the developer is
  207. * responsible for ensuring no harmful html is used. If set to false, the
  208. * content is passed to the browser as plain text.
  209. *
  210. * @param htmlContentAllowed
  211. * true if the captions are used as html, false if used as plain
  212. * text
  213. */
  214. public void setHtmlContentAllowed(boolean htmlContentAllowed) {
  215. this.htmlContentAllowed = htmlContentAllowed;
  216. markAsDirty();
  217. }
  218. /**
  219. * Checks whether captions are interpreted as html or plain text.
  220. *
  221. * @return true if the captions are used as html, false if used as plain
  222. * text
  223. * @see #setHtmlContentAllowed(boolean)
  224. */
  225. public boolean isHtmlContentAllowed() {
  226. return htmlContentAllowed;
  227. }
  228. }