* refactor file access utilities into new class UserFiles

This commit is contained in:
Reinhard Pointner 2014-07-28 20:01:46 +00:00
parent 88008a2b0f
commit 0298b58fe4
11 changed files with 133 additions and 114 deletions

View File

@ -279,6 +279,8 @@
<option value="-Dapplication.cache=./Library/Caches/ehcache.disk.store" />
<option value="-Djava.io.tmpdir=./Library/Caches/java.io.tmpdir" />
<option value="-Dnet.filebot.UserFiles.useNative=true" />
<option value="-Dapplication.deployment=mas" />
<option value="-Dapplication.update=skip" />
<option value="-Dapplication.analytics=false" />

View File

@ -0,0 +1,114 @@
package net.filebot;
import static net.filebot.util.ui.TunedUtilities.*;
import java.awt.Dialog;
import java.awt.FileDialog;
import java.awt.Frame;
import java.io.File;
import java.io.FilenameFilter;
import javax.swing.JFileChooser;
public class UserFiles {
public static boolean useNative = Boolean.getBoolean("net.filebot.UserFiles.useNative");
public static void useNative(boolean b) {
useNative = b;
}
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 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();
}
public static 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);
}
}

View File

@ -1,5 +1,6 @@
package net.filebot.ui.analyze;
import static net.filebot.UserFiles.*;
import static net.filebot.ui.NotificationLogging.*;
import static net.filebot.util.ExceptionUtilities.*;
import static net.filebot.util.FileUtilities.*;
@ -29,7 +30,6 @@ import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import net.filebot.ResourceManager;
import net.filebot.Settings;
import net.filebot.archive.Archive;
import net.filebot.archive.FileMapper;
import net.filebot.util.FileUtilities;
@ -112,7 +112,7 @@ class ExtractTool extends Tool<TableModel> {
if (archives.isEmpty())
return;
File selectedFile = showOpenDialogSelectFolder(archives.get(0).getParentFile(), "Extract to ...", evt.getSource(), Settings.isSandboxed());
File selectedFile = showOpenDialogSelectFolder(archives.get(0).getParentFile(), "Extract to ...", evt.getSource());
if (selectedFile == null)
return;

View File

@ -1,6 +1,7 @@
package net.filebot.ui.rename;
import static net.filebot.MediaTypes.*;
import static net.filebot.UserFiles.*;
import static net.filebot.ui.NotificationLogging.*;
import static net.filebot.util.ui.TunedUtilities.*;
@ -48,7 +49,6 @@ import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import net.filebot.ResourceManager;
import net.filebot.Settings;
import net.filebot.format.ExpressionFormat;
import net.filebot.format.MediaBindingBean;
import net.filebot.media.MediaDetection;
@ -355,7 +355,7 @@ 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(), Settings.isSandboxed());
File[] file = showLoadDialogSelectFiles(false, false, getMediaFile(), mediaFiles, (String) getValue(NAME), evt.getSource());
if (file.length > 0) {
// update text field

View File

@ -4,8 +4,8 @@ import static java.awt.Font.*;
import static java.util.Collections.*;
import static java.util.regex.Pattern.*;
import static javax.swing.JOptionPane.*;
import static net.filebot.UserFiles.*;
import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.ui.TunedUtilities.*;
import java.awt.Color;
import java.awt.Component;
@ -515,7 +515,7 @@ class HistoryDialog extends JDialog {
// change directory option
if (selectedOption == Option.ChangeDirectory) {
directory = showOpenDialogSelectFolder(directory, selectedOption.toString(), evt.getSource(), Settings.isSandboxed());
directory = showOpenDialogSelectFolder(directory, selectedOption.toString(), evt.getSource());
}
}

View File

@ -1,6 +1,7 @@
package net.filebot.ui.subtitle;
import static net.filebot.MediaTypes.*;
import static net.filebot.UserFiles.*;
import static net.filebot.subtitle.SubtitleUtilities.*;
import static net.filebot.ui.NotificationLogging.*;
import static net.filebot.util.FileUtilities.*;
@ -278,7 +279,7 @@ class SubtitleDownloadComponent extends JComponent {
private void save(Object[] selection) {
try {
// multiple files
File outputFolder = showOpenDialogSelectFolder(null, "Save Subtitles", this, Settings.isSandboxed());
File outputFolder = showOpenDialogSelectFolder(null, "Save Subtitles", this);
if (outputFolder != null) {
for (Object object : selection) {
MemoryFile file = (MemoryFile) object;
@ -304,7 +305,7 @@ class SubtitleDownloadComponent extends JComponent {
// just use default values when we can't use a JFC with accessory component
if (Settings.isSandboxed()) {
// AWT
selectedOutputFolder = showOpenDialogSelectFolder(null, "Export Subtitles", this, Settings.isSandboxed());
selectedOutputFolder = showOpenDialogSelectFolder(null, "Export Subtitles", this);
} else {
// Swing
SubtitleFileChooser sfc = new SubtitleFileChooser();

View File

@ -2,6 +2,7 @@ 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.*;
import static net.filebot.ui.NotificationLogging.*;
import static net.filebot.ui.transfer.FileTransferable.*;
@ -31,7 +32,6 @@ import javax.swing.JButton;
import javax.swing.JDialog;
import net.filebot.ResourceManager;
import net.filebot.Settings;
import net.filebot.util.FileUtilities;
import net.filebot.util.FileUtilities.ParentFilter;
import net.filebot.web.OpenSubtitlesClient;
@ -128,7 +128,7 @@ 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(), Settings.isSandboxed());
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) {

View File

@ -1,6 +1,7 @@
package net.filebot.ui.subtitle;
import static net.filebot.MediaTypes.*;
import static net.filebot.UserFiles.*;
import static net.filebot.media.MediaDetection.*;
import static net.filebot.util.ui.TunedUtilities.*;
@ -50,7 +51,6 @@ import javax.swing.table.TableCellRenderer;
import net.filebot.Analytics;
import net.filebot.Language;
import net.filebot.ResourceManager;
import net.filebot.Settings;
import net.filebot.WebServices;
import net.filebot.media.MediaDetection;
import net.filebot.ui.LanguageComboBox;
@ -227,7 +227,7 @@ 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), Settings.isSandboxed());
File[] files = showLoadDialogSelectFiles(false, false, mapping.getSubtitle().getParentFile(), VIDEO_FILES, "Select Video File", getWindow(SubtitleUploadDialog.this));
if (files.length > 0) {
mapping.setVideo(files[0]);
mapping.setState(SubtitleMapping.Status.CheckPending);

View File

@ -1,7 +1,7 @@
package net.filebot.ui.transfer;
import static net.filebot.UserFiles.*;
import static net.filebot.ui.NotificationLogging.*;
import static net.filebot.util.ui.TunedUtilities.*;
import java.awt.event.ActionEvent;
import java.io.File;
@ -55,7 +55,7 @@ public class LoadAction extends AbstractAction {
return;
}
File[] files = showLoadDialogSelectFiles(true, true, getDefaultFolder(), new TransferablePolicyFileFilter(transferablePolicy), (String) getValue(Action.NAME), evt.getSource(), Settings.isSandboxed());
File[] files = showLoadDialogSelectFiles(true, true, getDefaultFolder(), new TransferablePolicyFileFilter(transferablePolicy), (String) getValue(Action.NAME), evt.getSource());
if (files == null || files.length == 0) {
return;
}

View File

@ -1,7 +1,7 @@
package net.filebot.ui.transfer;
import static net.filebot.UserFiles.*;
import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.ui.TunedUtilities.*;
import java.awt.event.ActionEvent;
import java.io.File;
@ -63,7 +63,7 @@ public class SaveAction extends AbstractAction {
try {
if (canExport()) {
File defaultFile = new File(getDefaultFolder(), validateFileName(getDefaultFileName()));
File file = showSaveDialogSelectFile(false, defaultFile, (String) getValue(Action.NAME), evt.getSource(), Settings.isSandboxed());
File file = showSaveDialogSelectFile(false, defaultFile, (String) getValue(Action.NAME), evt.getSource());
if (file != null) {
setDefaultFolder(file.getParentFile());

View File

@ -5,9 +5,7 @@ import static javax.swing.JOptionPane.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.Image;
@ -19,8 +17,6 @@ import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
@ -32,7 +28,6 @@ import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
@ -45,99 +40,6 @@ import javax.swing.undo.UndoManager;
public final class TunedUtilities {
public static File[] showLoadDialogSelectFiles(boolean folderMode, boolean multiSelection, File defaultFile, final FilenameFilter filter, String title, Object parent, boolean useNative) {
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, boolean useNative) {
File[] folder = showLoadDialogSelectFiles(true, false, defaultFile, null, title, parent, useNative);
return folder.length > 0 ? folder[0] : null;
}
public static File showSaveDialogSelectFile(boolean folderMode, File defaultFile, String title, Object parent, boolean useNative) {
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();
}
public static 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);
}
public static void checkEventDispatchThread() {
if (!SwingUtilities.isEventDispatchThread()) {
throw new IllegalStateException("Method must be accessed from the Swing Event Dispatch Thread, but was called on Thread \"" + Thread.currentThread().getName() + "\"");