Skip to content

Commit

Permalink
Use native File dialogs if possible (issue #63):
Browse files Browse the repository at this point in the history
- #63
- native dialogs are not always possible since they are not as flexible
  as the Swing dialogs. In these cases, the Swing dialog will be used
  instead
  • Loading branch information
miho committed Jan 31, 2016
1 parent ab30314 commit faaf2a4
Show file tree
Hide file tree
Showing 6 changed files with 526 additions and 78 deletions.
119 changes: 47 additions & 72 deletions VRL/src/eu/mihosoft/vrl/dialogs/FileDialogManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import eu.mihosoft.vrl.io.FileSaver;
import eu.mihosoft.vrl.io.FileLoader;
import eu.mihosoft.vrl.io.RestrictedFileSystemView;
import eu.mihosoft.vrl.io.VExtensionFileFilter;
import eu.mihosoft.vrl.system.VSysUtil;
import eu.mihosoft.vrl.visual.Canvas;
import eu.mihosoft.vrl.visual.VSwingUtil;
Expand All @@ -69,6 +70,7 @@
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
* A file dialog manager is responsible for showing a dialog for loading/saving
Expand All @@ -88,7 +90,6 @@ public static File getDefaultDir() {
return CACHED_DIR;
}


// public static final String FILE_OR_FOLDER_LOADED_ACTION = "file-or-folder-selected";
// //
// protected File latestFileOrFolder;
Expand Down Expand Up @@ -150,14 +151,10 @@ public Object loadFile(Component parent, FileLoader loader, File directory,

Object result = null;

if (VSysUtil.isMacOSX()) {
VSwingUtil.forceAppleLAF(null);
}

final JFileChooser fc = new JFileChooser();

directory = chooseDefaultDir(directory);

final JFileChooser fc = createFileChooser(restrict, filter);

if (restrict) {
fc.setFileSystemView(new RestrictedFileSystemView(directory));
}
Expand All @@ -173,7 +170,6 @@ public Object loadFile(Component parent, FileLoader loader, File directory,
}
}

fc.setFileFilter(filter);
int returnVal = fc.showOpenDialog(parent);

if (returnVal == JFileChooser.APPROVE_OPTION) {
Expand Down Expand Up @@ -203,7 +199,6 @@ public Object loadFile(Component parent, FileLoader loader, File directory,
FileNotFoundDialog.show(parent);
}


} else {
System.out.println(
">> Operation \"load file\" cancelled by user." + "\n");
Expand All @@ -216,6 +211,27 @@ public Object loadFile(Component parent, FileLoader loader, File directory,
return result;
}

private JFileChooser createFileChooser(boolean restrict, FileFilter filter) {
final JFileChooser fc;
boolean nativeChooser = !restrict
&& (filter instanceof FileNameExtensionFilter
|| filter instanceof VExtensionFileFilter || filter == null);
if (nativeChooser) {
fc = new VNativeFileChooser();
} else {
if (VSysUtil.isMacOSX()) {
VSwingUtil.forceAppleLAF(null);
}
fc = new JFileChooser();
}

if (filter != null) {
fc.setFileFilter(filter);
}

return fc;
}

/**
* Opens a load file dialog and returns the selected file.
*
Expand All @@ -230,6 +246,7 @@ public File getLoadFile(Component parent, FileFilter filter) {

/**
* Chooses the finally used default dir for all file dialogs in this class.
*
* @param directory the directory (may be <code>null</code>)
* @return the finally used default dir or <code>null</code>
*/
Expand All @@ -242,10 +259,11 @@ private static File chooseDefaultDir(File directory) {
}

/**
* Defines the default directory that shall be used if no directory has
* been specified.
* @param fileOrDir the default directory to set
* (if it is a file, the parent dir wil be used)
* Defines the default directory that shall be used if no directory has been
* specified.
*
* @param fileOrDir the default directory to set (if it is a file, the
* parent dir wil be used)
*/
public static void setDefaultDir(File fileOrDir) {

Expand Down Expand Up @@ -278,10 +296,7 @@ public File getLoadFile(Component parent,

File result = null;

if (VSysUtil.isMacOSX()) {
VSwingUtil.forceAppleLAF(null);
}
final JFileChooser fc = new JFileChooser();
final JFileChooser fc = createFileChooser(restrict, filter);

if (restrict) {
fc.setFileSystemView(new RestrictedFileSystemView(directory));
Expand All @@ -290,8 +305,6 @@ public File getLoadFile(Component parent,

fc.setCurrentDirectory(directory);


fc.setFileFilter(filter);
int returnVal = fc.showOpenDialog(parent);

if (returnVal == JFileChooser.APPROVE_OPTION) {
Expand Down Expand Up @@ -328,20 +341,15 @@ public File[] getLoadFiles(Component parent,
File directory, FileFilter filter, boolean restrict) {
File[] result = null;

if (VSysUtil.isMacOSX()) {
VSwingUtil.forceAppleLAF(null);
}

final JFileChooser fc = new JFileChooser();

directory = chooseDefaultDir(directory);

final JFileChooser fc = createFileChooser(restrict, filter);

if (restrict) {
fc.setFileSystemView(new RestrictedFileSystemView(directory));
}

fc.setCurrentDirectory(directory);
fc.setFileFilter(filter);
fc.setMultiSelectionEnabled(true);
int returnVal = fc.showOpenDialog(parent);

Expand Down Expand Up @@ -397,15 +405,9 @@ public File getLoadFileOrFolder(Component parent,
File directory, boolean restrict, int loadType, FileFilter filter) {
File result = null;

if (VSysUtil.isMacOSX()) {
VSwingUtil.forceAppleLAF(null);
}

final JFileChooser fc = new JFileChooser();

directory = chooseDefaultDir(directory);

fc.setFileFilter(filter);
final JFileChooser fc = createFileChooser(restrict, filter);

if (restrict) {
fc.setFileSystemView(new RestrictedFileSystemView(directory));
Expand Down Expand Up @@ -439,7 +441,6 @@ public File getLoadFileOrFolder(Component parent,

//
// fireAction(new ActionEvent(this, 0, FILE_OR_FOLDER_LOADED_ACTION));

return result;
}

Expand All @@ -459,15 +460,9 @@ public File getSaveFileOrFolder(Component parent,
File directory, boolean restrict, int saveType, FileFilter filter) {
File result = null;

if (VSysUtil.isMacOSX()) {
VSwingUtil.forceAppleLAF(null);
}

final JFileChooser fc = new JFileChooser();

directory = chooseDefaultDir(directory);

fc.setFileFilter(filter);
final JFileChooser fc = createFileChooser(restrict, filter);

if (restrict) {
fc.setFileSystemView(new RestrictedFileSystemView(directory));
Expand Down Expand Up @@ -501,7 +496,6 @@ public File getSaveFileOrFolder(Component parent,

//
// fireAction(new ActionEvent(this, 0, FILE_OR_FOLDER_LOADED_ACTION));

return result;
}

Expand Down Expand Up @@ -538,14 +532,10 @@ public void saveFile(Component parent, Object o,
FileSaver fileSaver, File directory,
FileFilter filter, boolean restrict) {

if (VSysUtil.isMacOSX()) {
VSwingUtil.forceAppleLAF(null);
}

final JFileChooser fc = new JFileChooser();

directory = chooseDefaultDir(directory);

final JFileChooser fc = createFileChooser(restrict, filter);

if (restrict) {
fc.setFileSystemView(new RestrictedFileSystemView(directory));
}
Expand All @@ -561,8 +551,6 @@ public void saveFile(Component parent, Object o,
}
}

fc.setFileFilter(filter);

int returnVal = fc.showSaveDialog(parent);

if (returnVal == JFileChooser.APPROVE_OPTION) {
Expand Down Expand Up @@ -631,21 +619,17 @@ public File getSaveFile(Component parent,
File directory, FileFilter filter, boolean restrict) {
File result = null;

if (VSysUtil.isMacOSX()) {
VSwingUtil.forceAppleLAF(null);
}

final JFileChooser fc = new JFileChooser();

directory = chooseDefaultDir(directory);

final JFileChooser fc = createFileChooser(restrict, filter);

if (restrict) {
fc.setFileSystemView(new RestrictedFileSystemView(directory));
}

fc.setFileHidingEnabled(true);
fc.setCurrentDirectory(directory);
fc.setFileFilter(filter);

int returnVal = fc.showSaveDialog(parent);

if (returnVal == JFileChooser.APPROVE_OPTION) {
Expand Down Expand Up @@ -687,19 +671,15 @@ public File[] getSaveFiles(Component parent,
File directory, FileFilter filter, boolean restrict) {
File[] result = null;

if (VSysUtil.isMacOSX()) {
VSwingUtil.forceAppleLAF(null);
}

final JFileChooser fc = new JFileChooser();

directory = chooseDefaultDir(directory);

final JFileChooser fc = createFileChooser(restrict, filter);

if (restrict) {
fc.setFileSystemView(new RestrictedFileSystemView(directory));
}

// allow mut liple selections
// allow multiple selections
fc.setMultiSelectionEnabled(true);
fc.setFileHidingEnabled(true);
fc.setCurrentDirectory(directory);
Expand Down Expand Up @@ -746,14 +726,10 @@ public File getSaveFolder(Component parent,
File directory, boolean restrict) {
File result = null;

if (VSysUtil.isMacOSX()) {
VSwingUtil.forceAppleLAF(null);
}

final JFileChooser fc = new JFileChooser();

directory = chooseDefaultDir(directory);

final JFileChooser fc = createFileChooser(restrict, null);

if (restrict) {
fc.setFileSystemView(new RestrictedFileSystemView(directory));
}
Expand All @@ -765,7 +741,7 @@ public File getSaveFolder(Component parent,

if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();

setDefaultDir(file);

// if (file.exists()) {
Expand Down Expand Up @@ -913,7 +889,6 @@ public static void restrictNavigation(JFileChooser fc, File dir) {
for (Component c : VSwingUtil.getAllChildren(fc, JComboBox.class)) {
JComboBox box = (JComboBox) c;


if (box.getItemCount() == 0) {
break;
}
Expand Down
Loading

0 comments on commit faaf2a4

Please sign in to comment.