/** * Inserts an item into the list box, specifying its direction and an initial value for the item. If the index is less than zero, or greater than or equal * to the length of the list, then the item will be appended to the end of the list. * * @param item the text of the item to be inserted * @param dir the item's direction. If {@code null}, the item is displayed in the widget's overall direction, or, if a direction estimator has been set, in * the item's estimated direction. * @param value the item's value, to be submitted if it is part of a {@link FormPanel}. * @param index the index at which to insert it */ public void insertItem(String item, Direction dir, String value, int index) { SelectElement select = getSelectElement(); OptionElement option = Document.get().createOptionElement(); setOptionText(option, item, dir); option.setValue(value); int itemCount = select.getLength(); if (index < 0 || index > itemCount) { index = itemCount; } if (index == itemCount) { select.add(option, null); } else { OptionElement before = select.getOptions().getItem(index); select.add(option, before); } }
/** * Sets the text of an option element. If the direction of the text is opposite to the page's direction, also wraps it with Unicode bidi formatting * characters to prevent garbling, and indicates that this was done by setting the option's <code>BIDI_ATTR_NAME</code> custom attribute. * * @param option an option element * @param text text to be set to the element * @param dir the text's direction. If {@code null} and direction estimation is turned off, direction is ignored. */ protected void setOptionText(OptionElement option, String text, Direction dir) { if (dir == null && estimator != null) { dir = estimator.estimateDirection(text); } if (dir == null) { option.setText(text); option.removeAttribute(BIDI_ATTR_NAME); } else { String formattedText = BidiFormatter.getInstanceForCurrentLocale().unicodeWrapWithKnownDir(dir, text, false /* isHtml */, false /* dirReset */); option.setText(formattedText); if (formattedText.length() > text.length()) { option.setAttribute(BIDI_ATTR_NAME, ""); } else { option.removeAttribute(BIDI_ATTR_NAME); } } }
protected OptionElement getOption(int index) { checkIndex(index); int childIndex = index; for (OptGroup group : groups) { int count = group.getCount(); if (childIndex < count) { return group.getChildOption(childIndex); } else { childIndex -= count; } } throw new IndexOutOfBoundsException("problem in getOption: index="+index+" range=[0-"+(getItemCount()-1)+"]"); }
@Override public void insertItem(String item, Direction dir, String value, int index) { SelectElement select = getElement().cast(); OptionElement option = Document.get().createOptionElement(); setOptionText(option, item, dir); option.setValue(value); option.setTitle(item); int itemCount = select.getLength(); if (index < 0 || index > itemCount) { index = itemCount; } if (index == itemCount) { select.add(option, null); } else { OptionElement before = select.getOptions().getItem(index); select.add(option, before); } }
void updateItemMap(Widget widget, boolean toAdd) { // Option ==> update with this option if (widget instanceof Option) { Option option = (Option) widget; if (toAdd) itemMap.put(option.getSelectElement(), option); else itemMap.remove(option.getSelectElement()); } else if (widget instanceof OptGroup) { // OptGroup ==> update with all optGroup options OptGroup optGroup = (OptGroup) widget; if (toAdd) itemMap.putAll(optGroup.getItemMap()); else for (Entry<OptionElement, Option> entry : optGroup.getItemMap().entrySet()) { OptionElement optElem = entry.getKey(); itemMap.remove(optElem); } } }
@Override protected void setSelectedValue(List<String> value) { if (isAttached()) { final JsArrayString arr = JavaScriptObject.createArray().cast(); for (final String val : value) { arr.push(val); } setValue(getElement(), arr); } else { for (Entry<OptionElement, Option> entry : itemMap.entrySet()) { Option opt = entry.getValue(); boolean selected = value.contains(opt.getValue()); opt.setSelected(selected); } } }
/** * Retrieves the text of an option element. If the text was set by {@link #setOptionText} and was wrapped with Unicode bidi formatting characters, also * removes those additional formatting characters. * * @param option an option element * @return the element's text */ protected String getOptionText(OptionElement option) { String text = option.getText(); if (option.hasAttribute(BIDI_ATTR_NAME) && text.length() > 1) { text = text.substring(1, text.length() - 1); } return text; }
private OptionElement createChildOption(Document document, SelectElement select, String value, String label) { OptionElement option = document.createOptionElement(); option.setValue(value); option.setInnerText(label); select.add(option, null); return option; }
private void setupLocaleSelect() { final SelectElement select = (SelectElement) Document.get().getElementById(LANG_ELEMENT_ID); String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName(); String[] localeNames = LocaleInfo.getAvailableLocaleNames(); for (String locale : localeNames) { if (!DEFAULT_LOCALE.equals(locale)) { String displayName = LocaleInfo.getLocaleNativeDisplayName(locale); OptionElement option = Document.get().createOptionElement(); option.setValue(locale); option.setText(displayName); select.add(option, null); if (locale.equals(currentLocale)) { select.setSelectedIndex(select.getLength() - 1); } } } EventDispatcherPanel.of(select).registerChangeHandler(null, new WaveChangeHandler() { @Override public boolean onChange(ChangeEvent event, Element context) { UrlBuilder builder = Location.createUrlBuilder().setParameter( LOCALE_URLBUILDER_PARAMETER, select.getValue()); Window.Location.replace(builder.buildString()); localeService.storeLocale(select.getValue()); return true; } }); }
protected void showHint(String hint) { if (hintEnabled) { SelectElement selectElement = SelectElement.as(listBox.getElement()); NodeList<OptionElement> options = selectElement.getOptions(); options.getItem(0).setText(hint); } else { listBox.addItem(hint); hintEnabled = true; } }
@Override public void setItemTitle(int index, String title) { SelectElement selectElement = SelectElement.as(listBox.getElement()); NodeList<OptionElement> options = selectElement.getOptions(); OptionElement optionElement = options.getItem(index + (hintEnabled ? 1: 0)); if (optionElement != null) { optionElement.setTitle(title); } }
protected OptionElement getOption(int index) { checkIndex(index); // first check ungrouped Element elm = getElement(); int sz = elm.getChildCount(); int firstGroup = getIndexOfFirstGroup(); if (index >= 0 && index < firstGroup && index < sz) { return option(elm.getChild(index)); } // then go through the groups int childIndex = index - firstGroup; for (int i = firstGroup; i <= index && i < sz; i++) { Node child = elm.getChild(i); if (isGroup(child)) { if (childIndex < child.getChildCount()) { return option(child.getChild(childIndex)); } else { childIndex -= child.getChildCount(); } } } return null; }
protected OptionElement createOption(String item, String value) { OptionElement option = Document.get().createOptionElement(); option.setText(item); option.setInnerText(item); option.setValue(value); return option; }
@Override public void removeItem(int index) { int childIndex = index; for (int i=0; i<groups.size(); i++) { OptGroup group = groups.get(i); int count = group.getCount(); if (childIndex < count) { // do the remove OptionElement element = group.getChildOption(childIndex); element.removeFromParent(); group.decrement(); // remove empty groups if (group.getCount() <= 0) { group.remove(); groups.remove(i); } return; } else { childIndex -= count; } } throw new IndexOutOfBoundsException("problem in removeItem: index="+index+" range=[0-"+(getItemCount()-1)+"]"); }
@Override public void setItemText(int index, String text) { if (text == null) { throw new NullPointerException("Cannot set an option to have null text"); } OptionElement option = getOption(index); option.setText(text); }
public void addItemWithStyle(String item, String value) { final SelectElement select = getElement().cast(); final OptionElement option = Document.get().createOptionElement(); option.setText(item); option.setValue(value); option.setClassName(styleName); select.add(option, null); }
private void setupLocaleSelect() { final SelectElement select = (SelectElement) Document.get().getElementById("lang"); String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName(); String[] localeNames = LocaleInfo.getAvailableLocaleNames(); for (String locale : localeNames) { if (!DEFAULT_LOCALE.equals(locale)) { String displayName = LocaleInfo.getLocaleNativeDisplayName(locale); OptionElement option = Document.get().createOptionElement(); option.setValue(locale); option.setText(displayName); select.add(option, null); if (locale.equals(currentLocale)) { select.setSelectedIndex(select.getLength() - 1); } } } EventDispatcherPanel.of(select).registerChangeHandler(null, new WaveChangeHandler() { @Override public boolean onChange(ChangeEvent event, Element context) { UrlBuilder builder = Location.createUrlBuilder().setParameter( "locale", select.getValue()); Window.Location.replace(builder.buildString()); localeService.storeLocale(select.getValue()); return true; } }); }
private String getSelectedValue() { for (Entry<OptionElement, Option> entry : itemMap.entrySet()) { Option opt = entry.getValue(); if (opt.isSelected()) return opt.getValue(); } return null; }
@Override protected void setSelectedValue(String value) { if (isAttached()) { setValue(getElement(), value); } else { for (Entry<OptionElement, Option> entry : itemMap.entrySet()) { Option opt = entry.getValue(); opt.setSelected(opt.getValue().equals(value)); } } }
/** * Returns the selected item or <code>null</code> if no item is selected. * * @return the selected items list */ public Option getSelectedItem() { for (Entry<OptionElement, Option> entry : itemMap.entrySet()) { Option opt = entry.getValue(); if (opt.isSelected()) return opt; } return null; }
/** * Returns the item list. * * @return the item list */ public List<Option> getItems() { List<Option> selectedItems = new ArrayList<>(0); NodeList<OptionElement> items = selectElement.getOptions(); for (int i = 0; i < items.getLength(); i++) { OptionElement item = items.getItem(i); Option option = itemMap.get(item); if (option != null) selectedItems.add(option); } return selectedItems; }
private List<String> getSelectedValues() { final List<String> allSelected = new ArrayList<>(0); for (Entry<OptionElement, Option> entry : itemMap.entrySet()) { Option opt = entry.getValue(); if (opt.isSelected()) allSelected.add(opt.getValue()); } return allSelected; }
/** * Returns the selected items list. If no item is selected, this method * returns an empty list. * * @return the selected items list */ public List<Option> getSelectedItems() { final List<Option> items = new ArrayList<>(0); for (Entry<OptionElement, Option> entry : itemMap.entrySet()) { Option opt = entry.getValue(); if (opt.isSelected()) items.add(opt); } return items; }
private void setSelectAll(boolean selected) { if (isAttached()) { String cmd = selected ? SelectCommand.SELECT_ALL : SelectCommand.DESELECT_ALL; command(getElement(), cmd); } else { for (Entry<OptionElement, Option> entry : itemMap.entrySet()) { entry.getValue().setSelected(selected); } } }
@Override public void add(String tag) { if (isAttached()) super.add(tag); else { OptionElement option = Document.get().createOptionElement(); option.setValue(tag); option.setInnerText(tag); getElement().appendChild(option); } }
public void add(Option option) { getSelectElement().add(OptionElement.as(option.getElement()), null); values.add(option.getValue()); }
public OptionElement getOptionElement(int index) { return getSelectElement().getOptions().getItem(index); }
public Option() { super(Document.get().createElement(OptionElement.TAG)); }
/** * The index of this OPTION in its parent SELECT, starting from 0. */ public int getIndex() { return OptionElement.as(this.getElement()).getIndex(); }
/** * The text contained within the option element. */ public String getText() { return OptionElement.as(this.getElement()).getText(); }
/** * The text contained within the option element. */ public void setText(String text) { OptionElement.as(this.getElement()).setText(text); }
public OptionElement getItem(int aIndex) { checkIndex(aIndex); return getElement().<SelectElement> cast().getOptions().getItem(aIndex); }
public String getKey(int aIndex) { OptionElement option = getItem(aIndex); return option.getValue(); }
public String getItemStyleName(int aIndex) { checkIndex(aIndex); OptionElement option = getElement().<SelectElement> cast().getOptions().getItem(aIndex); return option.getClassName(); }
public void setItemStyleName(int aIndex, String aValue) { checkIndex(aIndex); OptionElement option = getElement().<SelectElement> cast().getOptions().getItem(aIndex); option.setClassName(aValue); }
private OptionElement option(Node node) { if (node == null) return null; return OptionElement.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); }