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

📄 rtextareaeditorkit.java~2~

📁 具有不同语法高亮的编辑器实例
💻 JAVA~2~
📖 第 1 页 / 共 5 页
字号:
   */
  public static class BeginLineAction
      extends RecordableTextAction {

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

    public BeginLineAction(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 begOffs = Utilities.getRowStart(textArea, offs);
        if (select) {
          textArea.moveCaretPosition(begOffs);
        }
        else {
          textArea.setCaretPosition(begOffs);
        }
      }
      catch (BadLocationException bl) {
        UIManager.getLookAndFeel().provideErrorFeedback(textArea);
      }
    }

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

  }

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

  /**
   * Action that begins recording a macro.
   */
  public static class BeginRecordingMacroAction
      extends RecordableTextAction {

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

    public BeginRecordingMacroAction() {
      super(rtaBeginRecordingMacroAction);
    }

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

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

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

    public final String getMacroID() {
      return rtaBeginRecordingMacroAction;
    }

  }

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

  /**
   * Positions the caret at the beginning of the word.
   */
  public static class BeginWordAction
      extends RecordableTextAction {

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

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

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

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

  }

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

  /**
   * Action for copying text.
   */
  public static class CopyAction
      extends RecordableTextAction {

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

    public CopyAction() {
      super(DefaultEditorKit.copyAction);
    }

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

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

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

  }

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

  /**
   * Action for cutting text.
   */
  public static class CutAction
      extends RecordableTextAction {

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

    public CutAction() {
      super(DefaultEditorKit.cutAction);
    }

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

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

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

  }

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

  /**
   * Action for decreasing the font size.
   */
  public static class DecreaseFontSizeAction
      extends RecordableTextAction {

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

    protected float decreaseAmount;

    protected static final float MINIMUM_SIZE = 2.0f;

    public DecreaseFontSizeAction() {
      super(rtaDecreaseFontSizeAction);
      initialize();
    }

    public DecreaseFontSizeAction(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 - decreaseAmount;
      if (newSize >= MINIMUM_SIZE) {
        // Shrink by decreaseAmount.
        font = font.deriveFont(newSize);
        textArea.setFont(font);
      }
      else if (oldSize > MINIMUM_SIZE) {
        // Can't shrink by full decreaseAmount, but can shrink a
        // little bit.
        font = font.deriveFont(MINIMUM_SIZE);
        textArea.setFont(font);
      }
      else {
        // Our font size must be at or below MINIMUM_SIZE.
        UIManager.getLookAndFeel().provideErrorFeedback(textArea);
      }
      textArea.requestFocusInWindow();
    }

    public final String getMacroID() {
      return rtaDecreaseFontSizeAction;
    }

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

  }

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

  /**
   * The action to use when no actions in the input/action map meet the key
   * pressed.  This is actually called from the keymap I believe.
   */
  public static class DefaultKeyTypedAction
      extends RecordableTextAction {

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

    public DefaultKeyTypedAction() {
      super(DefaultEditorKit.defaultKeyTypedAction, null, null, null,
            null);
    }

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
      if (!textArea.isEditable() || !textArea.isEnabled()) {
        return;
      }
      String content = e.getActionCommand();
      int mod = e.getModifiers();
      if ( (content != null) && (content.length() > 0) &&
          ( (mod & ActionEvent.ALT_MASK) == (mod & ActionEvent.CTRL_MASK))) {
        char c = content.charAt(0);
        if ( (c >= 0x20) && (c != 0x7F)) {
          textArea.replaceSelection(content);
        }
      }
    }

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

  }

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

  /**
   * Deletes the character of content that follows the current caret
   * position.
   */
  public static class DeleteNextCharAction
      extends RecordableTextAction {

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

    public DeleteNextCharAction() {
      super(DefaultEditorKit.deleteNextCharAction, null, null,
            null, null);
    }

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

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {

      boolean beep = true;
      if ( (textArea != null) && (textArea.isEditable())) {
        try {
          Document doc = textArea.getDocument();
          Caret caret = textArea.getCaret();
          int dot = caret.getDot();
          int mark = caret.getMark();
          if (dot != mark) {
            doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
            beep = false;
          }
          else if (dot < doc.getLength()) {
            int delChars = 1;
            if (dot < doc.getLength() - 1) {
              String dotChars = doc.getText(dot, 2);
              char c0 = dotChars.charAt(0);
              char c1 = dotChars.charAt(1);
              if (c0 >= '\uD800' && c0 <= '\uDBFF' &&
                  c1 >= '\uDC00' && c1 <= '\uDFFF') {
                delChars = 2;
              }
            }
            doc.remove(dot, delChars);
            beep = false;
          }
        }
        catch (BadLocationException bl) {
        }
      }

      if (beep) {
        UIManager.getLookAndFeel().provideErrorFeedback(textArea);

      }
      textArea.requestFocusInWindow();

    }

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

  }

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

  /**
   * Deletes the character of content that precedes the current caret
   * position.
   */
  public static class DeletePrevCharAction
      extends RecordableTextAction {

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

    public DeletePrevCharAction() {
      super(deletePrevCharAction);
    }

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {

      boolean beep = true;
      if ( (textArea != null) && (textArea.isEditable())) {
        try {
          Document doc = textArea.getDocument();
          Caret caret = textArea.getCaret();
          int dot = caret.getDot();
          int mark = caret.getMark();
          if (dot != mark) {
            doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
            beep = false;
          }
          else if (dot > 0) {
            int delChars = 1;
            if (dot > 1) {
              String dotChars = doc.getText(dot - 2, 2);
              char c0 = dotChars.charAt(0);
              char c1 = dotChars.charAt(1);
              if (c0 >= '\uD800' && c0 <= '\uDBFF' &&
                  c1 >= '\uDC00' && c1 <= '\uDFFF') {
                delChars = 2;
              }
            }
            doc.remove(dot - delChars, delChars);
            beep = false;
          }
        }
        catch (BadLocationException bl) {
        }
      }

      if (beep) {
        UIManager.getLookAndFeel().provideErrorFeedback(textArea);

      }
    }

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

  }

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

  /**
   * Action that deletes all text from the caret position to the end of the
   * caret's line.
   */
  public static class DeleteRestOfLineAction
      extends RecordableTextAction {

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

    public DeleteRestOfLineAction() {
      super(rtaDeleteRestOfLineAction);
    }

    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).
        Document document = textArea.getDocument();
        int caretPosition = textArea.getCaretPosition();
        Element map = document.getDefaultRootElement();
        int currentLineNum = map.getElementIndex(caretPosition);
        Element currentLineElement = map.getElement(currentLineNum);
        // Always take -1 as we don't want to remove the newline.
        int currentLineEnd = currentLineElement.getEndOffset() - 1;
        if (caretPosition < currentLineEnd) {
          document.remove(caretPosition,
                          currentLineEnd - caretPosition);

        }
      }
      catch (BadLocationException ble) {
        ble.printStackTrace();
      }

⌨️ 快捷键说明

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