Module java.desktop
Package javax.swing.text

Class GapContent

java.lang.Object
javax.swing.text.GapContent
所有已实现的接口:
Serializable, AbstractDocument.Content

public class GapContent extends Object implements AbstractDocument.Content, Serializable
使用类似于emacs使用的间隙缓冲区实现的AbstractDocument.Content接口的实现。底层存储是一个Unicode字符数组,其中间隙位于某个位置。间隙会移动到更改的位置,以利用大多数更改发生在相同位置的常见行为。在间隙边界发生的更改通常很便宜,移动间隙通常比直接移动数组内容以适应更改要便宜。

跟踪更改的位置通常也很便宜。位置实现(标记)存储数组索引,并且可以轻松地从当前间隙位置计算顺序位置。只有在移动间隙时才需要更新旧间隙边界和新间隙边界之间的标记,因此通常更新标记是相当便宜的。标记被排序存储,因此它们可以通过二进制搜索快速定位。这增加了添加标记的成本,减少了保持标记更新的成本。

  • Constructor Summary

    Constructors
    Constructor
    Description
    创建一个新的GapContent对象。
    GapContent(int initialLength)
    创建一个新的GapContent对象,指定初始大小。
  • Method Summary

    Modifier and Type
    Method
    Description
    protected Object
    allocateArray(int len)
    分配一个数组来存储适当类型的项目(由子类确定)。
    createPosition(int offset)
    在内容中创建一个将跟踪更改的位置。
    protected final Object
    访问数组。
    protected int
    获取分配数组的长度。
    void
    getChars(int where, int len, Segment chars)
    检索内容的一部分。
    protected final int
    访问间隙的末尾。
    protected final int
    访问间隙的开始。
    protected Vector
    getPositionsInRange(Vector v, int offset, int length)
    返回一个Vector,其中包含范围为offsetoffset + length的位置的UndoPosRef实例。
    getString(int where, int len)
    检索内容的一部分。
    insertString(int where, String str)
    将字符串插入内容中。
    int
    length()
    返回内容的长度。
    remove(int where, int nitems)
    删除内容的一部分。
    protected void
    replace(int position, int rmSize, Object addItems, int addSize)
    用给定的新项目替换存储中的给定逻辑位置。
    protected void
    重置所有偏移量为0的标记,使其索引也为零。
    protected void
    shiftEnd(int newSize)
    使间隙变大,移动任何必要的数据并更新适当的标记。
    protected void
    shiftGap(int newGapStart)
    将间隙的开始移动到新位置,而不更改间隙的大小。
    protected void
    shiftGapEndUp(int newGapEnd)
    向上调整间隙末尾。
    protected void
    shiftGapStartDown(int newGapStart)
    向下调整间隙末尾。
    protected void
    updateUndoPositions(Vector positions, int offset, int length)
    重置positions中所有UndoPosRef实例的位置。

    Methods declared in class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • GapContent

      public GapContent()
      创建一个新的GapContent对象。初始大小默认为10。
    • GapContent

      public GapContent(int initialLength)
      创建一个新的GapContent对象,指定初始大小。初始大小不会低于2,以便为隐含的换行符和间隙留出空间。
      参数:
      initialLength - 初始大小
  • Method Details

    • allocateArray

      protected Object allocateArray(int len)
      分配一个数组来存储适当类型的项目(由子类确定)。
      参数:
      len - 数组的长度
      返回:
      某种类型的java数组
    • getArrayLength

      protected int getArrayLength()
      获取分配数组的长度。
      返回:
      数组的长度
    • length

      public int length()
      返回内容的长度。
      指定者:
      length 在接口 AbstractDocument.Content
      返回:
      长度 >= 1
      参见:
    • insertString

      public UndoableEdit insertString(int where, String str) throws BadLocationException
      将字符串插入内容中。
      指定者:
      insertString 在接口 AbstractDocument.Content
      参数:
      where - 起始位置 >= 0, < length()
      str - 要插入的非空字符串
      返回:
      一个用于撤消的UndoableEdit对象
      抛出:
      BadLocationException - 如果指定位置无效
      参见:
    • remove

      public UndoableEdit remove(int where, int nitems) throws BadLocationException
      删除内容的一部分。
      指定者:
      remove 在接口 AbstractDocument.Content
      参数:
      where - 起始位置 >= 0, where + nitems < length()
      nitems - 要删除的字符数 >= 0
      返回:
      一个用于撤消的UndoableEdit对象
      抛出:
      BadLocationException - 如果指定位置无效
      参见:
    • getString

      public String getString(int where, int len) throws BadLocationException
      检索内容的一部分。
      指定者:
      getString 在接口 AbstractDocument.Content
      参数:
      where - 起始位置 >= 0
      len - 要检索的长度 >= 0
      返回:
      代表内容的字符串
      抛出:
      BadLocationException - 如果指定位置无效
      参见:
    • getChars

      public void getChars(int where, int len, Segment chars) throws BadLocationException
      检索内容的一部分。如果所需内容跨越间隙,则复制内容。如果所需内容不跨越间隙,则返回实际存储以避免复制,因为它是连续的。
      指定者:
      getChars 在接口 AbstractDocument.Content
      参数:
      where - 起始位置 >= 0, where + len <= length()
      len - 要检索的字符数 >= 0
      chars - 要返回字符的Segment对象
      抛出:
      BadLocationException - 如果指定位置无效
      参见:
    • createPosition

      public Position createPosition(int offset) throws BadLocationException
      在内容中创建一个将跟踪更改的位置。
      指定者:
      createPosition 在接口 AbstractDocument.Content
      参数:
      offset - 要跟踪的偏移量 >= 0
      返回:
      位置
      抛出:
      BadLocationException - 如果指定位置无效
    • shiftEnd

      protected void shiftEnd(int newSize)
      使间隙变大,移动任何必要的数据并更新适当的标记。
      参数:
      newSize - 新容量
    • shiftGap

      protected void shiftGap(int newGapStart)
      将间隙的开始移动到新位置,而不更改间隙的大小。这会移动数组中的数据并相应地更新标记。
      参数:
      newGapStart - 间隙的新开始
    • resetMarksAtZero

      protected void resetMarksAtZero()
      重置所有偏移量为0的标记,使其索引也为零。
    • shiftGapStartDown

      protected void shiftGapStartDown(int newGapStart)
      向下调整间隙末尾。这不会移动任何数据,但会更新受边界更改影响的任何标记。从旧间隙开始到新间隙开始的所有标记都被挤压到间隙的末尾(它们的位置已被移除)。
      参数:
      newGapStart - 间隙的新开始
    • shiftGapEndUp

      protected void shiftGapEndUp(int newGapEnd)
      向上调整间隙末尾。这不会移动任何数据,但会更新受边界更改影响的任何标记。从旧间隙结束到新间隙结束的所有标记都被挤压到间隙的末尾(它们的位置已被移除)。
      参数:
      newGapEnd - 间隙的新结束
    • getPositionsInRange

      protected Vector getPositionsInRange(Vector v, int offset, int length)
      返回一个Vector,其中包含范围为offsetoffset + length的位置的UndoPosRef实例。如果v不为null,则匹配的位置将放入其中。返回具有结果位置的向量。
      参数:
      v - 要使用的Vector,在null时创建一个新的
      offset - 起始偏移量 >= 0
      length - 长度 >= 0
      返回:
      实例集
    • updateUndoPositions

      protected void updateUndoPositions(Vector positions, int offset, int length)
      重置所有UndoPosRef实例中的位置。

      这是为内部使用而设计的,通常对子类不感兴趣。

      参数:
      positions - 要重置的UndoPosRef实例
      offset - 插入字符串的位置
      length - 插入字符串的长度
    • getArray

      protected final Object getArray()
      访问数组。数组的实际类型只有子类知道。
      返回:
      某种类型的java数组
    • getGapStart

      protected final int getGapStart()
      访问间隙的开始。
      返回:
      间隙的开始
    • getGapEnd

      protected final int getGapEnd()
      访问间隙的结束。
      返回:
      间隙的结束
    • replace

      protected void replace(int position, int rmSize, Object addItems, int addSize)
      用给定的新项目替换存储中的给定逻辑位置。如果间隙当前不位于更改位置,则将移动间隙到正在更改的区域。
      参数:
      position - 要进行替换的位置。这不是底层存储数组中的位置,而是被建模连续空间中的位置。
      rmSize - 要移除的项目数
      addItems - 要放入存储的新项目
      addSize - 要添加的项目数