KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > swing > FileDropHandler


1 package com.opensymphony.workflow.designer.swing;
2
3 import java.awt.*;
4 import java.awt.datatransfer.DataFlavor JavaDoc;
5 import java.awt.datatransfer.Transferable JavaDoc;
6 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
7 import java.awt.dnd.*;
8 import java.awt.event.HierarchyEvent JavaDoc;
9 import java.awt.event.HierarchyListener JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.TooManyListenersException JavaDoc;
14 import javax.swing.*;
15
16 public abstract class FileDropHandler
17 {
18   private transient java.awt.dnd.DropTargetListener JavaDoc dropListener;
19
20   public FileDropHandler(Component target)
21   {
22     this(target, true);
23   }
24
25   public FileDropHandler(final Component target, final boolean recursive)
26   {
27     dropListener = new DropTargetListener()
28     {
29       public void dragEnter(DropTargetDragEvent evt)
30       {
31         if(isDragOk(evt))
32         {
33           evt.acceptDrag(DnDConstants.ACTION_COPY);
34         }
35         else
36         {
37           evt.rejectDrag();
38         }
39       }
40
41       public void dragOver(DropTargetDragEvent evt)
42       {}
43
44       public void drop(DropTargetDropEvent evt)
45       {
46         try
47         {
48           Transferable JavaDoc tr = evt.getTransferable();
49
50           if(tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
51           {
52             evt.acceptDrop(DnDConstants.ACTION_COPY);
53
54             List JavaDoc fileList = (List JavaDoc)tr.getTransferData(DataFlavor.javaFileListFlavor);
55
56             File JavaDoc[] filesTemp = new File JavaDoc[fileList.size()];
57             fileList.toArray(filesTemp);
58             final File JavaDoc[] files = filesTemp;
59
60             filesDropped(files);
61
62             evt.getDropTargetContext().dropComplete(true);
63           }
64           else
65           {
66             evt.rejectDrop();
67           }
68         }
69         catch(IOException JavaDoc io)
70         {
71           io.printStackTrace();
72           evt.rejectDrop();
73         }
74         catch(UnsupportedFlavorException JavaDoc ufe)
75         {
76           ufe.printStackTrace();
77           evt.rejectDrop();
78         }
79       }
80
81       public void dragExit(DropTargetEvent evt)
82       {
83       }
84
85       public void dropActionChanged(DropTargetDragEvent evt)
86       {
87         if(isDragOk(evt))
88         {
89           evt.acceptDrag(DnDConstants.ACTION_COPY);
90         }
91         else
92         {
93           evt.rejectDrag();
94         }
95       }
96     };
97
98     makeDropTarget(target, recursive);
99   }
100
101   private void makeDropTarget(final Component target, boolean recursive)
102   {
103     final DropTarget dt = new DropTarget();
104     try
105     {
106       dt.addDropTargetListener(dropListener);
107     }
108     catch(TooManyListenersException JavaDoc e)
109     {
110       e.printStackTrace();
111     }
112
113     target.addHierarchyListener(new HierarchyListener JavaDoc()
114     {
115       public void hierarchyChanged(HierarchyEvent JavaDoc evt)
116       {
117         Component parent = target.getParent();
118         if(parent == null)
119         {
120           target.setDropTarget(null);
121         }
122         else
123         {
124           new DropTarget(target, dropListener);
125         }
126       }
127     });
128     if(target.getParent() != null)
129       new DropTarget(target, dropListener);
130
131     if(recursive && (target instanceof Container))
132     {
133       Container cont = (Container)target;
134       Component[] comps = cont.getComponents();
135
136       for(int i = 0; i < comps.length; i++)
137         makeDropTarget(comps[i], recursive);
138     }
139   }
140
141   private boolean isDragOk(final DropTargetDragEvent evt)
142   {
143     boolean ok = false;
144
145     DataFlavor JavaDoc[] flavors = evt.getCurrentDataFlavors();
146     int i = 0;
147     while(!ok && i < flavors.length)
148     {
149       if(flavors[i].equals(DataFlavor.javaFileListFlavor))
150         ok = true;
151       i++;
152     }
153     return ok;
154   }
155
156   public abstract void filesDropped(File JavaDoc[] files);
157
158   public static void main(String JavaDoc[] args)
159   {
160     JFrame frame = new JFrame("FileDrop");
161     final JTree tree = new JTree();
162     frame.getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
163
164     new FileDropHandler(tree)
165     {
166       public void filesDropped(File JavaDoc[] files)
167       {
168         for(int i = 0; i < files.length; i++)
169         {
170           System.out.println("dropped " + files[i]);
171         }
172       }
173     };
174
175     frame.setBounds(100, 100, 300, 400);
176     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
177     frame.show();
178   }
179 }
Popular Tags