Skip to content

Commit

Permalink
Fix dragging playlist from playlists-directory panel into playlist ed…
Browse files Browse the repository at this point in the history
…itor
  • Loading branch information
Borewit committed Mar 27, 2023
1 parent 5736f07 commit c69775a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
Empty file.
45 changes: 45 additions & 0 deletions src/main/java/listfix/view/FileListTransferable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package listfix.view;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* Converts a List of Files into a <code>java.awt.datatransfer.Transferable</code> of flavour <code>application/x-java-file-list;class=java.util.List</code>
* @author Borewit
* @see DataFlavor#javaFileListFlavor
*/
public class FileListTransferable implements Transferable
{
private List<File> fileList;
private DataFlavor[] dataFlavors;

public FileListTransferable(List<File> fileList)
{
this.fileList = Collections.unmodifiableList(fileList);
this.dataFlavors = new DataFlavor[] {DataFlavor.javaFileListFlavor};
}

@Override
public DataFlavor[] getTransferDataFlavors()
{
return dataFlavors;
}

@Override
public boolean isDataFlavorSupported(DataFlavor flavor)
{
return Arrays.stream(this.dataFlavors).anyMatch(f -> f.equals(flavor));
}

@Override
public List<File> getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException
{
return this.fileList;
}
}
25 changes: 17 additions & 8 deletions src/main/java/listfix/view/PlaylistFolderTransferHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import java.util.List;
import java.util.stream.Collectors;

/**
* Used to drag playlist from playlists-directories pane to editor
*/
public class PlaylistFolderTransferHandler extends TransferHandler
{
private final IListFixGui listFixGui;
Expand All @@ -34,6 +37,7 @@ public boolean canImport(JComponent comp, DataFlavor[] transferFlavors)

/**
* Causes a transfer to occur from a clipboard or a drag and drop operation.
*
* @param support the object containing the details of the transfer, not <code>null</code>.
* @return true if the data was inserted into the component, false otherwise
*/
Expand Down Expand Up @@ -71,14 +75,19 @@ public int getSourceActions(JComponent c)
@Override
protected Transferable createTransferable(JComponent c)
{
int[] rows = playlistDirectoryTree.getSelectionRows();
if (rows == null)
return null;
List<File> fileList = Arrays.stream(rows).mapToObj(selRow -> {
TreePath selPath = playlistDirectoryTree.getPathForRow(selRow);
return FileTreeNodeGenerator.treePathToFileSystemPath(selPath).toFile();
}).collect(Collectors.toList());
if (c instanceof JTree)
{
int[] rows = playlistDirectoryTree.getSelectionRows();
if (rows == null)
return null;
List<File> fileList = Arrays.stream(rows).mapToObj(selRow -> {
TreePath selPath = playlistDirectoryTree.getPathForRow(selRow);
return FileTreeNodeGenerator.treePathToFileSystemPath(selPath).toFile();
}).collect(Collectors.toList());

return null; // ToDO?
return new FileListTransferable(fileList);
}
_logger.warn(String.format("JComponent type not supported for transfer %s", c.getClass().getName()));
return null;
}
}

0 comments on commit c69775a

Please sign in to comment.