/** * {@inheritDoc} * * @see AbstractSelectionExecutable#execute(String) */ public boolean execute(String param) { // Get the selected anchor AnchorElement selectedAnchor = LinkExecutableUtils.getSelectedAnchor(rta); if (selectedAnchor == null) { return false; } Range range = rta.getDocument().getSelection().getRangeAt(0); // unlink // but first check where is the selection. If the selection is a caret and is at one side of the // anchor, just // move the caret out instead of removing the link boolean moveSelection = range.isCollapsed(); boolean isBeginning = false; boolean isEnd = false; if (moveSelection) { // check if it's at the beginning or at the end isBeginning = (domUtils.getFirstAncestor( domUtils.getPreviousLeaf(range), LinkExecutableUtils.ANCHOR_TAG_NAME) != selectedAnchor) && range.getStartOffset() == 0; isEnd = (domUtils.getFirstAncestor( domUtils.getNextLeaf(range), LinkExecutableUtils.ANCHOR_TAG_NAME) != selectedAnchor) && range.getEndOffset() == domUtils.getLength(range.getEndContainer()); } if (moveSelection && (isEnd || isBeginning) && selectedAnchor.getOffsetWidth() > 0) { // cursor it's at the beginning or at the end, move it out of the anchor moveCaretOuside(rta, Element.as(selectedAnchor), isEnd); } else { Element.as(selectedAnchor).unwrap(); } return true; }
/** * {@inheritDoc} * * @see AbstractSelectionExecutable#isEnabled() */ public boolean isEnabled() { // check that there is a selected anchor return super.isEnabled() && LinkExecutableUtils.getSelectedAnchor(rta) != null; }