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:com.dom_distiller.client.SchemaOrgParser.java

License:Open Source License

private void parse(Element root) {
    if (DomUtil.supportQuerySelectorAll(root)) {
        NodeList<Element> allProp = DomUtil.querySelectorAll(root, "[ITEMPROP],[ITEMSCOPE]");

        // Root node (html) is not included in the result of querySelectorAll, so need to
        // handle it explicitly here.
        parseElement(root, null);//from  w  ww . ja  v  a 2s. c  o  m

        for (int i = 0; i < allProp.getLength(); i++) {
            Element e = allProp.getItem(i);
            parseElement(e, getItemScopeParent(e));
        }

        // As per http://schema.org/author (or http://schema.org/Article and search for "author"
        // property), if <a> or <link> tags specify rel="author", extract it.
        allProp = DomUtil.querySelectorAll(root, "A[rel=author],LINK[rel=author]");
        for (int i = 0; i < allProp.getLength(); i++) {
            Element e = allProp.getItem(i);
            if (mAuthorFromRel.isEmpty())
                mAuthorFromRel = getAuthorFromRelAttribute(e);
        }
    } else {
        // Since there's no way to construct NodeList, we cannot share the same code for
        // both paths. However, the logic here is simple enough so that we can consider the
        // path using querySelectorAll is somehow tested, except for the query string of
        // querySelectorAll.
        List<Element> allProp = new ArrayList<Element>();
        getElementsWithItemAttribute(root, allProp);
        for (Element e : allProp) {
            parseElement(e, getItemScopeParent(e));
        }

        allProp = new ArrayList<Element>();
        getAuthorElements(root, allProp);
        for (Element e : allProp) {
            if (mAuthorFromRel.isEmpty())
                mAuthorFromRel = getAuthorFromRelAttribute(e);
        }
    }
}

From source file:com.dom_distiller.client.TableClassifier.java

License:Open Source License

public static Type table(TableElement t) {
    sReason = Reason.UNKNOWN;//from   w w  w .j a v a 2 s. c  o m

    // The following heuristics are dropped from said url:
    // - table created by CSS display style is layout table, because we only handle actual
    //   <table> elements.

    // 1) Table inside editable area is layout table, different from said url because we ignore
    //    editable areas during distillation.
    Element parent = t.getParentElement();
    while (parent != null) {
        if (parent.hasTagName("INPUT") || parent.getAttribute("contenteditable").equalsIgnoreCase("true")) {
            return logAndReturn(Reason.INSIDE_EDITABLE_AREA, "", Type.LAYOUT);
        }
        parent = parent.getParentElement();
    }

    // 2) Table having role="presentation" is layout table.
    String tableRole = t.getAttribute("role").toLowerCase();
    if (tableRole.equals("presentation")) {
        return logAndReturn(Reason.ROLE_TABLE, "_" + tableRole, Type.LAYOUT);
    }

    // 3) Table having ARIA table-related roles is data table.
    if (sARIATableRoles.contains(tableRole) || sARIARoles.contains(tableRole)) {
        return logAndReturn(Reason.ROLE_TABLE, "_" + tableRole, Type.DATA);
    }

    // 4) Table having ARIA table-related roles in its descendants is data table.
    // This may have deviated from said url if it only checks for <table> element but not its
    // descendants.
    List<Element> directDescendants = getDirectDescendants(t);
    for (Element e : directDescendants) {
        String role = e.getAttribute("role").toLowerCase();
        if (sARIATableDescendantRoles.contains(role) || sARIARoles.contains(role)) {
            return logAndReturn(Reason.ROLE_DESCENDANT, "_" + role, Type.DATA);
        }
    }

    // 5) Table having datatable="0" attribute is layout table.
    if (t.getAttribute("datatable").equals("0")) {
        return logAndReturn(Reason.DATATABLE_0, "", Type.LAYOUT);
    }

    // 6) Table having nested table(s) is layout table.
    // The order here and #7 (table having <=1 row/col is layout table) is different from said
    // url: the latter has these heuristics after #10 (table having "summary" attribute is
    // data table), but our eval sets indicate the need to bump these way up to here, because
    // many (old) pages have layout tables that are nested or with <TH>/<CAPTION>s but only 1
    // row or col.
    if (hasNestedTables(t))
        return logAndReturn(Reason.NESTED_TABLE, "", Type.LAYOUT);

    // 7) Table having only one row or column is layout table.
    // See comments for #6 about deviation from said url.
    NodeList<TableRowElement> rows = t.getRows();
    if (rows.getLength() <= 1)
        return logAndReturn(Reason.LESS_EQ_1_ROW, "", Type.LAYOUT);
    NodeList<TableCellElement> cols = getMaxColsAmongRows(rows);
    if (cols == null || cols.getLength() <= 1) {
        return logAndReturn(Reason.LESS_EQ_1_COL, "", Type.LAYOUT);
    }

    // 8) Table having legitimate data table structures is data table:
    // a) table has <caption>, <thead>, <tfoot>, <colgroup>, <col>, or <th> elements
    Element caption = t.getCaption();
    if ((caption != null && hasValidText(caption)) || t.getTHead() != null || t.getTFoot() != null
            || hasOneOfElements(directDescendants, sHeaderTags)) {
        return logAndReturn(Reason.CAPTION_THEAD_TFOOT_COLGROUP_COL_TH, "", Type.DATA);
    }

    // Extract all <td> elements from direct descendants, for easier/faster multiple access.
    List<Element> directTDs = new ArrayList<Element>();
    for (Element e : directDescendants) {
        if (e.hasTagName("TD"))
            directTDs.add(e);
    }

    for (Element e : directTDs) {
        // b) table cell has abbr, headers, or scope attributes
        if (e.hasAttribute("abbr") || e.hasAttribute("headers") || e.hasAttribute("scope")) {
            return logAndReturn(Reason.ABBR_HEADERS_SCOPE, "", Type.DATA);
        }
        // c) table cell has <abbr> element as a single child element.
        NodeList<Element> children = e.getElementsByTagName("*");
        if (children.getLength() == 1 && children.getItem(0).hasTagName("ABBR")) {
            return logAndReturn(Reason.ONLY_HAS_ABBR, "", Type.DATA);
        }
    }

    // 9) Table occupying > 95% of document width without viewport meta is layout table;
    // viewport condition is not in said url, added here for typical mobile-optimized sites.
    // The order here is different from said url: the latter has it after #14 (>=20 rows is
    // data table), but our eval sets indicate the need to bump this way up to here, because
    // many (old) pages have layout tables with the "summary" attribute (#10).
    Element docElement = t.getOwnerDocument().getDocumentElement();
    int docWidth = docElement.getOffsetWidth();
    if (docWidth > 0 && (double) t.getOffsetWidth() > 0.95 * (double) docWidth) {
        boolean viewportFound = false;
        NodeList<Element> allMeta = docElement.getElementsByTagName("META");
        for (int i = 0; i < allMeta.getLength() && !viewportFound; i++) {
            MetaElement meta = MetaElement.as(allMeta.getItem(i));
            viewportFound = meta.getName().equalsIgnoreCase("viewport");
        }
        if (!viewportFound) {
            return logAndReturn(Reason.MORE_95_PERCENT_DOC_WIDTH, "", Type.LAYOUT);
        }
    }

    // 10) Table having summary attribute is data table.
    // This is different from said url: the latter lumps "summary" attribute with #8, but we
    // split it so as to insert #9 in between.  Many (old) pages have tables that are clearly
    // layout: their "summary" attributes say they're for layout.  They also occupy > 95% of
    // document width, so #9 coming before #10 will correctly classify them as layout.
    if (t.hasAttribute("summary"))
        return logAndReturn(Reason.SUMMARY, "", Type.DATA);

    // 11) Table having >=5 columns is data table.
    if (cols.getLength() >= 5)
        return logAndReturn(Reason.MORE_EQ_5_COLS, "", Type.DATA);

    // 12) Table having borders around cells is data table.
    for (Element e : directTDs) {
        String border = DomUtil.getComputedStyle(e).getBorderStyle();
        if (!border.isEmpty() && !border.equals("none") && !border.equals("hidden")) {
            return logAndReturn(Reason.CELLS_HAVE_BORDER, "_" + border, Type.DATA);
        }
    }

    // 13) Table having differently-colored rows is data table.
    String prevBackgroundColor = null;
    for (int i = 0; i < rows.getLength(); i++) {
        String color = DomUtil.getComputedStyle(rows.getItem(i)).getBackgroundColor();
        if (prevBackgroundColor == null) {
            prevBackgroundColor = color;
            continue;
        }
        if (!prevBackgroundColor.equalsIgnoreCase(color)) {
            return logAndReturn(Reason.DIFFERENTLY_COLORED_ROWS, "", Type.DATA);
        }
    }

    // 14) Table having >=20 rows is data table.
    if (rows.getLength() >= 20)
        return logAndReturn(Reason.MORE_EQ_20_ROWS, "", Type.DATA);

    // 15) Table having <=10 cells is layout table.
    if (directTDs.size() <= 10)
        return logAndReturn(Reason.LESS_EQ_10_CELLS, "", Type.LAYOUT);

    // 16) Table containing <embed>, <object>, <applet> or <iframe> elements (typical
    //     advertisement elements) is layout table.
    if (hasOneOfElements(directDescendants, sObjectTags)) {
        return logAndReturn(Reason.EMBED_OBJECT_APPLET_IFRAME, "", Type.LAYOUT);
    }

    // 17) Table occupying > 90% of document height is layout table.
    // This is not in said url, added here because many (old) pages have tables that don't fall
    // into any of the above heuristics but are for layout, and hence shouldn't default to data
    // by #18.
    int docHeight = docElement.getOffsetHeight();
    if (docHeight > 0 && (double) t.getOffsetHeight() > 0.9 * (double) docHeight) {
        return logAndReturn(Reason.MORE_90_PERCENT_DOC_HEIGHT, "", Type.LAYOUT);
    }

    // 18) Otherwise, it's data table.
    return logAndReturn(Reason.DEFAULT, "", Type.DATA);
}

From source file:com.dom_distiller.client.TableClassifier.java

License:Open Source License

private static List<Element> getDirectDescendants(Element t) {
    List<Element> directDescendants = new ArrayList<Element>();
    NodeList<Element> allDescendants = t.getElementsByTagName("*");
    if (!hasNestedTables(t)) {
        for (int i = 0; i < allDescendants.getLength(); i++) {
            directDescendants.add(allDescendants.getItem(i));
        }//from   w  w  w  .  j  av a 2s.c o m
    } else {
        for (int i = 0; i < allDescendants.getLength(); i++) {
            // Check if the current element is a direct descendent of the |t| table element in
            // question, as opposed to being a descendent of a nested table in |t|.
            Element e = allDescendants.getItem(i);
            Element parent = e.getParentElement();
            while (parent != null) {
                if (parent.hasTagName("TABLE")) {
                    if (parent == t)
                        directDescendants.add(e);
                    break;
                }
                parent = parent.getParentElement();
            }
        }
    }
    return directDescendants;
}

From source file:com.dom_distiller.client.TableClassifier.java

License:Open Source License

private static NodeList<TableCellElement> getMaxColsAmongRows(NodeList<TableRowElement> rows) {
    NodeList<TableCellElement> cols = null;
    int maxCols = 0;
    for (int i = 0; i < rows.getLength(); i++) {
        NodeList<TableCellElement> currCols = rows.getItem(i).getCells();
        if (currCols.getLength() > maxCols) {
            maxCols = currCols.getLength();
            cols = currCols;//from  w w  w . ja va 2s  .c o m
        }
    }
    return cols;
}

From source file:com.ephesoft.gxt.core.client.ui.widget.ScrollableSuggestDisplay.java

License:Open Source License

/**
 * Scrolls the selectedItem in to the view. It ensures that selected item is always visible in the SuggestionDisplay.
 *///from ww  w  .j  av  a 2  s .  c o  m
private void scrollSelectedItemIntoView() {
    //TODO Refactoring. NULL Checks missing
    if (null != suggestionMenu) {
        final Element divElement = suggestionMenu.getElement();
        final Node tableNode = divElement.getChild(divElement.getChildCount() - 1);
        final NodeList<Node> optionsList = HTMLDomUtil.getGrandChildren(tableNode, 1);
        Element tableRowElement = null;
        if (optionsList != null) {
            for (int optionIndex = 0; optionIndex < optionsList.getLength(); optionIndex++) {
                tableRowElement = (Element) optionsList.getItem(optionIndex);
                if (((Element) tableRowElement.getChild(0)).getClassName()
                        .contains(StyleConstant.SUGGEST_BOX_SELECTED_ITEM_CLASS_APPENDER)) {
                    tableRowElement.scrollIntoView();
                    break;
                }
            }
        }
    }
}

From source file:com.extjs.gxt.desktop.client.TaskBar.java

License:Open Source License

private void autoSize() {
    int count = items.size();
    int aw = el().getStyleWidth();

    if (!resizeButtons || count < 1) {
        return;/*from  w w w  . j  a  va 2s .com*/
    }

    int each = (int) Math.max(Math.min(Math.floor((aw - 4) / count) - buttonMargin, buttonWidth),
            minButtonWidth);
    NodeList<com.google.gwt.dom.client.Element> btns = stripWrap.dom.getElementsByTagName("button");

    El b = items.get(0).el();
    lastButtonWidth = b.findParent("li", 5).getWidth();

    for (int i = 0, len = btns.getLength(); i < len; i++) {
        Element btn = btns.getItem(i).cast();

        int tw = items.get(i).el().getParent().dom.getOffsetWidth();
        int iw = btn.getOffsetWidth();
        btn.getStyle().setPropertyPx("width", (each - (tw - iw)));
    }
}

From source file:com.extjs.gxt.samples.client.examples.grid.CheckGroupingGridExample.java

License:Open Source License

@Override
protected void onRender(Element parent, int index) {
    super.onRender(parent, index);

    setLayout(new FlowLayout(10));

    GroupingStore<Stock> store = new GroupingStore<Stock>();
    store.setMonitorChanges(true);//  w  w  w . ja  va  2 s  . c  om
    store.add(TestData.getCompanies());
    store.groupBy("industry");

    final CheckBoxSelectionModel<Stock> sm = new CheckBoxSelectionModel<Stock>() {
        @Override
        public void deselectAll() {
            super.deselectAll();
            NodeList<com.google.gwt.dom.client.Element> groups = view.getGroups();
            for (int i = 0; i < groups.getLength(); i++) {
                com.google.gwt.dom.client.Element group = groups.getItem(i).getFirstChildElement();
                setGroupChecked((Element) group, false);
            }
        }

        @Override
        public void selectAll() {
            super.selectAll();
            NodeList<com.google.gwt.dom.client.Element> groups = view.getGroups();
            for (int i = 0; i < groups.getLength(); i++) {
                com.google.gwt.dom.client.Element group = groups.getItem(i).getFirstChildElement();
                setGroupChecked((Element) group, true);
            }
        }

        @Override
        protected void doDeselect(List<Stock> models, boolean supressEvent) {
            super.doDeselect(models, supressEvent);
            NodeList<com.google.gwt.dom.client.Element> groups = view.getGroups();
            search: for (int i = 0; i < groups.getLength(); i++) {
                com.google.gwt.dom.client.Element group = groups.getItem(i);
                NodeList<Element> rows = El.fly(group).select(".x-grid3-row");
                for (int j = 0, len = rows.getLength(); j < len; j++) {
                    Element r = rows.getItem(j);
                    int idx = grid.getView().findRowIndex(r);
                    Stock m = grid.getStore().getAt(idx);
                    if (!isSelected(m)) {
                        setGroupChecked((Element) group, false);
                        continue search;
                    }
                }
            }

        }

        @Override
        protected void doSelect(List<Stock> models, boolean keepExisting, boolean supressEvent) {
            super.doSelect(models, keepExisting, supressEvent);
            NodeList<com.google.gwt.dom.client.Element> groups = view.getGroups();
            search: for (int i = 0; i < groups.getLength(); i++) {
                com.google.gwt.dom.client.Element group = groups.getItem(i);
                NodeList<Element> rows = El.fly(group).select(".x-grid3-row");
                for (int j = 0, len = rows.getLength(); j < len; j++) {
                    Element r = rows.getItem(j);
                    int idx = grid.getView().findRowIndex(r);
                    Stock m = grid.getStore().getAt(idx);
                    if (!isSelected(m)) {
                        continue search;
                    }
                }
                setGroupChecked((Element) group, true);

            }
        }
    };

    ColumnConfig company = new ColumnConfig("name", "Company", 60);
    ColumnConfig price = new ColumnConfig("open", "Price", 20);
    ColumnConfig change = new ColumnConfig("change", "Change", 20);
    ColumnConfig industry = new ColumnConfig("industry", "Industry", 20);
    ColumnConfig last = new ColumnConfig("date", "Last Updated", 20);
    last.setDateTimeFormat(DateTimeFormat.getFormat("MM/dd/y"));

    List<ColumnConfig> config = new ArrayList<ColumnConfig>();
    config.add(sm.getColumn());
    config.add(company);
    config.add(price);
    config.add(change);
    config.add(industry);
    config.add(last);

    final ColumnModel cm = new ColumnModel(config);

    view = new GroupingView() {

        @Override
        protected void onMouseDown(GridEvent<ModelData> ge) {
            El hd = ge.getTarget(".x-grid-group-hd", 10);
            El target = ge.getTargetEl();
            if (hd != null && target.hasStyleName(uncheckedStyle) || target.hasStyleName(checkedStyle)) {
                boolean checked = !ge.getTargetEl().hasStyleName(uncheckedStyle);
                checked = !checked;
                if (checked) {
                    ge.getTargetEl().replaceStyleName(uncheckedStyle, checkedStyle);
                } else {
                    ge.getTargetEl().replaceStyleName(checkedStyle, uncheckedStyle);
                }

                Element group = (Element) findGroup(ge.getTarget());
                if (group != null) {
                    NodeList<Element> rows = El.fly(group).select(".x-grid3-row");
                    List<ModelData> temp = new ArrayList<ModelData>();
                    for (int i = 0; i < rows.getLength(); i++) {
                        Element r = rows.getItem(i);
                        int idx = findRowIndex(r);
                        ModelData m = grid.getStore().getAt(idx);
                        temp.add(m);
                    }
                    if (checked) {
                        grid.getSelectionModel().select(temp, true);
                    } else {
                        grid.getSelectionModel().deselect(temp);
                    }
                }
                return;
            }
            super.onMouseDown(ge);
        }

    };
    view.setShowGroupedColumn(false);
    view.setForceFit(true);
    view.setGroupRenderer(new GridGroupRenderer() {
        public String render(GroupColumnData data) {
            String f = cm.getColumnById(data.field).getHeader();
            String l = data.models.size() == 1 ? "Item" : "Items";
            return "<div class='x-grid3-group-checker'><div class='" + uncheckedStyle
                    + "'>&nbsp;</div></div>&nbsp;" + f + ": " + data.group + " (" + data.models.size() + " " + l
                    + ")";
        }
    });

    Grid<Stock> grid = new Grid<Stock>(store, cm);
    grid.setView(view);
    grid.setBorders(true);
    grid.addPlugin(sm);
    grid.setSelectionModel(sm);

    ContentPanel panel = new ContentPanel();
    panel.setHeading("Grouping Example");
    panel.setIcon(Resources.ICONS.table());
    panel.setCollapsible(true);
    panel.setFrame(true);
    panel.setSize(700, 450);
    panel.setLayout(new FitLayout());
    panel.add(grid);

    ToolBar toolBar = new ToolBar();
    toolBar.add(new LabelToolItem("Selection Mode: "));
    final SimpleComboBox<String> type = new SimpleComboBox<String>();
    type.setTriggerAction(TriggerAction.ALL);
    type.setEditable(false);
    type.setFireChangeEventOnSetValue(true);
    type.setWidth(100);
    type.add("Multi");
    type.add("Simple");
    type.setSimpleValue("Multi");
    type.addListener(Events.Change, new Listener<FieldEvent>() {
        public void handleEvent(FieldEvent be) {
            boolean simple = type.getSimpleValue().equals("Simple");
            sm.deselectAll();
            sm.setSelectionMode(simple ? SelectionMode.SIMPLE : SelectionMode.MULTI);
        }
    });

    toolBar.add(type);
    toolBar.add(new SeparatorToolItem());
    panel.setTopComponent(toolBar);

    add(panel);
}

From source file:com.extjs.gxt.ui.client.util.Util.java

License:sencha.com license

public static Element[] toElementArray(NodeList<Element> nodes) {
    Element[] e = new Element[nodes.getLength()];
    for (int i = 0; i < nodes.getLength(); i++) {
        e[i] = nodes.getItem(i);/*from  w  ww . j  a v a 2s.c o m*/
    }
    return e;
}

From source file:com.extjs.gxt.ui.client.widget.CheckBoxListView.java

License:sencha.com license

public List<M> getChecked() {
    if (rendered) {
        List<M> l = new ArrayList<M>();
        NodeList<Element> nodes = el().select(checkBoxSelector);
        for (int i = 0; i < nodes.getLength(); i++) {
            if (InputElement.is(nodes.getItem(i))) {
                InputElement e = InputElement.as(nodes.getItem(i));
                if (e.isChecked()) {
                    l.add(getStore().getAt(i));
                }/* w  ww  .j a  va 2  s.  c  o  m*/
            }
        }
        return l;
    } else {
        return checkedPreRender != null ? new ArrayList<M>(checkedPreRender) : new ArrayList<M>();
    }
}

From source file:com.extjs.gxt.ui.client.widget.ColorPalette.java

License:sencha.com license

@Override
protected void onRender(Element target, int index) {
    setElement(DOM.createDiv(), target, index);
    super.onRender(target, index);

    rowCount = (int) Math.ceil(colors.length / ((double) columnCount));
    Grid grid = new Grid(rowCount, columnCount);
    grid.getElement().setAttribute("role", "presentation");
    grid.setCellPadding(0);//from w ww .j  av a  2s. c o m
    grid.setCellSpacing(0);
    int mark = 0;

    for (int i = 0; i < rowCount; i++) {
        for (int j = 0; j < columnCount && mark < colors.length; j++) {
            String c = colors[mark];
            String name = colorNames.get(c);
            String lbl = name == null ? c : name;
            grid.setHTML(i, j,
                    "<a aria-label=" + lbl + " id=" + XDOM.getUniqueId() + " role=gridcell row=" + i + " col="
                            + j + " class=\"color-" + c
                            + "\"><em role=presentation><span role=presentation style=\"background:#" + c
                            + "\" unselectable=\"on\">&#160;</span></em></a>");
            mark++;
        }
    }
    getElement().appendChild(grid.getElement());

    if (GXT.isAriaEnabled()) {
        NodeList<Element> trs = el().select("tr");
        for (int i = 0; i < trs.getLength(); i++) {
            trs.getItem(i).setAttribute("role", "row");
        }

        NodeList<Element> tds = el().select("td");
        for (int i = 0; i < tds.getLength(); i++) {
            Element elem = tds.getItem(i);
            if (elem.getAttribute("row").equals("")) {
                elem.setAttribute("role", "presentation");
            }
        }
    }

    new KeyNav<ComponentEvent>(this) {
        @Override
        public void onDown(ComponentEvent ce) {
            onKeyDown(ce);
        }

        @Override
        public void onEnter(ComponentEvent ce) {
            onKeyEnter(ce);
        }

        @Override
        public void onLeft(ComponentEvent ce) {
            onKeyLeft(ce);
        }

        @Override
        public void onRight(ComponentEvent ce) {
            onKeyRight(ce);
        }

        @Override
        public void onUp(ComponentEvent ce) {
            onKeyUp(ce);
        }

    };

    el().setTabIndex(0);
    el().setElementAttribute("hideFocus", "true");

    elements = el().select("a");

    if (GXT.isAriaEnabled()) {
        getElement().setAttribute("role", "grid");
        getAriaSupport().setLabel(GXT.MESSAGES.colorPalette());
    }

    sinkEvents(Event.ONCLICK | Event.ONMOUSEOVER | Event.ONMOUSEOUT | Event.FOCUSEVENTS);
}