⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 token.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

  /**
   * Returns the token after this one in the linked list.
   *
   * @return The next token.
   * @see #setNextToken
   */
  public Token getNextToken() {
    return nextToken;
  }

      /*****************************************************************************/

  /**
   * Returns the position in the document that represents the last character
   * in the token that will fit into <code>endBeforeX-startX</code> pixels.
   * For example, if you're using a monospaced 8-pixel-per-character font,
   * have the token "while" and <code>startX</code> is <code>0</code> and
   * <code>endBeforeX</code> is <code>30</code>, this method will return the
   * document position of the "i" in "while", because the "i" ends at pixel
   * <code>24</code>, while the "l" ends at <code>32</code>.  If not even the
   * first character fits in <code>endBeforeX-startX</code>, the first
   * character's position is still returned so calling methods don't go into
   * infinite loops.
   *
   * @param fm The font metrics used to paint the token.
   * @param e How to expand tabs.
   * @param startX The x-coordinate at which the token will be painted.  This
   *               is needed because of tabs.
   * @param endBeforeX The x-coordinate for which you want to find the last
   *                   character of <code>t</code> which comes before it.
   * @return The last document position that will fit in the specified amount
   *         of pixels.
   */
  /*
   * @see #getTokenListOffsetBeforeX
   * FIXME:  This method does not compute correctly!  It needs to be abstract
   * and implemented by subclasses.
   */
  public int getOffsetBeforeX(FontMetrics fm, TabExpander e, float startX,
                              float endBeforeX) {

    int i = textOffset;
    int stop = i + textCount;
    float x = startX;

    while (i < stop) {
      if (text[i] == '\t') {
        x = e.nextTabStop(x, 0);
      }
      else {
        x += fm.charWidth(text[i]);
      }
      if (x > endBeforeX) {
        // If not even the first character fits into the space, go
        // ahead and say the first char does fit so we don't go into
        // an infinite loop.
        int intoToken = Math.max(i - textOffset, 1);
        return offset + intoToken;
      }
      i++;
    }

    // If we got here, the whole token fit in (endBeforeX-startX) pixels.
    return stop - 1;

  }

      /*****************************************************************************/

  /**
   * Returns the width of this token given the specified parameters.
   *
   * @param fm The metrics of the font used to paint the token.
   * @param e Describes how to expand tabs.  This parameter cannot be
   *          <code>null</code>.
   * @param x0 The pixel-location at which the token begins.  This is needed
   *           because of tabs.
   * @return The width of the token, in pixels.
   * @see #getWidthUpTo
   */
  public float getWidth(FontMetrics fm, TabExpander e, float x0) {
    return getWidthUpTo(textCount, fm, e, x0);
  }

      /*****************************************************************************/

  /**
   * Returns the width of a specified number of characters in this token.
   * For example, for the token "while", specifying a value of <code>3</code>
   * here returns the width of the "whi" portion of the token.<p>
   *
   * This method is abstract so subclasses who paint themselves differently
   * (i.e., {@link VisibleWhitespaceToken} is painted a tad differently than
   * {@link DefaultToken} when rendering hints are enabled) can still return
   * accurate results.
   *
   * @param numChars The number of characters for which to get the width.
   * @param fm The font metrics used to paint this token.
   * @param e How to expand tabs.  This value cannot be <code>null</code>.
   * @param x0 The pixel-location at which this token begins.  This is needed
   *           because of tabs.
   * @return The width of the specified number of characters in this token.
   * @see #getWidth
   */
  public abstract float getWidthUpTo(int numChars, FontMetrics fm,
                                     TabExpander e, float x0);

      /*****************************************************************************/

  /**
   * Returns whether or not this token is "paintable;" i.e., whether or not
   * the type of this token is one such that it has an associated syntax
   * style.  What this boils down to is whether the token type is greater
   * than <code>Token.NULL</code>.
   *
   * @return Whether or not this token is paintable.
   */
  public boolean isPaintable() {
    return type > Token.NULL;
  }

      /*****************************************************************************/

  /**
   * Returns whether or not this token is whitespace.
   *
   * @return <code>true</code> iff this token is whitespace.
   */
  public boolean isWhitespace() {
    return type == WHITESPACE;
  }

      /*****************************************************************************/

  /**
   * Returns the bounding box for the specified document location.  The
   * location must be in the specified token list; if it isn't,
   * <code>null</code> is returned.
   *
   * @param textArea The text area from which the token list was derived.
   * @param e How to expand tabs.
   * @param pos The position in the document for which to get the bounding
   *            box in the view.
   * @param x0 The pixel x-location that is the beginning of
   *           <code>tokenList</code>.
   * @param rect The rectangle in which we'll be returning the results.  This
   *             object is reused to keep from frequent memory allocations.
   * @return The bounding box for the specified position in the model.
   */
  public abstract Rectangle listOffsetToView(RSyntaxTextArea textArea,
                                             TabExpander e, int pos, int x0,
                                             Rectangle rect);

      /*****************************************************************************/

  /**
   * Makes this token start at the specified offset into the document.<p>
   *
   * This method is abstract because some token implementations may need
   * to adjust cached values, such as widths
   * (such as <code>GlyphVectorToken</code>).
   *
   * @param pos The offset into the document this token should start at.
   *            Note that this token must already contain this position; if
   *            it doesn't, an exception is thrown.
   */
  public abstract void makeStartAt(int pos);

      /*****************************************************************************/

  /**
   * Paints this token.
   *
   * @param g The graphics context in which to paint.
   * @param x The x-coordinate at which to paint.
   * @param y The y-coordinate at which to paint.
   * @param scheme The syntax highlighting scheme to use.
   * @param host The text area this token is in.
   * @param e How to expand tabs.
   * @return The x-coordinate representing the end of the painted text.
   */
  public final float paint(Graphics2D g, float x, float y,
                           SyntaxScheme scheme,
                           RSyntaxTextArea host, TabExpander e) {
    return paint(g, x, y, scheme, host, e, 0);
  }

      /*****************************************************************************/

  /**
   * Paints this token.
   *
   * @param g The graphics context in which to paint.
   * @param x The x-coordinate at which to paint.
   * @param y The y-coordinate at which to paint.
   * @param scheme The syntax highlighting scheme to use.
   * @param host The text area this token is in.
   * @param e How to expand tabs.
   * @param clipStart The left boundary of the clip rectangle in which we're
   *        painting.  This optimizes painting by allowing us to not paint
   *        paint when this token is "to the left" of the clip rectangle.
   * @return The x-coordinate representing the end of the painted text.
   */
  public abstract float paint(Graphics2D g, float x, float y,
                              SyntaxScheme scheme, RSyntaxTextArea host,
                              TabExpander e, float clipStart);

      /*****************************************************************************/

  /**
   * Paints the background of a token.
   *
   * @param x The x-coordinate of the token.
   * @param y The y-coordinate of the token.
   * @param width The width of the token (actually, the width of the part of
   *              the token to paint).
   * @param height The height of the token.
   * @param g The graphics context with which to paint.
   * @param fontAscent The ascent of the token's font.
   * @param host The text area.
   * @param color The color with which to paint.
   */
  protected void paintBackground(float x, float y, float width, float height,
                                 Graphics2D g, int fontAscent,
                                 RSyntaxTextArea host, Color color) {
    g.setXORMode(host.getBackground()); // Never null.
    g.setColor(color);
    g.fill(new Rectangle2D.Float(x, y - fontAscent, width, height));
    g.setPaintMode();
  }

      /*****************************************************************************/

  /**
   * Sets the value of this token to a particular segment of a document.
   * The "next token" value is cleared.
   *
   * @param line The segment from which to get the token.
   * @param beg The first character's position in <code>line</code>.
   * @param end The last character's position in <code>line</code>.
   * @param offset The offset into the document at which this token begins.
   * @param type A token type listed as "generic" above.
   */
  public void set(final char[] line, final int beg, final int end,
                  final int offset, final int type) {
    this.text = line;
    this.textOffset = beg;
    this.textCount = end - beg + 1;
    this.type = type;
    this.offset = offset;
    nextToken = null;
  }

      /*****************************************************************************/

  /**
   * Sets the "next token" pointer of this token to point to the specified
   * token.
   *
   * @param nextToken The new next token.
   * @see #getNextToken
   */
  public void setNextToken(Token nextToken) {
    this.nextToken = nextToken;
  }

      /*****************************************************************************/

  /**
   * Returns the position in the document corresponding to the specified
   * position in this token's internal char array (<code>textOffset</code> -
   * <code>textOffset+textCount-1</code>).<p>
   * Note that this method does NOT do any bounds checking; you can pass in
   * an invalid token position, and you will not receive an Exception or any
   * other indication that the returned document position is invalid.  It is
   * up to the user to ensure valid input.
   *
   * @param pos A position in the token's internal char array
   *        (<code>textOffset</code> - <code>textOffset+textCount</code>).
   * @return The corresponding position in the document.
   * @see #documentToToken
   */
  public int tokenToDocument(int pos) {
    return pos + (offset - textOffset);
  }

      /*****************************************************************************/

  /**
   * Returns this token as a <code>String</code>, which is useful for
   * debugging.
   *
   * @return A string describing this token.
   */
  public String toString() {
    return "[Token: " +
        (type == Token.NULL ? "<null token>" :
         "text: '" +
         (text == null ? "<null>" :
          new String(text, textOffset, textCount)) + "'; " +
         "offset: " + offset + "; type: " + type + "; " +
         "isPaintable: " + isPaintable() +
         "; nextToken==null: " + (nextToken == null)) +
        "]";
  }

      /*****************************************************************************/

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -