Class DefaultJavaFoldingStructureProvider

java.lang.Object
org.eclipse.jdt.ui.text.folding.DefaultJavaFoldingStructureProvider
All Implemented Interfaces:
IJavaFoldingStructureProvider, IJavaFoldingStructureProviderExtension

public class DefaultJavaFoldingStructureProvider extends Object implements IJavaFoldingStructureProvider, IJavaFoldingStructureProviderExtension
Updates the projection model of a class file or compilation unit.

Clients may instantiate or subclass. Subclasses must make sure to always call the superclass' code when overriding methods that are marked with "subclasses may extend".

Since:
3.0 (internal), 3.2 (API)
  • Constructor Details

    • DefaultJavaFoldingStructureProvider

      public DefaultJavaFoldingStructureProvider()
      Creates a new folding provider. It must be installed on an editor/viewer pair before it can be used, and uninstalled when not used any longer.

      The projection state may be reset by calling initialize().

  • Method Details

    • install

      public void install(org.eclipse.ui.texteditor.ITextEditor editor, org.eclipse.jface.text.source.projection.ProjectionViewer viewer)
      Installs this structure provider on the given editor and viewer. Implementations should listen to the projection events generated by viewer and enable / disable generation of projection structure accordingly.

      Subclasses may extend.

      Specified by:
      install in interface IJavaFoldingStructureProvider
      Parameters:
      editor - the editor that this provider works on
      viewer - the projection viewer that displays the annotations created by this structure provider
    • uninstall

      public void uninstall()
      Uninstalls this structure provider. Any references to editors or viewers should be cleared.

      Subclasses may extend.

      Specified by:
      uninstall in interface IJavaFoldingStructureProvider
    • isInstalled

      protected final boolean isInstalled()
      Returns true if the provider is installed, false otherwise.
      Returns:
      true if the provider is installed, false otherwise
    • handleProjectionEnabled

      protected void handleProjectionEnabled()
      Called whenever projection is enabled, for example when the viewer issues a projectionEnabled message. When the provider is already enabled when this method is called, it is first disabled.

      Subclasses may extend.

    • handleProjectionDisabled

      protected void handleProjectionDisabled()
      Called whenever projection is disabled, for example when the provider is uninstalled, when the viewer issues a projectionDisabled message and before enabling the provider. Implementations must be prepared to handle multiple calls to this method even if the provider is already disabled.

      Subclasses may extend.

    • initialize

      public final void initialize()
      Description copied from interface: IJavaFoldingStructureProvider
      (Re-)initializes the structure provided by the receiver.
      Specified by:
      initialize in interface IJavaFoldingStructureProvider
    • computeFoldingStructure

      protected void computeFoldingStructure(IJavaElement element, DefaultJavaFoldingStructureProvider.FoldingStructureComputationContext ctx)
      Computes the folding structure for a given java element. Computed projection annotations are added to the computation context.

      Subclasses may extend or replace. The default implementation creates projection annotations for the following elements:

      • true members (not for top-level types)
      • the javadoc comments of any member
      • header comments (javadoc or multi-line comments appearing before the first type's javadoc or before the package or import declarations).
      Parameters:
      element - the java element to compute the folding structure for
      ctx - the computation context
    • computeProjectionRanges

      protected final org.eclipse.jface.text.IRegion[] computeProjectionRanges(ISourceReference reference, DefaultJavaFoldingStructureProvider.FoldingStructureComputationContext ctx)
      Computes the projection ranges for a given ISourceReference. More than one range or none at all may be returned. If there are no foldable regions, an empty array is returned.

      The last region in the returned array (if not empty) describes the region for the java element that implements the source reference. Any preceding regions describe javadoc comments of that java element.

      Parameters:
      reference - a java element that is a source reference
      ctx - the folding context
      Returns:
      the regions to be folded
    • createCommentPosition

      protected final org.eclipse.jface.text.Position createCommentPosition(org.eclipse.jface.text.IRegion aligned)
      Creates a comment folding position from an aligned region.
      Parameters:
      aligned - an aligned region
      Returns:
      a folding position corresponding to aligned
    • createMemberPosition

      protected final org.eclipse.jface.text.Position createMemberPosition(org.eclipse.jface.text.IRegion aligned, IMember member)
      Creates a folding position that remembers its member from an aligned region.
      Parameters:
      aligned - an aligned region
      member - the member to remember
      Returns:
      a folding position corresponding to aligned
    • alignRegion

      protected final org.eclipse.jface.text.IRegion alignRegion(org.eclipse.jface.text.IRegion region, DefaultJavaFoldingStructureProvider.FoldingStructureComputationContext ctx)
      Aligns region to start and end at a line offset. The region's start is decreased to the next line offset, and the end offset increased to the next line start or the end of the document. null is returned if region is null itself or does not comprise at least one line delimiter, as a single line cannot be folded.
      Parameters:
      region - the region to align, may be null
      ctx - the folding context
      Returns:
      a region equal or greater than region that is aligned with line offsets, null if the region is too small to be foldable (e.g. covers only one line)
    • collapseMembers

      public final void collapseMembers()
      Description copied from interface: IJavaFoldingStructureProviderExtension
      Collapses all members except for top level types.
      Specified by:
      collapseMembers in interface IJavaFoldingStructureProviderExtension
    • collapseComments

      public final void collapseComments()
      Description copied from interface: IJavaFoldingStructureProviderExtension
      Collapses all comments.
      Specified by:
      collapseComments in interface IJavaFoldingStructureProviderExtension
    • collapseElements

      public final void collapseElements(IJavaElement[] elements)
      Description copied from interface: IJavaFoldingStructureProviderExtension
      Collapses the given elements.
      Specified by:
      collapseElements in interface IJavaFoldingStructureProviderExtension
      Parameters:
      elements - the java elements to collapse (the array and its elements must not be modified)
    • expandElements

      public final void expandElements(IJavaElement[] elements)
      Description copied from interface: IJavaFoldingStructureProviderExtension
      Expands the given elements.
      Specified by:
      expandElements in interface IJavaFoldingStructureProviderExtension
      Parameters:
      elements - the java elements to expand (the array and its elements must not be modified)