View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.util;
21  
22  import java.awt.Component;
23  import java.awt.dnd.DragGestureEvent;
24  import java.awt.dnd.DragGestureListener;
25  import java.awt.dnd.DragSource;
26  import java.awt.dnd.DragSourceDragEvent;
27  import java.awt.dnd.DragSourceDropEvent;
28  import java.awt.dnd.DragSourceEvent;
29  import java.awt.dnd.DragSourceListener;
30  import java.awt.dnd.InvalidDnDOperationException;
31  import java.io.File;
32  
33  import org.apache.log4j.Logger;
34  import org.xnap.gui.FileProvider;
35  
36  /***
37   * DragFilesSupport enables dragging of files for a component implementing the
38   * FileProvider interface.
39   *
40   * @see FileProvider
41   */
42  public class DragFilesSupport 
43      implements DragSourceListener, DragGestureListener
44  {
45      //--- Constant(s) ---
46      
47      //--- Data field(s) ---
48  
49      private DragSource dragSource = DragSource.getDefaultDragSource();
50      private FileProvider fp;
51  
52      private static Logger logger = Logger.getLogger(DragFilesSupport.class);
53  
54      //--- Constructor(s) ---
55      
56      /***
57       * Constructs a new DragFilesSupport.
58       *
59       * @param c
60       * @param fp
61       */
62      public DragFilesSupport(Component c, FileProvider fp)
63      {
64  	this.fp = fp;
65  //	DragGestureRecognizer dgr = new MouseDragAdapter
66  //	    (dragSource, c, DnDConstants.ACTION_MOVE, this);
67      }
68      
69      //--- Method(s) ---
70  
71      public void dragDropEnd(DragSourceDropEvent event)
72      {
73  
74      }
75  
76      public void dragEnter(DragSourceDragEvent event)
77      {
78  
79      }
80  
81      public void dragExit(DragSourceEvent event)
82      {
83  
84      }
85      
86      public void dragOver(DragSourceDragEvent event)
87      {
88  
89      }
90  
91      public void dropActionChanged(DragSourceDragEvent event)
92      {
93  
94      }
95  
96      /***
97       * Called by the {@link java.awt.dnd.DragGestureRecognizer} when a drag gesture is
98       * recognized.
99       */
100     public void dragGestureRecognized(DragGestureEvent event)
101     {
102 	File files[] = fp.getFiles();
103 	
104 	if (files == null || files.length == 0)
105 	    return;
106 	
107 	TransferableFile t = new TransferableFile(files);
108 
109 	try {
110 	    dragSource.startDrag(event, DragSource.DefaultMoveDrop, t, this);
111 	}
112 	catch (InvalidDnDOperationException ie) {
113 	    logger.debug("drag failed", ie);
114 	}
115     }
116 }