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

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

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

org.netbeans.editor
Class Formatter

java.lang.Object
  extended by org.netbeans.editor.Formatter
All Implemented Interfaces:
EventListener, SettingsChangeListener
Direct Known Subclasses:
ExtFormatter

public class Formatter
extends Object
implements SettingsChangeListener

Various services related to indentation and text formatting are located here. Each kit can have different formatter so the first action should be getting the right formatter for the given kit by calling Formatter.getFormatter(kitClass).


Constructor Summary
Formatter(Class kitClass)
          Construct new formatter.
 
Method Summary
 void changeBlockIndent(BaseDocument doc, int startPos, int endPos, int shiftCnt)
          Increase/decrease indentation of the block of the code.
 void changeRowIndent(BaseDocument doc, int pos, int newIndent)
          Change the indent of the given row.
 Writer createWriter(Document doc, int offset, Writer writer)
          Creates a writer that formats text that is inserted into it.
 boolean expandTabs()
          Should the typed tabs be expanded to the spaces?
static Formatter getFormatter(Class kitClass)
          Get the formatter for the given kit-class
 String getIndentString(BaseDocument doc, int indent)
           
 String getIndentString(int indent)
          Get the string that is appropriate for the requested indentation.
 Class getKitClass()
          Get the kit-class for which this formatter is constructed.
 int getShiftWidth()
          Get the width of one indentation level for non-BaseDocument documents.
 int getSpacesPerTab()
          Get the number of spaces that should be inserted into the document instead of one typed tab.
 int getTabSize()
          Get the number of spaces the TAB character ('\t') visually represents for non-BaseDocument documents.
 int indentLine(Document doc, int offset)
          Indents the current line.
 int indentNewLine(Document doc, int offset)
          Inserts new line at given position and indents the new line with spaces.
 void insertTabString(BaseDocument doc, int dotPos)
          Modify the line to move the text starting at dotPos one tab column to the right.
 int reformat(BaseDocument doc, int startOffset, int endOffset)
          Reformat a block of code.
 void setExpandTabs(boolean expandTabs)
           
static void setFormatter(Class kitClass, Formatter formatter)
          Set the formatter for the given kit-class.
 void setShiftWidth(int shiftWidth)
          Set the width of one indentation level for non-BaseDocument documents.
 void setSpacesPerTab(int spacesPerTab)
           
 void setTabSize(int tabSize)
          Set the number of spaces the TAB character ('\t') visually represents for non-BaseDocument documents.
 void settingsChange(SettingsChangeEvent evt)
           
 void shiftLine(BaseDocument doc, int dotPos, boolean right)
          Shift line either left or right
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Formatter

public Formatter(Class kitClass)
Construct new formatter.

Parameters:
kitClass - the class of the kit for which this formatter is being constructed.
Method Detail

getFormatter

public static Formatter getFormatter(Class kitClass)
Get the formatter for the given kit-class


setFormatter

public static void setFormatter(Class kitClass,
                                Formatter formatter)
Set the formatter for the given kit-class.

Parameters:
kitClass - class of the kit for which the formatter is being assigned.
formatter - new formatter for the given kit

getKitClass

public Class getKitClass()
Get the kit-class for which this formatter is constructed.


settingsChange

public void settingsChange(SettingsChangeEvent evt)
Specified by:
settingsChange in interface SettingsChangeListener

getTabSize

public int getTabSize()
Get the number of spaces the TAB character ('\t') visually represents for non-BaseDocument documents. It shouldn't be used for BaseDocument based documents. The reason for that is that the returned value reflects the value of the setting for the kit class over which this formatter was constructed. However it's possible that the kit class of the document being formatted is different than the kit of the formatter. For example java document could be formatted by html formatter. Therefore BaseDocument.getTabSize() must be used for BaseDocuments to reflect the document's own tabsize.

See Also:
BaseDocument.getTabSize()

setTabSize

public void setTabSize(int tabSize)
Set the number of spaces the TAB character ('\t') visually represents for non-BaseDocument documents. It doesn't affect BaseDocument based documents.

See Also:
getTabSize(), BaseDocument.setTabSize()

getShiftWidth

public int getShiftWidth()
Get the width of one indentation level for non-BaseDocument documents. The algorithm first checks whether there's a value for the INDENT_SHIFT_WIDTH setting. If so it uses it, otherwise it uses getSpacesPerTab()

See Also:
setShiftWidth(), getSpacesPerTab()

setShiftWidth

public void setShiftWidth(int shiftWidth)
Set the width of one indentation level for non-BaseDocument documents. It doesn't affect BaseDocument based documents.

See Also:
getShiftWidth()

expandTabs

public boolean expandTabs()
Should the typed tabs be expanded to the spaces?


setExpandTabs

public void setExpandTabs(boolean expandTabs)

getSpacesPerTab

public int getSpacesPerTab()
Get the number of spaces that should be inserted into the document instead of one typed tab.


setSpacesPerTab

public void setSpacesPerTab(int spacesPerTab)

getIndentString

public String getIndentString(BaseDocument doc,
                              int indent)

getIndentString

public String getIndentString(int indent)
Get the string that is appropriate for the requested indentation. The returned string respects the expandTabs() and the getTabSize() and will contain either spaces only or fully or partially tabs as necessary.


insertTabString

public void insertTabString(BaseDocument doc,
                            int dotPos)
                     throws BadLocationException
Modify the line to move the text starting at dotPos one tab column to the right. Whitespace preceeding dotPos may be replaced by a TAB character if tabs expanding is on.

Parameters:
doc - document to operate on
dotPos - insertion point
Throws:
BadLocationException

changeRowIndent

public void changeRowIndent(BaseDocument doc,
                            int pos,
                            int newIndent)
                     throws BadLocationException
Change the indent of the given row. Document is atomically locked during this operation.

Throws:
BadLocationException

changeBlockIndent

public void changeBlockIndent(BaseDocument doc,
                              int startPos,
                              int endPos,
                              int shiftCnt)
                       throws BadLocationException
Increase/decrease indentation of the block of the code. Document is atomically locked during the operation.

Parameters:
doc - document to operate on
startPos - starting line position
endPos - ending line position
shiftCnt - positive/negative count of shiftwidths by which indentation should be shifted right/left
Throws:
BadLocationException

shiftLine

public void shiftLine(BaseDocument doc,
                      int dotPos,
                      boolean right)
               throws BadLocationException
Shift line either left or right

Throws:
BadLocationException

reformat

public int reformat(BaseDocument doc,
                    int startOffset,
                    int endOffset)
             throws BadLocationException
Reformat a block of code.

Parameters:
doc - document to work with
startOffset - offset at which the formatting starts
endOffset - offset at which the formatting ends
Returns:
length of the reformatted code
Throws:
BadLocationException

indentLine

public int indentLine(Document doc,
                      int offset)
Indents the current line. Should not affect any other lines.

Parameters:
doc - the document to work on
offset - the offset of a character on the line
Returns:
new offset of the original character

indentNewLine

public int indentNewLine(Document doc,
                         int offset)
Inserts new line at given position and indents the new line with spaces.

Parameters:
doc - the document to work on
offset - the offset of a character on the line
Returns:
new offset to place cursor to

createWriter

public Writer createWriter(Document doc,
                           int offset,
                           Writer writer)
Creates a writer that formats text that is inserted into it. The writer should not modify the document but use the provided writer to write to. Usually the underlaying writer will modify the document itself and optionally it can remember the current position in document. That is why the newly created writer should do no buffering.

The provided document and pos are only informational, should not be modified but only used to find correct indentation strategy.

Parameters:
doc - document
offset - position to begin inserts at
writer - writer to write to
Returns:
new writer that will format written text and pass it into the writer

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.