Example usage for com.google.gwt.dom.client OptionElement addClassName

List of usage examples for com.google.gwt.dom.client OptionElement addClassName

Introduction

In this page you can find the example usage for com.google.gwt.dom.client OptionElement addClassName.

Prototype

@Override
    public boolean addClassName(String className) 

Source Link

Usage

From source file:com.arcbees.chosen.client.gwt.ChosenListBox.java

License:Apache License

/**
 * Appends an item to the end of the list, adding the supplied class name to its class attribute. Specifying a
 * non-zero {@code indentLevel} will pad the item from the left by a fixed distance applied {@code indentLevel}
 * times.//from w w  w  .  j  a  va2 s . c om
 * <p/>
 * For example, a call:
 * <p/>
 * {@code
 * addStyledItem("My Item", "item1", "highlighted", 1);
 * }
 * <p/>
 * will result in the addition to the end of the {@code <select>} element of:
 * <p/>
 * {@code
 * <option value="item1" class="highlighted" style="padding-left: 15px;" >My Item</option>
 * }
 *
 * @param label       the item label to display to the user
 * @param value       the value of the item, meaningful in the context of an HTML form
 * @param className   the class name to add to this item (pass {@code null} to add no class name)
 * @param indentLevel the number of times to indent the item from the left (pass 0 for no indentation)
 */
public void addStyledItem(String label, String value, String className, int indentLevel) {
    if (indentLevel < 0) {
        throw new IllegalArgumentException("[indentLevel] must be non-negative.");
    }
    GQuery $selectElem = $(getElement());
    OptionElement option = Document.get().createOptionElement();
    option.setValue(value);
    option.setText(label);
    if (!(className == null || className.trim().isEmpty())) {
        option.addClassName(className);
    }
    if (indentLevel > 0) {
        int leftPadding = options.getResources().css().indent() * indentLevel;
        option.setAttribute("style", "padding-left: " + leftPadding + "px;");
    }
    $selectElem.append(option);
}

From source file:com.arcbees.chosen.client.gwt.ChosenListBox.java

License:Apache License

/**
 * Inserts an item into a group at the specified location. Additionally, the item can have an extra class name as
 * well as indent level assigned to it.//from   w w w.ja  v  a 2 s . c o  m
 * <p/>
 * <b>NB!</b> It is important to set text into the option after the option has been appended to the DOM
 * <br/>that's known bug in IE  @see <a href="http://bugs.jquery.com/ticket/3041">jQuery bug tracker</a>
 * <p/>
 *
 * @param item        the item label to display
 * @param value       the value of the item in the HTML form context
 * @param className   the class name to append to the option (pass {@code null} to append no class name)
 * @param dir         allows specifying an RTL, LTR or inherited direction ({@code null} is LTR)
 * @param indentLevel the number of times to indent the item from the left (pass 0 for no indentation)
 * @param groupIndex  the index of the group to insert the item into (if out of bounds, the last group will be used)
 * @param itemIndex   the index of the item within a group (if out of bounds, item will be placed last in the group)
 */
public void insertStyledItemToGroup(String item, String value, String className, Direction dir, int indentLevel,
        int groupIndex, int itemIndex) {
    int pos = groupIndex;
    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 (pos < 0 || pos > groupCount - 1) {
        pos = groupCount - 1;
    }

    GQuery optgroup = optgroupList.eq(pos);

    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);
}

From source file:org.xwiki.gwt.wysiwyg.client.plugin.style.StylePlugin.java

License:Open Source License

/**
 * Updates the selected state of all the options in the specified list.
 * //w  w w.  j  a  v a2  s .c o m
 * @param styleNameOptions the list of style name options to update
 * @param styleNameCommand the command used to retrieve the list of applied style names
 */
private void update(List<OptionElement> styleNameOptions, Command styleNameCommand) {
    // Ignore if the list of options is empty.
    if (styleNameOptions.size() == 0) {
        return;
    }

    // Check if the style name picker needs to be updated.
    String appliedStyleNames = getTextArea().getCommandManager().getStringValue(styleNameCommand);
    String previouslyAppliedStyleNames = previousValue.get(styleNameCommand);
    if (appliedStyleNames.equals(previouslyAppliedStyleNames)) {
        return;
    }
    previousValue.put(styleNameCommand, appliedStyleNames);

    // Update the style name picker.
    Set<String> styleNames = new HashSet<String>(Arrays.asList(appliedStyleNames.split("\\s+")));
    for (OptionElement option : styleNameOptions) {
        if (styleNames.contains(option.getValue())) {
            option.addClassName(SELECTED);
        } else {
            option.removeClassName(SELECTED);
        }
    }

    // Some browsers (e.g. Internet Explorer) don't update the selected options if we don't redisplay the select.
    styleNamePicker.getElement().getStyle().setDisplay(Display.NONE);
    styleNamePicker.getElement().getStyle().clearDisplay();
}