JIDE 3.5.15

com.jidesoft.editor
Class LazyLoadDocument

java.lang.Object
  extended by javax.swing.text.AbstractDocument
      extended by javax.swing.text.PlainDocument
          extended by com.jidesoft.editor.SyntaxDocument
              extended by com.jidesoft.editor.LazyLoadDocument
All Implemented Interfaces:
LineBreak, Serializable, Document

public class LazyLoadDocument
extends SyntaxDocument

This class is to provide lazy loading feature for CodeEditor to edit extremely large document.

Basically, this class will not load everything from the original file to save the memory. It will divide the file to many pages depending on getPageLineSize(). It will then load the page on request only. You don't have to manually load/unload the pages. The loading process is triggered by those methods like getText(int, int), getLineStartOffset(int) , etc. You could check if a line/offset is already loaded by isLineLoaded(int) and isOffsetLoaded(int). Those two methods will not trigger a page loading/unloading operation.

You could invoke setMaximumPages(int) to configure the maximum unmodified page in the memory. The less you configured, the less memory the document will use. However, you may have more chance to reload a new page, which takes time for a large file. You could also invoke setPageLineSize(int) to balance the memory usage and the performance.

We do provide page loading/loaded event. You could invoke addPageLoadingListener(PageLoadListener) to listen to that.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class com.jidesoft.editor.SyntaxDocument
SyntaxDocument.MyUndoableEditListener
 
Nested classes/interfaces inherited from class javax.swing.text.AbstractDocument
AbstractDocument.AbstractElement, AbstractDocument.AttributeContext, AbstractDocument.BranchElement, AbstractDocument.Content, AbstractDocument.DefaultDocumentEvent, AbstractDocument.ElementEdit, AbstractDocument.LeafElement
 
Field Summary
 
Fields inherited from class com.jidesoft.editor.SyntaxDocument
_compoundEdit, _compoundEditCount, _undoManager, tokenMarker
 
Fields inherited from class javax.swing.text.PlainDocument
lineLimitAttribute, tabSizeAttribute
 
Fields inherited from class javax.swing.text.AbstractDocument
BAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, listenerList, ParagraphElementName, SectionElementName
 
Fields inherited from interface com.jidesoft.editor.LineBreak
CR, LF, LINE_BREAK_CODE_EDITOR, LINE_BREAK_MAC, LINE_BREAK_MIXED, LINE_BREAK_PC, LINE_BREAK_UNIX, LINE_BREAK_UNKNOWN, STRING_CR, STRING_CR_LF, STRING_LF
 
Fields inherited from interface javax.swing.text.Document
StreamDescriptionProperty, TitleProperty
 
Constructor Summary
LazyLoadDocument()
          Constructor
 
Method Summary
 void addPageLoadingListener(PageLoadListener listener)
          Add page loading listener.
 char charAt(int offset)
           
protected  DocumentEvent createCombinedDocumentEvent(int offset, DocumentEvent originalEvent)
          Create a DocumentEvent to file.
protected  DocumentEvent createCombinedDocumentEvent(int offset, int length, DocumentEvent.EventType type)
          Create a DocumentEvent to file.
protected  AbstractDocument.AbstractElement createDefaultRoot()
           
protected  void firePageLoadedEvent(int startLineIndex, boolean pageLoadThread)
          Fire page loaded event.
protected  void firePageStartLoadingEvent(int startLineIndex, boolean pageLoadThread)
          Fire page start loading event.
 Element getDefaultRootElement()
           
 String getFileName()
          Get the file name the CodeEditor is currently editing.
 CharSequence getLazyLoadText()
          Get the lazy load text for search purpose.
 int getLength()
           
 int getLineEndOffset(int line)
           
 int getLineNumber(int offset)
           
 int getLineStartOffset(int line)
           
 String getLineText(int offset)
           
 int getMaximumPages()
          Get maximum pages.
 int getPageLineSize()
          Get line size in a page.
 PageLoadListener[] getPageLoadListeners()
          Get all page load listeners.
 String getText(int offset, int length)
          Gets a sequence of text from the document.
 void getText(int offset, int length, Segment txt)
           
 void insertString(int offs, String str, AttributeSet a)
           
 boolean isLineLoaded(int line)
          Check if the line is loaded in memory already.
 boolean isOffsetLoaded(int offset)
          Check if the offset is loaded in memory already.
protected  boolean needUndoRedo(int offs, int length, DocumentEvent.EventType type)
          Configure if an operation is undoable/redoable.
 void remove(int offs, int len)
           
 void removePageLoadingListener(PageLoadListener listener)
          Remove page loading listener.
 void setFileName(String fileName)
          Set the file name the CodeEditor is currently editing.
 void setMaximumPages(int maximumPages)
          Set maximum pages.
 void setPageLineSize(int pageLineSize)
          Set line size in a page.
 void setTokenMarker(TokenMarker tm)
          Sets the token marker that is to be used to split lines of this document up into tokens.
 
Methods inherited from class com.jidesoft.editor.SyntaxDocument
addUndoableEdit, beginCompoundEdit, convertLineBreaks, convertLineBreaks, endCompoundEdit, findNext, findNext, findPrevious, findPrevious, fireInsertUpdate, fireRemoveUpdate, fireRemovingUpdate, getLineCount, getLineLength, getLookupBufferSize, getTokenMarker, getUndoManager, isInCommentOrLiteral, isWordChar, redo, removeUpdate, setLookupBufferSize, setUndoManager, tokenizeLines, tokenizeLines, undo
 
Methods inherited from class javax.swing.text.PlainDocument
getParagraphElement, insertUpdate
 
Methods inherited from class javax.swing.text.AbstractDocument
addDocumentListener, addUndoableEditListener, createBranchElement, createLeafElement, createPosition, dump, fireChangedUpdate, fireUndoableEditUpdate, getAsynchronousLoadPriority, getAttributeContext, getBidiRootElement, getContent, getCurrentWriter, getDocumentFilter, getDocumentListeners, getDocumentProperties, getEndPosition, getListeners, getProperty, getRootElements, getStartPosition, getUndoableEditListeners, postRemoveUpdate, putProperty, readLock, readUnlock, removeDocumentListener, removeUndoableEditListener, render, replace, setAsynchronousLoadPriority, setDocumentFilter, setDocumentProperties, writeLock, writeUnlock
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LazyLoadDocument

public LazyLoadDocument()
Constructor

Method Detail

createCombinedDocumentEvent

protected DocumentEvent createCombinedDocumentEvent(int offset,
                                                    DocumentEvent originalEvent)
Create a DocumentEvent to file.

To fire correct document event from the user of LazyLoadDocument point of view, the document event is created independently. You may need override this method if you want to customize the event.

This method is invoked when the edit is inside a page.

Parameters:
offset - the start offset of the event
originalEvent - the original event from the underlying document
Returns:
the new document event to be fired.

createCombinedDocumentEvent

protected DocumentEvent createCombinedDocumentEvent(int offset,
                                                    int length,
                                                    DocumentEvent.EventType type)
Create a DocumentEvent to file.

To fire correct document event from the user of LazyLoadDocument point of view, the document event is created independently. You may need override this method if you want to customize the event.

This method is invoked when the edit is crossing multiple pages.

Parameters:
offset - the start offset of the event
length - the length of the event
type - the edit type of the event
Returns:
the new document event to be fired.

getFileName

public String getFileName()
Get the file name the CodeEditor is currently editing.

Returns:
the file name.

setFileName

public void setFileName(String fileName)
Set the file name the CodeEditor is currently editing.

Parameters:
fileName - the file name

getLength

public int getLength()
Specified by:
getLength in interface Document
Overrides:
getLength in class AbstractDocument

getDefaultRootElement

public Element getDefaultRootElement()
Specified by:
getDefaultRootElement in interface Document
Overrides:
getDefaultRootElement in class PlainDocument

createDefaultRoot

protected AbstractDocument.AbstractElement createDefaultRoot()
Overrides:
createDefaultRoot in class PlainDocument

getLineNumber

public int getLineNumber(int offset)
Overrides:
getLineNumber in class SyntaxDocument

getText

public String getText(int offset,
                      int length)
               throws BadLocationException
Gets a sequence of text from the document.

This method will copy all the texts inside the range to a String, which may consume memory if the length is huge. Please use getLazyLoadText() to get all the texts for searching or other implementations.

Specified by:
getText in interface Document
Overrides:
getText in class AbstractDocument
Parameters:
offset - the starting offset >= 0
length - the number of characters to retrieve >= 0
Returns:
the text
Throws:
BadLocationException - the range given includes a position that is not a valid position within the document
See Also:
Document.getText(int, int)

getText

public void getText(int offset,
                    int length,
                    Segment txt)
             throws BadLocationException
Specified by:
getText in interface Document
Overrides:
getText in class AbstractDocument
Throws:
BadLocationException

getLineText

public String getLineText(int offset)
                   throws BadLocationException
Overrides:
getLineText in class SyntaxDocument
Throws:
BadLocationException

getPageLineSize

public int getPageLineSize()
Get line size in a page.

Returns:
the line size.
See Also:
setPageLineSize(int)

setPageLineSize

public void setPageLineSize(int pageLineSize)
Set line size in a page.

By default, the value is 10000. You could reduce the value to save memory or enlarge the value to reduce the chance to reload new pages.

Parameters:
pageLineSize - the line size

getLazyLoadText

public CharSequence getLazyLoadText()
Get the lazy load text for search purpose.

If you want to implement search functionality over LazyLoadDocument, please try not use getText(int, int) just in case memory is used up if you have an extremely huge file.

Returns:
the lazy load char sequence.

charAt

public char charAt(int offset)
            throws BadLocationException
Overrides:
charAt in class SyntaxDocument
Throws:
BadLocationException

setTokenMarker

public void setTokenMarker(TokenMarker tm)
Description copied from class: SyntaxDocument
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.

Overrides:
setTokenMarker in class SyntaxDocument
Parameters:
tm - The new token marker

isLineLoaded

public boolean isLineLoaded(int line)
Check if the line is loaded in memory already. This method will not trigger a page loading operation.

If you only have offset on your hand and want to check if the offset is loaded, please use isOffsetLoaded(int). It will not trigger the page loading process. However, if you try to invoke getLineNumber(int) to get line number for the offset then invoke this method, you will always get true because the getLineNumber(int) will trigger page loading process.

Overrides:
isLineLoaded in class SyntaxDocument
Parameters:
line - the line index
Returns:
true if the line is already loaded in memory. Otherwise false.
See Also:
isOffsetLoaded(int)

isOffsetLoaded

public boolean isOffsetLoaded(int offset)
Check if the offset is loaded in memory already. This method will not trigger a page loading operation.

Overrides:
isOffsetLoaded in class SyntaxDocument
Parameters:
offset - the offset to check
Returns:
true if the offset is already loaded in memory. Otherwise false.

getMaximumPages

public int getMaximumPages()
Get maximum pages.

Returns:
the maximum pages
See Also:
setMaximumPages(int)

setMaximumPages

public void setMaximumPages(int maximumPages)
Set maximum pages.

By default, the value is 5. You could reduce the value to save memory or enlarge the value to reduce the chance to reload new pages.

Parameters:
maximumPages - the maximum pages.

addPageLoadingListener

public void addPageLoadingListener(PageLoadListener listener)
Add page loading listener.

Parameters:
listener - the listener
See Also:
removePageLoadingListener(PageLoadListener), getPageLoadListeners()

removePageLoadingListener

public void removePageLoadingListener(PageLoadListener listener)
Remove page loading listener.

Parameters:
listener - the listener

getPageLoadListeners

public PageLoadListener[] getPageLoadListeners()
Get all page load listeners.

Returns:
the listener array.

firePageStartLoadingEvent

protected void firePageStartLoadingEvent(int startLineIndex,
                                         boolean pageLoadThread)
Fire page start loading event.

Parameters:
startLineIndex - the start line index
pageLoadThread - the flag indicating if the event is fired from page load thread

firePageLoadedEvent

protected void firePageLoadedEvent(int startLineIndex,
                                   boolean pageLoadThread)
Fire page loaded event.

Parameters:
startLineIndex - the start line index
pageLoadThread - the flag indicating if the event is fired from page load thread

needUndoRedo

protected boolean needUndoRedo(int offs,
                               int length,
                               DocumentEvent.EventType type)
Configure if an operation is undoable/redoable.

By default, it just returns true. You could override this method to save memory for some operations if you don't care much about the undo/redo functionality for the operation.

Parameters:
offs - the start offset
length - the length
type - the edit type
Returns:
true if the operation is undoable. Otherwise false.

insertString

public void insertString(int offs,
                         String str,
                         AttributeSet a)
                  throws BadLocationException
Specified by:
insertString in interface Document
Overrides:
insertString in class PlainDocument
Throws:
BadLocationException

remove

public void remove(int offs,
                   int len)
            throws BadLocationException
Specified by:
remove in interface Document
Overrides:
remove in class AbstractDocument
Throws:
BadLocationException

getLineStartOffset

public int getLineStartOffset(int line)
Overrides:
getLineStartOffset in class SyntaxDocument

getLineEndOffset

public int getLineEndOffset(int line)
Overrides:
getLineEndOffset in class SyntaxDocument

JIDE 3.5.15