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

📄 rtextarea.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
   *   <li>redo</li>
   *   <li>selectall</li>
   * </ul>
   * If any of the above images don't exist, the corresponding action will
   * not have an icon.
   *
   * @param group The icon group to load.
   * @see #getIconGroup
   */
  public static synchronized void setIconGroup(IconGroup group) {
    Icon icon = group.getIcon("cut");
    cutAction.putValue(Action.SMALL_ICON, icon);
    icon = group.getIcon("copy");
    copyAction.putValue(Action.SMALL_ICON, icon);
    icon = group.getIcon("paste");
    pasteAction.putValue(Action.SMALL_ICON, icon);
    icon = group.getIcon("delete");
    deleteAction.putValue(Action.SMALL_ICON, icon);
    icon = group.getIcon("undo");
    undoAction.putValue(Action.SMALL_ICON, icon);
    icon = group.getIcon("redo");
    redoAction.putValue(Action.SMALL_ICON, icon);
    icon = group.getIcon("selectall");
    selectAllAction.putValue(Action.SMALL_ICON, icon);
    iconGroup = group;
  }

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

  /**
   * Sets the color used for "mark all."  This fires a property change of
   * type <code>MARK_ALL_COLOR_PROPERTY</code>.
   *
   * @param color The color to use for "mark all."
   * @see #getMarkAllHighlightColor
   */
  public void setMarkAllHighlightColor(Color color) {
    Color old = (Color) markAllHighlightPainter.getPaint();
    if (old != null && !old.equals(color)) {
      markAllHighlightPainter.setPaint(color);
      if (markedWord != null) {
        repaint(); // Repaint if words are highlighted.
      }
      firePropertyChange(MARK_ALL_COLOR_PROPERTY, old, color);
    }
  }

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

  /**
   * Sets whether the edges of selections are rounded in this text area.
   * This method fires a property change of type
   * <code>ROUNDED_SELECTION_PROPERTY</code>.
   *
   * @param rounded Whether selection edges should be rounded.
   * @see #getRoundedSelectionEdges
   */
  public void setRoundedSelectionEdges(boolean rounded) {
    if (getRoundedSelectionEdges() != rounded) {
      markAllHighlightPainter.setRoundedEdges(rounded);
      super.setRoundedSelectionEdges(rounded); // Fires event.
    }
  }

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

  /**
   * Sets the text mode for this editor pane.
   *
   * @param mode Either <code>RTextArea.INSERT_MODE</code> or
   *             <code>RTextArea.OVERWRITE_MODE</code>.
   */
  public void setTextMode(int mode) {

    if (mode != INSERT_MODE && mode != OVERWRITE_MODE) {
      mode = INSERT_MODE;

    }
    if (textMode != mode) {
      ConfigurableCaret cc = (ConfigurableCaret) getCaret();
      cc.setStyle(carets[mode]);
      textMode = mode;
    }

  }

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

  /**
   * Sets the UI used by this text area.  This is overridden so only the
   * right-click popup menu's UI is updated.  The look and feel of an
   * <code>RTextArea</code> is independent of the Java Look and Feel, and so
   * this method does not change the text area itself.  Subclasses (such as
   * <code>RSyntaxTextArea</code> can call <code>setRTextAreaUI</code> if
   * they wish to install a new UI.
   *
   * FIXME:  This method will be called once for each open
   * <code>RTextArea</code>, when it only needs to be called once!
   *
   * @param ui This parameter is ignored.
   */
  public final void setUI(TextUI ui) {
    // Update the popup menu's ui.
    if (RTextArea.rightClickMenu != null) {
      SwingUtilities.updateComponentTreeUI(RTextArea.rightClickMenu);
    }
  }

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

  /**
   * Attempt to undo an "action" done in this text area.
   */
  public void undoLastAction() {

    // NOTE: that the try/catch block shouldn't be necessary...

    try {
      if (undoManager.canUndo()) {
        undoManager.undo();
      }
    }
    catch (CannotUndoException f) {
      JOptionPane.showMessageDialog(this, "Error doing Undo: " + f +
                                    "\nPlease report this at " +
                                    "http://sourceforge.net/projects/rtext",
                                    "rtext - Error", JOptionPane.ERROR_MESSAGE);
    }

  }

      /*****************************************************************************/
      /********************** PRIVATE INNER CLASSES ********************************/
      /*****************************************************************************/

  /**
   * Modified from <code>MutableCaretEvent</code> in
   * <code>JTextComponent</code> so that mouse events get fired when the user
   * is selecting text with the mouse as well.  This class also displays the
   * popup menu when the user right-clicks in the text area.
   */
  private class RTextAreaMutableCaretEvent
      extends RTAMouseListener {

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

    RTextAreaMutableCaretEvent(RTextArea textArea) {
      super(textArea);
    }

    public void focusGained(FocusEvent e) {
      Caret c = getCaret();
      boolean enabled = c.getDot() != c.getMark();
      cutAction.setEnabled(enabled);
      copyAction.setEnabled(enabled);
      undoManager.updateActions(); // To reflect this text area.
    }

    public void focusLost(FocusEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
      if ( (e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
        Caret caret = getCaret();
        dot = caret.getDot();
        mark = caret.getMark();
        fireCaretUpdate(this);
      }
    }

    // Mouse moved with no buttons pressed.
    public void mouseMoved(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
      // WORKAROUND:  Since JTextComponent only updates the caret
      // location on mouse clicked and released, we'll do it on dragged
      // events when the left mouse button is clicked.
      if ( (e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
        Caret caret = getCaret();
        dot = caret.getDot();
        mark = caret.getMark();
        fireCaretUpdate(this);
      }
    }

    public void mouseReleased(MouseEvent e) {
      if ( (e.getModifiers() & MouseEvent.BUTTON3_MASK) != 0) {
        showPopup(e);
      }
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    /**
     * Shows a popup menu with cut, copy, paste, etc. options if the
     * user clicked the right button.
     *
     * @param e The mouse event that caused this method to be called.
     */
    private void showPopup(MouseEvent e) {
      if (RTextArea.rightClickMenu == null) {
        createRightClickMenu();
      }
      RTextArea.rightClickMenu.show(e.getComponent(),
                                    e.getX(), e.getY());
    }

  }

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

  /**
   * This class manages undos/redos for a particular editor pane.  It groups
   * all undos that occur one character position apart together, to avoid
   * Java's horrible "one character at a time" undo behavior.  It also
   * recognizes "replace" actions (i.e., text is selected, then the user
   * types), and treats it as a single action, instead of a remove/insert
   * action pair.
   */
  class RUndoManager
      extends UndoManager {

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

    public RCompoundEdit compoundEdit;
    private RTextArea textArea;
    private int lastOffset;
    private boolean internalAtomicEdit;

    public RUndoManager(RTextArea textArea) {
      this.textArea = textArea;
    }

    /**
     * Begins an "atomic" edit.  This method is called when RTextArea
     * KNOWS that some edits should be compound automatically, such as
     * when the user is typing in overwrite mode (the deletion of the
     * current char + insertion of the new one) or the playing back of a
     * macro.
     */
    public void beginInternalAtomicEdit() {
      if (compoundEdit != null) {
        compoundEdit.end();
      }
      compoundEdit = new RCompoundEdit();
      internalAtomicEdit = true;
    }

    /**
     * Ends an "atomic" edit.
     */
    public void endInternalAtomicEdit() {
      addEdit(compoundEdit);
      compoundEdit.end();
      compoundEdit = null;
      internalAtomicEdit = false;
      updateActions(); // Needed to show the new display name.
    }

    public void undoableEditHappened(UndoableEditEvent e) {

      // This happens when the first undoable edit occurs, and
      // just after an undo.  So, we need to update our actions.
      if (compoundEdit == null) {
        compoundEdit = startCompoundEdit(e.getEdit());
        updateActions();
        return;
      }

      else if (internalAtomicEdit == true) {
        compoundEdit.addEdit(e.getEdit());
        return;
      }

      // This happens when there's already an undo that has occured.
      // Test to see if these undos are on back-to-back characters,
      // and if they are, group them as a single edit.  Since an
      // undo has already occured, there is no need to update our
      // actions here.
      int diff = textArea.getCaretPosition() - lastOffset;
      // "<=1" allows contiguous "overwrite mode" keypresses to be
      // grouped together.
      if (Math.abs(diff) <= 1) { //==1) {
        compoundEdit.addEdit(e.getEdit());
        lastOffset += diff;
        //updateActions();
        return;
      }

      // This happens when this undoableedit didn't occur at the
      // character just after the presious undlabeledit.  Since an
      // undo has already occured, there is no need to update our
      // actions here either.
      compoundEdit.end();
      compoundEdit = startCompoundEdit(e.getEdit());
      //updateActions();

    }

    private RCompoundEdit startCompoundEdit(UndoableEdit edit) {
      lastOffset = textArea.getCaretPosition();
      compoundEdit = new RCompoundEdit();
      compoundEdit.addEdit(edit);
      addEdit(compoundEdit);
      return compoundEdit;
    }

    public void undo() throws CannotUndoException {
      inUndoRedo = true;
      super.undo();
      updateActions();
      inUndoRedo = false;
    }

    public void redo() throws CannotRedoException {
      inUndoRedo = true;
      super.redo();
      updateActions();
      inUndoRedo = false;
    }

    /**
     * Ensures that undo/redo actions are enabled appropriately and have
     * descriptive text at all times.
     */
    public void updateActions() {

      String text;

      if (canUndo() == true) {
        undoAction.setEnabled(true);
        text = getUndoPresentationName();
        undoAction.putValue(Action.NAME, text);
        undoAction.putValue(Action.SHORT_DESCRIPTION, text);
      }
      else {
        if (undoAction.isEnabled()) {
          undoAction.setEnabled(false);
          text = cantUndoText;
          undoAction.putValue(Action.NAME, text);
          undoAction.putValue(Action.SHORT_DESCRIPTION, text);
        }
      }
//			undoAction.putValue(Action.NAME, text);
//			undoAction.putValue(Action.SHORT_DESCRIPTION, text);

      if (canRedo() == true) {
        redoAction.setEnabled(true);
        text = getRedoPresentationName();
        redoAction.putValue(Action.NAME, text);
        redoAction.putValue(Action.SHORT_DESCRIPTION, text);
      }
      else {
        if (redoAction.isEnabled()) {
          redoAction.setEnabled(false);
          text = cantRedoText;
          redoAction.putValue(Action.NAME, text);
          redoAction.putValue(Action.SHORT_DESCRIPTION, text);
        }
      }
//			redoAction.putValue(Action.NAME, text);
//			redoAction.putValue(Action.SHORT_DESCRIPTION, text);

    }

    // Action used by RUndoManager.
    class RCompoundEdit
        extends CompoundEdit {

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

      public String getUndoPresentationName() {
        return UIManager.getString("AbstractUndoableEdit.undoText");
      }

      public String getRedoPresentationName() {
        return UIManager.getString("AbstractUndoableEdit.redoText");
      }

      public boolean isInProgress() {
        return false;
      }

      public void undo() throws CannotUndoException {
        if (compoundEdit != null) {
          compoundEdit.end();
        }
        super.undo();
        compoundEdit = null;
      }

    }

  }

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

}

⌨️ 快捷键说明

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