/** * Traverses the OutlineNode tree and adds a Position for each node to Document. * * <p>Also adds the nodes to type lists of the OutlineInput and calculates the tree depth. * * <p>Old Positions are removed before adding new ones. * * @param rootNodes * @param monitor monitor for the job calling this method */ private void updateDocumentPositions(List<OutlineNode> rootNodes, IProgressMonitor monitor) { TexOutlineInput newOutlineInput = new TexOutlineInput(rootNodes); IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput()); // remove previous positions try { document.removePositionCategory("__outline"); } catch (BadPositionCategoryException bpce) { // do nothing, the category will be added again next, it does not exists the first time } document.addPositionCategory("__outline"); pollCancel(monitor); // add new positions for nodes and their children int maxDepth = 0; for (Iterator<OutlineNode> iter = rootNodes.iterator(); iter.hasNext(); ) { OutlineNode node = iter.next(); int localDepth = addNodePosition(node, document, 0, newOutlineInput); if (localDepth > maxDepth) { maxDepth = localDepth; } pollCancel(monitor); } pollCancel(monitor); // set the new outline input newOutlineInput.setTreeDepth(maxDepth); this.outlineInput = newOutlineInput; }
/** * Handles a single node when traversing the outline tree. Used recursively. * * @param node * @param document * @param parentDepth * @param newOutlineInput * @return */ private int addNodePosition( OutlineNode node, IDocument document, int parentDepth, TexOutlineInput newOutlineInput) { // add the Document position int beginOffset = 0; int length = 0; Position position = null; try { beginOffset = document.getLineOffset(node.getBeginLine() - 1); if (node.getEndLine() - 1 == document.getNumberOfLines()) length = document.getLength() - beginOffset; else length = document.getLineOffset(node.getEndLine() - 1) - beginOffset; position = new Position(beginOffset, length); document.addPosition("__outline", position); } catch (BadLocationException bpe) { throw new OperationCanceledException(); } catch (BadPositionCategoryException bpce) { throw new OperationCanceledException(); } node.setPosition(position); // add node to outline input newOutlineInput.addNode(node); // iterate through the children List<OutlineNode> children = node.getChildren(); int maxDepth = parentDepth + 1; if (children != null) { for (Iterator<OutlineNode> iter = children.iterator(); iter.hasNext(); ) { int localDepth = addNodePosition(iter.next(), document, parentDepth + 1, newOutlineInput); if (localDepth > maxDepth) { maxDepth = localDepth; } } } return maxDepth; }