/Users/lyon/j4p/src/gui/tree/JLabelDragSource.java

1    package gui.tree; 
2     
3    import javax.swing.*; 
4    import java.awt.*; 
5    import java.awt.datatransfer.Transferable; 
6    import java.awt.dnd.*; 
7    import java.util.Iterator; 
8     
9    public class JLabelDragSource implements DragGestureListener, 
10           DragSourceListener { 
11       public JLabelDragSource(JLabel label) { 
12           this.label = label; 
13    
14           // Use the default DragSource 
15           DragSource dragSource = DragSource.getDefaultDragSource(); 
16    
17           // Create a DragGestureRecognizer and 
18           // register as the listener 
19           dragSource.createDefaultDragGestureRecognizer( 
20                   label, DnDConstants.ACTION_COPY_OR_MOVE, 
21                   this); 
22       } 
23    
24       // Implementation of DragGestureListener interface. 
25       public void dragGestureRecognized(DragGestureEvent dge) { 
26           if (DnDUtils.isDebugEnabled()) { 
27               DnDUtils.debugPrintln("Initiating event is " + dge.getTriggerEvent()); 
28               DnDUtils.debugPrintln("Complete event set is:"); 
29               Iterator iter = dge.iterator(); 
30               while (iter.hasNext()) { 
31                   DnDUtils.debugPrintln("\t" + iter.next()); 
32               } 
33           } 
34           Transferable transferable = new JLabelTransferable(label); 
35           dge.startDrag(null, transferable, this); 
36       } 
37    
38       // Implementation of DragSourceListener interface 
39       public void dragEnter(DragSourceDragEvent dsde) { 
40           DnDUtils.debugPrintln("Drag Source: dragEnter, drop action = " 
41                   + DnDUtils.showActions(dsde.getDropAction())); 
42       } 
43    
44       public void dragOver(DragSourceDragEvent dsde) { 
45           DnDUtils.debugPrintln("Drag Source: dragOver, drop action = " 
46                   + DnDUtils.showActions(dsde.getDropAction())); 
47       } 
48    
49       public void dragExit(DragSourceEvent dse) { 
50           DnDUtils.debugPrintln("Drag Source: dragExit"); 
51       } 
52    
53       public void dropActionChanged(DragSourceDragEvent dsde) { 
54           DnDUtils.debugPrintln("Drag Source: dropActionChanged, drop action = " 
55                   + DnDUtils.showActions(dsde.getDropAction())); 
56       } 
57    
58       public void dragDropEnd(DragSourceDropEvent dsde) { 
59           DnDUtils.debugPrintln("Drag Source: drop completed, drop action = " 
60                   + DnDUtils.showActions(dsde.getDropAction()) 
61                   + ", success: " + dsde.getDropSuccess()); 
62       } 
63    
64       public static void main(String[] args) { 
65           JFrame f = new JFrame("Draggable JLabel"); 
66           JLabel label = new JLabel("Drag this text", JLabel.CENTER); 
67           label.setFont(new Font("Serif", Font.BOLD, 32)); 
68           f.getContentPane().add(label); 
69           f.pack(); 
70           f.setVisible(true); 
71    
72           // Attach the drag source 
73           JLabelDragSource dragSource = new JLabelDragSource(label); 
74       } 
75    
76       protected JLabel label;     // The associated JLabel 
77   } 
78    
79