use of com.gargoylesoftware.htmlunit.html.HtmlOption in project java-design-patterns by iluwatar.
the class AlbumPage method changeAlbumYear.
/**
* Selects the select's option value based on the year value given
*
* @param year the new year value to set
* @return {@link AlbumPage}
*/
public AlbumPage changeAlbumYear(int year) {
HtmlSelect albumYearSelectOption = (HtmlSelect) page.getElementById("albumYear");
HtmlOption yearOption = albumYearSelectOption.getOptionByValue(Integer.toString(year));
albumYearSelectOption.setSelectedAttribute(yearOption, true);
return this;
}
use of com.gargoylesoftware.htmlunit.html.HtmlOption in project htmlunit by HtmlUnit.
the class HTMLOptionElement method jsConstructor.
/**
* 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, EDGE, FF, FF_ESR })
public void jsConstructor(final Object newText, final String newValue, final boolean defaultSelected, final boolean selected) {
final SgmlPage page = (SgmlPage) getWindow().getWebWindow().getEnclosedPage();
AttributesImpl attributes = null;
if (defaultSelected) {
attributes = new AttributesImpl();
attributes.addAttribute(null, "selected", "selected", null, "selected");
}
final HtmlOption htmlOption = (HtmlOption) page.getWebClient().getPageCreator().getHtmlParser().getFactory(HtmlOption.TAG_NAME).createElement(page, HtmlOption.TAG_NAME, attributes);
htmlOption.setSelected(selected);
setDomNode(htmlOption);
if (!Undefined.isUndefined(newText)) {
final String newTextString = Context.toString(newText);
htmlOption.appendChild(new DomText(page, newTextString));
htmlOption.setLabelAttribute(newTextString);
}
if (!"undefined".equals(newValue)) {
htmlOption.setValueAttribute(newValue);
}
}
use of com.gargoylesoftware.htmlunit.html.HtmlOption in project htmlunit by HtmlUnit.
the class HTMLOptionElement method setSelected.
/**
* 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.getSize() <= 1 && !enclosingSelect.isMultipleSelectEnabled()) {
enclosingSelect.getOption(0).setSelectedFromJavaScript(true);
return;
}
optionNode.setSelectedFromJavaScript(selected);
}
use of com.gargoylesoftware.htmlunit.html.HtmlOption in project htmlunit by HtmlUnit.
the class HTMLOptionsCollection method put.
/**
* Sets the index property.
* @param index the index
* @param start the scriptable object that was originally invoked for this property
* @param newValue the new value
*/
@Override
public void put(final int index, final Scriptable start, final Object newValue) {
if (newValue == null) {
// Remove the indexed option.
htmlSelect_.removeOption(index);
} else {
final HTMLOptionElement option = (HTMLOptionElement) newValue;
final HtmlOption htmlOption = (HtmlOption) option.getDomNodeOrNull();
if (index >= getLength()) {
setLength(index);
// Add a new option at the end.
htmlSelect_.appendOption(htmlOption);
} else {
// Replace the indexed option.
htmlSelect_.replaceOption(index, htmlOption);
}
}
}
use of com.gargoylesoftware.htmlunit.html.HtmlOption in project htmlunit by HtmlUnit.
the class HTMLOptionsCollection method setLength.
/**
* 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
*/
@JsxSetter
public void setLength(final int newLength) {
if (newLength < 0) {
if (getBrowserVersion().hasFeature(JS_SELECT_OPTIONS_IGNORE_NEGATIVE_LENGTH)) {
return;
}
throw Context.reportRuntimeError("Length is negative");
}
final int currentLength = htmlSelect_.getOptionSize();
if (currentLength > newLength) {
htmlSelect_.setOptionSize(newLength);
} else {
final SgmlPage page = htmlSelect_.getPage();
final ElementFactory factory = page.getWebClient().getPageCreator().getHtmlParser().getFactory(HtmlOption.TAG_NAME);
for (int i = currentLength; i < newLength; i++) {
final HtmlOption option = (HtmlOption) factory.createElement(page, HtmlOption.TAG_NAME, null);
htmlSelect_.appendOption(option);
}
}
}
Aggregations