Example usage for com.google.gwt.dom.client NodeList getLength

List of usage examples for com.google.gwt.dom.client NodeList getLength

Introduction

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

Prototype

public int getLength() 

Source Link

Usage

From source file:org.xwiki.gwt.wysiwyg.client.plugin.link.LinkMetaDataExtractor.java

License:Open Source License

/**
 * {@inheritDoc}/*from   ww  w. j av a 2 s. c o  m*/
 * 
 * @see InnerHTMLListener#onInnerHTMLChange(Element)
 */
public void onInnerHTMLChange(Element parent) {
    // look up all images in this subtree
    NodeList<com.google.gwt.dom.client.Element> anchors = parent.getElementsByTagName("a");
    for (int i = 0; i < anchors.getLength(); i++) {
        Element anchor = (Element) anchors.getItem(i);
        processElement(anchor);
    }
}

From source file:org.xwiki.gwt.wysiwyg.client.plugin.list.internal.MozillaListBehaviorAdjuster.java

License:Open Source License

/**
 * {@inheritDoc} In addition to default cleanup, also add a br in each empty list item (&lt;li /&gt;), so that it
 * stays editable./*from w  ww .  j a  v a 2s  . co m*/
 * 
 * @see ListBehaviorAdjuster#cleanUp(Element)
 */
protected void cleanUp(Element element) {
    // default cleanup behavior
    super.cleanUp(element);
    // now for each list item, if it's empty, add a line break inside
    NodeList<com.google.gwt.dom.client.Element> listItems = element.getElementsByTagName(LIST_ITEM_TAG);
    for (int i = 0; i < listItems.getLength(); i++) {
        Element currentListItem = (Element) listItems.getItem(i);
        if (currentListItem.getChildNodes().getLength() == 0) {
            currentListItem.appendChild(element.getOwnerDocument().createBRElement());
        }
    }
}

From source file:org.xwiki.gwt.wysiwyg.client.plugin.list.ListBehaviorAdjuster.java

License:Open Source License

/**
 * Executes lists clean up on the subtree rooted in the element passed as parameter. Lists cleanup consists of:
 * <ul>// ww  w .j  a  va  2  s.c  o m
 * <li>finding all the {@code ul} or {@code ol} tags which are in another {@code ul} or {@code ol} and adding a
 * {@code li} wrapper around each</li>
 * <li>finding all the {@code ul} or {@code ol} tags which are at the beginning (first child) of a list item and
 * making the parent list items editable</li>
 * </ul>
 * (but these operations are executed in a single pass). <br />
 * Note that while these operations are not enough from a strict xhtml cleaning point of view, they address all the
 * practical cases that appear so we chose to limit the operations executed to only these for performance reasons.
 * 
 * @param element the root element of the subtree in which to execute cleanup.
 */
protected void cleanUp(Element element) {
    // find all lists
    NodeList<com.google.gwt.dom.client.Element> orderedLists = element.getElementsByTagName(ORDERED_LIST_TAG);
    NodeList<com.google.gwt.dom.client.Element> unorderedLists = element
            .getElementsByTagName(UNORDERED_LIST_TAG);
    // send them to the actual cleaner
    cleanUpLists(orderedLists);
    cleanUpLists(unorderedLists);

    // Ensure that each list item can be edited.
    NodeList<com.google.gwt.dom.client.Element> listItems = element.getElementsByTagName(LIST_ITEM_TAG);
    for (int i = 0; i < listItems.getLength(); i++) {
        Element.as(listItems.getItem(i)).ensureEditable();
    }
}

From source file:org.xwiki.gwt.wysiwyg.client.plugin.list.ListBehaviorAdjuster.java

License:Open Source License

/**
 * Helper function to handle a list of list elements and clean them.
 * /*from   w  ww  . j a v  a 2  s .  c o m*/
 * @param listElements the list elements to clean up, according to the description at {@link #cleanUp(Element)}
 */
protected void cleanUpLists(NodeList<com.google.gwt.dom.client.Element> listElements) {
    for (int i = 0; i < listElements.getLength(); i++) {
        Element listElement = (Element) listElements.getItem(i);
        // check the parent of this list Element
        if (listElement.getParentNode().getNodeName().equalsIgnoreCase(ORDERED_LIST_TAG)
                || listElement.getParentNode().getNodeName().equalsIgnoreCase(UNORDERED_LIST_TAG)) {
            wrapList(listElement);
        }
        // check if this element is the first element of a list item
        if (listElement.getParentNode().getNodeName().equalsIgnoreCase(LIST_ITEM_TAG)
                && listElement.getPreviousSibling() == null) {
            // is first element
            handleEmptyListItem((Element) listElement.getParentNode());
        }
    }
}

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

License:Open Source License

/**
 * Initialize the style name picker./* w w w  .  j a  v a  2 s  . co m*/
 */
private void initStyleNamePicker() {
    styleNamePicker = new ListBox();
    styleNamePicker.setTitle(Strings.INSTANCE.stylePickerTitle());
    styleNamePicker.addStyleName("xStyleNamePicker");
    styleNamePicker.addItem(Strings.INSTANCE.stylePickerLabel(), "");
    saveRegistration(styleNamePicker.addChangeHandler(this));

    StyleDescriptorJSONParser parser = new StyleDescriptorJSONParser();
    for (StyleDescriptor descriptor : parser.parse(getConfig().getParameter("styleNames", "[]"))) {
        styleNamePicker.addItem(descriptor.getLabel(), descriptor.getName());
        NodeList<OptionElement> options = SelectElement.as(styleNamePicker.getElement()).getOptions();
        OptionElement option = options.getItem(options.getLength() - 1);
        option.setPropertyBoolean(INLINE, descriptor.isInline());
        (descriptor.isInline() ? inlineStyles : blockStyles).add(option);
    }

    if (blockStyles.size() > 0 && inlineStyles.size() > 0) {
        groupStyleNames(Strings.INSTANCE.styleBlockGroupLabel(), blockStyles);
        groupStyleNames(Strings.INSTANCE.styleInlineGroupLabel(), inlineStyles);
    }
    styleNamePicker.setSelectedIndex(0);

    toolBarExtension.addFeature("stylename", styleNamePicker);
}

From source file:org.xwiki.gwt.wysiwyg.client.plugin.sync.SyncPlugin.java

License:Open Source License

private void removeCursor(Document doc) {
    try {//  w  w  w.ja va2  s  .  c om
        Node cursorNode = null;
        NodeList list = doc.getElementsByTagName("span");
        for (int i = 0; i < list.getLength(); i++) {
            Element element = (Element) list.getItem(i);
            if (element.getId().equals("cursor-" + id)) {
                cursorNode = element;
            }
        }
        if (cursorNode != null) {
            debugMessage("found cursor element");
            Node firstNode = null;
            Node lastNode = null;
            Node pNode = cursorNode;
            NodeList childs = cursorNode.getChildNodes();
            // readd all childs of the cursor to the left of the cursor node
            int nb = childs.getLength();
            for (int i = nb - 1; i >= 0; i--) {
                Node node = childs.getItem(i);
                if (i == 0) {
                    firstNode = node;
                }
                if (i == nb - 1) {
                    lastNode = node;
                }
                // we want to insert the node in it's parent before the cursor Node
                cursorNode.removeChild(node);
                cursorNode.getParentNode().insertBefore(node, pNode);
                pNode = node;
            }
            // remove the cursor node itself
            Node previousNode = cursorNode.getPreviousSibling();
            debugMessage("removing cursor node");
            cursorNode.getParentNode().removeChild(cursorNode);

            debugMessage("creating new range");
            Range range = RangeFactory.INSTANCE.createRange(doc);

            if (firstNode != null) {
                debugMessage("set range with first node and last node");
                range.setStartBefore(firstNode);
                range.setEndAfter(lastNode);
            } else if (previousNode != null) {
                debugMessage("set range with previous node");
                Node nextNode = previousNode.getNextSibling();
                if (nextNode != null) {
                    range.setStart(nextNode, 0);
                } else {
                    range.setStartAfter(previousNode);
                    range.setEndAfter(previousNode);
                    range.collapse(true);
                }
            }

            doc.getSelection().removeAllRanges();
            doc.getSelection().addRange(range);
            getTextArea().setFocus(true);
        } else {
            debugMessage("could not find cursor element");
        }
    } catch (Exception e) {
        debugMessage("Uncaught exception in insertCursor: " + e.getMessage());
    }

}

From source file:org.xwiki.gwt.wysiwyg.client.plugin.table.feature.DeleteCol.java

License:Open Source License

/**
 * {@inheritDoc}/* w ww  . ja  v a 2s  .  c o  m*/
 * 
 * @see AbstractTableFeature#execute(String)
 */
public boolean execute(String parameter) {
    TableCellElement caretCell = TableUtils.getInstance()
            .getCell(TableUtils.getInstance().getCaretNode(rta.getDocument()));
    int cellIndex = caretCell.getCellIndex();
    TableElement table = TableUtils.getInstance().getTable(caretCell);
    NodeList<TableRowElement> rows = table.getRows();

    // Move caret
    TableCellElement newCaretCell = TableUtils.getInstance().getPreviousCellInRow(caretCell);
    if (newCaretCell == null) {
        newCaretCell = TableUtils.getInstance().getNextCellInRow(caretCell);
    }
    if (newCaretCell != null) {
        TableUtils.getInstance().putCaretInNode(rta, newCaretCell);
    }

    // Loop over table rows to delete a cell in each of them
    for (int i = 0; i < rows.getLength(); i++) {
        TableRowElement currentRow = rows.getItem(i);
        currentRow.deleteCell(cellIndex);
    }

    return true;
}

From source file:org.xwiki.gwt.wysiwyg.client.plugin.table.util.TableUtils.java

License:Open Source License

/**
 * Get next cell in cell's row.// www. j a  v  a2 s .  c o  m
 * 
 * @param cell currently edited cell.
 * @return the next TableCellElement if any, null otherwise.
 */
public TableCellElement getNextCellInRow(TableCellElement cell) {
    TableRowElement row = getRow(cell);
    NodeList<TableCellElement> cells = row.getCells();
    if (cells.getLength() > cell.getCellIndex() + 1) {
        return cells.getItem(cell.getCellIndex() + 1);
    } else {
        return null;
    }
}

From source file:org.xwiki.gwt.wysiwyg.client.plugin.table.util.TableUtils.java

License:Open Source License

/**
 * Get next cell in cell's column.//from  w ww  .  j  a va 2s  .c o  m
 * 
 * @param cell currently edited cell.
 * @return the next TableCellElement if any, null otherwise.
 */
public TableCellElement getNextCellInColumn(TableCellElement cell) {
    TableRowElement row = getRow(cell);
    TableElement table = getTable(row);
    NodeList<TableRowElement> rows = table.getRows();
    if (rows.getLength() > row.getRowIndex() + 1) {
        return rows.getItem(row.getRowIndex() + 1).getCells().getItem(cell.getCellIndex());
    } else {
        return null;
    }
}

From source file:org.xwiki.gwt.wysiwyg.client.plugin.table.util.TableUtils.java

License:Open Source License

/**
 * Insert a column in the currently edited table.
 * /*from ww w.  j  ava2 s  .  co m*/
 * @param doc the Document to get the selection from.
 * @param insertBefore indicates the creation position relatively to the currently edited column.
 */
public void insertCol(Document doc, boolean insertBefore) {
    TableCellElement cell = getCell(getCaretNode(doc));
    TableElement table = getTable(cell);
    NodeList<TableRowElement> rows = table.getRows();
    int index = cell.getCellIndex();

    // Loop over table rows to create a new cell in each of them
    for (int i = 0; i < rows.getLength(); i++) {
        TableRowElement currentRow = rows.getItem(i);
        TableCellElement newCell;

        if (isHeaderRow(currentRow)) {
            newCell = doc.createTHElement();
            if (insertBefore) {
                currentRow.insertBefore(newCell, currentRow.getCells().getItem(index));
            } else {
                DOMUtils.getInstance().insertAfter(newCell, currentRow.getCells().getItem(index));
            }
        } else {
            newCell = currentRow.insertCell(insertBefore ? index : index + 1);
        }
        newCell.setInnerHTML(CELL_DEFAULTHTML);
    }
}