* refactor and plan on adding a JavaFX fileChooser option later

This commit is contained in:
Reinhard Pointner 2014-07-29 06:40:35 +00:00
parent 0298b58fe4
commit b658dd7581
6 changed files with 144 additions and 101 deletions

View File

@ -9,6 +9,7 @@ import java.util.ResourceBundle;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import net.filebot.UserFiles.FileChooser;
import net.filebot.cli.ArgumentBean;
import net.filebot.util.ExceptionUtilities;
import net.filebot.util.PreferencesList;
@ -66,6 +67,10 @@ public final class Settings {
return "mas".equals(getApplicationDeployment());
}
public static FileChooser getPreferredFileChooser() {
return FileChooser.valueOf(System.getProperty("net.filebot.UserFiles.fileChooser", FileChooser.Swing.name()));
}
public static int getPreferredThreadPoolSize() {
try {
return Integer.parseInt(System.getProperty("threadPool"));

View File

@ -1,5 +1,7 @@
package net.filebot;
import static java.util.Arrays.*;
import static net.filebot.Settings.*;
import static net.filebot.util.ui.TunedUtilities.*;
import java.awt.Dialog;
@ -7,108 +9,144 @@ import java.awt.FileDialog;
import java.awt.Frame;
import java.io.File;
import java.io.FilenameFilter;
import java.util.List;
import javax.swing.JFileChooser;
public class UserFiles {
public static boolean useNative = Boolean.getBoolean("net.filebot.UserFiles.useNative");
private static FileChooser defaultFileChooser = getPreferredFileChooser();
public static void useNative(boolean b) {
useNative = b;
public static void setDefaultFileChooser(FileChooser fileChooser) {
defaultFileChooser = fileChooser;
}
public static File[] showLoadDialogSelectFiles(boolean folderMode, boolean multiSelection, File defaultFile, final FilenameFilter filter, String title, Object parent) {
if (useNative) {
FileDialog fileDialog = createFileDialog(parent, title, FileDialog.LOAD, folderMode);
if (defaultFile != null) {
fileDialog.setFile(defaultFile.getPath());
}
if (filter != null) {
fileDialog.setFilenameFilter(filter);
}
fileDialog.setMultipleMode(multiSelection);
fileDialog.setVisible(true);
return fileDialog.getFiles();
}
// use normal Swing JFileChooser by default
JFileChooser chooser = new JFileChooser();
if (filter != null) {
chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
@Override
public String getDescription() {
return filter.toString();
}
@Override
public boolean accept(File f) {
return f.isDirectory() || filter.accept(f.getParentFile(), f.getName());
}
});
}
chooser.setSelectedFile(defaultFile);
chooser.setFileSelectionMode(folderMode && filter == null ? JFileChooser.DIRECTORIES_ONLY : JFileChooser.FILES_AND_DIRECTORIES);
chooser.setMultiSelectionEnabled(multiSelection);
if (chooser.showOpenDialog(getWindow(parent)) == JFileChooser.APPROVE_OPTION) {
if (chooser.getSelectedFiles().length > 0)
return chooser.getSelectedFiles();
if (chooser.getSelectedFile() != null)
return new File[] { chooser.getSelectedFile() };
}
return new File[0];
}
public static File showOpenDialogSelectFolder(File defaultFile, String title, Object parent) {
File[] folder = showLoadDialogSelectFiles(true, false, defaultFile, null, title, parent);
return folder.length > 0 ? folder[0] : null;
public static List<File> showLoadDialogSelectFiles(boolean folderMode, boolean multiSelection, File defaultFile, final FilenameFilter filter, String title, Object parent) {
return defaultFileChooser.showLoadDialogSelectFiles(folderMode, multiSelection, defaultFile, filter, title, parent);
}
public static File showSaveDialogSelectFile(boolean folderMode, File defaultFile, String title, Object parent) {
if (useNative) {
FileDialog fileDialog = createFileDialog(getWindow(parent), title, FileDialog.SAVE, folderMode);
if (defaultFile != null) {
if (defaultFile.getParentFile() != null) {
fileDialog.setDirectory(defaultFile.getParentFile().getPath());
}
fileDialog.setFile(defaultFile.getName());
}
fileDialog.setMultipleMode(false);
fileDialog.setVisible(true);
File[] files = fileDialog.getFiles();
return files.length > 0 ? files[0] : null;
}
JFileChooser chooser = new JFileChooser();
chooser.setSelectedFile(defaultFile);
chooser.setFileSelectionMode(folderMode ? JFileChooser.DIRECTORIES_ONLY : JFileChooser.FILES_AND_DIRECTORIES);
chooser.setMultiSelectionEnabled(false);
if (chooser.showSaveDialog(getWindow(parent)) != JFileChooser.APPROVE_OPTION) {
return null;
}
return chooser.getSelectedFile();
return defaultFileChooser.showSaveDialogSelectFile(folderMode, defaultFile, title, parent);
}
public static FileDialog createFileDialog(Object parent, String title, int mode, boolean fileDialogForDirectories) {
System.setProperty("apple.awt.fileDialogForDirectories", String.valueOf(fileDialogForDirectories));
public static File showOpenDialogSelectFolder(File defaultFile, String title, Object parent) {
List<File> folder = defaultFileChooser.showLoadDialogSelectFiles(true, false, defaultFile, null, title, parent);
return folder.size() > 0 ? folder.get(0) : null;
}
if (parent instanceof Frame) {
return new FileDialog((Frame) parent, title, mode);
}
if (parent instanceof Dialog) {
return new FileDialog((Dialog) parent, title, mode);
}
public enum FileChooser {
Swing {
@Override
public List<File> showLoadDialogSelectFiles(boolean folderMode, boolean multiSelection, File defaultFile, FilenameFilter filter, String title, Object parent) {
JFileChooser chooser = new JFileChooser();
if (filter != null) {
chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
@Override
public String getDescription() {
return filter.toString();
}
@Override
public boolean accept(File f) {
return f.isDirectory() || filter.accept(f.getParentFile(), f.getName());
}
});
}
chooser.setSelectedFile(defaultFile);
chooser.setFileSelectionMode(folderMode && filter == null ? JFileChooser.DIRECTORIES_ONLY : JFileChooser.FILES_AND_DIRECTORIES);
chooser.setMultiSelectionEnabled(multiSelection);
if (chooser.showOpenDialog(getWindow(parent)) == JFileChooser.APPROVE_OPTION) {
if (chooser.getSelectedFiles().length > 0)
return asList(chooser.getSelectedFiles());
if (chooser.getSelectedFile() != null)
return asList(chooser.getSelectedFile());
}
return asList(new File[0]);
}
@Override
public File showSaveDialogSelectFile(boolean folderMode, File defaultFile, String title, Object parent) {
JFileChooser chooser = new JFileChooser();
chooser.setSelectedFile(defaultFile);
chooser.setFileSelectionMode(folderMode ? JFileChooser.DIRECTORIES_ONLY : JFileChooser.FILES_AND_DIRECTORIES);
chooser.setMultiSelectionEnabled(false);
if (chooser.showSaveDialog(getWindow(parent)) != JFileChooser.APPROVE_OPTION) {
return null;
}
return chooser.getSelectedFile();
}
},
AWT {
@Override
public List<File> showLoadDialogSelectFiles(boolean folderMode, boolean multiSelection, File defaultFile, FilenameFilter filter, String title, Object parent) {
FileDialog fileDialog = createFileDialog(parent, title, FileDialog.LOAD, folderMode);
if (defaultFile != null) {
fileDialog.setFile(defaultFile.getPath());
}
if (filter != null) {
fileDialog.setFilenameFilter(filter);
}
fileDialog.setMultipleMode(multiSelection);
fileDialog.setVisible(true);
return asList(fileDialog.getFiles());
}
@Override
public File showSaveDialogSelectFile(boolean folderMode, File defaultFile, String title, Object parent) {
FileDialog fileDialog = createFileDialog(getWindow(parent), title, FileDialog.SAVE, folderMode);
if (defaultFile != null) {
if (defaultFile.getParentFile() != null) {
fileDialog.setDirectory(defaultFile.getParentFile().getPath());
}
fileDialog.setFile(defaultFile.getName());
}
fileDialog.setMultipleMode(false);
fileDialog.setVisible(true);
File[] files = fileDialog.getFiles();
return files.length > 0 ? files[0] : null;
}
public FileDialog createFileDialog(Object parent, String title, int mode, boolean fileDialogForDirectories) {
System.setProperty("apple.awt.fileDialogForDirectories", String.valueOf(fileDialogForDirectories));
if (parent instanceof Frame) {
return new FileDialog((Frame) parent, title, mode);
}
if (parent instanceof Dialog) {
return new FileDialog((Dialog) parent, title, mode);
}
Frame[] frames = Frame.getFrames();
return new FileDialog(frames.length > 0 ? frames[0] : null, title, mode);
}
},
JavaFX {
@Override
public List<File> showLoadDialogSelectFiles(boolean folderMode, boolean multiSelection, File defaultFile, FilenameFilter filter, String title, Object parent) {
throw new UnsupportedOperationException();
}
@Override
public File showSaveDialogSelectFile(boolean folderMode, File defaultFile, String title, Object parent) {
throw new UnsupportedOperationException();
}
};
public abstract List<File> showLoadDialogSelectFiles(boolean folderMode, boolean multiSelection, File defaultFile, final FilenameFilter filter, String title, Object parent);
public abstract File showSaveDialogSelectFile(boolean folderMode, File defaultFile, String title, Object parent);
Frame[] frames = Frame.getFrames();
return new FileDialog(frames.length > 0 ? frames[0] : null, title, mode);
}
}

View File

@ -355,14 +355,14 @@ class BindingDialog extends JDialog {
@Override
public void actionPerformed(ActionEvent evt) {
ExtensionFileFilter mediaFiles = combineFilter(VIDEO_FILES, AUDIO_FILES, SUBTITLE_FILES);
File[] file = showLoadDialogSelectFiles(false, false, getMediaFile(), mediaFiles, (String) getValue(NAME), evt.getSource());
List<File> file = showLoadDialogSelectFiles(false, false, getMediaFile(), mediaFiles, (String) getValue(NAME), evt.getSource());
if (file.length > 0) {
if (file.size() > 0) {
// update text field
mediaFileTextField.setText(file[0].getAbsolutePath());
mediaFileTextField.setText(file.get(0).getAbsolutePath());
// set info object from xattr if possible
Object object = MediaDetection.readMetaInfo(file[0]);
Object object = MediaDetection.readMetaInfo(file.get(0));
if (object != null && infoObjectFormat.format(object) != null) {
setInfoObject(object);
}

View File

@ -1,6 +1,5 @@
package net.filebot.ui.subtitle;
import static java.util.Arrays.*;
import static net.filebot.MediaTypes.*;
import static net.filebot.UserFiles.*;
import static net.filebot.media.MediaDetection.*;
@ -128,11 +127,11 @@ abstract class SubtitleDropTarget extends JButton {
@Override
public void actionPerformed(ActionEvent evt) {
// collect media file extensions (video and subtitle files)
File[] files = showLoadDialogSelectFiles(true, true, null, combineFilter(VIDEO_FILES, SUBTITLE_FILES), "Select Video Folder", evt.getSource());
List<File> files = showLoadDialogSelectFiles(true, true, null, combineFilter(VIDEO_FILES, SUBTITLE_FILES), "Select Video Folder", evt.getSource());
if (files.length > 0) {
if (getDropAction(asList(files)) != DropAction.Cancel) {
handleDrop(asList(files));
if (files.size() > 0) {
if (getDropAction(files) != DropAction.Cancel) {
handleDrop(files);
}
}
}

View File

@ -227,9 +227,9 @@ public class SubtitleUploadDialog extends JDialog {
SubtitleMappingTableModel model = (SubtitleMappingTableModel) table.getModel();
SubtitleMapping mapping = model.getData()[table.convertRowIndexToModel(row)];
File[] files = showLoadDialogSelectFiles(false, false, mapping.getSubtitle().getParentFile(), VIDEO_FILES, "Select Video File", getWindow(SubtitleUploadDialog.this));
if (files.length > 0) {
mapping.setVideo(files[0]);
List<File> files = showLoadDialogSelectFiles(false, false, mapping.getSubtitle().getParentFile(), VIDEO_FILES, "Select Video File", getWindow(SubtitleUploadDialog.this));
if (files.size() > 0) {
mapping.setVideo(files.get(0));
mapping.setState(SubtitleMapping.Status.CheckPending);
startChecking();
}

View File

@ -5,6 +5,7 @@ import static net.filebot.ui.NotificationLogging.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.List;
import java.util.logging.Level;
import javax.swing.AbstractAction;
@ -55,8 +56,8 @@ public class LoadAction extends AbstractAction {
return;
}
File[] files = showLoadDialogSelectFiles(true, true, getDefaultFolder(), new TransferablePolicyFileFilter(transferablePolicy), (String) getValue(Action.NAME), evt.getSource());
if (files == null || files.length == 0) {
List<File> files = showLoadDialogSelectFiles(true, true, getDefaultFolder(), new TransferablePolicyFileFilter(transferablePolicy), (String) getValue(Action.NAME), evt.getSource());
if (files.isEmpty()) {
return;
}