/** Replaces a node with another, preserving attributes */ public static SNode replaceWithAnother(@NotNull SNode node, SNode replacer) { SNode nodeParent = node.getParent(); if (nodeParent == null) { SModel model = node.getModel(); if (model != null) { node.delete(); model.addRootNode(replacer); } return replacer; } SContainmentLink role = node.getContainmentLink(); assert role != null; if (replacer != null) { // old and new child can have the same node Id // thus it is important to remove old child first SNode anchor = node.getNextSibling(); nodeParent.removeChild(node); SNode replacerParent = replacer.getParent(); if (replacerParent != null) { replacerParent.removeChild(replacer); } nodeParent.insertChildBefore(role, replacer, anchor); } else { nodeParent.removeChild(node); } return replacer; }
private boolean canPasteBefore(EditorCell selectedCell, List<SNode> pasteNodes) { if (!selectedCell.isFirstPositionInBigCell()) return false; SNode anchor = selectedCell.getSNode(); if (anchor.getParent() == null) return false; NodeAndRole nodeAndRole = new NodePaster(pasteNodes).getActualAnchorNode(anchor, anchor.getRoleInParent()); if (nodeAndRole == null) return false; EditorCell targetCell = selectedCell.getEditor().findNodeCell(nodeAndRole.myNode); return targetCell != null && targetCell.getFirstLeaf(CellConditions.SELECTABLE) == selectedCell && new NodePaster(pasteNodes).canPasteRelative(nodeAndRole.myNode); }