com.bluemarsh.jswat.view
Class SourceDocument

java.lang.Object
  |
  +--javax.swing.text.AbstractDocument
        |
        +--javax.swing.text.DefaultStyledDocument
              |
              +--com.bluemarsh.jswat.view.SourceDocument
All Implemented Interfaces:
javax.swing.text.Document, java.io.Serializable, javax.swing.text.StyledDocument

public class SourceDocument
extends javax.swing.text.DefaultStyledDocument

Class SourceDocument holds the content for a styled source view. It extends the default styled document and adds line-awareness, such that the number of lines, and line offsets can be queried.

See Also:
Serialized Form

Inner classes inherited from class javax.swing.text.DefaultStyledDocument
javax.swing.text.DefaultStyledDocument.AttributeUndoableEdit, javax.swing.text.DefaultStyledDocument.ElementBuffer, javax.swing.text.DefaultStyledDocument.ElementSpec, javax.swing.text.DefaultStyledDocument.SectionElement
 
Inner classes inherited from class javax.swing.text.AbstractDocument
javax.swing.text.AbstractDocument.AbstractElement, javax.swing.text.AbstractDocument.AttributeContext, javax.swing.text.AbstractDocument.BranchElement, javax.swing.text.AbstractDocument.Content, javax.swing.text.AbstractDocument.DefaultDocumentEvent, javax.swing.text.AbstractDocument.ElementEdit, javax.swing.text.AbstractDocument.LeafElement
 
Fields inherited from class javax.swing.text.DefaultStyledDocument
buffer, BUFFER_SIZE_DEFAULT
 
Fields inherited from class javax.swing.text.AbstractDocument
BAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, listenerList, ParagraphElementName, SectionElementName
 
Fields inherited from interface javax.swing.text.Document
StreamDescriptionProperty, TitleProperty
 
Constructor Summary
SourceDocument()
          Constructs a SourceDocument.
 
Method Summary
 void dump(java.io.PrintStream out)
          Prints diagnostic information to the given output stream.
 int getLineCount()
          Returns the number of lines in this document.
 int getLineEndOffset(int line)
          Get the offset of the end of the given line.
 int getLineOfOffset(int offset)
          Find the line containing the given offset.
 int getLineStartOffset(int line)
          Get the offset of the start of the given line.
 void insertString(int offset, java.lang.String str, javax.swing.text.AttributeSet a)
          Inserts a string of content.
static java.lang.String replaceTabs(java.lang.String str, int tabWidth)
          Find and replace tabs with spaces.
 
Methods inherited from class javax.swing.text.DefaultStyledDocument
addDocumentListener, addStyle, create, createDefaultRoot, getBackground, getCharacterElement, getDefaultRootElement, getFont, getForeground, getLogicalStyle, getParagraphElement, getStyle, getStyleNames, insert, insertUpdate, removeDocumentListener, removeStyle, removeUpdate, setCharacterAttributes, setLogicalStyle, setParagraphAttributes, styleChanged
 
Methods inherited from class javax.swing.text.AbstractDocument
addUndoableEditListener, createBranchElement, createLeafElement, createPosition, fireChangedUpdate, fireInsertUpdate, fireRemoveUpdate, fireUndoableEditUpdate, getAsynchronousLoadPriority, getAttributeContext, getBidiRootElement, getContent, getCurrentWriter, getDocumentProperties, getEndPosition, getLength, getListeners, getProperty, getRootElements, getStartPosition, getText, getText, postRemoveUpdate, putProperty, readLock, readUnlock, remove, removeUndoableEditListener, render, setAsynchronousLoadPriority, setDocumentProperties, writeLock, writeUnlock
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.text.Document
addUndoableEditListener, createPosition, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, putProperty, remove, removeUndoableEditListener, render
 

Constructor Detail

SourceDocument

public SourceDocument()
Constructs a SourceDocument.
Method Detail

dump

public void dump(java.io.PrintStream out)
Prints diagnostic information to the given output stream.
Overrides:
dump in class javax.swing.text.AbstractDocument
Parameters:
out - stream to dump to.

getLineCount

public int getLineCount()
Returns the number of lines in this document. Lines are sequences of text separated by carriage return, line feed, or the pair of carriage return and line feed.
Returns:
line count.

getLineEndOffset

public int getLineEndOffset(int line)
                     throws javax.swing.text.BadLocationException
Get the offset of the end of the given line.
Parameters:
line - zero-based line for which to find the end.
Returns:
offset of end of line.
Throws:
javax.swing.text.BadLocationException - Thrown if line is invalid.

getLineOfOffset

public int getLineOfOffset(int offset)
                    throws javax.swing.text.BadLocationException
Find the line containing the given offset.
Parameters:
offset - offset within document >= 0.
Returns:
zero-based line containing that offset.
Throws:
javax.swing.text.BadLocationException - Thrown if offset is invalid.

getLineStartOffset

public int getLineStartOffset(int line)
                       throws javax.swing.text.BadLocationException
Get the offset of the start of the given line.
Parameters:
line - zero-based line for which to find the start.
Returns:
offset of start of line.
Throws:
javax.swing.text.BadLocationException - Thrown if line is invalid.

insertString

public void insertString(int offset,
                         java.lang.String str,
                         javax.swing.text.AttributeSet a)
                  throws javax.swing.text.BadLocationException
Inserts a string of content. See the superclass documentation for all the details.
Overrides:
insertString in class javax.swing.text.AbstractDocument
Parameters:
offset - the offset into the document to insert the content >= 0. All positions that track change at or after the given location will move.
str - the string to insert.
a - the attributes to associate with the inserted content. This may be null if there are no attributes.
Throws:
javax.swing.text.BadLocationException - the given insert position is not a valid position within the document

replaceTabs

public static java.lang.String replaceTabs(java.lang.String str,
                                           int tabWidth)
Find and replace tabs with spaces.
Parameters:
str - string on which to operate.
tabWidth - width of tabs.
Returns:
result of replacement.