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

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