public void deleteNextChar() throws DocumentValidationException { if (this.hasSelection()) { this.deleteSelection(); } else { int offset = this.getCaretOffset(); VEXDocument doc = this.getDocument(); int n = doc.getLength() - 1; VEXElement element = doc.getElementAt(offset); if (offset == n) { // nop } else if (this.isBetweenMatchingElements(offset)) { this.joinElementsAt(offset); } else if (this.isBetweenMatchingElements(offset + 1)) { this.joinElementsAt(offset + 1); } else if (element.isEmpty()) { // deleting the right sentinel of an empty element // so just delete the whole element an move on this.moveTo(offset - 1, false); this.moveTo(offset + 1, true); this.deleteSelection(); } else if (doc.getElementAt(offset + 1).isEmpty()) { // deleting the left sentinel of an empty element // so just delete the whole element an move on this.moveTo(offset + 2, true); this.deleteSelection(); } else { if (doc.getCharacterAt(offset) != 0) { this.moveTo(offset, false); this.moveTo(offset + 1, true); this.deleteSelection(); } } } }
/** * Joins the elements at the given offset. Only works if isBetweenMatchingElements returns true * for the same offset. Afterwards, the caret is left at the point where the join occurred. * * @param offset Offset where the two elements meet. */ private void joinElementsAt(int offset) throws DocumentValidationException { if (!isBetweenMatchingElements(offset)) { throw new DocumentValidationException("Cannot join elements at offset " + offset); } boolean success = false; try { this.beginWork(); this.moveTo(offset + 1); VEXElement element = this.getCurrentElement(); boolean moveContent = !element.isEmpty(); VEXDocumentFragment frag = null; if (moveContent) { this.moveTo(element.getEndOffset(), true); frag = this.getSelectedFragment(); this.deleteSelection(); } this.moveBy(-1); this.moveBy(2, true); this.deleteSelection(); this.moveBy(-1); if (moveContent) { int savedOffset = this.getCaretOffset(); this.insertFragment(frag); this.moveTo(savedOffset, false); } success = true; } finally { this.endWork(success); } }