jedittextarea.java

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

JAVA
2,487 行
字号
            painter.setCursor(java.awt.dnd.DragSource.DefaultCopyDrop);          else            painter.setCursor(java.awt.dnd.DragSource.DefaultMoveDrop);          dragCursor = true;        }        setShadowCaretPosition(xyToOffset(evt.getX(), evt.getY()));        return;      }      setSelectionRectangular(evt.isControlDown());      switch (clickCount)      {        case 1:          doSingleDrag(evt);          break;        case 2:          doDoubleDrag(evt);          break;        case 3:          doTripleDrag(evt);          break;      }    }    public void mouseMoved(MouseEvent evt)    {    }    private void doSingleDrag(MouseEvent evt)    {      select(getMarkPosition(), xyToOffset(evt.getX(), evt.getY()));    }    private void doDoubleDrag(MouseEvent evt)    {      int markLine = getMarkLine();      int markLineStart = getLineStartOffset(markLine);      int markLineLength = getLineLength(markLine);      int mark = getMarkPosition() - markLineStart;      int line = yToLine(evt.getY());      int lineStart = getLineStartOffset(line);      int lineLength = getLineLength(line);      int offset = xToOffset(line, evt.getX());      String lineText = getLineText(line);      String markLineText = getLineText(markLine);      String noWordSep = (String) document.getProperty("noWordSep");      if (markLineStart + mark > lineStart + offset)      {        if (offset != 0 && offset != lineLength)        {          offset = TextUtilities.findWordStart(lineText, offset, noWordSep);        }        if (markLineLength != 0)        {          mark = TextUtilities.findWordEnd(markLineText, mark, noWordSep);        }      }      else      {        if (offset != 0 && lineLength != 0)        {          offset = TextUtilities.findWordEnd(lineText, offset, noWordSep);        }        if (mark != 0 && mark != markLineLength)        {          mark = TextUtilities.findWordStart(markLineText, mark, noWordSep);        }      }      select(markLineStart + mark, lineStart + offset);    }    private void doTripleDrag(MouseEvent evt)    {      int mark = getMarkLine();      int mouse = yToLine(evt.getY());      int offset = xToOffset(mouse, evt.getX());      if (mark > mouse)      {        mark = getLineEndOffset(mark) - 1;        if (offset == getLineLength(mouse))          mouse = getLineEndOffset(mouse) - 1;        else          mouse = getLineStartOffset(mouse);      }      else      {        mark = getLineStartOffset(mark);        if (offset == 0)          mouse = getLineStartOffset(mouse);        else          mouse = getLineEndOffset(mouse) - 1;      }      select(mark, mouse);    }  }  class FocusHandler implements FocusListener  {    public void focusGained(FocusEvent evt)    {      setCaretVisible(true);      focusedComponent = JEditTextArea.this;    }    public void focusLost(FocusEvent evt)    {      setCaretVisible(false);      focusedComponent = null;    }  }  //////////////////////////////////////////////////////////////////  // ONE CLICK HACK  //////////////////////////////////////////////////////////////////  public void setOneClick(org.jext.OneClickAction oneClick)  {    setOneClick(oneClick, null);  }  public void setOneClick(org.jext.OneClickAction oneClick, ActionEvent oneClickEvent)  {    this.oneClick = oneClick;    this.oneClickEvent = oneClickEvent;    JextTextArea area = (JextTextArea) this;    area.getJextParent().setStatus(area);  }  protected ActionEvent oneClickEvent;  public org.jext.OneClickAction oneClick;  //////////////////////////////////////////////////////////////////  // END OF ONE CLICK HACK  //////////////////////////////////////////////////////////////////    private boolean dragText = false;  private boolean dragCursor = false;  private boolean dragControlDown = false;  private int shadowCaretLine = -1;  private int shadowCaretOffset = -1;  public void setShadowCaretPosition(int offset)  {    this.shadowCaretOffset = offset;    if (offset == -1)    {      shadowCaretLine = -1;    } else {      shadowCaretLine = getLineOfOffset(offset);      if (!scrollTo(shadowCaretLine, shadowCaretOffset - getLineStartOffset(shadowCaretLine)))        repaint();    }  }  public int getShadowCaretLine()  {    return shadowCaretLine;  }  public int getShadowCaretPosition()  {    return shadowCaretOffset;  }  class MouseHandler extends MouseAdapter  {    public void mouseReleased(MouseEvent evt)    {      int line = yToLine(evt.getY());      int offset = xToOffset(line, evt.getX());      int dot = getLineStartOffset(line) + offset;      if (dragText)      {        if (dot > getSelectionStart() && dot < getSelectionEnd())        {          doSingleClick(evt, line, offset, dot);        } else {          // moves text          String text = getSelectedText();          try          {            if (!evt.isControlDown())            {              if (getSelectionStart() < dot)                dot -= text.length();                          setSelectedText("");            }            document.insertString(dot, text, null);            select(dot, dot + text.length());          } catch (BadLocationException ble) { }        }        setShadowCaretPosition(-1);        dragText = false;        if (dragCursor)        {          dragCursor = false;          painter.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));        }      }    }    public void mousePressed(MouseEvent evt)    {      requestFocus();      // Focus events not fired sometimes?      setCaretVisible(true);      focusedComponent = JEditTextArea.this;      if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0 && popup != null)      {        doRightClick(evt);        return;      }      int line = yToLine(evt.getY());      int offset = xToOffset(line, evt.getX());      int dot = getLineStartOffset(line) + offset;      clickCount = evt.getClickCount();      switch (clickCount)      {        case 1:          if (getSelectionStart() != getSelectionEnd() &&              dot > getSelectionStart() && dot < getSelectionEnd())          {            dragText = true;          } else            doSingleClick(evt, line, offset, dot);          break;        case 2:          // It uses the bracket matching stuff, so          // it can throw a BLE          try          {            doDoubleClick(evt, line, offset, dot);          } catch (BadLocationException bl) {            bl.printStackTrace();          }          break;        case 3:          doTripleClick(evt, line, offset, dot);          break;      }    }    private void doSingleClick(MouseEvent evt, int line, int offset, int dot)    {      if (evt.isShiftDown())      {        rectSelect = evt.isControlDown();        select(getMarkPosition(), dot);      } else        setCaretPosition(dot);      if (oneClick != null)        oneClick.oneClickActionPerformed(oneClickEvent);     ((JextTextArea) JEditTextArea.this).endCurrentEdit();    }    private void doDoubleClick(MouseEvent evt, int line, int offset, int dot) throws BadLocationException    {      // Ignore empty lines      if (getLineLength(line) == 0)        return;      try      {        int bracket = TextUtilities.findMatchingBracket(document, Math.max(0, dot - 1));        if (bracket != -1)        {          int mark = getMarkPosition();          // Hack          if (bracket > mark)          {            bracket++;            mark--;          }          select(mark, bracket);          return;        }      } catch (BadLocationException bl) {        bl.printStackTrace();      }      // Ok, it's not a bracket... select the word      String lineText = getLineText(line);      String noWordSep = (String) document.getProperty("noWordSep");      if (offset == getLineLength(line))        offset--;      int wordStart = TextUtilities.findWordStart(lineText, offset, noWordSep);      int wordEnd = TextUtilities.findWordEnd(lineText, offset + 1, noWordSep);      int lineStart = getLineStartOffset(line);      select(lineStart + wordStart, lineStart + wordEnd);    }    private void doTripleClick(MouseEvent evt, int line, int offset, int dot)    {      select(getLineStartOffset(line), getLineEndOffset(line) - 1);    }    private void doRightClick(MouseEvent evt)    {      int x = evt.getX();      int y = evt.getY();      JextFrame view = ((JextTextArea) JEditTextArea.this).getJextParent();      if (view != null)      {        Dimension viewSize = view.getSize();        Point viewLocation = view.getLocationOnScreen();        Insets viewInsets = view.getInsets();        Point tapLocation = painter.getLocationOnScreen();        Dimension popupSize = popup.getSize();        if ((tapLocation.x + x + popupSize.width) > (viewLocation.x + viewSize.width - viewInsets.right))          x -= popupSize.width;        if ((tapLocation.y + y + popupSize.height) > (viewLocation.y + viewSize.height - viewInsets.bottom))          y = (viewLocation.y + viewSize.height - viewInsets.bottom) - (tapLocation.y + popupSize.height);      }      popup.show(painter, x, y);    }  }  class CaretUndo extends AbstractUndoableEdit  {    private int start;    private int end;    private int newStart;    private int newEnd;    CaretUndo(int start, int end, int newStart, int newEnd)    {      this.start = start;      this.end = end;      this.newStart = newStart;      this.newEnd = newEnd;    }    public boolean isSignificant()    {      return false;    }    public String getPresentationName()    {      return "caret move";    }    public void undo() throws CannotUndoException    {      super.undo();      select(start, end);    }    public boolean addEdit(UndoableEdit edit)    {      if (edit instanceof CaretUndo)      {        CaretUndo cedit = (CaretUndo) edit;        cedit.die();        return true;      } else        return false;    }    public String toString()    {      return getPresentationName() + "[start=" + start + ",end=" + end + "]";    }  }  static  {    caretTimer = new Timer(500, new CaretBlinker());    caretTimer.setInitialDelay(500);    caretTimer.start();  }}/*  * ChangeLog:  * $Log: JEditTextArea.java,v $  * Revision 1.10  2003/06/30 17:31:08  blaisorblade  * Fix for line-ends.  *  * Revision 1.9  2003/06/29 13:37:27  gfx  * Support of JDK 1.4.2  *  * Revision 1.8  2003/03/08 21:27:14  gfx  * Added a line term selector on each text area  *  * Revision 1.7  2003/01/21 23:53:16  gfx  * New stuffs  *  * Revision 1.6  2002/08/08 13:21:18  orangeherbert  * KeyEvent workaround added for ALT+arrows.  Thanks and credit to Slava.  *  * Revision 1.5  2001/09/27 23:38:34  gfx  * Jext 3.0pre10 <Seldon>  *  * Revision 1.4  2001/09/27 16:29:21  gfx  * Jext 3.0pre10 <Seldon>  *  * Revision 1.3  2001/09/27 16:21:42  gfx  * Jext 3.0pre10 <Seldon>  *  * Revision 1.2  2001/08/30 08:56:07  gfx  * Jext 3.0pre7  *  * Revision 1.1.1.1  2001/08/20 22:32:12  gfx  * Jext 3.0pre5  *  * Revision 1.6  2001/08/12 15:02:04  gfx  * Repeat bug fix  *  * Revision 1.5  2001/06/27 22:39:03  gfx  * Fix  *  * Revision 1.4  2001/06/18 22:47:23  gfx  * JDK 1.4 compliance  *  * Revision 1.3  2001/06/14 20:13:48  gfx  * 3.0pre3 <Fredericks> (part of)  *  * Revision 1.2  2001/06/07 00:13:13  gfx  * Jext 3.0pre2 <Madcat>  *  * Revision 1.1.1.1  2001/04/11 14:22:51  gfx  *  * Jext 2.11: GUI customization, bug fixes  *  * Revision 1.47  2000/03/20 03:42:55  sp  * Smoother syntax package, opening an already open file will ask if it should be  * reloaded, maybe some other changes  *  * Revision 1.46  2000/03/14 06:22:25  sp  * Lots of new stuff  *  * Revision 1.45  2000/02/27 00:39:51  sp  * Misc changes  *  * Revision 1.44  2000/02/15 07:44:30  sp  * bug fixes, doc updates, etc  *  * Revision 1.43  2000/02/04 05:50:27  sp  * More gutter updates from mike  *  * Revision 1.42  2000/02/01 06:12:33  sp  * Gutter added (still not fully functional)  *  * Revision 1.41  2000/01/29 03:27:20  sp  * Split window functionality added  *  * Revision 1.40  2000/01/29 01:56:51  sp  * Buffer tabs updates, some other stuff  *  * Revision 1.39  2000/01/28 00:20:58  sp  * Lots of stuff  *  * Revision 1.38  2000/01/21 00:35:29  sp  * Various updates  *  * Revision 1.37  2000/01/14 04:23:50  sp  * 2.3pre2 stuff  *  * Revision 1.36  1999/12/13 03:40:30  sp  * Bug fixes, syntax is now mostly GPL'd  *  * Revision 1.35  1999/12/06 00:06:15  sp  * Bug fixes  *  * Revision 1.34  1999/12/03 23:48:11  sp  * C+END/C+HOME, LOADING BufferUpdate message, misc stuff  *  */

⌨️ 快捷键说明

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