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

📄 acltextarea.java

📁 JADE(JAVA Agent开发框架)是一个完全由JAVA语言开发的软件,它简化了多Agent系统的实现。
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    if (horizontal != null && width != 0) {
      horizontal.setValues(-horizontalOffset, width, 0, width * 5);
      horizontal.setUnitIncrement(painter.getFontMetrics()
        .charWidth('w'));
      horizontal.setBlockIncrement(width / 2);
    }
  }


  /**
   *  Ensures that the caret is visible by scrolling the text area if
   *  necessary.
   *
   * @return    True if scrolling was actually performed, false if the caret
   *      was already visible
   */
  public boolean scrollToCaret() {
    int line = getCaretLine();
    int lineStart = getLineStartOffset(line);
    int offset = Math.max(0, Math.min(getLineLength(line) - 1,
      getCaretPosition() - lineStart));

    return scrollTo(line, offset);
  }


  /**
   *  Ensures that the specified line and offset is visible by scrolling the
   *  text area if necessary.
   *
   * @param  line    The line to scroll to
   * @param  offset  The offset in the line to scroll to
   * @return         True if scrolling was actually performed, false if the
   *      line and offset was already visible
   */
  public boolean scrollTo(int line, int offset) {
    // visibleLines == 0 before the component is realized
    // we can't do any proper scrolling then, so we have
    // this hack...
    if (visibleLines == 0) {
      setFirstLine(Math.max(0, line - electricScroll));
      return true;
    }

    int newFirstLine = firstLine;
    int newHorizontalOffset = horizontalOffset;

    if (line < firstLine + electricScroll) {
      newFirstLine = Math.max(0, line - electricScroll);
    }

    else if (line + electricScroll >= firstLine + visibleLines) {
      newFirstLine = (line - visibleLines) + electricScroll + 1;
      if (newFirstLine + visibleLines >= getLineCount()) {
        newFirstLine = getLineCount() - visibleLines;
      }

      if (newFirstLine < 0) {
        newFirstLine = 0;
      }

    }

    int x = offsetToX(line, offset);
    int width = painter.getFontMetrics().charWidth('W');

    if (x < 0) {
      newHorizontalOffset = Math.min(0, horizontalOffset
         - x + width);
    }

    else if (x + width >= painter.getWidth()) {
      newHorizontalOffset = horizontalOffset +
        (painter.getWidth() - x) - width;
    }

    return setOrigin(newFirstLine, newHorizontalOffset);
  }


  /**
   *  Converts a line index to a y co-ordinate.
   *
   * @param  line  The line
   * @return       Description of the Returned Value
   */
  public int lineToY(int line) {
    FontMetrics fm = painter.getFontMetrics();
    return (line - firstLine) * fm.getHeight()
       - (fm.getLeading() + fm.getMaxDescent());
  }


  /**
   *  Converts a y co-ordinate to a line index.
   *
   * @param  y  The y co-ordinate
   * @return    Description of the Returned Value
   */
  public int yToLine(int y) {
    FontMetrics fm = painter.getFontMetrics();
    int height = fm.getHeight();
    return Math.max(0, Math.min(getLineCount() - 1,
      y / height + firstLine));
  }


  /**
   *  Converts an offset in a line into an x co-ordinate.
   *
   * @param  line    The line
   * @param  offset  The offset, from the start of the line
   * @return         Description of the Returned Value
   */
  public int offsetToX(int line, int offset) {
    ACLSLTokenMarker tokenMarker = getTokenMarker();

    /*
        Use painter's cached info for speed
      */
    FontMetrics fm = painter.getFontMetrics();

    getLineText(line, lineSegment);

    int segmentOffset = lineSegment.offset;
    int x = horizontalOffset;

    /*
        If syntax coloring is disabled, do simple translation
      */
    if (tokenMarker == null) {
      lineSegment.count = offset;
      return x + Utilities.getTabbedTextWidth(lineSegment,
        fm, x, painter, 0);
    }
    /*
        If syntax coloring is enabled, we have to do this because
        tokens can vary in width
      */
    else {
      ACLToken tokens;
      if (painter.currentLineIndex == line) {
        tokens = painter.currentLineTokens;
      }

      else {
        painter.currentLineIndex = line;
        tokens = painter.currentLineTokens
           = tokenMarker.markTokens(lineSegment, line);
      }

      Toolkit toolkit = painter.getToolkit();
      Font defaultFont = painter.getFont();
      ACLSytntaxStyle[] styles = painter.getStyles();

      for (; ; ) {
        byte id = tokens.id;
        if (id == ACLToken.END) {
          return x;
        }

        if (id == ACLToken.NULL) {
          fm = painter.getFontMetrics();
        }

        else {
          fm = styles[id].getFontMetrics(defaultFont);
        }

        int length = tokens.length;

        if (offset + segmentOffset < lineSegment.offset + length) {
          lineSegment.count = offset - (lineSegment.offset - segmentOffset);
          return x + Utilities.getTabbedTextWidth(
            lineSegment, fm, x, painter, 0);
        }
        else {
          lineSegment.count = length;
          x += Utilities.getTabbedTextWidth(
            lineSegment, fm, x, painter, 0);
          lineSegment.offset += length;
        }
        tokens = tokens.next;
      }
    }
  }


  /**
   *  Converts an x co-ordinate to an offset within a line.
   *
   * @param  line  The line
   * @param  x     The x co-ordinate
   * @return       Description of the Returned Value
   */
  public int xToOffset(int line, int x) {
    ACLSLTokenMarker tokenMarker = getTokenMarker();

    /*
        Use painter's cached info for speed
      */
    FontMetrics fm = painter.getFontMetrics();

    getLineText(line, lineSegment);

    char[] segmentArray = lineSegment.array;
    int segmentOffset = lineSegment.offset;
    int segmentCount = lineSegment.count;

    int width = horizontalOffset;

    if (tokenMarker == null) {
      for (int i = 0; i < segmentCount; i++) {
        char c = segmentArray[i + segmentOffset];
        int charWidth;
        if (c == '\t') {
          charWidth = (int)painter.nextTabStop(width, i)
             - width;
        }

        else {
          charWidth = fm.charWidth(c);
        }

        if (painter.isBlockCaretEnabled()) {
          if (x - charWidth <= width) {
            return i;
          }

          else
            if (x - charWidth / 2 <= width) {
            return i;
          }
        }

        width += charWidth;
      }

      return segmentCount;
    }
    else {
      ACLToken tokens;
      if (painter.currentLineIndex == line) {
        tokens = painter.currentLineTokens;
      }

      else {
        painter.currentLineIndex = line;
        tokens = painter.currentLineTokens
           = tokenMarker.markTokens(lineSegment, line);
      }

      int offset = 0;
      Toolkit toolkit = painter.getToolkit();
      Font defaultFont = painter.getFont();
      ACLSytntaxStyle[] styles = painter.getStyles();

      for (; ; ) {
        byte id = tokens.id;
        if (id == ACLToken.END) {
          return offset;
        }

        if (id == ACLToken.NULL) {
          fm = painter.getFontMetrics();
        }

        else {
          fm = styles[id].getFontMetrics(defaultFont);
        }

        int length = tokens.length;

        for (int i = 0; i < length; i++) {
          char c = segmentArray[segmentOffset + offset + i];
          int charWidth;
          if (c == '\t') {
            charWidth = (int)painter.nextTabStop(width, offset + i)
               - width;
          }

          else {
            charWidth = fm.charWidth(c);
          }

          if (painter.isBlockCaretEnabled()) {
            if (x - charWidth <= width) {
              return offset + i;
            }

            else
              if (x - charWidth / 2 <= width) {
              return offset + i;
            }
          }

          width += charWidth;
        }

        offset += length;
        tokens = tokens.next;
      }
    }
  }


  /**
   *  Converts a point to an offset, from the start of the text.
   *
   * @param  x  The x co-ordinate of the point
   * @param  y  The y co-ordinate of the point
   * @return    Description of the Returned Value
   */
  public int xyToOffset(int x, int y) {
    int line = yToLine(y);
    int start = getLineStartOffset(line);
    return start + xToOffset(line, x);
  }


  /**
   *  Selects from the start offset to the end offset. This is the general
   *  selection method used by all other selecting methods. The caret position
   *  will be start if start &lt; end, and end if end &gt; start.
   *
   * @param  start  The start offset
   * @param  end    The end offset
   */
  public void select(int start, int end) {
    int newStart;
    int newEnd;
    boolean newBias;
    if (start <= end) {
      newStart = start;
      newEnd = end;
      newBias = false;
    }
    else {
      newStart = end;
      newEnd = start;
      newBias = true;
    }

    if (newStart < 0 || newEnd > getDocumentLength()) {
      throw new IllegalArgumentException("Bounds out of"
         + " range: " + newStart + "," +
        newEnd);
    }

    // If the new position is the same as the old, we don't
    // do all this crap, however we still do the stuff at
    // the end (clearing magic position, scrolling)
    if (newStart != selectionStart || newEnd != selectionEnd
       || newBias != biasLeft) {
      int newStartLine = getLineOfOffset(newStart);
      int newEndLine = getLineOfOffset(newEnd);

      if (painter.isBracketHighlightEnabled()) {
        if (bracketLine != -1) {
          painter._invalidateLine(bracketLine);
        }

        updateBracketHighlight(end);
        if (bracketLine != -1) {
          painter._invalidateLine(bracketLine);
        }

      }

      painter._invalidateLineRange(selectionStartLine, selectionEndLine);
      painter._invalidateLineRange(newStartLine, newEndLine);

      selectionStart = newStart;
      selectionEnd = newEnd;
      selectionStartLine = newStartLine;
      selectionEndLine = newEndLine;
      biasLeft = newBias;

      fireCaretEvent();
    }

    // When the user is typing, etc, we don't want the caret
    // to blink
    blink = true;
    caretTimer.restart();

    // Clear the `magic' caret position used by up/down
    magicCaret = -1;

    if (!scrollToCaret()) {
      painter.fastRepaint();
    }

  }


  /**
   *  Similar to <code>setSelectedText()</code>, but overstrikes the
   *  appropriate number of characters if overwrite mode is enabled.
   *
   * @param  str  The string
   * @see         #setSelectedText(String)
   * @see         #isOverwriteEnabled()
   */
  public void overwriteSetSelectedText(String str) {
    // Don't overstrike if there is a selection
    if (!overwrite || selectionStart != selectionEnd) {
      setSelectedText(str);
      return;
    }

    // Don't overstrike if we're on the end of
    // the line
    int caret = getCaretPosition();
    int caretLineEnd = getLineEndOffset(getCaretLine());
    if (caretLineEnd - caret <= str.length()) {
      setSelectedText(str);
      return;
    }

    try {
      document.remove(caret, str.length());
      document.insertString(caret, str, null);
    }
    catch (BadLocationException bl) {
      bl.printStackTrace();
    }
  }


  /**
   *  Deletes the selected text from the text area and places it into the
   *  clipboard.
   */
  public void cut() {
    if (editable) {
      copy();
      setSelectedText("");
    }
  }


  /**
   *  Places the selected text into the clipboard.
   */
  public void copy() {
    if (selectionStart != selectionEnd) {
      Clipboard clipboard = getToolkit().getSystemClipboard();
      StringSelection selection = new StringSelection(
        getSelectedText());
      clipboard.setContents(selection, null);
    }
  }


  /**
   *  Inserts the clipboard contents into the text.
   */
  public void paste() {
    if (editable) {
      Clipboard clipboard = getToolkit().getSystemClipboard();
      try {
        String selection = (String)(clipboard.getContents(this).getTransferData(
          DataFlavor.stringFlavor));

        // The MacOS MRJ doesn't convert \r to \n,
        // so do it here
        setSelectedText(selection.replace('\r', '\n'));
      }
      catch (Exception e) {
        getToolkit().beep();
        System.err.println("Clipboard does not"
           + " contain a string");
      }
    }
  }


  /**
   *  Called by the AWT when this component is removed from it's parent. This
   *  stops any autoscrolling and clears the currently focused component.
   */
  public void removeNotify() {
    super.removeNotify();
    if (focusedComponent == this) {
      focusedComponent = null;
    }

    if (scrollTimer.isRunning()) {
      scrollTimer.stop();
    }

  }


  /**
   *  Description of the Method
   *
   * @param  e  Description of Parameter
   */
  protected void processFocusEvent(FocusEvent e) {
    super.processFocusEvent(e);
    if (e.getID() == e.FOCUS_LOST) {
      focusLost(e);
    }

⌨️ 快捷键说明

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