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

📄 acltextarea.java

📁 java实现的P2P多agent中间件
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
   */
  public final void setTokenMarker(ACLSLTokenMarker tokenMarker) {
    document.setTokenMarker(tokenMarker);
  }


  /**
   *  Sets the selection start. The new selection will be the new selection
   *  start and the old selection end.
   *
   * @param  selectionStart  The selection start
   * @see                    #select(int,int)
   */
  public final void setSelectionStart(int selectionStart) {
    select(selectionStart, selectionEnd);
  }


  /**
   *  Sets the selection end. The new selection will be the old selection
   *  start and the bew selection end.
   *
   * @param  selectionEnd  The selection end
   * @see                  #select(int,int)
   */
  public final void setSelectionEnd(int selectionEnd) {
    select(selectionStart, selectionEnd);
  }


  /**
   *  Sets the caret position. The new selection will consist of the caret
   *  position only (hence no text will be selected)
   *
   * @param  caret  The caret position
   * @see           #select(int,int)
   */
  public final void setCaretPosition(int caret) {
    select(caret, caret);
  }


  /**
   *  Sets if this component is editable.
   *
   * @param  editable  True if this text area should be editable, false
   *      otherwise
   */
  public final void setEditable(boolean editable) {
    this.editable = editable;
  }


  /**
   *  Sets the right click popup menu.
   *
   * @param  popup  The popup
   */
  public final void setRightClickPopup(JPopupMenu popup) {
    this.popup = popup;
  }


  /**
   *  Sets the `magic' caret position. This can be used to preserve the column
   *  position when moving up and down lines.
   *
   * @param  magicCaret  The magic caret position
   */
  public final void setMagicCaretPosition(int magicCaret) {
    this.magicCaret = magicCaret;
  }


  /**
   *  Sets if overwrite mode should be enabled.
   *
   * @param  overwrite  True if overwrite mode should be enabled, false
   *      otherwise.
   */
  public final void setOverwriteEnabled(boolean overwrite) {
    this.overwrite = overwrite;
    painter.invalidateSelectedLines();
  }


  /**
   *  Blinks the caret.
   */
  public final void blinkCaret() {
    if (caretBlinks) {
      blink = !blink;
      painter.invalidateSelectedLines();
    }
    else {
      blink = true;
    }

  }


  /**
   *  Recalculates the number of visible lines. This should not be called
   *  directly.
   */
  public final void recalculateVisibleLines() {
    if (painter == null) {
      return;
    }
    int height = painter.getHeight();
    int lineHeight = painter.getFontMetrics().getHeight();
    int oldVisibleLines = visibleLines;
    visibleLines = height / lineHeight;
    painter.invalidateOffscreen();
    painter.repaint();
    updateScrollBars();
  }


  /**
   *  Selects all text in the document.
   */
  public final void selectAll() {
    select(0, getDocumentLength());
  }


  /**
   *  Adds a caret change listener to this text area.
   *
   * @param  listener  The listener
   */
  public final void addCaretListener(CaretListener listener) {
    listenerList.add(CaretListener.class, listener);
  }


  /**
   *  Removes a caret change listener from this text area.
   *
   * @param  listener  The listener
   */
  public final void removeCaretListener(CaretListener listener) {
    listenerList.remove(CaretListener.class, listener);
  }


  /**
   *  Returns the start offset of the specified line.
   *
   * @param  line  The line
   * @return       The start offset of the specified line, or -1 if the line
   *      is invalid
   */
  public int getLineStartOffset(int line) {
    Element lineElement = document.getDefaultRootElement()
      .getElement(line);
    if (lineElement == null) {
      return -1;
    }
    else {
      return lineElement.getStartOffset();
    }
  }


  /**
   *  Returns the end offset of the specified line.
   *
   * @param  line  The line
   * @return       The end offset of the specified line, or -1 if the line is
   *      invalid.
   */
  public int getLineEndOffset(int line) {
    Element lineElement = document.getDefaultRootElement()
      .getElement(line);
    if (lineElement == null) {
      return -1;
    }
    else {
      return lineElement.getEndOffset();
    }
  }


  /**
   *  Returns the length of the specified line.
   *
   * @param  line  The line
   * @return       The LineLength value
   */
  public int getLineLength(int line) {
    Element lineElement = document.getDefaultRootElement()
      .getElement(line);
    if (lineElement == null) {
      return -1;
    }
    else {
      return lineElement.getEndOffset()
         - lineElement.getStartOffset() - 1;
    }
  }


  /**
   *  Returns the entire text of this text area.
   *
   * @return    The Text value
   */
  public String getText() {
    try {
      return document.getText(0, document.getLength());
    }
    catch (BadLocationException bl) {
      return null;
    }
  }


  /**
   *  Sets the input handler.
   *
   * @param  inputHandler  The new input handler
   */
  public void setInputHandler(InputHandler inputHandler) {
    if (this.inputHandler != null) {
      removeKeyListener(this.inputHandler);
    }

    if (inputHandler != null) {
      addKeyListener(inputHandler);
    }

    this.inputHandler = inputHandler;
  }


  /**
   *  Toggles caret blinking.
   *
   * @param  caretBlinks  True if the caret should blink, false otherwise
   */
  public void setCaretBlinkEnabled(boolean caretBlinks) {
    this.caretBlinks = caretBlinks;
    if (!caretBlinks) {
      blink = false;
    }

    painter.invalidateSelectedLines();
  }


  /**
   *  Sets if the caret should be visible.
   *
   * @param  caretVisible  True if the caret should be visible, false
   *      otherwise
   */
  public void setCaretVisible(boolean caretVisible) {
    this.caretVisible = caretVisible;
    blink = true;

    painter.invalidateSelectedLines();
  }


  /**
   *  Sets the line displayed at the text area's origin without updating the
   *  scroll bars.
   *
   * @param  firstLine  The new FirstLine value
   */
  public void setFirstLine(int firstLine) {
    if (firstLine == this.firstLine) {
      return;
    }
    int oldFirstLine = this.firstLine;
    this.firstLine = firstLine;
    if (firstLine != vertical.getValue()) {
      updateScrollBars();
    }
    painter.scrollRepaint(oldFirstLine, firstLine);
    painter.repaint();
  }


  /**
   *  Sets the horizontal offset of drawn lines. This can be used to implement
   *  horizontal scrolling.
   *
   * @param  horizontalOffset  offset The new horizontal offset
   */
  public void setHorizontalOffset(int horizontalOffset) {
    if (horizontalOffset == this.horizontalOffset) {
      return;
    }
    this.horizontalOffset = horizontalOffset;
    if (horizontalOffset != horizontal.getValue()) {
      updateScrollBars();
    }

    painter.invalidateLineRange(firstLine, firstLine + visibleLines);
    painter.repaint();
  }


  /**
   *  A fast way of changing both the first line and horizontal offset.
   *
   * @param  firstLine         The new first line
   * @param  horizontalOffset  The new horizontal offset
   * @return                   True if any of the values were changed, false
   *      otherwise
   */
  public boolean setOrigin(int firstLine, int horizontalOffset) {
    boolean changed = false;
    boolean fullRepaint = false;
    int oldFirstLine = this.firstLine;

    if (horizontalOffset != this.horizontalOffset) {
      this.horizontalOffset = horizontalOffset;
      changed = fullRepaint = true;
    }

    if (firstLine != this.firstLine) {
      this.firstLine = firstLine;
      changed = true;
    }

    if (changed) {
      updateScrollBars();
      if (fullRepaint) {
        painter._invalidateLineRange(firstLine,
          firstLine + visibleLines);
      }

      else {
        painter.scrollRepaint(oldFirstLine, firstLine);
      }

      painter.repaint();
    }

    return changed;
  }


  /**
   *  Sets the document this text area is editing.
   *
   * @param  document  The document
   */
  public void setDocument(ACLSyntaxDocument document) {
    if (this.document == document) {
      return;
    }
    if (this.document != null) {
      this.document.removeDocumentListener(documentHandler);
    }

    this.document = document;

    document.addDocumentListener(documentHandler);

    select(0, 0);
    updateScrollBars();
    painter.invalidateOffscreen();
    painter.repaint();
  }


  /**
   *  Sets the entire text of this text area.
   *
   * @param  text  The new Text value
   */
  public void setText(String text) {
    try {
      document.remove(0, document.getLength());
      document.insertString(0, text, null);
    }
    catch (BadLocationException bl) {
      bl.printStackTrace();
    }
  }


  /**
   *  Replaces the selection with the specified text.
   *
   * @param  selectedText  The replacement text for the selection
   */
  public void setSelectedText(String selectedText) {
    if (!editable) {
      throw new InternalError("Text component"
         + " read only");
    }

    try {
      document.remove(selectionStart,
        selectionEnd - selectionStart);
      document.insertString(selectionStart,
        selectedText, null);
      setCaretPosition(selectionEnd);
    }
    catch (BadLocationException bl) {
      bl.printStackTrace();
      throw new InternalError("Cannot replace"
         + " selection");
    }
  }


  public void update() {
    this.register(msg, "Content");
  }


  /**
   *  Description of the Method
   *
   * @param  arg        Description of Parameter
   * @param  fieldName  Description of Parameter
   */
  public void register(Object arg, String fieldName) {
    this.msg = (ACLMessage)arg;
    this.fieldName = fieldName;
    contentLanguage = (msg.getLanguage() != null ? msg.getLanguage() : "<unknown>");
    String methodName = "get" + fieldName;
    String content = "";

    try {
      Method sn = msg.getClass().getMethod(methodName, (Class[]) null);
      content = (String)sn.invoke(msg, new Object[]{});
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    if (contentLanguage.indexOf("SL") >= 0) {
      //Only format when SL
      try {
        content = (String)new SLFormatter().format(content);
      }
      catch (Exception ex) {
        //too bad!
      }
    }

    while ((content != null) && (content.indexOf('\n')) == 0) {
      content = content.substring(1);
    }

    setText(content);
    this.setCaretPosition(0);
  }


  /**
   *  Description of the Method
   *
   * @param  arg  Description of Parameter
   * @param  str  Description of Parameter
   */
  public void unregister(Object arg, String str) {
//    msg.deleteObserver(this);
  }


  /**
   *  Description of the Method
   *
   * @param  e  Description of Parameter
   */
  public void focusLost(FocusEvent e) {
    String value = getText();
    while ((value != null) && ((value.indexOf('\n') == 0) || (value.indexOf(' ') == 0))) {
      value = value.substring(1);
    }

    String methodName = "set" + fieldName;
    String theType = "java.lang.String";
    try {
      Method sn = msg.getClass().getMethod(methodName, new Class[]{Class.forName(theType)});
      Object os = value;
      sn.invoke(msg, new Object[]{os});
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }


  /**
   *  Updates the state of the scroll bars. This should be called if the
   *  number of lines in the document changes, or when the size of the text
   *  are changes.
   */
  public void updateScrollBars() {

    if (vertical != null && visibleLines != 0) {
      vertical.setValues(firstLine, visibleLines, 0, getLineCount());
      vertical.setUnitIncrement(2);
      vertical.setBlockIncrement(visibleLines);
    }
    int width = painter.getWidth();

⌨️ 快捷键说明

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