站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > NetBeans API Javadoc (Current Development Version)

FormatWriter (Editor Library) - NetBeans API Javadoc (Current Development Version)

org.netbeans.modules.editor.lib/1 1.14.0 3

org.netbeans.editor.ext
Class FormatWriter

java.lang.Object
  extended by java.io.Writer
      extended by org.netbeans.editor.ext.FormatWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable

public final class FormatWriter
extends Writer

Formatting writter accepts the input-text, formats it and writes the output to the underlying writer. The data written to the writer are immediately splitted into token-items and the chain of token-items is created from them. Then the writer then waits for the flush() or close() method call which then makes the real formatting. The formatting is done through going through the format-layers registered in ExtFormatter and asking them for formatting. These layers go through the chain and possibly add or remove the tokens as necessary. The good thing is that the layers can ask the tokens before those written to the writer. In that case they will get the tokens from the document at point the formatting should be done. The advantage is that the chain is compact so the border between the tokens written to the writer and those that come from the document is invisible.


Field Summary
static boolean debug
          Whether debug messages should be displayed
static boolean debugModify
          Whether debug messages should be displayed
 
Fields inherited from class java.io.Writer
lock
 
Method Summary
 boolean canInsertToken(TokenItem beforeToken)
          Check whether a new token can be inserted into the chain before the given token-item.
 boolean canModifyToken(TokenItem token, int offset)
          Whether the token can be modified either by insertion or removal at the given offset.
 boolean canRemoveToken(TokenItem token)
          Whether the token-item can be removed.
 boolean canSplitEnd(TokenItem token, int endLength)
           
 boolean canSplitStart(TokenItem token, int startLength)
           
 String chainToString(TokenItem token)
           
 String chainToString(TokenItem token, int maxDocumentTokens)
          Debug the current state of the chain.
 void checkChain()
          Check the chain whether it's OK.
 void close()
           
 TokenItem findFirstToken(TokenItem token)
          Find the first token in the chain.
 TokenItem findNonEmptyToken(TokenItem token, boolean backward)
          Check whether the given token has empty text and if so start searching for token with non-empty text in the given direction.
 void flush()
           
 Document getDocument()
          Get the document being formatted
 FormatTokenPosition getFormatStartPosition()
          Get the first token that should be formatted.
 ExtFormatter getFormatter()
           
 int getIndentShift()
           
 TokenItem getLastToken()
          Get the last token in the chain.
 int getOffset()
          Get the starting offset of the formatting
 FormatTokenPosition getPosition(TokenItem token, int offset, Position.Bias bias)
          Get the token-position that corresponds to the given offset inside the given token.
 FormatTokenPosition getTextStartPosition()
          Get the first position that doesn't belong to the document.
 void insertString(TokenItem token, int offset, String text)
          Insert the text at the offset inside the given token.
 TokenItem insertToken(TokenItem beforeToken, TokenID tokenID, TokenContextPath tokenContextPath, String tokenImage)
          Create a new token-item and insert it before the token-item given as parameter.
 boolean isAfter(FormatTokenPosition testedPosition, FormatTokenPosition afterPosition)
          Checks whether the tested position is after some other position.
 boolean isAfter(TokenItem testedToken, TokenItem afterToken)
          It checks whether the tested token is after some other token in the chain.
 boolean isChainModified()
          Return the flag that is set automatically if the new removal or insertion into chain occurs.
 boolean isChainStartPosition(FormatTokenPosition pos)
          Check whether this is the first position in the chain of tokens.
 boolean isIndentOnly()
          Whether the purpose of this writer is to find the proper indentation instead of formatting the tokens.
 boolean isRestartFormat()
          Return whether the layer requested to restart the format.
 void remove(TokenItem token, int offset, int length)
          Remove the length of the characters at the given offset inside the given token.
 void removeToken(TokenItem token)
          Remove the token-item from the chain.
 void setChainModified(boolean chainModified)
           
 void setIndentOnly(boolean indentOnly)
          Sets whether the purpose of this writer is to find the proper indentation instead of formatting the tokens.
 void setIndentShift(int indentShift)
           
 void setRestartFormat(boolean restartFormat)
           
 TokenItem splitEnd(TokenItem token, int endLength, TokenID newTokenID, TokenContextPath newTokenContextPath)
          Create the additional token from the text at the end of the given token.
 TokenItem splitStart(TokenItem token, int startLength, TokenID newTokenID, TokenContextPath newTokenContextPath)
          Create the additional token from the text at the start of the given token.
 void write(char[] cbuf, int off, int len)
           
 void write(char[] cbuf, int off, int len, int[] saveOffsets, Position.Bias[] saveBiases)
           
 
Methods inherited from class java.io.Writer
append, append, append, write, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

debug

public static final boolean debug
Whether debug messages should be displayed


debugModify

public static final boolean debugModify
Whether debug messages should be displayed

Method Detail

getFormatter

public final ExtFormatter getFormatter()

getDocument

public final Document getDocument()
Get the document being formatted


getOffset

public final int getOffset()
Get the starting offset of the formatting


isIndentOnly

public final boolean isIndentOnly()
Whether the purpose of this writer is to find the proper indentation instead of formatting the tokens. It allows to have a modified formatting behavior for the cases when user presses Enter or a key that causes immediate reformatting of the line.


setIndentOnly

public void setIndentOnly(boolean indentOnly)
Sets whether the purpose of this writer is to find the proper indentation instead of formatting the tokens.

See Also:
isIndentOnly()

getFormatStartPosition

public FormatTokenPosition getFormatStartPosition()
Get the first token that should be formatted. This can change as the format-layers continue to change the token-chain. If the caller calls flush(), this method will return null. After additional writing to the writer, new tokens will be added and the first one of them will become the first token to be formatted.

Returns:
the first token that should be formatted. It can be null in case some layer removes all the tokens that should be formatted. Most of the layers probably do nothing in case this value is null.

getTextStartPosition

public FormatTokenPosition getTextStartPosition()
Get the first position that doesn't belong to the document. Initially it's the same as the getFormatStartPosition() but if there are multiple flushes performed on the writer they will differ.


getLastToken

public TokenItem getLastToken()
Get the last token in the chain. It can be null if there are no tokens in the chain.


findFirstToken

public TokenItem findFirstToken(TokenItem token)
Find the first token in the chain. It should be used only when necessary and possibly in situations when the start of the chain was already reached by other methods, because this method will extend the chain till the begining of the document.

Parameters:
token - token from which the search for previous tokens will start. It can be null in which case the last document token or last token are attempted instead.

isAfter

public boolean isAfter(TokenItem testedToken,
                       TokenItem afterToken)
It checks whether the tested token is after some other token in the chain.

Parameters:
testedToken - token to test (whether it's after afterToken or not)
afterToken - token to be compared to the testedToken
Returns:
true whether the testedToken is after afterToken or not. Returns false if the token == afterToken or not or if token is before the afterToken or not.

isAfter

public boolean isAfter(FormatTokenPosition testedPosition,
                       FormatTokenPosition afterPosition)
Checks whether the tested position is after some other position.


findNonEmptyToken

public TokenItem findNonEmptyToken(TokenItem token,
                                   boolean backward)
Check whether the given token has empty text and if so start searching for token with non-empty text in the given direction. If there's no non-empty token in the given direction the method returns null.

Parameters:
token - token to start to search from. If it has zero length, the search for non-empty token is performed in the given direction.

canInsertToken

public boolean canInsertToken(TokenItem beforeToken)
Check whether a new token can be inserted into the chain before the given token-item. The token can be inserted only into the tokens that come from the text that was written to the format-writer but was not yet written to the underlying writer.

Parameters:
beforeToken - token-item before which the new token-item is about to be inserted. It can be null to append the new token to the end of the chain.

insertToken

public TokenItem insertToken(TokenItem beforeToken,
                             TokenID tokenID,
                             TokenContextPath tokenContextPath,
                             String tokenImage)
Create a new token-item and insert it before the token-item given as parameter. The canInsertToken() should be called first to determine whether the given token can be inserted into the chain or not. The token can be inserted only into the tokens that come from the text that was written to the format-writer but was not yet written to the underlying writer.

Parameters:
beforeToken - token-item before which the new token-item is about to be inserted. It can be null to append the new token to the end of the chain.
tokenID - token-id of the new token-item
tokenContextPath - token-context-path of the new token-item
tokenImage - image of the new token-item

canRemoveToken

public boolean canRemoveToken(TokenItem token)
Whether the token-item can be removed. It can be removed only in case it doesn't come from the document's text and it wasn't yet written to the underlying writer.


removeToken

public void removeToken(TokenItem token)
Remove the token-item from the chain. It can be removed only in case it doesn't come from the document's text and it wasn't yet written to the underlying writer.


canSplitStart

public boolean canSplitStart(TokenItem token,
                             int startLength)

splitStart

public TokenItem splitStart(TokenItem token,
                            int startLength,
                            TokenID newTokenID,
                            TokenContextPath newTokenContextPath)
Create the additional token from the text at the start of the given token.

Parameters:
token - token being split.
startLength - length of the text at the begining of the token for which the additional token will be created.
tokenID - token-id that will be assigned to the new token
tokenContextPath - token-context-path that will be assigned to the new token

canSplitEnd

public boolean canSplitEnd(TokenItem token,
                           int endLength)

splitEnd

public TokenItem splitEnd(TokenItem token,
                          int endLength,
                          TokenID newTokenID,
                          TokenContextPath newTokenContextPath)
Create the additional token from the text at the end of the given token.

Parameters:
token - token being split.
endLength - length of the text at the end of the token for which the additional token will be created.
tokenID - token-id that will be assigned to the new token
tokenContextPath - token-context-path that will be assigned to the new token

canModifyToken

public boolean canModifyToken(TokenItem token,
                              int offset)
Whether the token can be modified either by insertion or removal at the given offset.


insertString

public void insertString(TokenItem token,
                         int offset,
                         String text)
Insert the text at the offset inside the given token. All the token-positions at and after the offset will be increased by text.length(). IllegalArgumentException is thrown if offset is wrong.

Parameters:
token - token in which the text is inserted.
offset - offset at which the text will be inserted.
text - text that will be inserted at the offset.

remove

public void remove(TokenItem token,
                   int offset,
                   int length)
Remove the length of the characters at the given offset inside the given token. IllegalArgumentException is thrown if offset or length are wrong.

Parameters:
token - token in which the text is removed.
offset - offset at which the text will be removed.
length - length of the removed text.

getPosition

public FormatTokenPosition getPosition(TokenItem token,
                                       int offset,
                                       Position.Bias bias)
Get the token-position that corresponds to the given offset inside the given token. The returned position is persistent and if the token is removed from chain the position is assigned to the end of the previous token or to the begining of the next token if there's no previous token.

Parameters:
token - token in which the position is created.
offset - inside the token at which the position will be created.
bias - forward or backward bias

isChainStartPosition

public boolean isChainStartPosition(FormatTokenPosition pos)
Check whether this is the first position in the chain of tokens.


write

public void write(char[] cbuf,
                  int off,
                  int len)
           throws IOException
Specified by:
write in class Writer
Throws:
IOException

write

public void write(char[] cbuf,
                  int off,
                  int len,
                  int[] saveOffsets,
                  Position.Bias[] saveBiases)
           throws IOException
Throws:
IOException

isChainModified

public boolean isChainModified()
Return the flag that is set automatically if the new removal or insertion into chain occurs. The formatter can use this flag to detect whether a particular format-layer changed the chain or not.


setChainModified

public void setChainModified(boolean chainModified)

isRestartFormat

public boolean isRestartFormat()
Return whether the layer requested to restart the format. The formatter can use this flag to restart the formatting from the first layer.


setRestartFormat

public void setRestartFormat(boolean restartFormat)

getIndentShift

public int getIndentShift()

setIndentShift

public void setIndentShift(int indentShift)

flush

public void flush()
           throws IOException
Specified by:
flush in interface Flushable
Specified by:
flush in class Writer
Throws:
IOException

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Specified by:
close in class Writer
Throws:
IOException

checkChain

public void checkChain()
Check the chain whether it's OK.


chainToString

public String chainToString(TokenItem token)

chainToString

public String chainToString(TokenItem token,
                            int maxDocumentTokens)
Debug the current state of the chain.

Parameters:
token - mark this token as current one. It can be null.
maxDocumentTokens - how many document tokens should be shown.

org.netbeans.modules.editor.lib/1 1.14.0 3

Built on May 28 2007.  |  Portions Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.