DnD sur JTree avec des fichiers
Bonjour,
Je cherche à créer un JTree à partir de fichiers préalablement sélectionnés.
Si possible, je souhaite pouvoir ajouter des fichiers de l'explorateur vers mon JTree aussi.
Voici un bout de ma classe contenant l'interface graphique :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class MainWindow extends javax.swing.JFrame implements ActionListener,Autoscroll {
private final JTree treeFiles;
private final model.FileDataModel fdm;
mod.FileTreeDragSource ds;
mod.FileTreeDropTarget dt;
public MainWindow() {
fdm=new model.FileDataModel(files);
treeFiles=new JTree(fdm);
treeFiles.setDragEnabled(true);
treeFiles.getSelectionModel().setSelectionMode(javax.swing.tree.TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
treeFiles.setRootVisible(false);
treeFiles.setCellRenderer(fileRenderer);
ds = new mod.FileTreeDragSource(treeFiles, DnDConstants.ACTION_COPY_OR_MOVE);
dt = new mod.FileTreeDropTarget(treeFiles);
} |
Mon implémentation de transférable :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public class FileTransferableTreeNode implements Transferable {
public static DataFlavor FILE_TREE_PATH_FLAVOR = new DataFlavor(model.ExtendedFile.class,
"Tree Path");
DataFlavor flavors[] = { FILE_TREE_PATH_FLAVOR };
model.ExtendedFile file;
public FileTransferableTreeNode(model.ExtendedFile ef) {
file = ef;
}
@Override
public synchronized Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) {
return (Object) file;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return flavors;
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return (flavor.getRepresentationClass() == model.ExtendedFile.class);
}
} |
L'objet dans l'arbre :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class ExtendedFile extends File {
private static final long serialVersionUID = -4954098568310603926L;
private ExtendedFile reference=null;
public final Vector<model.ExtendedFile> childs=new Vector<model.ExtendedFile>();
public ExtendedFile(String pathname) {
super(pathname);
}
public ExtendedFile(File f) {
super(f.getAbsolutePath());
}
public String toString() {
return getName();
}
public ExtendedFile getReference() {return reference;}
public boolean isLeaf() {
return childs.size()==0;
}
} |
Un bout de mon DragSource :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class FileTreeDragSource implements DragSourceListener, DragGestureListener {
DragSource source = new DragSource();
DragGestureRecognizer recognizer;
FileTransferableTreeNode transferable;
model.ExtendedFile oldNode;
JTree sourceTree;
@Override
public void dragGestureRecognized(DragGestureEvent dge) {
TreePath path = sourceTree.getSelectionPath();
if ((path == null) || (path.getPathCount() <= 1)) {
return;
}
oldNode = (model.ExtendedFile) path.getLastPathComponent();
transferable = new FileTransferableTreeNode(path); //PROBLEME ICI
source.startDrag(dge, DragSource.DefaultMoveNoDrop, transferable, this);
}
} |
Ma classe FileTreeDropTarget qui implémente DropTargetListener ne semble pas avoir de problème pour le moment car j'ai une exception quand je commence à "dragger" ;)
InvalidDnDOperationException je crois