Java Example Program/ Sample Source Code
import java.awt.*;
import javax.swing.*;
import java.awt.datatransfer.*;
import javax.swing.filechooser.*;
import java.io.*;
import java.awt.dnd.*;
import java.util.List;
import java.util.ArrayList;
import java.awt.image.*;
public class DropActionChanged_DragSourceAdapter {
/* Swing Hacks
* Tips and Tools for Killer GUIs
* By Joshua Marinacci, Chris Adamson
* First Edition June 2005
* Series: Hacks
* ISBN: 0-596-00907-0
* http://www.oreilly.com/catalog/swinghks/
*/
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("Drag and Drop File Hack");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FileSystemView fsv = FileSystemView.getFileSystemView();
Icon icon = fsv.getSystemIcon(File.createTempFile("myfile.", ".txt"));
System.out.println("icon = " + icon);
// ImageIcon iicn = (ImageIcon)icon;
frame.getContentPane().setLayout(new BorderLayout());
JTextArea text = new JTextArea();
JLabel label = new JLabel("myfile.txt", icon, SwingConstants.CENTER);
DragSource ds = DragSource.getDefaultDragSource();
ds.createDefaultDragGestureRecognizer(
label, DnDConstants.ACTION_MOVE, new FileDragGestureListener(
text));
frame.getContentPane().add("North", label);
frame.getContentPane().add("Center", text);
frame.pack();
frame.setSize(400, 300);
frame.setVisible(true);
}
}
class FileDragGestureListener extends DragSourceAdapter implements
DragGestureListener {
JTextArea text;
public FileDragGestureListener(JTextArea text) {
this.text = text;
}
Cursor cursor;
public void dragGestureRecognized(DragGestureEvent evt) {
try {
// generate the temp file
File temp_dir = File.createTempFile("tempdir", ".dir", null);
File temp = new File(temp_dir.getParent(), "myfile.txt");
FileOutputStream out = new FileOutputStream(temp);
out.write(text.getText().getBytes());
out.close();
// get the right icon
FileSystemView fsv = FileSystemView.getFileSystemView();
Icon icn = fsv.getSystemIcon(temp);
// we could cast to an image icon, but it might not be one.
// painting to a buffer first also solves the problem of passing in
// the
// the right sized buffer because the cursor might scale it
// convert to the right sized image
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getBestCursorSize(icn.getIconWidth(),
icn.getIconHeight());
BufferedImage buff = new BufferedImage(dim.width, dim.height,
BufferedImage.TYPE_INT_ARGB);
icn.paintIcon(text, buff.getGraphics(), 0, 0);
// set up drag image
if (DragSource.isDragImageSupported()) {
evt.startDrag(DragSource.DefaultCopyDrop, buff,
new Point(0, 0), new TextFileTransferable(temp), this);
} else {
cursor = tk.createCustomCursor(buff, new Point(0, 0),
"billybob");
evt.startDrag(cursor, null, new Point(0, 0),
new TextFileTransferable(temp), this);
}
} catch (IOException ex) {
System.out.println("exception: " + ex.getMessage());
}
}
public void dragEnter(DragSourceDragEvent evt) {
DragSourceContext ctx = evt.getDragSourceContext();
ctx.setCursor(cursor);
}
public void dragExit(DragSourceEvent evt) {
DragSourceContext ctx = evt.getDragSourceContext();
ctx.setCursor(DragSource.DefaultCopyNoDrop);
}
public void dragOver(DragSourceDragEvent evt) {
System.out.println("dragOver fired");
}
public void dragDropEnd(DragSourceDropEvent evt) {
System.out.println("dragDropEnd fired");
}
public void dragMouseMoved(DragSourceDragEvent evt) {
System.out.println("dragMouseMoved fired");
}
public void dropActionChanged(DragSourceDragEvent evt) {
System.out.println("dropActionChanged fired");
}
public static void p(String str) {
System.out.println(str);
}
}
// create a transferable for the right data flavor
class TextFileTransferable implements Transferable {
File temp;
public TextFileTransferable(File temp) throws IOException {
this.temp = temp;
}
public Object getTransferData(DataFlavor flavor) {
p("get trans data called");
List<File> list = new ArrayList<File>();
list.add(temp);
return list;
}
public DataFlavor[] getTransferDataFlavors() {
DataFlavor[] df = new DataFlavor[1];
df[0] = DataFlavor.javaFileListFlavor;
return df;
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
if (flavor == DataFlavor.javaFileListFlavor) {
return true;
}
return false;
}
public static void p(String str) {
System.out.println(str);
}
} |
|
|