Skip to content

Commit

Permalink
Better Drag and Drop support for DLFS Browser
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Apr 16, 2024
1 parent d3e2784 commit d651af1
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 81 deletions.
21 changes: 21 additions & 0 deletions convex-gui/src/main/java/convex/gui/dlfs/BrowserUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import java.nio.file.Path;
import java.util.List;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.filechooser.FileSystemView;

public class BrowserUtils {

Expand All @@ -26,4 +29,22 @@ protected static void copyFiles(JComponent parent, List<File> files, Path target
}
}

protected static Icon getFileIcon(Path path) {
String iconName= "FileView.fileIcon";
Icon icon =null;
if (Files.isDirectory(path)) {
iconName="FileView.directoryIcon";
}

if (path.getNameCount()==0) {
icon = UIManager.getIcon("FileView.hardDriveIcon"); // root icon
} else try {
icon= FileSystemView.getFileSystemView().getSystemIcon( path.toFile() );
} catch (Exception e) {
// ignore
}
if (icon==null) icon = UIManager.getIcon(iconName);
return icon;
}

}
78 changes: 13 additions & 65 deletions convex-gui/src/main/java/convex/gui/dlfs/DLFSPanel.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package convex.gui.dlfs;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.nio.file.Path;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.tree.TreePath;

import convex.dlfs.DLFileSystem;
import convex.dlfs.DLPath;
Expand Down Expand Up @@ -45,7 +38,12 @@ public DLFSPanel(DLFileSystem dlfs) {

directoryTree = new DirectoryTree(dlfs);
// treeView.setBackground(Color.BLACK);
directoryTree.setTransferHandler(new DropTransferHandler());
directoryTree.setTransferHandler(new DnDTransferHandler(this) {
@Override
protected Path getTargetPath() {
return getSelectedPath();
}
});

directoryTree.addTreeSelectionListener(e->{
DirectoryTree.Node node=(DirectoryTree.Node)directoryTree.getLastSelectedPathComponent();
Expand All @@ -58,7 +56,12 @@ public DLFSPanel(DLFileSystem dlfs) {
});

fileList=new FileList(selectedPath);
fileList.setTransferHandler(new DropTransferHandler());
fileList.setTransferHandler(new DnDTransferHandler(this) {
@Override
protected Path getTargetPath() {
return getSelectedPath();
}
});
JScrollPane listScrollPane=new JScrollPane(fileList);

JSplitPane splitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,new JScrollPane(directoryTree), listScrollPane);
Expand All @@ -72,7 +75,7 @@ public DLFSPanel(DLFileSystem dlfs) {
directoryTree.setSelectionPath(directoryTree.getPathForRow(0));
}

private void setSelectedPath(Path newPath) {
void setSelectedPath(Path newPath) {
if (!(newPath instanceof DLPath)) {
pathLabel.setText("No path selected");
selectedPath=null;
Expand All @@ -88,61 +91,6 @@ private void setSelectedPath(Path newPath) {
pathLabel.setText(newPath.toUri().toString());
}


public class DropTransferHandler extends TransferHandler {

public DropTransferHandler() {

}
@Override
public boolean canImport(TransferSupport support) {
if (!support.isDrop()) {
return false;
}
return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
}

@Override
public int getSourceActions(JComponent c) {
//PropertyDescriptor prop = getPropertyDescriptor(c);
return COPY;

}

@SuppressWarnings("unchecked")
public boolean importData(TransferSupport support) {
if (!canImport(support)) return false;

Transferable tf =support.getTransferable();
List<File> files;
Path targetDir=getSelectedPath();
System.out.println("Dropping to: "+targetDir);

DropLocation dropLocation = support.getDropLocation();
if (dropLocation instanceof JTree.DropLocation) {
JTree.DropLocation dl =(JTree.DropLocation)dropLocation;
TreePath treePath = dl.getPath();
if (treePath!=null) {
DirectoryTree.Node parent =(DirectoryTree.Node)treePath.getLastPathComponent();
System.out.println("Parent: "+parent);
targetDir=parent.getFilePath();
}
}

try {
files=(List<File>)(tf.getTransferData(DataFlavor.javaFileListFlavor));
if ((files==null)||(files.isEmpty())) return false;
BrowserUtils.copyFiles(DLFSPanel.this, files, targetDir);
setSelectedPath(getSelectedPath());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}


public DLPath getSelectedPath() {
return selectedPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import java.awt.Color;
import java.awt.Component;
import java.nio.file.Files;
import java.nio.file.Path;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
Expand Down Expand Up @@ -40,19 +38,7 @@ public Component getTreeCellRendererComponent(JTree tree, Object value, boolean
Path name=path.getFileName();
setText((name==null)?"DLFS Root":name.toString());

String iconName= "FileView.fileIcon";
if (Files.isDirectory(path)) {
iconName=expanded?"FileView.directoryIcon": "FileView.directoryIcon";
}

Icon icon;
if (name==null) {
icon = UIManager.getIcon("FileView.hardDriveIcon"); // root icon
} else try {
icon= FileSystemView.getFileSystemView().getSystemIcon( name.toFile() );
} catch (Exception e) {
icon = UIManager.getIcon(iconName);
}
Icon icon = BrowserUtils.getFileIcon(path);

setIcon(icon);
return this;
Expand Down
74 changes: 74 additions & 0 deletions convex-gui/src/main/java/convex/gui/dlfs/DnDTransferHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package convex.gui.dlfs;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.nio.file.Path;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.tree.TreePath;

@SuppressWarnings("serial")
public abstract class DnDTransferHandler extends TransferHandler {

/**
*
*/
private final DLFSPanel dlfsPanel;

public DnDTransferHandler(DLFSPanel dlfsPanel) {
this.dlfsPanel = dlfsPanel;

}
@Override
public boolean canImport(TransferSupport support) {
if (!support.isDrop()) {
return false;
}
return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
}

@Override
public int getSourceActions(JComponent c) {
//PropertyDescriptor prop = getPropertyDescriptor(c);
return COPY;

}

@SuppressWarnings("unchecked")
public boolean importData(TransferSupport support) {
if (!canImport(support)) return false;

Transferable tf =support.getTransferable();
List<File> files;
Path targetDir=getTargetPath();

DropLocation dropLocation = support.getDropLocation();
if (dropLocation instanceof JTree.DropLocation) {
JTree.DropLocation dl =(JTree.DropLocation)dropLocation;
TreePath treePath = dl.getPath();
if (treePath!=null) {
DirectoryTree.Node parent =(DirectoryTree.Node)treePath.getLastPathComponent();
System.out.println("Parent: "+parent);
targetDir=parent.getFilePath();
}
}
System.out.println("Dropping to: "+targetDir);

try {
files=(List<File>)(tf.getTransferData(DataFlavor.javaFileListFlavor));
if ((files==null)||(files.isEmpty())) return false;
BrowserUtils.copyFiles(this.dlfsPanel, files, targetDir);
this.dlfsPanel.setSelectedPath(this.dlfsPanel.getSelectedPath());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}

protected abstract Path getTargetPath();
}
23 changes: 22 additions & 1 deletion convex-gui/src/main/java/convex/gui/dlfs/FileList.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.nio.file.Files;
import java.nio.file.Path;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JList;

Expand All @@ -13,12 +14,32 @@
@SuppressWarnings("serial")
public class FileList extends JList<Path> {

public class Renderer extends DefaultListCellRenderer {
@Override public Renderer getListCellRendererComponent(
JList<?> list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

if (value instanceof Path) {
Path p=(Path)value;
setIcon(BrowserUtils.getFileIcon(p));
setText(p.getFileName().toString());
}
return this;
}
}

private Path directory;
DefaultListModel<Path> model;

public FileList(Path initialDir) {
this.directory=initialDir;
model=new DefaultListModel<Path>();
setCellRenderer(new Renderer());
this.setDragEnabled(true);
setModel(model);
}

Expand All @@ -34,7 +55,7 @@ public void setDirectory(Path newPath) {
if (!(directory instanceof DLPath)) return;
try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory)) {
for (Path path : stream) {
model.addElement(path.getFileName());
model.addElement(path);
}
} catch (IOException e) {
// TODO Auto-generated catch block
Expand Down

0 comments on commit d651af1

Please sign in to comment.