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

📄 rtextareaeditorkit.java~2~

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

    public final String getMacroID() {
      return rtaDeleteRestOfLineAction;
    }

  }

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

  /**
   * Moves the caret to the end of the document.
   */
  public static class EndAction
      extends RecordableTextAction {

    /**
     *
     */
    private static final long serialVersionUID = 8985841694929860823L;
    private boolean select;

    public EndAction(String name, boolean select) {
      super(name);
      this.select = select;
    }

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
      Document doc = textArea.getDocument();
      int dot = doc.getLength();
      if (select) {
        textArea.moveCaretPosition(dot);
      }
      else {
        textArea.setCaretPosition(dot);
      }
    }

    public final String getMacroID() {
      return getName();
    }

  }

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

  /**
   * Positions the caret at the end of the line.
   */
  public static class EndLineAction
      extends RecordableTextAction {

    /**
     *
     */
    private static final long serialVersionUID = -6780582445481818758L;
    private boolean select;

    public EndLineAction(String name, boolean select) {
      super(name);
      this.select = select;
    }

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
      try {
        int offs = textArea.getCaretPosition();
        // FIXME:  Replace Utilities call with custom version to
        // cut down on all of the modelToViews, as each call causes
        // a getTokenList => expensive!
        int endOffs = Utilities.getRowEnd(textArea, offs);
        if (select) {
          textArea.moveCaretPosition(endOffs);
        }
        else {
          textArea.setCaretPosition(endOffs);
        }
      }
      catch (Exception exc) {
        UIManager.getLookAndFeel().provideErrorFeedback(textArea);
      }
    }

    public final String getMacroID() {
      return getName();
    }

  }

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

  /**
   * Action that ends recording a macro.
   */
  public static class EndRecordingMacroAction
      extends RecordableTextAction {

    /**
     *
     */
    private static final long serialVersionUID = 6800085303369534925L;

    public EndRecordingMacroAction() {
      super(rtaEndRecordingMacroAction);
    }

    public EndRecordingMacroAction(String name, Icon icon,
                                   String desc, Integer mnemonic,
                                   KeyStroke accelerator) {
      super(name, icon, desc, mnemonic, accelerator);
    }

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
      RTextArea.endRecordingMacro();
    }

    public final String getMacroID() {
      return rtaEndRecordingMacroAction;
    }

    public boolean isRecordable() {
      return false; // Never record the recording of a macro!
    }

  }

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

  /**
   * Positions the caret at the end of the word.
   */
  static class EndWordAction
      extends RecordableTextAction {

    /**
     *
     */
    private static final long serialVersionUID = 8636594372227516193L;
    private boolean select;

    EndWordAction(String name, boolean select) {
      super(name);
      this.select = select;
    }

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
      try {
        int offs = textArea.getCaretPosition();
        int endOffs = Utilities.getWordEnd(textArea, offs);
        if (select) {
          textArea.moveCaretPosition(endOffs);
        }
        else {
          textArea.setCaretPosition(endOffs);
        }
      }
      catch (BadLocationException bl) {
        UIManager.getLookAndFeel().provideErrorFeedback(textArea);
      }
    }

    public final String getMacroID() {
      return getName();
    }

  }

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

  /**
   * The action for when the user hits the "Home" key.  This toggles the
   * caret's position between the first non-whitespace character on the
   * current line, and the beginning of the current line.
   */
  public static class HomeAction
      extends RecordableTextAction {

    /**
     *
     */
    private static final long serialVersionUID = 7202900978326860758L;
    private Segment currentLine = new Segment(); // For speed.

    public HomeAction() {
      super(rtaHomeAction);
    }

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {

      try {
        // We use the elements instead of calling getLineOfOffset(),
        // etc. to speed things up just a tad (i.e. micro-optimize).
        int caretPosition = textArea.getCaretPosition();
        Document document = textArea.getDocument();
        Element map = document.getDefaultRootElement();
        int currentLineNum = map.getElementIndex(caretPosition);
        Element currentLineElement = map.getElement(currentLineNum);
        int currentLineStart = currentLineElement.getStartOffset();
        int lineCount = map.getElementCount();
        int currentLineEnd = (currentLineNum == lineCount - 1) ?
            currentLineElement.getEndOffset() - 1 :
            currentLineElement.getEndOffset();
        int count = currentLineEnd - currentLineStart;
        if (count > 0) { // If there are chars in the line...
          document.getText(currentLineStart, count, currentLine);
          int firstNonWhitespace = getFirstNonWhitespacePos();
          firstNonWhitespace = currentLineStart +
              (firstNonWhitespace - currentLine.offset);
          if (caretPosition != firstNonWhitespace) {
            textArea.setCaretPosition(firstNonWhitespace);
          }
          else {
            textArea.setCaretPosition(currentLineStart);
          }
        }
        //e.consume();
      }
      catch (BadLocationException ble) {
        /* Shouldn't ever happen. */
        ble.printStackTrace();
      }

    }

    private final int getFirstNonWhitespacePos() {
      int offset = currentLine.offset;
      int end = offset + currentLine.count - 1;
      int pos = offset;
      char[] array = currentLine.array;
      char currentChar = array[pos];
      while ( (currentChar == '\t' || currentChar == ' ') && (++pos < end)) {
        currentChar = array[pos];
      }
      return pos;
    }

    public final String getMacroID() {
      return rtaHomeAction;
    }

  }

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

  /**
   * Action for increasing the font size.
   */
  public static class IncreaseFontSizeAction
      extends RecordableTextAction {

    /**
     *
     */
    private static final long serialVersionUID = -2467202760091273533L;

    protected float increaseAmount;

    protected static final float MAXIMUM_SIZE = 40.0f;

    public IncreaseFontSizeAction() {
      super(rtaIncreaseFontSizeAction);
      initialize();
    }

    public IncreaseFontSizeAction(String name, Icon icon, String desc,
                                  Integer mnemonic, KeyStroke accelerator) {
      super(name, icon, desc, mnemonic, accelerator);
      initialize();
    }

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
      Font font = textArea.getFont();
      float oldSize = font.getSize2D();
      float newSize = oldSize + increaseAmount;
      if (newSize <= MAXIMUM_SIZE) {
        // Grow by increaseAmount.
        font = font.deriveFont(newSize);
        textArea.setFont(font);
      }
      else if (oldSize < MAXIMUM_SIZE) {
        // Can't grow by full increaseAmount, but can grow a
        // little bit.
        font = font.deriveFont(MAXIMUM_SIZE);
        textArea.setFont(font);
      }
      else {
        // Our font size must be at or bigger than MAXIMUM_SIZE.
        UIManager.getLookAndFeel().provideErrorFeedback(textArea);
      }
      textArea.requestFocusInWindow();
    }

    public final String getMacroID() {
      return rtaIncreaseFontSizeAction;
    }

    protected void initialize() {
      increaseAmount = 1.0f;
    }

  }

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

  /**
   * Action for when the user presses the Enter key.
   */
  public static class InsertBreakAction
      extends RecordableTextAction {

    /**
     *
     */
    private static final long serialVersionUID = -5025766917286929583L;

    public InsertBreakAction() {
      super(DefaultEditorKit.insertBreakAction);
    }

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
      if (!textArea.isEditable() || !textArea.isEnabled()) {
        UIManager.getLookAndFeel().provideErrorFeedback(textArea);
        return;
      }
      textArea.replaceSelection("\n");
    }

    public final String getMacroID() {
      return DefaultEditorKit.insertBreakAction;
    }

  }

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

  /**
   * Action taken when content is to be inserted.
   */
  public static class InsertContentAction
      extends RecordableTextAction {

    /**
     *
     */
    private static final long serialVersionUID = -3797537655209982246L;

    public InsertContentAction() {
      super(DefaultEditorKit.insertContentAction, null, null, null,
            null);
    }

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
      if (!textArea.isEditable() || !textArea.isEnabled()) {
        UIManager.getLookAndFeel().provideErrorFeedback(textArea);
        return;
      }
      String content = e.getActionCommand();
      if (content != null) {
        textArea.replaceSelection(content);
      }
      else {
        UIManager.getLookAndFeel().provideErrorFeedback(textArea);
      }
    }

    public final String getMacroID() {
      return DefaultEditorKit.insertContentAction;
    }

  }

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

  /**
   * Places a tab character into the document. If there is a selection, it
   * is removed before the tab is added.
   */
  public static class InsertTabAction
      extends RecordableTextAction {

    /**
     *
     */
    private static final long serialVersionUID = -3409076237783259680L;

    public InsertTabAction() {
      super(insertTabAction);
    }

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
      if (!textArea.isEditable() || !textArea.isEnabled()) {
        UIManager.getLookAndFeel().provideErrorFeedback(textArea);
        return;
      }
      textArea.replaceSelection("\t");
    }

    public final String getMacroID() {
      return DefaultEditorKit.insertTabAction;
    }

  }

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

  /**
   * Action to invert the selection's case.
   */
  public static class InvertSelectionCaseAction
      extends RecordableTextAction {

    /**
     *
     */
    private static final long serialVersionUID = 8208747415774653068L;

    public InvertSelectionCaseAction() {
      super(rtaInvertSelectionCaseAction);
    }

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
      if (!textArea.isEditable() || !textArea.isEnabled()) {
        UIManager.getLookAndFeel().provideErrorFeedback(textArea);
        return;
      }
      String selection = textArea.getSelectedText();
      if (selection != null) {
        StringBuffer buffer = new StringBuffer(selection);
        int length = buffer.length();
        for (int i = 0; i < length; i++) {
          char c = buffer.charAt(i);
          if (Character.isUpperCase(c)) {
            buffer.setCharAt(i, Character.toLowerCase(c));
          }
          else if (Character.isLowerCase(c)) {
            buffer.setCharAt(i, Character.toUpperCase(c));
          }
        }
        textArea.replaceSelection(buffer.toString());
      }
      textArea.requestFocusInWindow();
    }

    public final String getMacroID() {
      return getName();
    }

  }

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

  /**
   * Action to join the current line and the following line.
   */
  public static class JoinLinesAction
      extends RecordableTextAction {

    /**

⌨️ 快捷键说明

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