/** * Returns the number of lines in the document. */ public final int getLineCount() { return document.getDefaultRootElement().getElementCount(); }
/** * Returns the line containing the specified offset. * @param offset The offset */ public final int getLineOfOffset(int offset) { return document.getDefaultRootElement().getElementIndex(offset); }
/** * Reparses the document, by passing all lines to the token * marker. This should be called after the document is first * loaded. */ public void tokenizeLines() { tokenizeLines(0,getDefaultRootElement().getElementCount()); }
/** * Returns the start offset of the specified line. * @param line The line * @return The start offset of the specified line, or -1 if the line is * invalid */ public int getLineStartOffset(int line) { Element lineElement = document.getDefaultRootElement() .getElement(line); if(lineElement == null) return -1; else return lineElement.getStartOffset(); }
/** * Returns the end offset of the specified line. * @param line The line * @return The end offset of the specified line, or -1 if the line is * invalid. */ public int getLineEndOffset(int line) { Element lineElement = document.getDefaultRootElement() .getElement(line); if(lineElement == null) return -1; else return lineElement.getEndOffset(); }
/** * Sets the token marker that is to be used to split lines of * this document up into tokens. May throw an exception if * this is not supported for this type of document. * @param tm The new token marker */ public void setTokenMarker(TokenMarker tm) { tokenMarker = tm; if(tm == null) return; tokenMarker.insertLines(0,getDefaultRootElement() .getElementCount()); tokenizeLines(); }
/** * Returns the length of the specified line. * @param line The line */ public int getLineLength(int line) { Element lineElement = document.getDefaultRootElement() .getElement(line); if(lineElement == null) return -1; else return lineElement.getEndOffset() - lineElement.getStartOffset() - 1; }
/** * Returns the offset where the selection ends on the specified * line. */ public int getSelectionEnd(int line) { if(line == selectionEndLine) return selectionEnd; else if(rectSelect) { Element map = document.getDefaultRootElement(); int end = selectionEnd - map.getElement(selectionEndLine) .getStartOffset(); Element lineElement = map.getElement(line); int lineStart = lineElement.getStartOffset(); int lineEnd = lineElement.getEndOffset() - 1; return Math.min(lineEnd,lineStart + end); } else return getLineEndOffset(line) - 1; }
Element map = document.getDefaultRootElement();
/** * Returns the offset where the selection starts on the specified * line. */ public int getSelectionStart(int line) { if(line == selectionStartLine) return selectionStart; else if(rectSelect) { Element map = document.getDefaultRootElement(); int start = selectionStart - map.getElement(selectionStartLine) .getStartOffset(); Element lineElement = map.getElement(line); int lineStart = lineElement.getStartOffset(); int lineEnd = lineElement.getEndOffset() - 1; return Math.min(lineEnd,lineStart + start); } else return getLineStartOffset(line); }
/** * We overwrite this method to update the token marker * state immediately so that any event listeners get a * consistent token marker. */ protected void fireInsertUpdate(DocumentEvent evt) { if(tokenMarker != null) { DocumentEvent.ElementChange ch = evt.getChange( getDefaultRootElement()); if(ch != null) { tokenMarker.insertLines(ch.getIndex() + 1, ch.getChildrenAdded().length - ch.getChildrenRemoved().length); } } super.fireInsertUpdate(evt); }
/** * We overwrite this method to update the token marker * state immediately so that any event listeners get a * consistent token marker. */ protected void fireRemoveUpdate(DocumentEvent evt) { if(tokenMarker != null) { DocumentEvent.ElementChange ch = evt.getChange( getDefaultRootElement()); if(ch != null) { tokenMarker.deleteLines(ch.getIndex() + 1, ch.getChildrenRemoved().length - ch.getChildrenAdded().length); } } super.fireRemoveUpdate(evt); } }
Element map = document.getDefaultRootElement();
/** * Reparses the document, by passing the specified lines to the * token marker. This should be called after a large quantity of * text is first inserted. * @param start The first line to parse * @param len The number of lines, after the first one to parse */ public void tokenizeLines(int start, int len) { if(tokenMarker == null || !tokenMarker.supportsMultilineTokens()) return; Segment lineSegment = new Segment(); Element map = getDefaultRootElement(); len += start; try { for(int i = start; i < len; i++) { Element lineElement = map.getElement(i); int lineStart = lineElement.getStartOffset(); getText(lineStart,lineElement.getEndOffset() - lineStart - 1,lineSegment); tokenMarker.markTokens(lineSegment,i); } } catch(BadLocationException bl) { bl.printStackTrace(); } }
protected void documentChanged(DocumentEvent evt) { DocumentEvent.ElementChange ch = evt.getChange( document.getDefaultRootElement()); int count; if(ch == null) count = 0; else count = ch.getChildrenAdded().length - ch.getChildrenRemoved().length; int line = getLineOfOffset(evt.getOffset()); if(count == 0) { painter.invalidateLine(line); } // do magic stuff else if(line < firstLine) { setFirstLine(firstLine + count); } // end of magic stuff else { painter.invalidateLineRange(line,firstLine + visibleLines); updateScrollBars(); } }