/jEdit/branches/ci_release_test/4-4-pre1/org/gjt/sp/jedit/textarea/MouseHandler.java

# · Java · 177 lines · 115 code · 25 blank · 37 comment · 27 complexity · a29dd1471d50b871830e1cad9098cafb MD5 · raw file

  1. /*
  2. * MouseHandler.java
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999, 2005 Slava Pestov
  7. * Portions copyright (C) 2000 Ollie Rutherfurd
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. package org.gjt.sp.jedit.textarea;
  24. //{{{ Imports
  25. import java.awt.event.*;
  26. import org.gjt.sp.jedit.EditBus;
  27. import org.gjt.sp.jedit.Registers;
  28. import org.gjt.sp.jedit.OperatingSystem;
  29. import org.gjt.sp.jedit.msg.PositionChanging;
  30. //}}}
  31. /** The mouseHandler used for jEdit.
  32. *
  33. */
  34. public class MouseHandler extends TextAreaMouseHandler
  35. {
  36. //{{{ MouseHandler constructor
  37. public MouseHandler(JEditTextArea textArea)
  38. {
  39. super(textArea);
  40. this.textArea = textArea;
  41. } //}}}
  42. //{{{ mousePressed() method
  43. @Override
  44. public void mousePressed(MouseEvent evt)
  45. {
  46. showCursor();
  47. control = (OperatingSystem.isMacOS() && evt.isMetaDown())
  48. || (!OperatingSystem.isMacOS() && evt.isControlDown());
  49. ctrlForRectangularSelection = textArea.isCtrlForRectangularSelection();
  50. // so that Home <mouse click> Home is not the same
  51. // as pressing Home twice in a row
  52. textArea.getInputHandler().resetLastActionCount();
  53. quickCopyDrag = (textArea.isQuickCopyEnabled() &&
  54. isMiddleButton(evt.getModifiers()));
  55. if(!quickCopyDrag)
  56. {
  57. textArea.requestFocus();
  58. TextArea.focusedComponent = textArea;
  59. }
  60. if(textArea.getBuffer().isLoading())
  61. return;
  62. EditBus.send(new PositionChanging(textArea));
  63. int x = evt.getX();
  64. int y = evt.getY();
  65. dragStart = textArea.xyToOffset(x,y,
  66. !(textArea.getPainter().isBlockCaretEnabled()
  67. || textArea.isOverwriteEnabled()));
  68. dragStartLine = textArea.getLineOfOffset(dragStart);
  69. dragStartOffset = dragStart - textArea.getLineStartOffset(
  70. dragStartLine);
  71. if(isPopupTrigger(evt)
  72. && textArea.getRightClickPopup() != null)
  73. {
  74. if(textArea.isRightClickPopupEnabled())
  75. textArea.handlePopupTrigger(evt);
  76. return;
  77. }
  78. dragged = false;
  79. textArea.blink = true;
  80. textArea.invalidateLine(textArea.getCaretLine());
  81. clickCount = evt.getClickCount();
  82. if(textArea.isDragEnabled()
  83. && textArea.selectionManager.insideSelection(x,y)
  84. && clickCount == 1 && !evt.isShiftDown())
  85. {
  86. maybeDragAndDrop = true;
  87. textArea.moveCaretPosition(dragStart,false);
  88. return;
  89. }
  90. maybeDragAndDrop = false;
  91. if(quickCopyDrag)
  92. {
  93. // ignore double clicks of middle button
  94. doSingleClick(evt);
  95. }
  96. else
  97. {
  98. switch(clickCount)
  99. {
  100. case 1:
  101. doSingleClick(evt);
  102. break;
  103. case 2:
  104. doDoubleClick();
  105. break;
  106. default: //case 3:
  107. doTripleClick();
  108. break;
  109. }
  110. }
  111. } //}}}
  112. //{{{ mouseReleased() method
  113. @Override
  114. public void mouseReleased(MouseEvent evt)
  115. {
  116. // middle mouse button drag inserts selection
  117. // at caret position
  118. Selection sel = textArea.getSelectionAtOffset(dragStart);
  119. if(dragged && sel != null)
  120. {
  121. Registers.setRegister('%',textArea.getSelectedText(sel));
  122. if(quickCopyDrag)
  123. {
  124. textArea.removeFromSelection(sel);
  125. Registers.paste(TextArea.focusedComponent,
  126. '%',sel instanceof Selection.Rect);
  127. TextArea.focusedComponent.requestFocus();
  128. }
  129. }
  130. else if(!dragged && textArea.isQuickCopyEnabled() &&
  131. isMiddleButton(evt.getModifiers()))
  132. {
  133. textArea.requestFocus();
  134. TextArea.focusedComponent = textArea;
  135. textArea.setCaretPosition(dragStart,false);
  136. if(!textArea.isEditable())
  137. textArea.getToolkit().beep();
  138. else
  139. Registers.paste(textArea,'%',control);
  140. }
  141. else if(maybeDragAndDrop
  142. && !textArea.isMultipleSelectionEnabled())
  143. {
  144. textArea.selectNone();
  145. }
  146. maybeDragAndDrop = false;
  147. dragged = false;
  148. } //}}}
  149. //{{{ Private members
  150. private JEditTextArea textArea;
  151. //}}}
  152. }