/** * Get the node absolute start location in its residing IStructuredModel. * * @param p * @return the location */ public static int getIndexedRegionLocation(IDOMPosition p) { if (!EditValidateUtil.validPosition(p)) { return -1; } Node parent = p.getContainerNode(); if (p.isText()) { return ((IndexedRegion) parent).getStartOffset() + p.getOffset(); } int index = p.getOffset(); if (!parent.hasChildNodes()) { // Element: if (!isDocument(parent)) { IStructuredDocumentRegion region = ((IDOMNode) parent).getStartStructuredDocumentRegion(); return region.getEnd(); } // Document node: int offset = ((IndexedRegion) parent).getStartOffset(); return offset; } NodeList children = parent.getChildNodes(); // After rightmost child if (children.getLength() == index) { if (!isDocument(parent)) { int pos = getNodeEndNameStartIndex(parent); return pos; } int offset = ((IndexedRegion) parent).getEndOffset(); return offset; } Node node = children.item(index); return ((IndexedRegion) node).getStartOffset(); }
/** * To see whether two IDOMPosition are pointed to a same location. * * @param p1 * @param p2 * @return true if same */ public static boolean isSame(IDOMPosition p1, IDOMPosition p2) { if (p1 == p2 || (p1.getContainerNode() == p2.getContainerNode() && p1.getOffset() == p2.getOffset())) { return true; } return false; }
/** * Valid position and return text, if it contains text node. * * @param position * @return the text */ public Text getText(IDOMPosition position) { if (position.isText()) { if (position.getContainerNode() != null) { return (Text) position.getContainerNode(); } } return null; }
/** * Get position's container node's editable items number. this is temp functions for future use. * * @param position * @return the size */ int getSize(IDOMPosition position) { EditValidateUtil.validPosition(position); if (position.isText()) { return ((Text) position.getContainerNode()).getLength(); } if (position.getContainerNode().hasChildNodes()) { return position.getContainerNode().getChildNodes().getLength(); } return 0; }
/** * Determine whether the position is at node's edge. When the offset is at edge, it is in the * leftmost or rightmost offset of node's region. * * @param position * @param forward * @return true if at edge */ boolean atEdge(IDOMPosition position, boolean forward) { Node node = position.getContainerNode(); int offset = position.getOffset(); if (forward) { if (EditModelQuery.isText(node)) { return offset == node.getNodeValue().length(); } return offset == node.getChildNodes().getLength(); } return offset == 0; }
/** * To determine whether the position is at the edge of a node. TODO: temp func for later * combination * * @param nodePos * @param position * @param left * @return true if linked */ boolean isLinked(IDOMPosition nodePos, IDOMPosition position, boolean left) { int index = getIndexedRegionLocation(position); if (left) { int pos = getIndexedRegionLocation(nodePos); return pos == index; } Node node = null; int end; if (nodePos.isText()) { node = nodePos.getContainerNode(); end = ((IndexedRegion) node).getEndOffset(); } else { node = nodePos.getNextSiblingNode(); Assert.isTrue(node != null); end = ((IndexedRegion) node).getEndOffset(); } return end == index; }
/** * Get position's container node's parent. * * @param position * @return the parent node */ Node getParent(IDOMPosition position) { if (position.isText()) { return position.getContainerNode().getParentNode(); } return position.getContainerNode(); }
/** * Get previous sibling node to position's container node. * * @param position * @return the node */ Node getPreviousSibling(IDOMPosition position) { if (position.isText()) { return position.getContainerNode().getPreviousSibling(); } return position.getPreviousSiblingNode(); }
/** * Get lowest common ancestor of two IDOMPositions' container nodes.. * * @param p1 * @param p2 * @return the nodeh */ public Node getCommonAncestor(IDOMPosition p1, IDOMPosition p2) { Node n1 = p1.getContainerNode(); Node n2 = p2.getContainerNode(); return getCommonAncestor(n1, n2); }
/** * @param p1 * @param p2 * @return true if p1 and p2 are within same text node */ final boolean isWithinSameText(IDOMPosition p1, IDOMPosition p2) { if (p1 == null || p2 == null) { return false; } return p1.isText() && p2.isText() && p1.getContainerNode() == p2.getContainerNode(); }