Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.eclipse.jface.text.formatter
Class ContentFormatter  view ContentFormatter download ContentFormatter.java

java.lang.Object
  extended byorg.eclipse.jface.text.formatter.ContentFormatter
All Implemented Interfaces:
IContentFormatter

public class ContentFormatter
extends java.lang.Object
implements IContentFormatter

Standard implementation of IContentFormatter. The formatter supports two operation modes: partition aware and partition unaware.

In the partition aware mode, the formatter determines the partitioning of the document region to be formatted. For each partition it determines all document positions which are affected when text changes are applied to the partition. Those which overlap with the partition are remembered as character positions. These character positions are passed over to the formatting strategy registered for the partition's content type. The formatting strategy returns a string containing the formatted document partition as well as the adapted character positions. The formatted partition replaces the old content of the partition. The remembered document positions are updated with the adapted character positions. In addition, all other document positions are accordingly adapted to the formatting changes.

In the partition unaware mode, the document's partitioning is ignored and the document is considered consisting of only one partition of the content type IDocument.DEFAULT_CONTENT_TYPE. The formatting process is similar to the partition aware mode, with the exception of having only one partition.

Usually, clients instantiate this class and configure it before using it.


Nested Class Summary
(package private)  class ContentFormatter.NonDeletingPositionUpdater
          The position updater used to update the remembered partitions.
(package private) static class ContentFormatter.PositionReference
          Defines a reference to either the offset or the end offset of a particular position.
(package private)  class ContentFormatter.RemoveAffectedPositions
          The position updater which runs as first updater on the document's positions.
(package private)  class ContentFormatter.UpdateAffectedPositions
          The position updater which runs as last updater on the document's positions.
 
Field Summary
private  org.eclipse.jface.text.IDocument fDocument
          The document this formatter works on.
private  java.lang.String[] fExternalPartitonManagingCategories
          The external partition managing categories.
private  boolean fIsPartitionAware
          The indicator of whether the formatter operates in partition aware mode or not
private  boolean fNeedsComputation
          Indicates whether fPartitionManagingCategories must be computed.
private  java.util.List fOverlappingPositionReferences
          The list of references to offset and end offset of all overlapping positions
private  java.lang.String fPartitioning
          The document partitioning used by this formatter.
private  org.eclipse.jface.text.IPositionUpdater fPartitioningUpdater
          Position updater used for partitioning positions
private  java.lang.String[] fPartitionManagingCategories
          The partition information managing document position categories
private  java.util.Map fStrategies
          The map of IFormattingStrategy objects
private static java.lang.String PARTITIONING
          Internal position category used for the formatter partitioning
 
Constructor Summary
ContentFormatter()
          Creates a new content formatter.
 
Method Summary
private  void addPartitioningUpdater()
          Installs those updaters which the formatter needs to keep track of the partitions.
private  void determinePositionsToUpdate(int offset, int length)
          Determines all embracing, overlapping, and follow up positions for the given region of the document.
 void enablePartitionAwareFormatting(boolean enable)
          Sets the formatter's operation mode.
 void format(org.eclipse.jface.text.IDocument document, org.eclipse.jface.text.IRegion region)
          Formats the given region of the specified document.The formatter may safely assume that it is the only subject that modifies the document at this point in time.
private  void format(IFormattingStrategy strategy, org.eclipse.jface.text.TypedPosition region)
          Formats the given region of the document using the specified formatting strategy.
private  void format(org.eclipse.jface.text.TypedPosition[] ranges)
          Formats one partition after the other using the formatter strategy registered for the partition's content type.
private  void formatPartitions(org.eclipse.jface.text.IRegion region)
          Determines the partitioning of the given region of the document.
private  void formatRegion(org.eclipse.jface.text.IRegion region)
          Formats the given region with the strategy registered for the default content type.
private  int[] getAffectedPositions(int offset, int length)
          Returns all offset and the end offset of all positions overlapping with the specified document range.
 IFormattingStrategy getFormattingStrategy(java.lang.String contentType)
          Returns the formatting strategy registered for the given content type.
private  java.lang.String getIndentation(int offset)
          Returns the indentation of the line of the given offset.
private  org.eclipse.jface.text.TypedPosition[] getPartitioning(org.eclipse.jface.text.IRegion region)
          Returns the partitioning of the given region of the document to be formatted.
private  java.lang.String[] getPartitionManagingCategories()
          Returns the partition managing position categories for the formatted document.
private  boolean ignoreCategory(java.lang.String category)
          Determines whether the given document position category should be ignored by this formatter's position updating.
private  boolean isLineStart(int offset)
          Determines whether the offset is the beginning of a line in the given document.
protected  boolean positionAboutToBeAdded(org.eclipse.jface.text.IDocument document, java.lang.String category, org.eclipse.jface.text.Position position)
          The given position is about to be added to the given position category of the given document.
private  void removeAffectedPositions(org.eclipse.jface.text.IDocument document)
          Removes the affected positions from their categories to avoid that they are invalidly updated.
private  void removePartitioningUpdater()
          Removes the formatter's internal position updater and category.
 void setDocumentPartitioning(java.lang.String partitioning)
          Sets the document partitioning to be used by this formatter.
 void setFormattingStrategy(IFormattingStrategy strategy, java.lang.String contentType)
          Registers a strategy for a particular content type.
 void setPartitionManagingPositionCategories(java.lang.String[] categories)
          Deprecated. incompatible with an open set of document partitionings. The provided information is only used if this formatter can not compute the partition managing position categories.
private  void start(org.eclipse.jface.text.TypedPosition[] regions, java.lang.String indentation)
          Fires formatterStarts to all formatter strategies which will be involved in the forthcoming formatting process.
private  void stop(org.eclipse.jface.text.TypedPosition[] regions)
          Fires formatterStops to all formatter strategies which were involved in the formatting process which is about to terminate.
protected  void updateAffectedPositions(org.eclipse.jface.text.IDocument document, int[] positions, int offset)
          Updates all the overlapping positions.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PARTITIONING

private static final java.lang.String PARTITIONING
Internal position category used for the formatter partitioning

See Also:
Constant Field Values

fStrategies

private java.util.Map fStrategies
The map of IFormattingStrategy objects


fIsPartitionAware

private boolean fIsPartitionAware
The indicator of whether the formatter operates in partition aware mode or not


fPartitionManagingCategories

private java.lang.String[] fPartitionManagingCategories
The partition information managing document position categories


fOverlappingPositionReferences

private java.util.List fOverlappingPositionReferences
The list of references to offset and end offset of all overlapping positions


fPartitioningUpdater

private org.eclipse.jface.text.IPositionUpdater fPartitioningUpdater
Position updater used for partitioning positions


fPartitioning

private java.lang.String fPartitioning
The document partitioning used by this formatter.

Since:
3.0

fDocument

private org.eclipse.jface.text.IDocument fDocument
The document this formatter works on.

Since:
3.0

fExternalPartitonManagingCategories

private java.lang.String[] fExternalPartitonManagingCategories
The external partition managing categories.

Since:
3.0

fNeedsComputation

private boolean fNeedsComputation
Indicates whether fPartitionManagingCategories must be computed.

Since:
3.0
Constructor Detail

ContentFormatter

public ContentFormatter()
Creates a new content formatter. The content formatter operates by default in the partition-aware mode. There are no preconfigured formatting strategies. Will use the default document partitioning if not further configured.

Method Detail

setFormattingStrategy

public void setFormattingStrategy(IFormattingStrategy strategy,
                                  java.lang.String contentType)
Registers a strategy for a particular content type. If there is already a strategy registered for this type, the new strategy is registered instead of the old one. If the given content type is null the given strategy is registered for all content types as is called only once per formatting session.


setPartitionManagingPositionCategories

public void setPartitionManagingPositionCategories(java.lang.String[] categories)
Deprecated. incompatible with an open set of document partitionings. The provided information is only used if this formatter can not compute the partition managing position categories.

Informs this content formatter about the names of those position categories which are used to manage the document's partitioning information and thus should be ignored when this formatter updates positions.


setDocumentPartitioning

public void setDocumentPartitioning(java.lang.String partitioning)
Sets the document partitioning to be used by this formatter.

Since:
3.0

enablePartitionAwareFormatting

public void enablePartitionAwareFormatting(boolean enable)
Sets the formatter's operation mode.


getFormattingStrategy

public IFormattingStrategy getFormattingStrategy(java.lang.String contentType)
Description copied from interface: IContentFormatter
Returns the formatting strategy registered for the given content type.

Specified by:
getFormattingStrategy in interface IContentFormatter

format

public void format(org.eclipse.jface.text.IDocument document,
                   org.eclipse.jface.text.IRegion region)
Description copied from interface: IContentFormatter
Formats the given region of the specified document.The formatter may safely assume that it is the only subject that modifies the document at this point in time.

Specified by:
format in interface IContentFormatter

formatPartitions

private void formatPartitions(org.eclipse.jface.text.IRegion region)
Determines the partitioning of the given region of the document. Informs the formatting strategies of each partition about the start, the process, and the termination of the formatting session.

Since:
3.0

formatRegion

private void formatRegion(org.eclipse.jface.text.IRegion region)
Formats the given region with the strategy registered for the default content type. The strategy is informed about the start, the process, and the termination of the formatting session.

Since:
3.0

getPartitioning

private org.eclipse.jface.text.TypedPosition[] getPartitioning(org.eclipse.jface.text.IRegion region)
                                                        throws org.eclipse.jface.text.BadLocationException
Returns the partitioning of the given region of the document to be formatted. As one partition after the other will be formatted and formatting will probably change the length of the formatted partition, it must be kept track of the modifications in order to submit the correct partition to all formatting strategies. For this, all partitions are remembered as positions in a dedicated position category. (As formatting strategies might rely on each other, calling them in reversed order is not an option.)

Since:
3.0

start

private void start(org.eclipse.jface.text.TypedPosition[] regions,
                   java.lang.String indentation)
Fires formatterStarts to all formatter strategies which will be involved in the forthcoming formatting process.


format

private void format(org.eclipse.jface.text.TypedPosition[] ranges)
Formats one partition after the other using the formatter strategy registered for the partition's content type.

Since:
3.0

format

private void format(IFormattingStrategy strategy,
                    org.eclipse.jface.text.TypedPosition region)
Formats the given region of the document using the specified formatting strategy. In order to maintain positions correctly, first all affected positions determined, after all document listeners have been informed about the coming change, the affected positions are removed to avoid that they are regularly updated. After all position updaters have run, the affected positions are updated with the formatter's information and added back to their categories, right before the first document listener is informed about that a change happened.

Since:
3.0

stop

private void stop(org.eclipse.jface.text.TypedPosition[] regions)
Fires formatterStops to all formatter strategies which were involved in the formatting process which is about to terminate.


addPartitioningUpdater

private void addPartitioningUpdater()
Installs those updaters which the formatter needs to keep track of the partitions.

Since:
3.0

removePartitioningUpdater

private void removePartitioningUpdater()
Removes the formatter's internal position updater and category.

Since:
3.0

getPartitionManagingCategories

private java.lang.String[] getPartitionManagingCategories()
Returns the partition managing position categories for the formatted document.

Since:
3.0

ignoreCategory

private boolean ignoreCategory(java.lang.String category)
Determines whether the given document position category should be ignored by this formatter's position updating.


determinePositionsToUpdate

private void determinePositionsToUpdate(int offset,
                                        int length)
Determines all embracing, overlapping, and follow up positions for the given region of the document.

Since:
3.0

getAffectedPositions

private int[] getAffectedPositions(int offset,
                                   int length)
Returns all offset and the end offset of all positions overlapping with the specified document range.

Since:
3.0

removeAffectedPositions

private void removeAffectedPositions(org.eclipse.jface.text.IDocument document)
Removes the affected positions from their categories to avoid that they are invalidly updated.


updateAffectedPositions

protected void updateAffectedPositions(org.eclipse.jface.text.IDocument document,
                                       int[] positions,
                                       int offset)
Updates all the overlapping positions. Note, all other positions are automatically updated by their document position updaters.


positionAboutToBeAdded

protected boolean positionAboutToBeAdded(org.eclipse.jface.text.IDocument document,
                                         java.lang.String category,
                                         org.eclipse.jface.text.Position position)
The given position is about to be added to the given position category of the given document.

This default implementation return true.


getIndentation

private java.lang.String getIndentation(int offset)
Returns the indentation of the line of the given offset.

Since:
3.0

isLineStart

private boolean isLineStart(int offset)
                     throws org.eclipse.jface.text.BadLocationException
Determines whether the offset is the beginning of a line in the given document.

Since:
3.0