Skip to content

Commit 6a02f42

Browse files
committed
Be able to drag new media directories into the media-list
1 parent ab3af46 commit 6a02f42

File tree

3 files changed

+149
-167
lines changed

3 files changed

+149
-167
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package listfix.view;
2+
3+
import org.apache.logging.log4j.LogManager;
4+
import org.apache.logging.log4j.Logger;
5+
6+
import javax.swing.*;
7+
import java.awt.datatransfer.DataFlavor;
8+
import java.awt.datatransfer.Transferable;
9+
import java.io.File;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
public abstract class FileListTransferHandler extends TransferHandler
14+
{
15+
16+
private final Logger _logger = LogManager.getLogger(FileListTransferHandler.class);
17+
18+
@Override
19+
public boolean canImport(JComponent comp, DataFlavor[] transferFlavors)
20+
{
21+
return Arrays.stream(transferFlavors).anyMatch(DataFlavor.javaFileListFlavor::equals);
22+
}
23+
24+
/**
25+
* Causes a transfer to occur from a clipboard or a drag and drop operation.
26+
*
27+
* @param support the object containing the details of the transfer, not <code>null</code>.
28+
* @return true if the data was inserted into the component, false otherwise
29+
*/
30+
@Override
31+
public boolean importData(TransferSupport support)
32+
{
33+
final Transferable transferable = support.getTransferable();
34+
if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
35+
{
36+
try
37+
{
38+
handleFileList((List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor));
39+
}
40+
catch (Exception e)
41+
{
42+
_logger.error("Transfer Java-file-list failed", e);
43+
}
44+
}
45+
return false;
46+
}
47+
48+
/**
49+
* Called when a Java-File-List is transferred
50+
* @param fileList Transferred Java-File-List
51+
*/
52+
public abstract boolean handleFileList(List<File> fileList);
53+
}

src/main/java/listfix/view/GUIScreen.java

Lines changed: 96 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,20 @@ private void postInitComponents()
150150
initPlaylistListener();
151151

152152
// drag-n-drop support for the playlist directory tree
153-
_playlistDirectoryTree.setTransferHandler(new PlaylistFolderTransferHandler(_playlistDirectoryTree, this));
153+
_playlistDirectoryTree.setTransferHandler(new FileListTransferHandler()
154+
{
155+
@Override
156+
public boolean handleFileList(List<File> fileList)
157+
{
158+
return fileList.stream()
159+
.filter(File::isDirectory)
160+
.map(folder -> {
161+
GUIScreen.this.addPlaylistFolder(folder);
162+
return true;
163+
})
164+
.reduce(false, (t, v) -> true);
165+
}
166+
});
154167
_playlistDirectoryTree.setRootVisible(false);
155168
// Show tooltips
156169
ToolTipManager.sharedInstance().registerComponent(_playlistDirectoryTree);
@@ -638,13 +651,22 @@ public void windowClosing(WindowEvent evt)
638651

639652
_mediaLibraryPanel.add(_mediaLibraryButtonPanel, BorderLayout.SOUTH);
640653

641-
_mediaLibraryScrollPane.setMaximumSize(null);
642-
_mediaLibraryScrollPane.setMinimumSize(null);
643-
644654
_lstMediaLibraryDirs.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
645-
_lstMediaLibraryDirs.setMaximumSize(null);
646-
_lstMediaLibraryDirs.setMinimumSize(null);
647-
_lstMediaLibraryDirs.setPreferredSize(null);
655+
_lstMediaLibraryDirs.setTransferHandler(new FileListTransferHandler()
656+
{
657+
@Override
658+
public boolean handleFileList(List<File> fileList)
659+
{
660+
return fileList.stream()
661+
.filter(File::isDirectory)
662+
.map(folder -> {
663+
GUIScreen.this.addMediaFolder(folder);
664+
return true;
665+
})
666+
.reduce(false, (t, v) -> true);
667+
}
668+
});
669+
648670
_mediaLibraryScrollPane.setViewportView(_lstMediaLibraryDirs);
649671

650672
_mediaLibraryPanel.add(_mediaLibraryScrollPane, BorderLayout.CENTER);
@@ -656,13 +678,7 @@ public void windowClosing(WindowEvent evt)
656678
_playlistDirectoryPanel.setAlignmentY(0.0F);
657679
_playlistDirectoryPanel.setLayout(new BorderLayout());
658680

659-
_treeScrollPane.setMaximumSize(null);
660-
_treeScrollPane.setMinimumSize(null);
661-
662681
_playlistDirectoryTree.setDragEnabled(true);
663-
_playlistDirectoryTree.setMaximumSize(null);
664-
_playlistDirectoryTree.setMinimumSize(null);
665-
_playlistDirectoryTree.setPreferredSize(null);
666682
_playlistDirectoryTree.addKeyListener(new KeyAdapter()
667683
{
668684
@Override
@@ -1877,82 +1893,88 @@ private void _addMediaDirButtonActionPerformed()
18771893
int response = _jMediaDirChooser.showOpenDialog(this);
18781894
if (response == JFileChooser.APPROVE_OPTION)
18791895
{
1880-
try
1896+
this.addMediaFolder(_jMediaDirChooser.getSelectedFile());
1897+
}
1898+
else
1899+
{
1900+
_jMediaDirChooser.cancelSelection();
1901+
}
1902+
updateMediaDirButtons();
1903+
}
1904+
1905+
private void addMediaFolder(File mediaFolderToAdd)
1906+
{
1907+
try
1908+
{
1909+
UNCFile mediaDir = new UNCFile(mediaFolderToAdd);
1910+
if (getApplicationConfig().getAlwaysUseUNCPaths())
18811911
{
1882-
UNCFile mediaDir = new UNCFile(_jMediaDirChooser.getSelectedFile());
1883-
if (getApplicationConfig().getAlwaysUseUNCPaths())
1912+
if (mediaDir.onNetworkDrive())
18841913
{
1885-
if (mediaDir.onNetworkDrive())
1886-
{
1887-
mediaDir = new UNCFile(mediaDir.getUNCPath());
1888-
}
1914+
mediaDir = new UNCFile(mediaDir.getUNCPath());
18891915
}
1890-
final String dir = mediaDir.getPath();
1916+
}
1917+
final String dir = mediaDir.getPath();
18911918

1892-
// first let's see if this is a subdirectory of any of the media directories already in the list, and error out if so...
1893-
if (ArrayFunctions.containsStringPrefixingAnotherString(_listFixController.getMediaLibrary().getMediaDirectories(), dir, !ListFixController.FILE_SYSTEM_IS_CASE_SENSITIVE))
1894-
{
1895-
JOptionPane.showMessageDialog(this, new JTransparentTextArea("The directory you attempted to add is a subdirectory of one already in your media library, no change was made."),
1896-
"Reminder", JOptionPane.INFORMATION_MESSAGE);
1897-
return;
1898-
}
1899-
else
1919+
// first let's see if this is a subdirectory of any of the media directories already in the list, and error out if so...
1920+
if (ArrayFunctions.containsStringPrefixingAnotherString(_listFixController.getMediaLibrary().getMediaDirectories(), dir, !ListFixController.FILE_SYSTEM_IS_CASE_SENSITIVE))
1921+
{
1922+
JOptionPane.showMessageDialog(this, new JTransparentTextArea("The directory you attempted to add is a subdirectory of one already in your media library, no change was made."),
1923+
"Reminder", JOptionPane.INFORMATION_MESSAGE);
1924+
return;
1925+
}
1926+
else
1927+
{
1928+
// Now check if any of the media directories is a subdirectory of the one we're adding and remove the media directory if so.
1929+
int matchCount = 0;
1930+
for (String dirToCheck : _listFixController.getMediaLibrary().getMediaDirectories())
19001931
{
1901-
// Now check if any of the media directories is a subdirectory of the one we're adding and remove the media directory if so.
1902-
int matchCount = 0;
1903-
for (String dirToCheck : _listFixController.getMediaLibrary().getMediaDirectories())
1932+
if (dirToCheck.startsWith(dir))
19041933
{
1905-
if (dirToCheck.startsWith(dir))
1934+
// Only showing the message the first time we find this condition...
1935+
if (matchCount == 0)
19061936
{
1907-
// Only showing the message the first time we find this condition...
1908-
if (matchCount == 0)
1909-
{
1910-
JOptionPane.showMessageDialog(this,
1911-
new JTransparentTextArea("One or more of your existing media directories is a subdirectory of the directory you just added. These directories will be removed from your list automatically."),
1912-
"Reminder", JOptionPane.INFORMATION_MESSAGE);
1913-
}
1914-
removeMediaDir(dirToCheck);
1915-
matchCount++;
1937+
JOptionPane.showMessageDialog(this,
1938+
new JTransparentTextArea("One or more of your existing media directories is a subdirectory of the directory you just added. These directories will be removed from your list automatically."),
1939+
"Reminder", JOptionPane.INFORMATION_MESSAGE);
19161940
}
1941+
removeMediaDir(dirToCheck);
1942+
matchCount++;
19171943
}
19181944
}
1945+
}
19191946

1920-
ProgressWorker<Void, Void> worker = new ProgressWorker<>()
1921-
{
1922-
@Override
1923-
protected Void doInBackground()
1924-
{
1925-
MediaLibraryOperator operator = new MediaLibraryOperator(this);
1926-
operator.addDirectory(dir);
1927-
return null;
1928-
}
1929-
};
1930-
ProgressDialog pd = new ProgressDialog(this, true, worker, "Updating Media Library...", true, true);
1931-
pd.setVisible(true);
1932-
1933-
try
1934-
{
1935-
worker.get();
1936-
}
1937-
catch (InterruptedException | CancellationException ex)
1938-
{
1939-
_logger.debug("Cancelled");
1940-
}
1941-
catch (ExecutionException ex)
1947+
ProgressWorker<Void, Void> worker = new ProgressWorker<>()
1948+
{
1949+
@Override
1950+
protected Void doInBackground()
19421951
{
1943-
_logger.error(ex);
1952+
MediaLibraryOperator operator = new MediaLibraryOperator(this);
1953+
operator.addDirectory(dir);
1954+
return null;
19441955
}
1945-
_lstMediaLibraryDirs.setListData(new Vector<>(_listFixController.getMediaLibrary().getMediaDirectories()));
1956+
};
1957+
ProgressDialog pd = new ProgressDialog(this, true, worker, "Updating Media Library...", true, true);
1958+
pd.setVisible(true);
1959+
1960+
try
1961+
{
1962+
worker.get();
1963+
}
1964+
catch (InterruptedException | CancellationException ex)
1965+
{
1966+
_logger.debug("Cancelled");
19461967
}
1947-
catch (HeadlessException e)
1968+
catch (ExecutionException ex)
19481969
{
1949-
JOptionPane.showMessageDialog(this, new JTransparentTextArea("An error has occurred, media directory could not be added."));
1950-
_logger.error("Error adding media directory", e);
1970+
_logger.error(ex);
19511971
}
1972+
_lstMediaLibraryDirs.setListData(new Vector<>(_listFixController.getMediaLibrary().getMediaDirectories()));
19521973
}
1953-
else
1974+
catch (HeadlessException e)
19541975
{
1955-
_jMediaDirChooser.cancelSelection();
1976+
JOptionPane.showMessageDialog(this, new JTransparentTextArea("An error has occurred, media directory could not be added."));
1977+
_logger.error("Error adding media directory", e);
19561978
}
19571979
updateMediaDirButtons();
19581980
}
@@ -2042,8 +2064,8 @@ protected Void doInBackground()
20422064
}
20432065
}
20442066

2045-
private void _miExactMatchesSearchActionPerformed()//GEN-FIRST:event__miExactMatchesSearchActionPerformed
2046-
{//GEN-HEADEREND:event__miExactMatchesSearchActionPerformed
2067+
private void _miExactMatchesSearchActionPerformed()
2068+
{
20472069
try
20482070
{
20492071
runExactMatchesSearchOnSelectedPlaylists();
@@ -2052,7 +2074,7 @@ private void _miExactMatchesSearchActionPerformed()//GEN-FIRST:event__miExactMat
20522074
{
20532075
throw new RuntimeException(e);
20542076
}
2055-
}//GEN-LAST:event__miExactMatchesSearchActionPerformed
2077+
}
20562078

20572079
private void launchListFixProjectUrl()
20582080
{

src/main/java/listfix/view/PlaylistFolderTransferHandler.java

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)