Skip to content

Commit

Permalink
Be able to drag playlist folders into Playlists-Directories panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Feb 25, 2023
1 parent 9f69797 commit 6e60a1d
Showing 1 changed file with 57 additions and 27 deletions.
84 changes: 57 additions & 27 deletions src/main/java/listfix/view/GUIScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ private void handleMouseEvent(MouseEvent e)
_miOpenSelectedPlaylists.setEnabled(true);
_miDeletePlaylist.setEnabled(true);
_miRenameSelectedItem.setEnabled(_playlistDirectoryTree.getSelectionCount() == 1
&& !allTopLevel
&& Files.isRegularFile(GUIScreen.this.getSelectedPlaylistTreeNodes().get(0).getUserObject())
);
&& !allTopLevel
&& Files.isRegularFile(GUIScreen.this.getSelectedPlaylistTreeNodes().get(0).getUserObject())
);
}
else
{
Expand Down Expand Up @@ -365,14 +365,38 @@ private TransferHandler createPlaylistTreeTransferHandler()
return new TransferHandler()
{
@Override
public boolean canImport(TransferHandler.TransferSupport info)
public boolean canImport(JComponent comp, DataFlavor[] transferFlavors)
{
return false;
return Arrays.stream(transferFlavors).anyMatch(DataFlavor.javaFileListFlavor :: equals);
}

/**
* 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
*/
@Override
public boolean importData(TransferHandler.TransferSupport info)
public boolean importData(TransferHandler.TransferSupport support)
{
final Transferable transferable = support.getTransferable();
if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
{
try
{
List<File> fileList = (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor);
return fileList.stream()
.filter(File :: isDirectory)
.map(folder -> {
GUIScreen.this.addPlaylist(folder);
return true;
})
.reduce(false, (t, v) -> true);
}
catch (Exception e)
{
_logger.error("Dragging onto playlists directory failed", e);
}
}
return false;
}

Expand Down Expand Up @@ -1332,7 +1356,8 @@ private void deleteTreeSelectedPlaylists()
public List<PlaylistTreeNode> getSelectedPlaylistTreeNodes()
{
int[] selRows = _playlistDirectoryTree.getSelectionRows();
if (selRows != null) {
if (selRows != null)
{
return Arrays.stream(selRows).mapToObj(i -> (PlaylistTreeNode) _playlistDirectoryTree.getPathForRow(i).getLastPathComponent()).collect(Collectors.toList());
}
return Collections.emptyList();
Expand All @@ -1341,7 +1366,8 @@ public List<PlaylistTreeNode> getSelectedPlaylistTreeNodes()
private void renameTreeSelectedNode()
{
List<PlaylistTreeNode> selectedTreeNodes = getSelectedPlaylistTreeNodes();
if (selectedTreeNodes.size() != 1) {
if (selectedTreeNodes.size() != 1)
{
_logger.debug(String.format("Will only rename when exactly 1 file is selected, got %s.", selectedTreeNodes.size()));
return;
}
Expand Down Expand Up @@ -2448,25 +2474,7 @@ private void _btnAddPlaylistsDirActionPerformed(java.awt.event.ActionEvent evt)/
int response = _jMediaDirChooser.showOpenDialog(this);
if (response == JFileChooser.APPROVE_OPTION)
{
String path = _jMediaDirChooser.getSelectedFile().getPath();
if (new File(path).exists())
{
_logger.info(String.format("Add playlist directory to configuration: %s", path));
this.getApplicationConfig().getPlaylistDirectories().add(path);
}
else
{
JOptionPane.showMessageDialog(this, new JTransparentTextArea("The directory you selected/entered does not exist."));
}
try
{
this._listFixController.getApplicationConfiguration().write();
}
catch (IOException e)
{
throw new RuntimeException("Failed to write application configuration", e);
}
this.updatePlaylistDirectoryPanel();
this.addPlaylist(_jMediaDirChooser.getSelectedFile().getAbsoluteFile());
}
}//GEN-LAST:event__btnSetPlaylistsDirActionPerformed

Expand Down Expand Up @@ -2803,6 +2811,28 @@ public static void main(String[] args) throws IOException, InterruptedException,
}
}

private void addPlaylist(File playlistFile)
{
if (playlistFile.exists())
{
_logger.info(String.format("Add playlist directory to configuration: %s", playlistFile));
this.getApplicationConfig().getPlaylistDirectories().add(playlistFile.getAbsolutePath());
}
else
{
JOptionPane.showMessageDialog(this, new JTransparentTextArea("The directory you selected/entered does not exist."));
}
try
{
this._listFixController.getApplicationConfiguration().write();
}
catch (IOException e)
{
throw new RuntimeException("Failed to write application configuration", e);
}
this.updatePlaylistDirectoryPanel();
}

// <editor-fold defaultstate="collapsed" desc="Generated Code">
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenuItem _aboutMenuItem;
Expand Down

0 comments on commit 6e60a1d

Please sign in to comment.