Java Code Examples for com.google.gwt.dom.client.Element#getChildCount()

The following examples show how to use com.google.gwt.dom.client.Element#getChildCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DOMHelper.java    From gwt-material with Apache License 2.0 6 votes vote down vote up
public static Element getChildElementByClass(Element parent, String className) {
    if (parent != null) {
        for (int i = 0; i < parent.getChildCount(); i++) {
            Node childNode = parent.getChild(i);
            if (Element.is(childNode)) {
                Element child = Element.as(childNode);
                if (child.getClassName().contains(className)) {
                    return child;
                }

                if (child.getChildCount() > 0) {
                    return getChildElementByClass(child, className);
                }
            }
        }
    }
    return null;
}
 
Example 2
Source File: DOMHelper.java    From gwt-material with Apache License 2.0 6 votes vote down vote up
public static Element getChildElementById(Element parent, String id) {
    if (parent != null) {
        for (int i = 0; i < parent.getChildCount(); i++) {
            Node childNode = parent.getChild(i);
            if (Element.is(childNode)) {
                Element child = Element.as(childNode);
                if (child.getId().equals(id)) {
                    return child;
                }

                if (child.getChildCount() > 0) {
                    return getChildElementById(child, id);
                }
            }
        }
    }
    return null;
}
 
Example 3
Source File: GroupedListBox.java    From swcv with MIT License 6 votes vote down vote up
protected int getIndexInGroup(String group, int index)
{
    if (group == null)
        return index;

    int adjusted = index;
    Element elm = getElement();
    int sz = elm.getChildCount();
    for (int i = 0; i < sz; i++)
    {
        Node child = elm.getChild(i);
        if (isMatchingGroup(child, group))
        {
            break;
        }
        if (isGroup(child))
        {
            adjusted -= child.getChildCount();
        }
        else
        {
            adjusted -= 1;
        }
    }
    return adjusted;
}
 
Example 4
Source File: NavSpy.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void collectHeadings(Element element, List<Element> headings) {
	for (int i = 0; i < element.getChildCount(); i++) {
		Node node = element.getChild(i);
		if (node instanceof Element) {
			Element child = (Element) node;
			String tagName = child.getTagName();
			if (tagName != null && Heading.HEADING_TAGS.contains(tagName.toLowerCase())) {
				if (this.spyName != null && this.spyName.equals(child.getAttribute(Heading.ATTRIBUTE_DATA_SUMMARY))) {
					headings.add(child);
				}
			} else {
				this.collectHeadings(child, headings);
			}
		}
	}
}
 
Example 5
Source File: ClientUtils.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Dumps Element content and traverse its children.
 * <p/><b>WARNING:</b> it is com.google.gwt.dom.client.Element not com.google.gwt.xml.client.Element!!!
 * 
 * @param elm an element to dump
 * @param indent row indentation for current level
 * @param indentIncrement increment for next level
 * @param sb dumped content
 * @return dumped content
 */
public static StringBuilder dump(Element elm, String indent, String indentIncrement, StringBuilder sb) {
    int childCount = elm.getChildCount();
    String innerText = elm.getInnerText();
    String lang = elm.getLang();
    String nodeName = elm.getNodeName();
    short nodeType = elm.getNodeType();
    String getString = elm.getString();
    String tagNameWithNS = elm.getTagName();
    String xmlLang = elm.getAttribute("xml:lang");

    sb.append(ClientUtils.format("%sElement {nodeName: %s, nodeType: %s, tagNameWithNS: %s, lang: %s,"
            + " childCount: %s, getString: %s, xmlLang: %s}\n",
            indent, nodeName, nodeType, tagNameWithNS, lang, childCount, getString, xmlLang));
    NodeList<Node> childNodes = elm.getChildNodes();
    indent += indentIncrement;
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.getItem(i);
        if (Element.is(child)) {
            dump(Element.as(child), indent, indentIncrement, sb);
        } else {
            sb.append(ClientUtils.format("%sNode: nodeType: %s, nodeName: %s, childCount: %s, nodeValue: %s\n",
                    indent, child.getNodeType(), child.getNodeName(), child.getChildCount(), child.getNodeValue()));
        }
    }
    return sb;
}
 
Example 6
Source File: GroupedListBox.java    From swcv with MIT License 5 votes vote down vote up
protected OptionElement getOption(int index)
{
    checkIndex(index);

    // first check ungrouped
    Element elm = getElement();
    int sz = elm.getChildCount();
    int firstGroup = getIndexOfFirstGroup();
    if (index >= 0 && index < firstGroup && index < sz)
    {
        return option(elm.getChild(index));
    }

    // then go through the groups
    int childIndex = index - firstGroup;
    for (int i = firstGroup; i <= index && i < sz; i++)
    {
        Node child = elm.getChild(i);
        if (isGroup(child))
        {
            if (childIndex < child.getChildCount())
            {
                return option(child.getChild(childIndex));
            }
            else
            {
                childIndex -= child.getChildCount();
            }
        }
    }
    return null;
}
 
Example 7
Source File: GroupedListBox.java    From swcv with MIT License 5 votes vote down vote up
protected int getIndexOfFirstGroup()
{
    Element elm = getElement();
    int sz = elm.getChildCount();
    for (int i = 0; i < sz; i++)
    {
        if (isGroup(elm.getChild(i)))
        {
            return i;
        }
    }
    return sz;
}
 
Example 8
Source File: VDDFormLayoutDropHandler.java    From cuba with Apache License 2.0 4 votes vote down vote up
private Widget getTableRowWidgetFromDragEvent(VDragEvent event) {

        /**
         * Find the widget of the row
         */
        Element e = event.getElementOver();

        if (getLayout().table.getRowCount() == 0) {
            /*
             * Empty layout
             */
            return getLayout();
        }

        /**
         * Check if element is inside one of the table widgets
         */
        for (int i = 0; i < getLayout().table.getRowCount(); i++) {
            Element caption = getLayout().table
                    .getWidget(i, getLayout().COLUMN_CAPTION).getElement();
            Element error = getLayout().table
                    .getWidget(i, getLayout().COLUMN_ERRORFLAG).getElement();
            Element widget = getLayout().table
                    .getWidget(i, getLayout().COLUMN_WIDGET).getElement();
            if (caption.isOrHasChild(e) || error.isOrHasChild(e)
                    || widget.isOrHasChild(e)) {
                return getLayout().table.getWidget(i,
                        getLayout().COLUMN_WIDGET);
            }
        }

        /*
         * Is the element a element outside the row structure but inside the
         * layout
         */
        Element rowElement = getLayout().getRowFromChildElement(e,
                getLayout().getElement());
        if (rowElement != null) {
            Element tableElement = rowElement.getParentElement();
            for (int i = 0; i < tableElement.getChildCount(); i++) {
                Element r = tableElement.getChild(i).cast();
                if (r.equals(rowElement)) {
                    return getLayout().table.getWidget(i,
                            getLayout().COLUMN_WIDGET);
                }
            }
        }

        /*
         * Element was not found in rows so defaulting to the form layout
         * instead
         */
        return getLayout();
    }
 
Example 9
Source File: SelectionImplIE.java    From swellrt with Apache License 2.0 4 votes vote down vote up
private Point<Node> binarySearchForRange(JsTextRangeIE target, Element parent) {
  try {
    JsTextRangeIE attempt = JsTextRangeIE.create();
    int low = 0;
    int high = parent.getChildCount() - 1;
    while (low <= high) {
      int mid = (low + high) >>> 1;
      Node node = parent.getChild(mid);
      node.getParentNode().insertBefore(setter, node);
      attempt.moveToElementText(setter).collapse(false);
      int cmp = attempt.compareEndPoints(CompareMode.StartToEnd, target);

      if (cmp == 0) {
        if (DomHelper.isTextNode(node)) {
          return Point.inText(node, 0);
        } else {
          return Point.inElement(parent, node);
        }
      } else if (cmp > 0) {
        high = mid - 1;
      } else {
        if (DomHelper.isTextNode(node)) {
          JsTextRangeIE dup = attempt.duplicate();
          dup.setEndPoint(EndToEnd, target);
          if (dup.getText().length() <= node.<Text> cast().getLength()) {
            return Point.inText(node, dup.getText().length());
          }
        } else {
          attempt.moveToElementText(node.<Element> cast()).collapse(false);
          if (attempt.compareEndPoints(StartToStart, target) >= 0) {
            return Point.inElement(parent, node);
          }

        }
        low = mid + 1;
      }
      setter.removeFromParent();
    }
    return Point.<Node> end(parent);
  } finally {
    setter.removeFromParent();
  }
}
 
Example 10
Source File: HTMLPretty.java    From swellrt with Apache License 2.0 4 votes vote down vote up
/**
 * @param n Node to pretty print
 * @param selection Selection to mark in pretty print
 * @param indent Indentation level to print with.
 * @param multiLine True if output should be multi-line
 * @return A pretty-print HTML string (with '<' and '>' already escaped)
 */
private static String print(Node n, PointRange<Node> selection, int indent, boolean multiLine) {

  // Inspect selection's relevance to this element
  boolean collapsed = selection != null && selection.isCollapsed();
  boolean printStartMarker =
      selection != null && selection.getFirst().getContainer().equals(n);
  boolean printEndMarker =
      selection != null && !collapsed && selection.getSecond().getContainer().equals(n);
  String startMarker =
      printStartMarker ? (collapsed ? "|" : "[") : "";
  String endMarker = printEndMarker ? "]" : "";
  if (DomHelper.isTextNode(n)) {
    // Print text node as 'value'
    String value = displayWhitespace(n.getNodeValue());
    int startOffset = printStartMarker ? selection.getFirst().getTextOffset() : 0;
    int endOffset = printEndMarker ? selection.getSecond().getTextOffset() : value.length();
    String ret = "'" + value.substring(0, startOffset)
        + startMarker
        + value.substring(startOffset, endOffset)
        + endMarker
        + value.substring(endOffset, value.length())
        + "'" ;
    return multiLine ? StringUtil.xmlEscape(ret) : ret;
  } else {
    Element e = n.cast();
    if (e.getChildCount() == 0) {
      // Print child-less element as self-closing tag
      return startTag(e, true, multiLine);
    } else {
      boolean singleLineHtml = multiLine &&
        (e.getChildCount() == 1 &&
          e.getFirstChild()
          .getChildCount() == 0);
      // Print element w/ children. One line each for start tag, child, end tag
      String pretty = startTag(e, false, multiLine);
      Node child = e.getFirstChild();
      Node startNodeAfter = selection.getFirst().getNodeAfter();
      Node endNodeAfter = selection.getSecond().getNodeAfter();
      while (child != null) {
        pretty += (multiLine && !singleLineHtml ? newLine(indent + 1) : "")
          + (printStartMarker && child.equals(startNodeAfter) ? startMarker : "")
          + (printEndMarker && child.equals(endNodeAfter) ? endMarker : "")
          + print(child, selection, indent + 1, multiLine);
        child = child.getNextSibling();
      }
      if (printEndMarker && endNodeAfter == null) {
        pretty += endMarker;
      }
      return pretty + (multiLine  && !singleLineHtml ? newLine(indent) : "")
          + endTag(e, multiLine);
    }
  }
}
 
Example 11
Source File: GroupedListBox.java    From swcv with MIT License 4 votes vote down vote up
@Override
public void insertItem(String item, String value, int index)
{
    // find the delimiter if there is one
    int pipe = (item != null) ? item.indexOf('|') : -1;
    while (pipe != -1 && pipe + 1 != item.length() && item.charAt(pipe + 1) == '|')
    {
        pipe = item.indexOf('|', pipe + 2);
    }

    // extract the group if we found a delimiter
    String group = null;
    if (pipe != -1)
    {
        group = item.substring(0, pipe).trim();
        item = item.substring(pipe + 1).trim();

        // make sure we convert || -> | in the group name
        group = group.replace("||", "|");
    }

    Element parent = getSelectElement();
    Node before = null;

    if (group != null)
    {
        OptGroupElement optgroup = findOptGroupElement(group);
        if (optgroup != null)
        {
            // add it to this optgroup
            parent = optgroup;

            // adjust the index to inside the group
            int adjusted = getIndexInGroup(group, index);

            // we had a real index (wasn't negative which means
            // add to the end), but it was too low for this group.
            // put it at the beginning of the group.
            if (adjusted < 0 && index >= 0)
            {
                adjusted = 0;
            }

            // check the range and if it's out of range, we'll
            // just add it to the end
            // of the group (before == null)
            if (0 <= adjusted && adjusted < optgroup.getChildCount())
            {
                before = optgroup.getChild(adjusted);
            }
        }
        else
        {
            // add a new group and add the item to it
            optgroup = Document.get().createOptGroupElement();
            optgroup.setLabel(group);
            parent.appendChild(optgroup);
            parent = optgroup;
            before = null;
        }
    }
    else
    {
        // make sure we're not past the initial "group" of
        // ungrouped options
        int max = getIndexOfFirstGroup();
        if (index < 0 || index > max)
        {
            before = (max < parent.getChildCount()) ? parent.getChild(max) : null;
        }
        else if (0 <= index && index < parent.getChildCount())
        {
            before = parent.getChild(index);
        }
    }

    OptionElement option = createOption(item, value);
    parent.insertBefore(option, before);
}
 
Example 12
Source File: SelectionImplIE.java    From incubator-retired-wave with Apache License 2.0 4 votes vote down vote up
private Point<Node> binarySearchForRange(JsTextRangeIE target, Element parent) {
  try {
    JsTextRangeIE attempt = JsTextRangeIE.create();
    int low = 0;
    int high = parent.getChildCount() - 1;
    while (low <= high) {
      int mid = (low + high) >>> 1;
      Node node = parent.getChild(mid);
      node.getParentNode().insertBefore(setter, node);
      attempt.moveToElementText(setter).collapse(false);
      int cmp = attempt.compareEndPoints(CompareMode.StartToEnd, target);

      if (cmp == 0) {
        if (DomHelper.isTextNode(node)) {
          return Point.inText(node, 0);
        } else {
          return Point.inElement(parent, node);
        }
      } else if (cmp > 0) {
        high = mid - 1;
      } else {
        if (DomHelper.isTextNode(node)) {
          JsTextRangeIE dup = attempt.duplicate();
          dup.setEndPoint(EndToEnd, target);
          if (dup.getText().length() <= node.<Text> cast().getLength()) {
            return Point.inText(node, dup.getText().length());
          }
        } else {
          attempt.moveToElementText(node.<Element> cast()).collapse(false);
          if (attempt.compareEndPoints(StartToStart, target) >= 0) {
            return Point.inElement(parent, node);
          }

        }
        low = mid + 1;
      }
      setter.removeFromParent();
    }
    return Point.<Node> end(parent);
  } finally {
    setter.removeFromParent();
  }
}
 
Example 13
Source File: HTMLPretty.java    From incubator-retired-wave with Apache License 2.0 4 votes vote down vote up
/**
 * @param n Node to pretty print
 * @param selection Selection to mark in pretty print
 * @param indent Indentation level to print with.
 * @param multiLine True if output should be multi-line
 * @return A pretty-print HTML string (with '<' and '>' already escaped)
 */
private static String print(Node n, PointRange<Node> selection, int indent, boolean multiLine) {

  // Inspect selection's relevance to this element
  boolean collapsed = selection != null && selection.isCollapsed();
  boolean printStartMarker =
      selection != null && selection.getFirst().getContainer().equals(n);
  boolean printEndMarker =
      selection != null && !collapsed && selection.getSecond().getContainer().equals(n);
  String startMarker =
      printStartMarker ? (collapsed ? "|" : "[") : "";
  String endMarker = printEndMarker ? "]" : "";
  if (DomHelper.isTextNode(n)) {
    // Print text node as 'value'
    String value = displayWhitespace(n.getNodeValue());
    int startOffset = printStartMarker ? selection.getFirst().getTextOffset() : 0;
    int endOffset = printEndMarker ? selection.getSecond().getTextOffset() : value.length();
    String ret = "'" + value.substring(0, startOffset)
        + startMarker
        + value.substring(startOffset, endOffset)
        + endMarker
        + value.substring(endOffset, value.length())
        + "'" ;
    return multiLine ? StringUtil.xmlEscape(ret) : ret;
  } else {
    Element e = n.cast();
    if (e.getChildCount() == 0) {
      // Print child-less element as self-closing tag
      return startTag(e, true, multiLine);
    } else {
      boolean singleLineHtml = multiLine &&
        (e.getChildCount() == 1 &&
          e.getFirstChild()
          .getChildCount() == 0);
      // Print element w/ children. One line each for start tag, child, end tag
      String pretty = startTag(e, false, multiLine);
      Node child = e.getFirstChild();
      Node startNodeAfter = selection.getFirst().getNodeAfter();
      Node endNodeAfter = selection.getSecond().getNodeAfter();
      while (child != null) {
        pretty += (multiLine && !singleLineHtml ? newLine(indent + 1) : "")
          + (printStartMarker && child.equals(startNodeAfter) ? startMarker : "")
          + (printEndMarker && child.equals(endNodeAfter) ? endMarker : "")
          + print(child, selection, indent + 1, multiLine);
        child = child.getNextSibling();
      }
      if (printEndMarker && endNodeAfter == null) {
        pretty += endMarker;
      }
      return pretty + (multiLine  && !singleLineHtml ? newLine(indent) : "")
          + endTag(e, multiLine);
    }
  }
}
 
Example 14
Source File: DomHelper.java    From swellrt with Apache License 2.0 2 votes vote down vote up
/**
 * Given a node/offset pair, return the node after the point.
 *
 * @param container
 * @param offset
 */
public static Node nodeAfterFromOffset(Element container, int offset) {
  return offset >= container.getChildCount() ? null : container.getChild(offset);
}
 
Example 15
Source File: DomHelper.java    From incubator-retired-wave with Apache License 2.0 2 votes vote down vote up
/**
 * Given a node/offset pair, return the node after the point.
 *
 * @param container
 * @param offset
 */
public static Node nodeAfterFromOffset(Element container, int offset) {
  return offset >= container.getChildCount() ? null : container.getChild(offset);
}