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

📄 rtextareaeditorkit.java~2~

📁 具有不同语法高亮的编辑器实例
💻 JAVA~2~
📖 第 1 页 / 共 5 页
字号:
/*
 * 08/13/2004
 *
 * RTextAreaEditorKit.java - The editor kit used by RTextArea.
 * Copyright (C) 2004 Robert Futrell
 * email@address.com
 * www.website.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.fife.ui.rtextarea;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.DateFormat;
import java.util.Date;
import javax.swing.*;
import javax.swing.text.*;

/**
 * An extension of <code>DefaultEditorKit</code> that adds functionality found
 * in <code>RTextArea</code>.
 *
 * @author Robert Futrell
 * @version 0.1
 */
// FIXME:  Replace Utilities calls with custom versions (in RSyntaxUtilities) to
// cut down on all of the modelToViews, as each call causes
// a getTokenList => expensive!
public class RTextAreaEditorKit
    extends DefaultEditorKit {

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

  /**
   * The name of the action that begins recording a macro.
   */
  public static final String rtaBeginRecordingMacroAction =
      "RTextArea.BeginRecordingMacroAction";

  /**
   * The name of the action to decrease the font size.
   */
  public static final String rtaDecreaseFontSizeAction =
      "RTextArea.DecreaseFontSizeAction";

  /**
   * The name of the action taken to delete the remainder of the line (from
   * the caret position to the end of the line).
   */
  public static final String rtaDeleteRestOfLineAction =
      "RTextArea.DeleteRestOfLineAction";

  /**
   * The name of the action that ends recording a macro.
   */
  public static final String rtaEndRecordingMacroAction =
      "RTextArea.EndRecordingMacroAction";

  /**
   * The name of the action taken when the user hits the Home key.
   */
  public static final String rtaHomeAction = "RTextArea.HomeAction";

  /**
   * The name of the action to increase the font size.
   */
  public static final String rtaIncreaseFontSizeAction =
      "RTextArea.IncreaseFontSizeAction";

  /**
   * The name of the action that inverts the case of the current selection.
   */
  public static final String rtaInvertSelectionCaseAction =
      "RTextArea.InvertCaseAction";

  /**
   * The name of the action to join two lines.
   */
  public static final String rtaJoinLinesAction = "RTextArea.JoinLinesAction";

  /**
   * The name of the action to make the current selection lower-case.
   */
  public static final String rtaLowerSelectionCaseAction =
      "RTextArea.LowerCaseAction";

  /**
   * The name of the action that "plays back" the last macro.
   */
  public static final String rtaPlaybackLastMacroAction =
      "RTextArea.PlaybackLastMacroAction";

  /**
   * The name of the action for "redoing" the last action undone.
   */
  public static final String rtaRedoAction = "RTextArea.RedoAction";

  /**
   * The name of the action for "paging up" with the selection.
   */
  public static final String rtaSelectionPageUpAction =
      "RTextArea.SelectionPageUpAction";

  /**
   * The name of the action for "paging down" with the selection.
   */
  public static final String rtaSelectionPageDownAction =
      "RTextArea.SelectionPageDownAction";

  /**
   * The name of the action for "paging left" with the selection.
   */
  public static final String rtaSelectionPageLeftAction =
      "RTextArea.SelectionPageLeftAction";

  /**
   * The name of the action for "paging right" with the selection.
   */
  public static final String rtaSelectionPageRightAction =
      "RTextArea.SelectionPageRightAction";

  /**
   * The name of the action for inserting a time/date stamp.
   */
  public static final String rtaTimeDateAction = "RTextArea.TimeDateAction";

  /**
   * The name of the action taken when the user hits the Insert key (thus
   * toggling between insert and overwrite modes).
   */
  public static final String rtaToggleTextModeAction =
      "RTextArea.ToggleTextModeAction";

  /**
   * The name of the action for "undoing" the last action done.
   */
  public static final String rtaUndoAction = "RTextArea.UndoAction";

  /**
   * The name of the action for unselecting any selected text in the text
   * area.
   */
  public static final String rtaUnselectAction = "RTextArea.UnselectAction";

  /**
   * The name of the action for making the current selection upper-case.
   */
  public static final String rtaUpperSelectionCaseAction =
      "RTextArea.UpperCaseAction";

  /**
   * The actions that <code>RTextAreaEditorKit</code> adds to those of
   * the default editor kit.
   */
  private static final RecordableTextAction[] defaultActions = {
      new BeginAction(beginAction, false),
      new BeginAction(selectionBeginAction, true),
      new BeginLineAction(beginLineAction, false),
      new BeginLineAction(selectionBeginLineAction, true),
      new BeginRecordingMacroAction(),
      new BeginWordAction(beginWordAction, false),
      new BeginWordAction(selectionBeginWordAction, true),
      new CopyAction(),
      new CutAction(),
      new DefaultKeyTypedAction(),
      new DeleteNextCharAction(),
      new DeletePrevCharAction(),
      new DeleteRestOfLineAction(),
      new EndAction(endAction, false),
      new EndAction(selectionEndAction, true),
      new EndLineAction(endLineAction, false),
      new EndLineAction(selectionEndLineAction, true),
      new EndRecordingMacroAction(),
      new EndWordAction(endWordAction, false),
      new EndWordAction(endWordAction, true),
      new HomeAction(),
      new InsertBreakAction(),
      new InsertContentAction(),
      new InsertTabAction(),
      new InvertSelectionCaseAction(),
      new JoinLinesAction(),
      new LowerSelectionCaseAction(),
      new NextVisualPositionAction(forwardAction, false, SwingConstants.EAST),
      new NextVisualPositionAction(backwardAction, false, SwingConstants.WEST),
      new NextVisualPositionAction(selectionForwardAction, true,
                                   SwingConstants.EAST),
      new NextVisualPositionAction(selectionBackwardAction, true,
                                   SwingConstants.WEST),
      new NextVisualPositionAction(upAction, false, SwingConstants.NORTH),
      new NextVisualPositionAction(downAction, false, SwingConstants.SOUTH),
      new NextVisualPositionAction(selectionUpAction, true,
                                   SwingConstants.NORTH),
      new NextVisualPositionAction(selectionDownAction, true,
                                   SwingConstants.SOUTH),
      new NextWordAction(nextWordAction, false),
      new NextWordAction(selectionNextWordAction, true),
      new PageAction(rtaSelectionPageLeftAction, true, true),
      new PageAction(rtaSelectionPageRightAction, false, true),
      new PasteAction(),
      new PlaybackLastMacroAction(),
      new PreviousWordAction(previousWordAction, false),
      new PreviousWordAction(selectionPreviousWordAction, true),
      new RedoAction(),
      new SelectAllAction(),
      new SelectLineAction(),
      new SelectWordAction(),
      new SetReadOnlyAction(),
      new SetWritableAction(),
      new ToggleTextModeAction(),
      new UndoAction(),
      new UnselectAction(),
      new UpperSelectionCaseAction(),
      new VerticalPageAction(pageUpAction, -1, false),
      new VerticalPageAction(pageDownAction, 1, false),
      new VerticalPageAction(rtaSelectionPageUpAction, -1, true),
      new VerticalPageAction(rtaSelectionPageDownAction, 1, true)
  };

  /**
   * The amount of characters read at a time when reading a file.
   */
  private static final int READBUFFER_SIZE = 32768;

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

  /**
   * Constructor.
   */
  public RTextAreaEditorKit() {
    super();
  }

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

  /**
   * Returns the default document used by <code>RTextArea</code>s.
   *
   * @return The document.
   */
  public Document createDefaultDocument() {
//		return super.createDefaultDocument();
    return new RTextAreaDocument();
  }

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

  /**
   * Fetches the set of commands that can be used
   * on a text component that is using a model and
   * view produced by this kit.
   *
   * @return the command list
   */
  public Action[] getActions() {
    return defaultActions;
  }

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

  /**
   * Inserts content from the given stream, which will be
   * treated as plain text.  This method is overridden merely
   * so we can increase the number of characters read at a time.
   *
   * @param in  The stream to read from
   * @param doc The destination for the insertion.
   * @param pos The location in the document to place the
   *   content >= 0.
   * @exception IOException on any I/O error
   * @exception BadLocationException if pos represents an invalid
   *   location within the document.
   */
  public void read(Reader in, Document doc, int pos) throws IOException,
      BadLocationException {

    char[] buff = new char[READBUFFER_SIZE];
    int nch;
    boolean lastWasCR = false;
    boolean isCRLF = false;
    boolean isCR = false;
    int last;
    boolean wasEmpty = (doc.getLength() == 0);

    // Read in a block at a time, mapping \r\n to \n, as well as single
    // \r's to \n's. If a \r\n is encountered, \r\n will be set as the
    // newline string for the document, if \r is encountered it will
    // be set as the newline character, otherwise the newline property
    // for the document will be removed.
    while ( (nch = in.read(buff, 0, buff.length)) != -1) {
      last = 0;
      for (int counter = 0; counter < nch; counter++) {
        switch (buff[counter]) {
          case '\r':
            if (lastWasCR) {
              isCR = true;
              if (counter == 0) {
                doc.insertString(pos, "\n", null);
                pos++;
              }
              else {
                buff[counter - 1] = '\n';
              }
            }
            else {
              lastWasCR = true;
            }
            break;
          case '\n':
            if (lastWasCR) {
              if (counter > (last + 1)) {
                doc.insertString(pos, new String(buff, last,
                                                 counter - last - 1), null);
                pos += (counter - last - 1);
              }
              // else nothing to do, can skip \r, next write will
              // write \n
              lastWasCR = false;
              last = counter;
              isCRLF = true;
            }
            break;
          default:
            if (lastWasCR) {
              isCR = true;
              if (counter == 0) {
                doc.insertString(pos, "\n", null);
                pos++;
              }
              else {
                buff[counter - 1] = '\n';
              }
              lastWasCR = false;
            }
            break;
        } // End of switch (buff[counter]).
      } // End of for (int counter = 0; counter < nch; counter++).

      if (last < nch) {
        if (lastWasCR) {
          if (last < (nch - 1)) {
            doc.insertString(pos, new String(buff, last,
                                             nch - last - 1), null);
            pos += (nch - last - 1);
          }
        }
        else {
          doc.insertString(pos, new String(buff, last,
                                           nch - last), null);
          pos += (nch - last);
        }
      }

    } // End of while ((nch = in.read(buff, 0, buff.length)) != -1).

    if (lastWasCR) {
      doc.insertString(pos, "\n", null);
      isCR = true;
    }

    if (wasEmpty) {
      if (isCRLF) {
        doc.putProperty(EndOfLineStringProperty, "\r\n");
      }
      else if (isCR) {
        doc.putProperty(EndOfLineStringProperty, "\r");
      }
      else {
        doc.putProperty(EndOfLineStringProperty, "\n");
      }
    }

  }

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

  /**
   * Creates a beep.
   */
  public static class BeepAction
      extends RecordableTextAction {

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

    public BeepAction() {
      super(beepAction);
    }

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
      UIManager.getLookAndFeel().provideErrorFeedback(textArea);
    }

    public final String getMacroID() {
      return beepAction;
    }

  }

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

  /**
   * Moves the caret to the beginning of the document.
   */
  public static class BeginAction
      extends RecordableTextAction {

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

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

    public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
      if (select) {
        textArea.moveCaretPosition(0);
      }
      else {
        textArea.setCaretPosition(0);
      }
    }

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

  }

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

  /**
   * Position the caret at the beginning of the line.

⌨️ 快捷键说明

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