Search in sources :

Example 1 with HTMLOptionElement

use of org.loboevolution.html.dom.HTMLOptionElement in project LoboEvolution by LoboEvolution.

the class SelectOption method resetItemList.

/**
 * Reset item list.
 *
 * @param selectElement a {@link org.loboevolution.html.dom.domimpl.HTMLSelectElementImpl} object.
 */
public void resetItemList(HTMLSelectElementImpl selectElement) {
    boolean isMultiple = selectElement.isMultiple();
    if (isMultiple && this.state != STATE_LIST) {
        this.state = STATE_LIST;
        control.removeAll();
        JScrollPane scrollPane = new JScrollPane(this.list);
        control.add(scrollPane);
    } else if (!isMultiple && this.state != STATE_COMBO) {
        this.state = STATE_COMBO;
        control.removeAll();
        control.add(this.comboBox);
    }
    this.suspendSelections = true;
    try {
        HTMLOptionsCollection optionElements = selectElement.getOptions();
        if (this.state == STATE_COMBO) {
            JComboBox<OptionItem> comboBox = this.comboBox;
            // First determine current selected option
            HTMLOptionElement priorSelectedOption = null;
            int priorIndex = selectElement.getSelectedIndex();
            if (priorIndex != -1) {
                int numOptions = optionElements.getLength();
                for (int index = 0; index < numOptions; index++) {
                    HTMLOptionElement option = (HTMLOptionElement) optionElements.item(index);
                    if (index == priorIndex) {
                        priorSelectedOption = option;
                    }
                }
            }
            comboBox.removeAllItems();
            OptionItem defaultItem = null;
            OptionItem selectedItem = null;
            OptionItem firstItem = null;
            int numOptions = optionElements.getLength();
            for (int index = 0; index < numOptions; index++) {
                HTMLOptionElement option = (HTMLOptionElement) optionElements.item(index);
                if (option != null) {
                    OptionItem item = new OptionItem(option);
                    if (firstItem == null) {
                        firstItem = item;
                        comboBox.addItem(item);
                        // Undo automatic selection that occurs
                        // when adding the first item.
                        // This might set the deferred index as well.
                        selectElement.setSelectedIndex(-1);
                        if (priorSelectedOption != null) {
                            priorSelectedOption.setSelected(true);
                        }
                    } else {
                        comboBox.addItem(item);
                    }
                    if (option.isSelected()) {
                        selectedItem = item;
                    }
                    if (option.isDefaultSelected()) {
                        defaultItem = item;
                    }
                }
            }
            if (selectedItem != null) {
                comboBox.setSelectedItem(selectedItem);
            } else if (defaultItem != null) {
                comboBox.setSelectedItem(defaultItem);
            } else if (firstItem != null) {
                comboBox.setSelectedItem(firstItem);
            }
        } else {
            JList<OptionItem> list = this.list;
            Collection<Integer> defaultSelectedIndexes = null;
            Collection<Integer> selectedIndexes = null;
            OptionItem firstItem = null;
            DefaultListModel<OptionItem> listModel = this.listModel;
            listModel.clear();
            int numOptions = optionElements.getLength();
            for (int index = 0; index < numOptions; index++) {
                HTMLOptionElement option = (HTMLOptionElement) optionElements.item(index);
                OptionItem item = new OptionItem(option);
                if (firstItem == null) {
                    firstItem = item;
                    listModel.addElement(item);
                    // Do not select first item automatically.
                    list.setSelectedIndex(-1);
                } else {
                    listModel.addElement(item);
                }
                if (option.isSelected()) {
                    if (selectedIndexes == null) {
                        selectedIndexes = new LinkedList<>();
                    }
                    selectedIndexes.add(index);
                }
                if (option.isDefaultSelected()) {
                    if (defaultSelectedIndexes == null) {
                        defaultSelectedIndexes = new LinkedList<>();
                    }
                    defaultSelectedIndexes.add(index);
                }
            }
            if (ArrayUtilities.isNotBlank(selectedIndexes)) {
                for (Integer si : selectedIndexes) {
                    list.addSelectionInterval(si, si);
                }
            } else if (ArrayUtilities.isNotBlank(defaultSelectedIndexes)) {
                for (Integer si : defaultSelectedIndexes) {
                    list.addSelectionInterval(si, si);
                }
            }
        }
    } finally {
        this.suspendSelections = false;
    }
}
Also used : JScrollPane(javax.swing.JScrollPane) HTMLOptionsCollection(org.loboevolution.html.dom.HTMLOptionsCollection) HTMLOptionElement(org.loboevolution.html.dom.HTMLOptionElement)

Example 2 with HTMLOptionElement

use of org.loboevolution.html.dom.HTMLOptionElement in project LoboEvolution by LoboEvolution.

the class HTMLOptionsCollectionImpl method addElements.

private void addElements(HTMLOptionElement element, HTMLElement before) {
    List<Node> nodeList = getList();
    if (nodeList.size() == 0) {
        nodeList.add(0, element);
    } else {
        boolean found = false;
        HTMLOptionElement bef = (HTMLOptionElement) before;
        for (int i = 0; i < nodeList.size(); i++) {
            HTMLOptionElement elem = (HTMLOptionElement) nodeList.get(i);
            if (elem.getText().equals(bef.getText())) {
                nodeList.add(i, element);
                found = true;
                break;
            }
        }
        if (!found)
            throw new DOMException(DOMException.NOT_FOUND_ERR, "Record not found");
    }
}
Also used : DOMException(com.gargoylesoftware.css.dom.DOMException) HTMLOptionElement(org.loboevolution.html.dom.HTMLOptionElement) Node(org.loboevolution.html.node.Node)

Aggregations

HTMLOptionElement (org.loboevolution.html.dom.HTMLOptionElement)2 DOMException (com.gargoylesoftware.css.dom.DOMException)1 JScrollPane (javax.swing.JScrollPane)1 HTMLOptionsCollection (org.loboevolution.html.dom.HTMLOptionsCollection)1 Node (org.loboevolution.html.node.Node)1