/** * Returns the start offset of the next sibling of the parent element. Returns -1 if there is no * previous sibling in the parent. * * @param vexWidget VexWidget to use. */ public static int getPreviousSiblingStart(IVexWidget vexWidget) { int startOffset; if (vexWidget.hasSelection()) { startOffset = vexWidget.getSelectionStart(); } else { Box box = vexWidget.findInnermostBox( new IBoxFilter() { public boolean matches(Box box) { return box instanceof BlockBox && box.getElement() != null; } }); if (box.getElement() == vexWidget.getDocument().getRootElement()) { return -1; } startOffset = box.getElement().getStartOffset(); } int previousSiblingStart = -1; VEXElement parent = vexWidget.getDocument().getElementAt(startOffset); List<VEXNode> children = parent.getChildNodes(); for (VEXNode child : children) { if (startOffset == child.getStartOffset()) { break; } previousSiblingStart = child.getStartOffset(); } return previousSiblingStart; }
/** * Returns an array of the selected block boxes. Text nodes between boxes are not returned. If the * selection does not enclose any block boxes, returns an empty array. * * @param vexWidget VexWidget to use. */ public static BlockBox[] getSelectedBlockBoxes(final IVexWidget vexWidget) { if (!vexWidget.hasSelection()) { return new BlockBox[0]; } Box parent = vexWidget.findInnermostBox( new IBoxFilter() { public boolean matches(Box box) { System.out.println("Matching " + box); return box instanceof BlockBox && box.getStartOffset() <= vexWidget.getSelectionStart() && box.getEndOffset() >= vexWidget.getSelectionEnd(); } }); System.out.println("Matched " + parent); List<BlockBox> blockList = new ArrayList<BlockBox>(); Box[] children = parent.getChildren(); System.out.println("Parent has " + children.length + " children"); for (Box child : children) { if (child instanceof BlockBox && child.getStartOffset() >= vexWidget.getSelectionStart() && child.getEndOffset() <= vexWidget.getSelectionEnd()) { System.out.println(" adding " + child); blockList.add((BlockBox) child); } else { System.out.println(" skipping " + child); } } return blockList.toArray(new BlockBox[blockList.size()]); }
/** * Returns true if the given element or range is at least partially selected. * * @param vexWidget IVexWidget being tested. * @param elementOrRange Element or IntRange being tested. */ public static boolean elementOrRangeIsPartiallySelected( IVexWidget vexWidget, Object elementOrRange) { IntRange range = getInnerRange(elementOrRange); return range.getEnd() >= vexWidget.getSelectionStart() && range.getStart() <= vexWidget.getSelectionEnd(); }