/** * {@inheritDoc} * * @see org.xwiki.rendering.block.Block#addChildren(java.util.List) */ public void addChildren(List< ? extends Block> blocksToAdd) { for (Block blockToAdd : blocksToAdd) { addChild(blockToAdd); } }
/** * {@inheritDoc} * * @see org.xwiki.rendering.block.Block#clone() */ @Override public Block clone() { return clone(null); }
/** * Get the position of the provided block in the list of children. * <p> * Can't use {@link List#indexOf(Object)} since it's using {@link Object#equals(Object)} internally which is not * what we want since two WordBlock with the same text or two spaces are equals for example but we want to be able * to target one specific Block. * * @param block the block * @return the position of the block, -1 if the block can't be found */ private int indexOfChild(Block block) { return indexOfBlock(block, getChildren()); }
/** * {@inheritDoc} * * @see org.xwiki.rendering.block.Block#getPreviousBlockByType(java.lang.Class, boolean) */ public <T extends Block> T getPreviousBlockByType(Class<T> blockClass, boolean recurse) { if (getParent() == null) { return null; } int index = indexOfBlock(this, getParent().getChildren()); // test previous brothers List<Block> blocks = getParent().getChildren(); for (int i = index - 1; i >= 0; --i) { Block previousBlock = blocks.get(i); if (blockClass.isAssignableFrom(previousBlock.getClass())) { return blockClass.cast(previousBlock); } } // test parent if (blockClass.isAssignableFrom(getParent().getClass())) { return blockClass.cast(getParent()); } // recurse return recurse ? getParent().getPreviousBlockByType(blockClass, true) : null; }
/** * {@inheritDoc} * * @see org.xwiki.rendering.block.Block#insertChildAfter(org.xwiki.rendering.block.Block, * org.xwiki.rendering.block.Block) */ public void insertChildAfter(Block blockToInsert, Block previousBlock) { if (previousBlock == null) { insertChildBefore(blockToInsert, null); } else { // If there's a next block to previousBlock then get it to set its previous sibling Block nextBlock = previousBlock.getNextSibling(); if (nextBlock != null) { nextBlock.setPreviousSiblingBlock(blockToInsert); blockToInsert.setNextSiblingBlock(nextBlock); } else { blockToInsert.setNextSiblingBlock(null); } blockToInsert.setPreviousSiblingBlock(previousBlock); previousBlock.setNextSiblingBlock(blockToInsert); this.childrenBlocks.add(indexOfChild(previousBlock) + 1, blockToInsert); } }
int position = indexOfChild(oldBlock); List<Block> blocks = getChildren();
/** * {@inheritDoc} * * @see org.xwiki.rendering.block.Block#getChildrenByType(java.lang.Class, boolean) */ public <T extends Block> List<T> getChildrenByType(Class<T> blockClass, boolean recurse) { List<T> typedBlocks = new ArrayList<T>(); for (Block block : getChildren()) { if (blockClass.isAssignableFrom(block.getClass())) { typedBlocks.add(blockClass.cast(block)); } if (recurse && !block.getChildren().isEmpty()) { typedBlocks.addAll(block.getChildrenByType(blockClass, true)); } } return typedBlocks; }
/** * {@inheritDoc} * * @see org.xwiki.rendering.block.Block#addChild(org.xwiki.rendering.block.Block) */ public void addChild(Block blockToAdd) { insertChildAfter(blockToAdd, null); }
/** * {@inheritDoc} * * @see org.xwiki.rendering.block.Block#replaceChild(Block, Block) */ public void replaceChild(Block newBlock, Block oldBlock) { replaceChild(Collections.singletonList(newBlock), oldBlock); }
/** * {@inheritDoc} * * @see org.xwiki.rendering.block.Block#getParentBlockByType(java.lang.Class) */ public <T extends Block> T getParentBlockByType(Class<T> blockClass) { Block parent = getParent(); if (parent == null || blockClass.isAssignableFrom(parent.getClass())) { return blockClass.cast(parent); } return parent.getParentBlockByType(blockClass); }
this.childrenBlocks.add(indexOfChild(nextBlock), blockToInsert);
/** * {@inheritDoc} * * @see org.xwiki.rendering.block.Block#removeBlock(Block) * @since 2.6RC1 */ public void removeBlock(Block childBlockToRemove) { getChildren().remove(childBlockToRemove); if (childBlockToRemove != null) { Block previousBlock = childBlockToRemove.getPreviousSibling(); if (previousBlock != null) { previousBlock.setNextSiblingBlock(childBlockToRemove.getNextSibling()); } Block nextBlock = childBlockToRemove.getNextSibling(); if (nextBlock != null) { nextBlock.setPreviousSiblingBlock(previousBlock); } childBlockToRemove.setNextSiblingBlock(null); childBlockToRemove.setPreviousSiblingBlock(null); } }
/** * {@inheritDoc} * * @see org.xwiki.rendering.block.AbstractBlock#clone(org.xwiki.rendering.block.BlockFilter) * @since 1.8RC2 */ @Override public ImageBlock clone(BlockFilter blockFilter) { ImageBlock clone = (ImageBlock) super.clone(blockFilter); clone.reference = getReference().clone(); return clone; } }