KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * TextAreaTransferHandler.java - Drag and drop support
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2004 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.textarea;
24
25 //{{{ Imports
26
import javax.swing.*;
27 import java.awt.dnd.*;
28 import java.awt.*;
29 import org.gjt.sp.jedit.buffer.JEditBuffer;
30 import org.gjt.sp.util.Log;
31 //}}}
32

33 class TextAreaDropHandler extends DropTargetAdapter
34 {
35     private TextArea textArea;
36     private JEditBuffer savedBuffer;
37     private int savedCaret;
38
39     TextAreaDropHandler(TextArea textArea)
40     {
41         this.textArea = textArea;
42     }
43
44     public void dragEnter(DropTargetDragEvent dtde)
45     {
46         Log.log(Log.DEBUG,this,"Drag enter");
47         savedBuffer = textArea.getBuffer();
48         textArea.setDragInProgress(true);
49         //textArea.getBuffer().beginCompoundEdit();
50
savedCaret = textArea.getCaretPosition();
51     }
52
53     public void dragOver(DropTargetDragEvent dtde)
54     {
55         Point p = dtde.getLocation();
56         p = SwingUtilities.convertPoint(textArea,p,
57             textArea.getPainter());
58         int pos = textArea.xyToOffset(p.x,p.y,
59             !(textArea.getPainter().isBlockCaretEnabled()
60             || textArea.isOverwriteEnabled()));
61         if(pos != -1)
62         {
63             textArea.moveCaretPosition(pos,
64                 TextArea.ELECTRIC_SCROLL);
65         }
66     }
67
68     public void dragExit(DropTargetEvent dtde)
69     {
70         Log.log(Log.DEBUG,this,"Drag exit");
71         textArea.setDragInProgress(false);
72         //textArea.getBuffer().endCompoundEdit();
73
if(textArea.getBuffer() == savedBuffer)
74         {
75             textArea.moveCaretPosition(savedCaret,
76                 TextArea.ELECTRIC_SCROLL);
77         }
78         savedBuffer = null;
79     }
80
81     public void drop(DropTargetDropEvent dtde)
82     {
83         Log.log(Log.DEBUG,this,"Drop");
84         textArea.setDragInProgress(false);
85         //textArea.getBuffer().endCompoundEdit();
86
}
87 }
88
Popular Tags