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.

VOptionGroupBase.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright 2000-2018 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.v7.client.ui;
  17. import java.util.Set;
  18. import com.google.gwt.event.dom.client.ChangeEvent;
  19. import com.google.gwt.event.dom.client.ChangeHandler;
  20. import com.google.gwt.event.dom.client.ClickEvent;
  21. import com.google.gwt.event.dom.client.ClickHandler;
  22. import com.google.gwt.event.dom.client.KeyCodes;
  23. import com.google.gwt.event.dom.client.KeyPressEvent;
  24. import com.google.gwt.event.dom.client.KeyPressHandler;
  25. import com.google.gwt.user.client.ui.Composite;
  26. import com.google.gwt.user.client.ui.FlowPanel;
  27. import com.google.gwt.user.client.ui.HasEnabled;
  28. import com.google.gwt.user.client.ui.Panel;
  29. import com.google.gwt.user.client.ui.Widget;
  30. import com.vaadin.client.ApplicationConnection;
  31. import com.vaadin.client.Focusable;
  32. import com.vaadin.client.UIDL;
  33. import com.vaadin.client.ui.Field;
  34. import com.vaadin.client.ui.VNativeButton;
  35. public abstract class VOptionGroupBase extends Composite implements Field,
  36. ClickHandler, ChangeHandler, KeyPressHandler, Focusable, HasEnabled {
  37. public static final String CLASSNAME_OPTION = "v-select-option";
  38. /** For internal use only. May be removed or replaced in the future. */
  39. public ApplicationConnection client;
  40. /** For internal use only. May be removed or replaced in the future. */
  41. public String paintableId;
  42. /** For internal use only. May be removed or replaced in the future. */
  43. public Set<String> selectedKeys;
  44. /** For internal use only. May be removed or replaced in the future. */
  45. public boolean immediate;
  46. /** For internal use only. May be removed or replaced in the future. */
  47. public boolean multiselect;
  48. private boolean enabled;
  49. private boolean readonly;
  50. /** For internal use only. May be removed or replaced in the future. */
  51. public int cols = 0;
  52. /** For internal use only. May be removed or replaced in the future. */
  53. public int rows = 0;
  54. /** For internal use only. May be removed or replaced in the future. */
  55. public boolean nullSelectionAllowed = true;
  56. /** For internal use only. May be removed or replaced in the future. */
  57. public boolean nullSelectionItemAvailable = false;
  58. /**
  59. * Widget holding the different options (e.g. ListBox or Panel for radio
  60. * buttons) (optional, fallbacks to container Panel)
  61. * <p>
  62. * For internal use only. May be removed or replaced in the future.
  63. */
  64. public Widget optionsContainer;
  65. /**
  66. * Panel containing the component.
  67. * <p>
  68. * For internal use only. May be removed or replaced in the future.
  69. */
  70. public final Panel container;
  71. /** For internal use only. May be removed or replaced in the future. */
  72. public VTextField newItemField;
  73. /** For internal use only. May be removed or replaced in the future. */
  74. public VNativeButton newItemButton;
  75. public VOptionGroupBase(String classname) {
  76. container = new FlowPanel();
  77. initWidget(container);
  78. optionsContainer = container;
  79. container.setStyleName(classname);
  80. immediate = false;
  81. multiselect = false;
  82. }
  83. /*
  84. * Call this if you wish to specify your own container for the option
  85. * elements (e.g. SELECT)
  86. */
  87. public VOptionGroupBase(Widget w, String classname) {
  88. this(classname);
  89. optionsContainer = w;
  90. container.add(optionsContainer);
  91. }
  92. protected boolean isImmediate() {
  93. return immediate;
  94. }
  95. protected boolean isMultiselect() {
  96. return multiselect;
  97. }
  98. @Override
  99. public boolean isEnabled() {
  100. return enabled;
  101. }
  102. public boolean isReadonly() {
  103. return readonly;
  104. }
  105. protected boolean isNullSelectionAllowed() {
  106. return nullSelectionAllowed;
  107. }
  108. protected boolean isNullSelectionItemAvailable() {
  109. return nullSelectionItemAvailable;
  110. }
  111. /**
  112. * For internal use only. May be removed or replaced in the future.
  113. *
  114. * @return "cols" specified in uidl, 0 if not specified
  115. */
  116. public int getColumns() {
  117. return cols;
  118. }
  119. /**
  120. * For internal use only. May be removed or replaced in the future.
  121. *
  122. * @return "rows" specified in uidl, 0 if not specified
  123. */
  124. public int getRows() {
  125. return rows;
  126. }
  127. public abstract void setTabIndex(int tabIndex);
  128. @Override
  129. public void onClick(ClickEvent event) {
  130. if (event.getSource() == newItemButton
  131. && !newItemField.getText().equals("")) {
  132. client.updateVariable(paintableId, "newitem",
  133. newItemField.getText(), true);
  134. newItemField.setText("");
  135. }
  136. }
  137. @Override
  138. public void onChange(ChangeEvent event) {
  139. if (multiselect) {
  140. client.updateVariable(paintableId, "selected", getSelectedItems(),
  141. immediate);
  142. } else {
  143. client.updateVariable(paintableId, "selected",
  144. new String[] { "" + getSelectedItem() }, immediate);
  145. }
  146. }
  147. @Override
  148. public void onKeyPress(KeyPressEvent event) {
  149. if (event.getSource() == newItemField
  150. && event.getCharCode() == KeyCodes.KEY_ENTER) {
  151. newItemButton.click();
  152. }
  153. }
  154. public void setReadonly(boolean readonly) {
  155. if (this.readonly != readonly) {
  156. this.readonly = readonly;
  157. updateEnabledState();
  158. }
  159. }
  160. @Override
  161. public void setEnabled(boolean enabled) {
  162. if (this.enabled != enabled) {
  163. this.enabled = enabled;
  164. updateEnabledState();
  165. }
  166. }
  167. /** For internal use only. May be removed or replaced in the future. */
  168. public abstract void buildOptions(UIDL uidl);
  169. protected abstract String[] getSelectedItems();
  170. protected abstract void updateEnabledState();
  171. protected String getSelectedItem() {
  172. final String[] sel = getSelectedItems();
  173. if (sel.length > 0) {
  174. return sel[0];
  175. } else {
  176. return null;
  177. }
  178. }
  179. }