public void insertStyledItemToGroup(String item, String value, String className, Direction dir, int indentLevel,
int groupIndex, int itemIndex) {
if (indentLevel < 0) {
throw new IllegalArgumentException("[indentLevel] must be non-negative.");
}
GQuery optgroupList = $(OPTGROUP_TAG, getElement());
int groupCount = optgroupList.size();
if (groupCount == 0) {
// simply insert the item to the listbox
insertItem(item, dir, value, itemIndex);
return;
}
if (groupIndex < 0 || groupIndex > groupCount - 1) {
groupIndex = groupCount - 1;
}
GQuery optgroup = optgroupList.eq(groupIndex);
OptionElement option = Document.get().createOptionElement();
if (!(className == null || className.trim().isEmpty())) {
option.addClassName(className);
}
if (indentLevel > 0) {
// Calculate total indentation, not forgetting that being in a group is adding one extra indent step
int leftPadding = options.getResources().css().indent() * (indentLevel + 1);
option.setAttribute("style", "padding-left: " + leftPadding + "px;");
}
Element optGroupElement = optgroup.get(0);
int itemCount = optGroupElement.getChildCount();
if (itemIndex < 0 || itemIndex > itemCount - 1) {
optgroup.append(option);
} else {
GQuery before = $(optGroupElement.getChild(itemIndex));
before.before(option);
}
// setText must be after the element has been appended to the DOM - see javadoc
setOptionText(option, item, dir);
option.setValue(value);
}