KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > launcher > DroppableTextPane


1 /**
2  * $RCSfile: DroppableTextPane.java,v $
3  * $Revision: 1.1 $
4  * $Date: 2005/04/24 17:22:45 $
5  *
6  * Copyright (C) 2005 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.launcher;
13
14 import javax.swing.*;
15 import java.awt.dnd.*;
16 import java.awt.datatransfer.Transferable JavaDoc;
17 import java.awt.datatransfer.DataFlavor JavaDoc;
18 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 /**
25  * A droppable text pane allows for DnD of file objects from the OS onto the actual
26  * pane via <code>File</code>.
27  *
28  * @author Matt Tucker
29  */

30 public abstract class DroppableTextPane extends JTextPane implements DropTargetListener,
31         DragSourceListener, DragGestureListener
32  {
33     private DragSource dragSource = DragSource.getDefaultDragSource();
34
35     /**
36      * Creates a droppable text pane.
37      */

38     public DroppableTextPane() {
39         new DropTarget(this, this);
40         dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
41     }
42
43     public void dragDropEnd(DragSourceDropEvent DragSourceDropEvent) {
44     }
45
46     public void dragEnter(DragSourceDragEvent DragSourceDragEvent) {
47     }
48
49     public void dragExit(DragSourceEvent DragSourceEvent) {
50     }
51
52     public void dragOver(DragSourceDragEvent DragSourceDragEvent) {
53     }
54
55     public void dropActionChanged(DragSourceDragEvent DragSourceDragEvent) {
56     }
57
58     public void dragEnter(DropTargetDragEvent dropTargetDragEvent) {
59         dropTargetDragEvent.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
60     }
61
62     public void dragExit(DropTargetEvent dropTargetEvent) {
63     }
64
65     public void dragOver(DropTargetDragEvent dropTargetDragEvent) {
66     }
67
68     public void dropActionChanged(DropTargetDragEvent dropTargetDragEvent) {
69     }
70
71     public void drop(DropTargetDropEvent dropTargetDropEvent) {
72         try {
73             Transferable JavaDoc transferable = dropTargetDropEvent.getTransferable();
74             if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
75                 dropTargetDropEvent.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
76                 List JavaDoc fileList = (List JavaDoc) transferable.getTransferData(DataFlavor.javaFileListFlavor);
77                 Iterator JavaDoc iterator = fileList.iterator();
78                 while (iterator.hasNext()) {
79                     File JavaDoc file = (File JavaDoc) iterator.next();
80                     if (file.isFile()) {
81                         fileDropped(file);
82                     }
83
84                     if (file.isDirectory()) {
85                         directoryDropped(file);
86                     }
87                 }
88                 dropTargetDropEvent.getDropTargetContext().dropComplete(true);
89             }
90             else {
91                 dropTargetDropEvent.rejectDrop();
92             }
93         }
94         catch (IOException JavaDoc io) {
95             io.printStackTrace();
96             dropTargetDropEvent.rejectDrop();
97         }
98         catch (UnsupportedFlavorException JavaDoc ufe) {
99             ufe.printStackTrace();
100             dropTargetDropEvent.rejectDrop();
101         }
102     }
103
104     public void dragGestureRecognized(DragGestureEvent dragGestureEvent) {
105
106     }
107
108     /**
109      * Notified when a file has been dropped onto the frame.
110      *
111      * @param file the file that has been dropped.
112      */

113     public void fileDropped(File JavaDoc file){
114
115     }
116
117     /**
118      * Notified when a directory has been dropped onto the frame.
119      *
120      * @param file the directory that has been dropped.
121      */

122     public void directoryDropped(File JavaDoc file){
123
124     }
125 }
Popular Tags