下面列出了怎么用com.google.gwt.dom.client.OptionElement的API类实例代码及写法,或者点击链接到github查看源代码。
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)+"]");
}
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);
}
}
}
public List<String> getItems() {
List<String> items = new ArrayList<>();
for (int i = 0; i < getItemCount(); i++) {
OptionElement optionElement = (OptionElement) getOptionElement(i);
items.add(getOptionText(optionElement));
}
return items;
}
public CubaListSelectWidget() {
getOptionsContainer().addDoubleClickHandler(event -> {
if (!isEnabled() || isReadonly()) {
return;
}
Element element = WidgetUtil.getElementUnderMouse(event.getNativeEvent());
if (OptionElement.is(element)) {
doubleClickListener.accept(((OptionElement) element).getIndex());
}
});
}
protected void removeEmptyPlaceHolder() {
// indeed the first item/value is emptyPlaceHolder
listBox.removeItem(0);
values.remove(0);
OptionElement currentPlaceholder = getOptionElement(0);
if (currentPlaceholder != null) {
currentPlaceholder.setDisabled(false);
}
}
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;
}
});
}
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);
}
protected OptionElement createOption(String item, String value) {
OptionElement option = Document.get().createOptionElement();
option.setText(item);
option.setInnerText(item);
option.setValue(value);
return option;
}
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);
}
}
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);
}
private OptionElement option(Node node)
{
if (node == null)
return null;
return OptionElement.as(Element.as(node));
}