jedittextarea.java

来自「java写的多功能文件编辑器」· Java 代码 · 共 2,487 行 · 第 1/5 页

JAVA
2,487
字号
      Clipboard clipboard = getToolkit().getSystemClipboard();      try      {        // The MacOS MRJ doesn't convert \r to \n,        // so do it here        String selection =                ((String) clipboard.getContents(this).getTransferData(DataFlavor.stringFlavor)).replace('\r','\n');        setSelectedText(selection);      } catch (Exception e) {        getToolkit().beep();      }    }  }  /**    * Returns the status bar component (which was added with a name    * of LEFT_OF_SCROLLBAR).    */  public Component getStatus()  {    return ((ScrollLayout) getLayout()).leftOfScrollBar;  }  /**    * Called by the AWT when this component is added to a parent.    * Adds document listener.    */  public void addNotify()  {    super.addNotify();    if (!documentHandlerInstalled)    {      documentHandlerInstalled = true;      document.addDocumentListener(documentHandler);    }  }  /**    * Called by the AWT when this component is removed from it's parent.    * This clears the pointer to the currently focused component.    * Also removes document listener.    */  public void removeNotify()  {    super.removeNotify();    if (focusedComponent == this)      focusedComponent = null;    if (documentHandlerInstalled)    {      document.removeDocumentListener(documentHandler);      documentHandlerInstalled = false;    }  }  protected void processKeyEvent(KeyEvent evt)  {    //JextFrame view = MenuAction.getJextParent(evt);    evt = KeyEventWorkaround.processKeyEvent(evt);    if (evt == null)      return;    InputHandler inputHandler = view.getInputHandler();    KeyListener keyEventInterceptor = view.getKeyEventInterceptor();    switch (evt.getID())    {      case KeyEvent.KEY_TYPED:        if (keyEventInterceptor != null)          keyEventInterceptor.keyTyped(evt);        else          inputHandler.keyTyped(evt);        break;      case KeyEvent.KEY_PRESSED:        if (keyEventInterceptor != null)          keyEventInterceptor.keyPressed(evt);        else          inputHandler.keyPressed(evt);        break;      case KeyEvent.KEY_RELEASED:        if (keyEventInterceptor != null)          keyEventInterceptor.keyReleased(evt);        else          inputHandler.keyReleased(evt);        break;    }    if (!evt.isConsumed())      super.processKeyEvent(evt);  }  // package-private members  Segment lineSegment;  // protected members  protected static String CENTER = "center";  protected static String RIGHT = "right";  protected static String LEFT = "left";  protected static String BOTTOM = "bottom";  protected static JEditTextArea focusedComponent;  protected static Timer caretTimer;  protected TextAreaPainter painter;  protected Gutter gutter;  protected JPopupMenu popup;  protected EventListenerList listenerList;  protected MutableCaretEvent caretEvent;  protected boolean caretBlinks;  protected boolean caretVisible;  protected boolean blink;  protected boolean editable;  protected int maxHorizontalScrollWidth;  protected int firstLine;  protected int visibleLines;  protected int electricScroll;  protected int horizontalOffset;  protected JScrollBar vertical;  protected JScrollBar horizontal;  protected boolean scrollBarsInitialized;  protected InputHandler inputHandler;  protected SyntaxDocument document;  protected DocumentHandler documentHandler;  protected boolean documentHandlerInstalled;  protected int selectionStart;  protected int selectionStartLine;  protected int selectionEnd;  protected int selectionEndLine;  protected boolean biasLeft;  protected int bracketPosition;  protected int bracketLine;  protected int magicCaret;  protected boolean overwrite;  protected boolean rectSelect;  protected void fireCaretEvent()  {    Object[] listeners = listenerList.getListenerList();    for (int i = listeners.length - 2; i >= 0; i--)    {      if (listeners[i] == CaretListener.class)      {        ((CaretListener) listeners[i + 1]).caretUpdate(caretEvent);      }    }  }  protected void updateBracketHighlight(int newCaretPosition)  {    if (!painter.isBracketHighlightEnabled())      return;    if (bracketLine != -1)      painter.invalidateLine(bracketLine);    if (newCaretPosition == 0)    {      bracketPosition = bracketLine = -1;      return;    }    try    {      int offset = TextUtilities.findMatchingBracket(document, newCaretPosition - 1);      if (offset != -1)      {        bracketLine = getLineOfOffset(offset);        bracketPosition = offset - getLineStartOffset(bracketLine);        if (bracketLine != -1)          painter.invalidateLine(bracketLine);        return;      }    }    catch (BadLocationException bl)    {      bl.printStackTrace();    }    bracketLine = bracketPosition = -1;  }  protected void documentChanged(DocumentEvent evt)  {    DocumentEvent.ElementChange ch = evt.getChange(document.getDefaultRootElement());    int count;    if (ch == null)      count = 0;    else      count = ch.getChildrenAdded().length - ch.getChildrenRemoved().length;    int line = getLineOfOffset(evt.getOffset());    if (count == 0)    {      painter.invalidateLine(line);    }    // do magic stuff    else if (line < firstLine)    {      setFirstLine(firstLine + count);    }    // end of magic stuff    else    {      painter.invalidateLineRange(line, firstLine + visibleLines);      gutter.repaint();      updateScrollBars();    }  }  // private members  // for event handlers only  private int clickCount;  class ScrollLayout implements LayoutManager  {    public void addLayoutComponent(String name, Component comp)    {      if (name.equals(CENTER))        center = comp;      else if (name.equals(RIGHT))        right = comp;      else if (name.equals(LEFT))        left = comp;      else if (name.equals(BOTTOM))        bottom = comp;      else if (name.equals(LEFT_OF_SCROLLBAR))        leftOfScrollBar = comp;    }    public void removeLayoutComponent(Component comp)    {      if (center == comp)        center = null;      else if (right == comp)        right = null;      else if (left == comp)        left = null;      else if (bottom == comp)        bottom = null;      else        leftOfScrollBar = null;    }    public Dimension preferredLayoutSize(Container parent)    {      Dimension dim = new Dimension();      Insets insets = getInsets();      dim.width = insets.left + insets.right;      dim.height = insets.top + insets.bottom;      Dimension leftPref = left.getPreferredSize();      dim.width += leftPref.width;      Dimension centerPref = center.getPreferredSize();      dim.width += centerPref.width;      dim.height += centerPref.height;      Dimension rightPref = right.getPreferredSize();      dim.width += rightPref.width;      Dimension bottomPref = bottom.getPreferredSize();      dim.height += bottomPref.height;      return dim;    }    public Dimension minimumLayoutSize(Container parent)    {      Dimension dim = new Dimension();      Insets insets = getInsets();      dim.width = insets.left + insets.right;      dim.height = insets.top + insets.bottom;      Dimension leftPref = left.getMinimumSize();      dim.width += leftPref.width;      Dimension centerPref = center.getMinimumSize();      dim.width += centerPref.width;      dim.height += centerPref.height;      Dimension rightPref = right.getMinimumSize();      dim.width += rightPref.width;      Dimension bottomPref = bottom.getMinimumSize();      dim.height += bottomPref.height;      return dim;    }    public void layoutContainer(Container parent)    {      Dimension size = parent.getSize();      Insets insets = parent.getInsets();      int itop = insets.top;      int ileft = insets.left;      int ibottom = insets.bottom;      int iright = insets.right;      int rightWidth = right.getPreferredSize().width;      int leftWidth = left.getPreferredSize().width;      int bottomHeight = bottom.getPreferredSize().height;      int centerWidth = size.width - leftWidth - rightWidth - ileft - iright;      int centerHeight = size.height - bottomHeight - itop - ibottom;      left.setBounds(ileft, itop, leftWidth, centerHeight);      center.setBounds(ileft + leftWidth, itop, centerWidth, centerHeight);      right.setBounds(ileft + leftWidth + centerWidth, itop, rightWidth, centerHeight);      if (leftOfScrollBar != null)      {        Dimension dim = leftOfScrollBar.getPreferredSize();        leftOfScrollBar.setBounds(ileft, itop + centerHeight, dim.width, bottomHeight);        ileft += dim.width;      }      bottom.setBounds(ileft, itop + centerHeight, size.width - rightWidth - ileft - iright, bottomHeight);    }    Component center;    Component left;    Component right;    Component bottom;    Component leftOfScrollBar;  }  static class CaretBlinker implements ActionListener  {    public void actionPerformed(ActionEvent evt)    {      if (focusedComponent != null && focusedComponent.hasFocus())        focusedComponent.blinkCaret();    }  }  class MutableCaretEvent extends CaretEvent  {    MutableCaretEvent()    {      super(JEditTextArea.this);    }    public int getDot()    {      return getCaretPosition();    }    public int getMark()    {      return getMarkPosition();    }  }  class AdjustHandler implements AdjustmentListener  {    public void adjustmentValueChanged(final AdjustmentEvent evt)    {      if (!scrollBarsInitialized)        return;      // If this is not done, mousePressed events accumilate      // and the result is that scrolling doesn't stop after      // the mouse is released      SwingUtilities.invokeLater(new Runnable()                                 {                                   public void run()                                   {                                   if (evt.getAdjustable() == vertical)                                       setFirstLine(vertical.getValue());                                   else                                       setHorizontalOffset(-horizontal.getValue());                                   }                                 }                                );    }  }  class ComponentHandler extends ComponentAdapter  {    public void componentResized(ComponentEvent evt)    {      recalculateVisibleLines();      scrollBarsInitialized = true;    }  }  class DocumentHandler implements DocumentListener  {    public void insertUpdate(DocumentEvent evt)    {      documentChanged(evt);      int offset = evt.getOffset();      int length = evt.getLength();      int newStart;      int newEnd;      boolean change = false;      if (selectionStart > offset || (selectionStart == selectionEnd && selectionStart == offset))      {        change = true;        newStart = selectionStart + length;      }      else        newStart = selectionStart;      if (selectionEnd >= offset)      {        change = true;        newEnd = selectionEnd + length;      }      else        newEnd = selectionEnd;      if (change)        select(newStart, newEnd);      else        updateBracketHighlight(getCaretPosition());    }    public void removeUpdate(DocumentEvent evt)    {      documentChanged(evt);      int offset = evt.getOffset();      int length = evt.getLength();      int newStart;      int newEnd;      boolean change = false;      if (selectionStart > offset)      {        change = true;        if (selectionStart > offset + length)          newStart = selectionStart - length;        else          newStart = offset;      }      else        newStart = selectionStart;      if (selectionEnd > offset)      {        change = true;        if (selectionEnd > offset + length)          newEnd = selectionEnd - length;        else          newEnd = offset;      }      else        newEnd = selectionEnd;      if (change)        select(newStart, newEnd);      else        updateBracketHighlight(getCaretPosition());    }    public void changedUpdate(DocumentEvent evt)    {    }  }  class DragHandler implements MouseMotionListener  {    public void mouseDragged(MouseEvent evt)    {      if (popup != null && popup.isVisible())        return;      if (dragText)      {        boolean ctrldown = evt.isControlDown();        if (ctrldown != dragControlDown)          dragCursor = false;        dragControlDown = ctrldown;        if (!dragCursor)        {          if (evt.isControlDown())

⌨️ 快捷键说明

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