KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > textarea > MouseHandler


1 /*
2  * MouseHandler.java - jEdit's text component
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
24 package org.gjt.sp.jedit.textarea;
25
26 //{{{ Imports
27
import java.awt.event.*;
28
29 import org.gjt.sp.jedit.Registers;
30 import org.gjt.sp.jedit.OperatingSystem;
31 //}}}
32

33 /**
34  * The mouseHandler used for jEdit.
35  */

36 public class MouseHandler extends TextAreaMouseHandler
37 {
38     //{{{ MouseHandler constructor
39
public MouseHandler(JEditTextArea textArea)
40     {
41         super(textArea);
42         this.textArea = textArea;
43     } //}}}
44

45     //{{{ mousePressed() method
46
public void mousePressed(MouseEvent evt)
47     {
48         showCursor();
49
50         control = (OperatingSystem.isMacOS() && evt.isMetaDown())
51             || (!OperatingSystem.isMacOS() && evt.isControlDown());
52
53         // so that Home <mouse click> Home is not the same
54
// as pressing Home twice in a row
55
textArea.getInputHandler().resetLastActionCount();
56
57         quickCopyDrag = (textArea.isQuickCopyEnabled() &&
58             isMiddleButton(evt.getModifiers()));
59
60         if(!quickCopyDrag)
61         {
62             textArea.requestFocus();
63             TextArea.focusedComponent = textArea;
64         }
65
66         if(textArea.getBuffer().isLoading())
67             return;
68
69         int x = evt.getX();
70         int y = evt.getY();
71
72         dragStart = textArea.xyToOffset(x,y,
73             !(textArea.getPainter().isBlockCaretEnabled()
74             || textArea.isOverwriteEnabled()));
75         dragStartLine = textArea.getLineOfOffset(dragStart);
76         dragStartOffset = dragStart - textArea.getLineStartOffset(
77             dragStartLine);
78
79         if(isPopupTrigger(evt)
80             && textArea.getRightClickPopup() != null)
81         {
82             if(textArea.isRightClickPopupEnabled())
83                 textArea.handlePopupTrigger(evt);
84             return;
85         }
86
87         dragged = false;
88
89         textArea.blink = true;
90         textArea.invalidateLine(textArea.getCaretLine());
91
92         clickCount = evt.getClickCount();
93
94         if(textArea.isDragEnabled()
95             && textArea.selectionManager.insideSelection(x,y)
96             && clickCount == 1 && !evt.isShiftDown())
97         {
98             maybeDragAndDrop = true;
99             textArea.moveCaretPosition(dragStart,false);
100             return;
101         }
102
103         maybeDragAndDrop = false;
104
105         if(quickCopyDrag)
106         {
107             // ignore double clicks of middle button
108
doSingleClick(evt);
109         }
110         else
111         {
112             switch(clickCount)
113             {
114             case 1:
115                 doSingleClick(evt);
116                 break;
117             case 2:
118                 doDoubleClick();
119                 break;
120             default: //case 3:
121
doTripleClick();
122                 break;
123             }
124         }
125     } //}}}
126

127     //{{{ mouseReleased() method
128
public void mouseReleased(MouseEvent evt)
129     {
130         // middle mouse button drag inserts selection
131
// at caret position
132
Selection sel = textArea.getSelectionAtOffset(dragStart);
133         if(dragged && sel != null)
134         {
135             Registers.setRegister('%',textArea.getSelectedText(sel));
136             if(quickCopyDrag)
137             {
138                 textArea.removeFromSelection(sel);
139                 Registers.paste((JEditTextArea) TextArea.focusedComponent,
140                     '%',sel instanceof Selection.Rect);
141
142                 TextArea.focusedComponent.requestFocus();
143             }
144         }
145         else if(!dragged && textArea.isQuickCopyEnabled() &&
146             isMiddleButton(evt.getModifiers()))
147         {
148             textArea.requestFocus();
149             TextArea.focusedComponent = textArea;
150
151             textArea.setCaretPosition(dragStart,false);
152             if(!textArea.isEditable())
153                 textArea.getToolkit().beep();
154             else
155                 Registers.paste((JEditTextArea) textArea,'%',control);
156         }
157         else if(maybeDragAndDrop
158             && !textArea.isMultipleSelectionEnabled())
159         {
160             textArea.selectNone();
161         }
162
163         dragged = false;
164     } //}}}
165

166     //{{{ Private members
167
private JEditTextArea textArea;
168     //}}}
169
}
170
Popular Tags