| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 
 |  
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.SystemColor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
 
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.ExpandVetoException;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
 
public class DeviceExplorer extends JTree implements DragSourceListener,DragGestureListener,TreeWillExpandListener {
 
	boolean newDrag=false;
	private Rectangle2D 	_raGhost		= new Rectangle2D.Float();	
	BufferedImage imgGhost;					
	Point ptOffset = new Point();
 
 
	public DeviceExplorer() {
	DragSource dragSource = DragSource.getDefaultDragSource();
		dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE,this);	
	}
 
	public void dragGestureRecognized(DragGestureEvent e){
		Point ptDragOrigin = e.getDragOrigin();
		TreePath path = getPathForLocation(ptDragOrigin.x, ptDragOrigin.y);
		newDrag=true;
		if (path == null)
			return;
		if (isRootPath(path))
			return;	//On ne peut pas drag le root
 
		Rectangle raPath = getPathBounds(path);
		ptOffset.setLocation(ptDragOrigin.x-raPath.x, ptDragOrigin.y-raPath.y);
 
		//On recupére l'image du dossier ou du fichier qu'on drag
		JLabel lbl = (JLabel) getCellRenderer().getTreeCellRendererComponent(
									this, 											
									path.getLastPathComponent(),					
									false,											
									isExpanded(path), 								
									getModel().isLeaf(path.getLastPathComponent()), 
									0, 												
									false								
								);
		lbl.setSize((int)raPath.getWidth(), (int)raPath.getHeight());
 
		imgGhost = new BufferedImage((int)raPath.getWidth(), (int)raPath.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
		Graphics2D g2 = imgGhost.createGraphics();
 
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 0.5f));
		lbl.paint(g2);
 
		Icon icon = lbl.getIcon();
		int nStartOfText = (icon == null) ? 0 : icon.getIconWidth()+lbl.getIconTextGap();
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_OVER, 0.5f));	
		g2.setPaint(new GradientPaint(nStartOfText,	0, SystemColor.controlShadow, getWidth(),	0, new Color(255,255,255,0)));
		g2.fillRect(nStartOfText, 0, getWidth(), imgGhost.getHeight());
 
		g2.dispose();
 
		setSelectionPath(path);	
		System.out.println("DRAGGING: "+path.getLastPathComponent());
 
		Transferable transferable = new CTransferableTreePath(path);		
		e.startDrag(null, imgGhost, new Point(5,5), transferable, this);
	}	
 
	public void dragEnter(DragSourceDragEvent e){}	
 
	public void dragOver(DragSourceDragEvent e){
		Point pt = e.getLocation();
 
		if(!pt.equals(lastpt){ // Si la souris a bougé on redessiné l'image du drag
			lastpt=pt;
			Graphics2D g2 = (Graphics2D) getGraphics();
			if (!DragSource.isDragImageSupported()){
				paintImmediately(_raGhost.getBounds());		
				_raGhost.setRect( (pt.x), (pt.y),imgGhost.getWidth(), imgGhost.getHeight());
				g2.drawImage(imgGhost, AffineTransform.getTranslateInstance(_raGhost.getX(), _raGhost.getY()), null);	
			}
		}
	}	
	public void dragExit(DragSourceEvent e){}	
	public void dropActionChanged(DragSourceDragEvent e){}	
	public void dragDropEnd(DragSourceDropEvent e){} | 
Partager