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

📄 rtextareaeditorkit.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        caretPos = lineElem.getEndOffset() - 1;
        c.setDot(caretPos); // Gets rid of any selection.
        doc.remove(caretPos, 1); // Should be '\n'.
      }
      catch (BadLocationException ble) {
        /* Shouldn't ever happen. */
        ble.printStackTrace();
      }
      textArea.requestFocusInWindow();
    }

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

  }

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

  /**
   * Action to make the selection lower-case.
   */
  public static class LowerSelectionCaseAction
      extends RecordableTextAction {

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

    public LowerSelectionCaseAction() {
      super(rtaLowerSelectionCaseAction);
    }

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
      if (!textArea.isEditable() || !textArea.isEnabled()) {
        UIManager.getLookAndFeel().provideErrorFeedback(textArea);
        return;
      }
      String selection = textArea.getSelectedText();
      if (selection != null) {
        textArea.replaceSelection(selection.toLowerCase());
      }
      textArea.requestFocusInWindow();
    }

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

  }

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

  /**
   * Action to move the selection and/or caret. Constructor indicates
   * direction to use.
   */
  public static class NextVisualPositionAction
      extends RecordableTextAction {

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

    private boolean select;
    private int direction;

    public NextVisualPositionAction(String nm, boolean select, int direction) {
      super(nm);
      this.select = select;
      this.direction = direction;
    }

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {

      Caret caret = textArea.getCaret();
      int dot = caret.getDot();

      /*
       * Move to the beginning/end of selection on a "non-shifted"
       * left- or right-keypress.  We shouldn't have to worry about
       * navigation filters as, if one is being used, it let us get
       * to that position before.
       */
      if (!select) {
        switch (direction) {
          case SwingConstants.EAST:
            int mark = caret.getMark();
            if (dot != mark) {
              caret.setDot(Math.max(dot, mark));
              return;
            }
            break;
          case SwingConstants.WEST:
            mark = caret.getMark();
            if (dot != mark) {
              caret.setDot(Math.min(dot, mark));
              return;
            }
            break;
          default:
        }
      }

      Position.Bias[] bias = new Position.Bias[1];
      Point magicPosition = caret.getMagicCaretPosition();

      try {

        if (magicPosition == null &&
            (direction == SwingConstants.NORTH ||
             direction == SwingConstants.SOUTH)) {
          Rectangle r = textArea.modelToView(dot);
          magicPosition = new Point(r.x, r.y);
        }

        NavigationFilter filter = textArea.getNavigationFilter();

        if (filter != null) {
          dot = filter.getNextVisualPositionFrom(textArea, dot,
                                                 Position.Bias.Forward,
                                                 direction, bias);
        }
        else {
          dot = textArea.getUI().getNextVisualPositionFrom(
              textArea, dot,
              Position.Bias.Forward, direction, bias);
        }
        if (select) {
          caret.moveDot(dot);
        }
        else {
          caret.setDot(dot);

        }
        if (magicPosition != null &&
            (direction == SwingConstants.NORTH ||
             direction == SwingConstants.SOUTH)) {
          caret.setMagicCaretPosition(magicPosition);
        }

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

    }

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

  }

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

  /**
   * Positions the caret at the next word.
   */
  public static class NextWordAction
      extends RecordableTextAction {

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

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

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {

      int offs = textArea.getCaretPosition();
      int oldOffs = offs;
      // FIXME:  Replace Utilities call with custom version to
      // cut down on all of the modelToViews, as each call causes
      // a getTokenList => expensive!
      Element curPara =
          Utilities.getParagraphElement(textArea, offs);

      try {
        offs = Utilities.getNextWord(textArea, offs);
        if (offs >= curPara.getEndOffset() &&
            oldOffs != curPara.getEndOffset() - 1) {
          // we should first move to the end of current
          // paragraph (bug #4278839)
          offs = curPara.getEndOffset() - 1;
        }
      }
      catch (BadLocationException bl) {
        int end = textArea.getDocument().getLength();
        if (offs != end) {
          if (oldOffs != curPara.getEndOffset() - 1) {
            offs = curPara.getEndOffset() - 1;
          }
          else {
            offs = end;
          }
        }
      }

      if (select) {
        textArea.moveCaretPosition(offs);
      }
      else {
        textArea.setCaretPosition(offs);

      }
    }

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

  }

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

  /**
   * Pages one view to the left or right.
   */
  static class PageAction
      extends RecordableTextAction {

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

    private boolean select;
    private boolean left;

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

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {

      int selectedIndex;
      Rectangle visible = new Rectangle();
      textArea.computeVisibleRect(visible);
      if (left) {
        visible.x = Math.max(0, visible.x - visible.width);
      }
      else {
        visible.x += visible.width;

      }
      selectedIndex = textArea.getCaretPosition();
      if (selectedIndex != -1) {
        if (left) {
          selectedIndex = textArea.viewToModel(
              new Point(visible.x, visible.y));
        }
        else {
          selectedIndex = textArea.viewToModel(
              new Point(visible.x + visible.width - 1,
                        visible.y + visible.height - 1));
        }
        Document doc = textArea.getDocument();
        if ( (selectedIndex != 0) &&
            (selectedIndex > (doc.getLength() - 1))) {
          selectedIndex = doc.getLength() - 1;
        }
        else if (selectedIndex < 0) {
          selectedIndex = 0;
        }
        if (select) {
          textArea.moveCaretPosition(selectedIndex);
        }
        else {
          textArea.setCaretPosition(selectedIndex);
        }
      }

    }

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

  }

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

  /**
   * Action for pasting text.
   */
  public static class PasteAction
      extends RecordableTextAction {

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

    public PasteAction() {
      super(DefaultEditorKit.pasteAction);
    }

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

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

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

  }

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

  /**
   * "Plays back" the last macro recorded.
   */
  public static class PlaybackLastMacroAction
      extends RecordableTextAction {

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

    public PlaybackLastMacroAction() {
      super(rtaPlaybackLastMacroAction);
    }

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

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

    public boolean isRecordable() {
      return false; // Don't record macro playbacks.
    }

    public final String getMacroID() {
      return rtaPlaybackLastMacroAction;
    }

  }

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

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

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

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

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {

      int offs = textArea.getCaretPosition();
      boolean failed = false;
      try {

        // FIXME:  Replace Utilities call with custom version to
        // cut down on all of the modelToViews, as each call causes
        // a getTokenList => expensive!
        Element curPara = Utilities.getParagraphElement(textArea, offs);
        offs = Utilities.getPreviousWord(textArea, offs);
        if (offs < curPara.getStartOffset()) {
          offs = Utilities.getParagraphElement(textArea, offs).
              getEndOffset() - 1;
        }

      }
      catch (BadLocationException bl) {
        if (offs != 0) {
          offs = 0;
        }
        else {
          failed = true;
        }
      }

      if (!failed) {
        if (select) {
          textArea.moveCaretPosition(offs);
        }
        else {
          textArea.setCaretPosition(offs);
        }
      }
      else {
        UIManager.getLookAndFeel().provideErrorFeedback(textArea);

      }
    }

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

  }

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

  /**
   * Redoes the last action undone.
   */
  public static class RedoAction
      extends RecordableTextAction {

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

    public RedoAction() {
      super(rtaRedoAction);
    }

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

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
      if (textArea.isEnabled() && textArea.isEditable()) {
        textArea.redoLastAction();
        textArea.requestFocusInWindow();
      }
    }

    public final String getMacroID() {
      return rtaRedoAction;
    }

⌨️ 快捷键说明

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