Refine search
/** * Selects the option if it's not already selected. * {@inheritDoc} */ @Override protected boolean doClickStateUpdate(final boolean shiftKey, final boolean ctrlKey) throws IOException { boolean changed = false; if (!isSelected()) { setSelected(true, true, true, shiftKey, ctrlKey); changed = true; } else if (getEnclosingSelect().isMultipleSelectEnabled()) { if (ctrlKey) { setSelected(false, true, true, shiftKey, ctrlKey); changed = true; } else { getEnclosingSelect().setOnlySelected(this, true); } } super.doClickStateUpdate(shiftKey, ctrlKey); return changed; }
private String getSelectOptionValueForLabel(HtmlSelect sel, String label) { for (HtmlOption opt : sel.getOptions()) { if (opt.asText().equals(label)) { return opt.getValueAttribute(); } } throw new RuntimeException("Unable to find option " + label + " for " + sel.getNameAttribute()); }
model.addElement(new HtmlOption("foo bar", "<html><b>foo</b> <i>bar</i></html>"));
/** * Selects the option if it's not already selected. * {@inheritDoc} */ @Override protected Page doClickAction(final Page defaultPage) throws IOException { if (!isSelected()) { return setSelected(true); } return defaultPage; }
/** * Sets the selected state of this option. This will possibly also change the * selected properties of sibling option elements. * * @param selected true if this option should be selected * @return the page that occupies this window after this change is made (may or * may not be the same as the original page) */ public Page setSelected(final boolean selected) { setSelected(selected, true, false, false, false); return getPage(); }
/** * Changes the number of options: removes options if the new length * is less than the current one else add new empty options to reach the * new length. * @param newLength the new length property value */ public void jsxSet_length(final int newLength) { final int currentLength = htmlSelect_.getOptionSize(); if (currentLength > newLength) { htmlSelect_.setOptionSize(newLength); } else { for (int i = currentLength; i < newLength; i++) { final HtmlOption option = (HtmlOption) HTMLParser.getFactory(HtmlOption.TAG_NAME).createElement( htmlSelect_.getPage(), HtmlOption.TAG_NAME, null); htmlSelect_.appendOption(option); if (!getBrowserVersion().isIE()) { option.appendChild(new DomText(option.getPage(), "")); } } } }
/** * Returns a text representation of this element that represents what would * be visible to the user if this page was shown in a web browser. If the user * can only select one option at a time, this method returns the selected option. * If the user can select multiple options, this method returns all options. * * @return the element as text */ // we need to preserve this method as it is there since many versions with the above documentation. @Override public String asText() { final List<HtmlOption> options; if (isMultipleSelectEnabled()) { options = getOptions(); } else { options = getSelectedOptions(); } final StringBuilder builder = new StringBuilder(); for (final Iterator<HtmlOption> i = options.iterator(); i.hasNext();) { final HtmlOption currentOption = i.next(); if (currentOption != null) { builder.append(currentOption.asText()); } if (i.hasNext()) { builder.append("\n"); } } return builder.toString(); }
/** * {@inheritDoc} */ @Override public void insertBefore(final DomNode newNode) { super.insertBefore(newNode); if (newNode instanceof HtmlOption) { final HtmlOption option = (HtmlOption) newNode; if (option.isSelected()) { getEnclosingSelect().setSelectedAttribute(option, true); } } }
/** * Sets the value of the {@code selected} property. * @param selected the new selected property */ @JsxSetter public void setSelected(final boolean selected) { final HtmlOption optionNode = (HtmlOption) getDomNodeOrNull(); final HtmlSelect enclosingSelect = optionNode.getEnclosingSelect(); if (!selected && optionNode.isSelected() && enclosingSelect != null && !enclosingSelect.isMultipleSelectEnabled()) { enclosingSelect.getOption(0).setSelectedFromJavaScript(true); } else { optionNode.setSelectedFromJavaScript(selected); } }
@Override public void unselectOptions(String selectName, String[] options) { HtmlSelect sel = getForm().getSelectByName(selectName); if (!sel.isMultipleSelectEnabled() && options.length > 1) { throw new RuntimeException("Multiselect not enabled"); } for (String option : options) { boolean found = false; for (HtmlOption opt : sel.getOptions()) { if (opt.asText().equals(option)) { sel.setSelectedAttribute(opt, false); found = true; break; } } if (!found) { throw new RuntimeException("Option " + option + " not found"); } } }
/** * Sets the text for this HtmlOption. * @param text the text */ public void setText(final String text) { if (text == null || text.isEmpty()) { removeAllChildren(); } else { final DomNode child = getFirstChild(); if (child == null) { appendChild(new DomText(getPage(), text)); } else { child.setNodeValue(text); } } }
/** * {@inheritDoc} */ @Override public NameValuePair[] getSubmitNameValuePairs() { final String name = getNameAttribute(); final List<HtmlOption> selectedOptions = getSelectedOptions(); final NameValuePair[] pairs = new NameValuePair[selectedOptions.size()]; int i = 0; for (final HtmlOption option : selectedOptions) { pairs[i++] = new NameValuePair(name, option.getValueAttribute()); } return pairs; }
HtmlSelect select = page.getHtmlElementById("myId"); for (HtmlOption o : select.getOptions()) { if (o.getValueAttribute().contains("myValue")) { select.setSelectedAttribute(o, true); } }
/** * {@inheritDoc} * @see SubmittableElement#setDefaultValue(String) */ @Override public String getDefaultValue() { final List<HtmlOption> options = getSelectedOptions(); if (options.size() > 0) { return options.get(0).getValueAttribute(); } return ""; }
/** * JavaScript constructor. * @param newText the text * @param newValue the value * @param defaultSelected Whether the option is initially selected * @param selected the current selection state of the option */ @JsxConstructor({CHROME, FF, EDGE}) public void jsConstructor(final String newText, final String newValue, final boolean defaultSelected, final boolean selected) { final HtmlPage page = (HtmlPage) getWindow().getWebWindow().getEnclosedPage(); AttributesImpl attributes = null; if (defaultSelected) { attributes = new AttributesImpl(); attributes.addAttribute(null, "selected", "selected", null, "selected"); } final HtmlOption htmlOption = (HtmlOption) HTMLParser.getFactory(HtmlOption.TAG_NAME).createElement( page, HtmlOption.TAG_NAME, attributes); htmlOption.setSelected(selected); setDomNode(htmlOption); if (!"undefined".equals(newText)) { htmlOption.appendChild(new DomText(page, newText)); htmlOption.setLabelAttribute(newText); } if (!"undefined".equals(newValue)) { htmlOption.setValueAttribute(newValue); } }
/** * JavaScript constructor. This must be declared in every JavaScript file because * the rhino engine won't walk up the hierarchy looking for constructors. * @param newText the text * @param newValue the value * @param defaultSelected Whether the option is initially selected * @param selected the current selection state of the option */ public void jsConstructor(final String newText, final String newValue, final boolean defaultSelected, final boolean selected) { final HtmlPage page = (HtmlPage) getWindow().getWebWindow().getEnclosedPage(); AttributesImpl attributes = null; if (defaultSelected) { attributes = new AttributesImpl(); attributes.addAttribute(null, "selected", "selected", null, "selected"); } final HtmlOption htmlOption = (HtmlOption) HTMLParser.getFactory(HtmlOption.TAG_NAME).createElement( page, HtmlOption.TAG_NAME, attributes); htmlOption.setSelected(selected); setDomNode(htmlOption); if (newText != null && !newText.equals("undefined")) { htmlOption.appendChild(new DomText(page, newText)); } if (newValue != null && !newValue.equals("undefined")) { htmlOption.setValueAttribute(newValue); } }
element = new HtmlOption(namespaceURI, qualifiedName, page, attributeMap); element = new HtmlSelect(namespaceURI, qualifiedName, page, attributeMap);
/** * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br> * * Sets the selected state of this option. This will possibly also change the * selected properties of sibling option elements. * * @param selected true if this option should be selected */ public void setSelectedFromJavaScript(final boolean selected) { setSelected(selected, false, false, true, false); }
/** * Return a string array of select box option values. * * @param selectName name of the select box. */ @Override public String[] getSelectOptionValues(String selectName) { HtmlSelect sel = getForm().getSelectByName(selectName); ArrayList<String> result = new ArrayList<String>(); for (HtmlOption opt : sel.getOptions()) { result.add(opt.getValueAttribute()); } return result.toArray(new String[result.size()]); }
/** * Returns the value of the "selected" property. * @return the text property */ public boolean jsxGet_selected() { return getDomNodeOrNull().isSelected(); }