001: package com.opensymphony.workflow.designer.swing;
002:
003: import java.awt.*;
004: import java.awt.datatransfer.DataFlavor;
005: import java.awt.datatransfer.Transferable;
006: import java.awt.datatransfer.UnsupportedFlavorException;
007: import java.awt.dnd.*;
008: import java.awt.event.HierarchyEvent;
009: import java.awt.event.HierarchyListener;
010: import java.io.File;
011: import java.io.IOException;
012: import java.util.List;
013: import java.util.TooManyListenersException;
014: import javax.swing.*;
015:
016: public abstract class FileDropHandler {
017: private transient java.awt.dnd.DropTargetListener dropListener;
018:
019: public FileDropHandler(Component target) {
020: this (target, true);
021: }
022:
023: public FileDropHandler(final Component target,
024: final boolean recursive) {
025: dropListener = new DropTargetListener() {
026: public void dragEnter(DropTargetDragEvent evt) {
027: if (isDragOk(evt)) {
028: evt.acceptDrag(DnDConstants.ACTION_COPY);
029: } else {
030: evt.rejectDrag();
031: }
032: }
033:
034: public void dragOver(DropTargetDragEvent evt) {
035: }
036:
037: public void drop(DropTargetDropEvent evt) {
038: try {
039: Transferable tr = evt.getTransferable();
040:
041: if (tr
042: .isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
043: evt.acceptDrop(DnDConstants.ACTION_COPY);
044:
045: List fileList = (List) tr
046: .getTransferData(DataFlavor.javaFileListFlavor);
047:
048: File[] filesTemp = new File[fileList.size()];
049: fileList.toArray(filesTemp);
050: final File[] files = filesTemp;
051:
052: filesDropped(files);
053:
054: evt.getDropTargetContext().dropComplete(true);
055: } else {
056: evt.rejectDrop();
057: }
058: } catch (IOException io) {
059: io.printStackTrace();
060: evt.rejectDrop();
061: } catch (UnsupportedFlavorException ufe) {
062: ufe.printStackTrace();
063: evt.rejectDrop();
064: }
065: }
066:
067: public void dragExit(DropTargetEvent evt) {
068: }
069:
070: public void dropActionChanged(DropTargetDragEvent evt) {
071: if (isDragOk(evt)) {
072: evt.acceptDrag(DnDConstants.ACTION_COPY);
073: } else {
074: evt.rejectDrag();
075: }
076: }
077: };
078:
079: makeDropTarget(target, recursive);
080: }
081:
082: private void makeDropTarget(final Component target,
083: boolean recursive) {
084: final DropTarget dt = new DropTarget();
085: try {
086: dt.addDropTargetListener(dropListener);
087: } catch (TooManyListenersException e) {
088: e.printStackTrace();
089: }
090:
091: target.addHierarchyListener(new HierarchyListener() {
092: public void hierarchyChanged(HierarchyEvent evt) {
093: Component parent = target.getParent();
094: if (parent == null) {
095: target.setDropTarget(null);
096: } else {
097: new DropTarget(target, dropListener);
098: }
099: }
100: });
101: if (target.getParent() != null)
102: new DropTarget(target, dropListener);
103:
104: if (recursive && (target instanceof Container)) {
105: Container cont = (Container) target;
106: Component[] comps = cont.getComponents();
107:
108: for (int i = 0; i < comps.length; i++)
109: makeDropTarget(comps[i], recursive);
110: }
111: }
112:
113: private boolean isDragOk(final DropTargetDragEvent evt) {
114: boolean ok = false;
115:
116: DataFlavor[] flavors = evt.getCurrentDataFlavors();
117: int i = 0;
118: while (!ok && i < flavors.length) {
119: if (flavors[i].equals(DataFlavor.javaFileListFlavor))
120: ok = true;
121: i++;
122: }
123: return ok;
124: }
125:
126: public abstract void filesDropped(File[] files);
127:
128: public static void main(String[] args) {
129: JFrame frame = new JFrame("FileDrop");
130: final JTree tree = new JTree();
131: frame.getContentPane().add(new JScrollPane(tree),
132: BorderLayout.CENTER);
133:
134: new FileDropHandler(tree) {
135: public void filesDropped(File[] files) {
136: for (int i = 0; i < files.length; i++) {
137: System.out.println("dropped " + files[i]);
138: }
139: }
140: };
141:
142: frame.setBounds(100, 100, 300, 400);
143: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
144: frame.show();
145: }
146: }
|