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

View File

@ -1,5 +1,7 @@
package net.filebot; package net.filebot;
import static java.util.Arrays.*;
import static net.filebot.Settings.*;
import static net.filebot.util.ui.TunedUtilities.*; import static net.filebot.util.ui.TunedUtilities.*;
import java.awt.Dialog; import java.awt.Dialog;
@ -7,34 +9,36 @@ import java.awt.FileDialog;
import java.awt.Frame; import java.awt.Frame;
import java.io.File; import java.io.File;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.util.List;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
public class UserFiles { public class UserFiles {
public static boolean useNative = Boolean.getBoolean("net.filebot.UserFiles.useNative"); private static FileChooser defaultFileChooser = getPreferredFileChooser();
public static void useNative(boolean b) { public static void setDefaultFileChooser(FileChooser fileChooser) {
useNative = b; defaultFileChooser = fileChooser;
} }
public static File[] showLoadDialogSelectFiles(boolean folderMode, boolean multiSelection, File defaultFile, final FilenameFilter filter, String title, Object parent) { public static List<File> showLoadDialogSelectFiles(boolean folderMode, boolean multiSelection, File defaultFile, final FilenameFilter filter, String title, Object parent) {
if (useNative) { return defaultFileChooser.showLoadDialogSelectFiles(folderMode, multiSelection, defaultFile, filter, title, 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 fileDialog.getFiles();
} }
// use normal Swing JFileChooser by default public static File showSaveDialogSelectFile(boolean folderMode, File defaultFile, String title, Object parent) {
return defaultFileChooser.showSaveDialogSelectFile(folderMode, defaultFile, title, parent);
}
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;
}
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(); JFileChooser chooser = new JFileChooser();
if (filter != null) { if (filter != null) {
chooser.setFileFilter(new javax.swing.filechooser.FileFilter() { chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
@ -57,20 +61,46 @@ public class UserFiles {
if (chooser.showOpenDialog(getWindow(parent)) == JFileChooser.APPROVE_OPTION) { if (chooser.showOpenDialog(getWindow(parent)) == JFileChooser.APPROVE_OPTION) {
if (chooser.getSelectedFiles().length > 0) if (chooser.getSelectedFiles().length > 0)
return chooser.getSelectedFiles(); return asList(chooser.getSelectedFiles());
if (chooser.getSelectedFile() != null) if (chooser.getSelectedFile() != null)
return new File[] { chooser.getSelectedFile() }; return asList(chooser.getSelectedFile());
} }
return new File[0]; return asList(new File[0]);
} }
public static File showOpenDialogSelectFolder(File defaultFile, String title, Object parent) { @Override
File[] folder = showLoadDialogSelectFiles(true, false, defaultFile, null, title, parent); public File showSaveDialogSelectFile(boolean folderMode, File defaultFile, String title, Object parent) {
return folder.length > 0 ? folder[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();
}
},
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());
} }
public static File showSaveDialogSelectFile(boolean folderMode, File defaultFile, String title, Object parent) { @Override
if (useNative) { public File showSaveDialogSelectFile(boolean folderMode, File defaultFile, String title, Object parent) {
FileDialog fileDialog = createFileDialog(getWindow(parent), title, FileDialog.SAVE, folderMode); FileDialog fileDialog = createFileDialog(getWindow(parent), title, FileDialog.SAVE, folderMode);
if (defaultFile != null) { if (defaultFile != null) {
@ -86,18 +116,7 @@ public class UserFiles {
return files.length > 0 ? files[0] : null; return files.length > 0 ? files[0] : null;
} }
JFileChooser chooser = new JFileChooser(); public FileDialog createFileDialog(Object parent, String title, int mode, boolean fileDialogForDirectories) {
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();
}
public static FileDialog createFileDialog(Object parent, String title, int mode, boolean fileDialogForDirectories) {
System.setProperty("apple.awt.fileDialogForDirectories", String.valueOf(fileDialogForDirectories)); System.setProperty("apple.awt.fileDialogForDirectories", String.valueOf(fileDialogForDirectories));
if (parent instanceof Frame) { if (parent instanceof Frame) {
@ -110,5 +129,24 @@ public class UserFiles {
Frame[] frames = Frame.getFrames(); Frame[] frames = Frame.getFrames();
return new FileDialog(frames.length > 0 ? frames[0] : null, title, mode); 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);
}
} }

View File

@ -355,14 +355,14 @@ class BindingDialog extends JDialog {
@Override @Override
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
ExtensionFileFilter mediaFiles = combineFilter(VIDEO_FILES, AUDIO_FILES, SUBTITLE_FILES); 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 // update text field
mediaFileTextField.setText(file[0].getAbsolutePath()); mediaFileTextField.setText(file.get(0).getAbsolutePath());
// set info object from xattr if possible // 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) { if (object != null && infoObjectFormat.format(object) != null) {
setInfoObject(object); setInfoObject(object);
} }

View File

@ -1,6 +1,5 @@
package net.filebot.ui.subtitle; package net.filebot.ui.subtitle;
import static java.util.Arrays.*;
import static net.filebot.MediaTypes.*; import static net.filebot.MediaTypes.*;
import static net.filebot.UserFiles.*; import static net.filebot.UserFiles.*;
import static net.filebot.media.MediaDetection.*; import static net.filebot.media.MediaDetection.*;
@ -128,11 +127,11 @@ abstract class SubtitleDropTarget extends JButton {
@Override @Override
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
// collect media file extensions (video and subtitle files) // 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 (files.size() > 0) {
if (getDropAction(asList(files)) != DropAction.Cancel) { if (getDropAction(files) != DropAction.Cancel) {
handleDrop(asList(files)); handleDrop(files);
} }
} }
} }

View File

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

View File

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