protected boolean isMatchingGroup(Node child, String group) { if (isGroup(child)) { OptGroupElement optgroup = optgroup(child); return group.equals(optgroup.getLabel()); } else { return false; } }
protected OptGroupElement findOptGroupElement(String name) { if (name == null) return null; NodeList<Element> optgroups = getElement().getElementsByTagName("OPTGROUP"); for (int i = 0; i < optgroups.getLength(); i++) { Element optgroup = optgroups.getItem(i); if (isMatchingGroup(optgroup, name)) { return OptGroupElement.as(optgroup); } } return null; }
private OptGroupElement optgroup(Node node) { if (node == null) return null; return OptGroupElement.as(Element.as(node)); }
@Override public void insertItem(String item, String value, int index) { // find the delimiter if there is one int pipe = (item != null) ? item.indexOf('|') : -1; while (pipe != -1 && pipe + 1 != item.length() && item.charAt(pipe + 1) == '|') { pipe = item.indexOf('|', pipe + 2); } // extract the group if we found a delimiter String group = null; if (pipe != -1) { group = item.substring(0, pipe).trim(); item = item.substring(pipe + 1).trim(); // make sure we convert || -> | in the group name group = group.replace("||", "|"); } Element parent = getSelectElement(); Node before = null; if (group != null) { OptGroupElement optgroup = findOptGroupElement(group); if (optgroup != null) { // add it to this optgroup parent = optgroup; // adjust the index to inside the group int adjusted = getIndexInGroup(group, index); // we had a real index (wasn't negative which means // add to the end), but it was too low for this group. // put it at the beginning of the group. if (adjusted < 0 && index >= 0) { adjusted = 0; } // check the range and if it's out of range, we'll // just add it to the end // of the group (before == null) if (0 <= adjusted && adjusted < optgroup.getChildCount()) { before = optgroup.getChild(adjusted); } } else { // add a new group and add the item to it optgroup = Document.get().createOptGroupElement(); optgroup.setLabel(group); parent.appendChild(optgroup); parent = optgroup; before = null; } } else { // make sure we're not past the initial "group" of // ungrouped options int max = getIndexOfFirstGroup(); if (index < 0 || index > max) { before = (max < parent.getChildCount()) ? parent.getChild(max) : null; } else if (0 <= index && index < parent.getChildCount()) { before = parent.getChild(index); } } OptionElement option = createOption(item, value); parent.insertBefore(option, before); }
/** * Option label for use in hierarchical menus. * * @see <a * href="http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#adef-label-OPTION">W3C * HTML Specification</a> */ public String getLabel() { return OptGroupElement.as(this.getElement()).getLabel(); }
/** * The control is unavailable in this context. * * @see <a * href="http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#adef-disabled">W3C * HTML Specification</a> */ public boolean isDisabled() { return OptGroupElement.as(this.getElement()).isDisabled(); }
/** * The control is unavailable in this context. * * @see <a * href="http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#adef-disabled">W3C * HTML Specification</a> */ public void setDisabled(boolean disabled) { OptGroupElement.as(this.getElement()).setDisabled(disabled); }
/** * Option label for use in hierarchical menus. * * @see <a * href="http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#adef-label-OPTION">W3C * HTML Specification</a> */ public void setLabel(String label) { OptGroupElement.as(this.getElement()).setLabel(label); }