use of java.awt.FileDialog in project briefcase by opendatakit.
the class FileChooser method buildFileDialog.
static FileDialog buildFileDialog(Container parent, Optional<File> initialLocation, JFileChooser fileChooser) {
System.setProperty("apple.awt.fileDialogForDirectories", fileChooser.getFileSelectionMode() == DIRECTORIES_ONLY ? "true" : "false");
Window windowAncestor = SwingUtilities.getWindowAncestor(parent);
FileDialog fileDialog = windowAncestor instanceof Frame ? new FileDialog((Frame) windowAncestor, fileChooser.getDialogTitle()) : new FileDialog((Dialog) windowAncestor, fileChooser.getDialogTitle());
if (fileChooser.getFileSelectionMode() == DIRECTORIES_ONLY)
fileDialog.setFilenameFilter((dir, name) -> new File(dir, name).isDirectory());
initialLocation.ifPresent(file -> fileDialog.setFile(file.getAbsolutePath()));
return fileDialog;
}
use of java.awt.FileDialog in project cytoscape-impl by cytoscape.
the class FileUtilImpl method getFolder.
@Override
public File getFolder(Component parent, String title, String startDir) {
if (parent == null)
throw new NullPointerException("\"parent\" must not be null.");
final CyApplicationManager applicationManager = serviceRegistrar.getService(CyApplicationManager.class);
final String osName = System.getProperty("os.name");
if (osName.startsWith("Mac")) {
final String property = System.getProperty("apple.awt.fileDialogForDirectories");
System.setProperty("apple.awt.fileDialogForDirectories", "true");
try {
FileDialog chooser;
if (parent instanceof Dialog)
chooser = new FileDialog((Dialog) parent, title, FileDialog.LOAD);
else if (parent instanceof Frame)
chooser = new FileDialog((Frame) parent, title, FileDialog.LOAD);
else
throw new IllegalArgumentException("parent must be Dialog or Frame");
if (startDir != null)
chooser.setDirectory(startDir);
else
chooser.setDirectory(applicationManager.getCurrentDirectory().getAbsolutePath());
chooser.setModal(true);
chooser.setLocationRelativeTo(parent);
chooser.setVisible(true);
String file = chooser.getFile();
String dir = chooser.getDirectory();
if (file == null || dir == null) {
return null;
}
return new File(dir + File.separator + file);
} finally {
if (property != null) {
System.setProperty("apple.awt.fileDialogForDirectories", property);
}
}
} else {
// this is not a Mac, use the Swing based file dialog
final JFileChooser chooser;
if (startDir != null)
chooser = new JFileChooser(new File(startDir));
else
chooser = new JFileChooser(applicationManager.getCurrentDirectory());
chooser.setDialogTitle(title);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
File result = null;
if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
result = chooser.getSelectedFile();
}
if (result != null && chooser.getCurrentDirectory().getPath() != null)
applicationManager.setCurrentDirectory(chooser.getCurrentDirectory());
return result;
}
}
use of java.awt.FileDialog in project cytoscape-impl by cytoscape.
the class FileUtilImpl method getFiles.
@Override
public File[] getFiles(final Component parent, final String title, final int loadSaveCustom, String startDir, final String customApproveText, final boolean multiselect, final Collection<FileChooserFilter> filters) {
if (parent == null)
throw new NullPointerException("\"parent\" must not be null.");
final String osName = System.getProperty("os.name");
final CyApplicationManager applicationManager = serviceRegistrar.getService(CyApplicationManager.class);
if (osName.startsWith("Mac")) {
// This is a Macintosh, use the AWT style file dialog
final String fileDialogForDirectories = System.getProperty("apple.awt.fileDialogForDirectories");
System.setProperty("apple.awt.fileDialogForDirectories", "false");
try {
final FileDialog chooser;
if (parent instanceof Frame)
chooser = new FileDialog((Frame) parent, title, loadSaveCustom);
else if (parent instanceof Dialog)
chooser = new FileDialog((Dialog) parent, title, loadSaveCustom);
else if (parent instanceof JMenuItem) {
JComponent jcomponent = (JComponent) ((JPopupMenu) parent.getParent()).getInvoker();
chooser = new FileDialog((Frame) jcomponent.getTopLevelAncestor(), title, loadSaveCustom);
} else {
throw new IllegalArgumentException("Cannot (not implemented yet) create a dialog " + "own by a parent component of type: " + parent.getClass().getCanonicalName());
}
if (startDir != null)
chooser.setDirectory(startDir);
else
chooser.setDirectory(applicationManager.getCurrentDirectory().getAbsolutePath());
chooser.setModal(true);
chooser.setFilenameFilter(new CombinedFilenameFilter(filters));
chooser.setLocationRelativeTo(parent);
chooser.setMultipleMode(multiselect);
chooser.setVisible(true);
if (chooser.getFile() != null) {
final File[] results;
if (loadSaveCustom == SAVE) {
String newFileName = chooser.getFile();
final String fileNameWithExt = addFileExt(filters, newFileName);
if (!fileNameWithExt.equals(newFileName)) {
final File file = new File(chooser.getDirectory() + File.separator + fileNameWithExt);
if (file.exists()) {
int answer = JOptionPane.showConfirmDialog(parent, "The file '" + file.getName() + "' already exists. Are you sure you want to overwrite it?", "File exists", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (// Try again
answer == JOptionPane.NO_OPTION)
return getFiles(parent, title, loadSaveCustom, file.getParent(), customApproveText, multiselect, filters);
}
newFileName = fileNameWithExt;
}
results = new File[1];
results[0] = new File(chooser.getDirectory() + File.separator + newFileName);
} else
results = chooser.getFiles();
if (chooser.getDirectory() != null)
applicationManager.setCurrentDirectory(new File(chooser.getDirectory()));
return results;
}
} finally {
if (fileDialogForDirectories != null)
System.setProperty("apple.awt.fileDialogForDirectories", fileDialogForDirectories);
}
return null;
} else {
// this is not a Mac, use the Swing based file dialog
final JFileChooser chooser;
if (startDir != null)
chooser = new JFileChooser(new File(startDir));
else
chooser = new JFileChooser(applicationManager.getCurrentDirectory());
// set multiple selection, if applicable
chooser.setMultiSelectionEnabled(multiselect);
// set the dialog title
chooser.setDialogTitle(title);
chooser.setAcceptAllFileFilterUsed(loadSaveCustom == LOAD);
int i = 0;
FileChooserFilter defaultFilter = null;
for (final FileChooserFilter filter : filters) {
// do it now!
if (++i == filters.size() && defaultFilter == null)
defaultFilter = filter;
else // with "All ", make it the default.
if (defaultFilter == null && filter.getDescription().startsWith("All "))
defaultFilter = filter;
chooser.addChoosableFileFilter(filter);
}
File[] results = null;
File tmp = null;
// set the dialog type
if (loadSaveCustom == LOAD) {
if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
if (multiselect)
results = chooser.getSelectedFiles();
else if ((tmp = chooser.getSelectedFile()) != null) {
results = new File[1];
results[0] = tmp;
}
if (filters != null && !filters.isEmpty()) {
boolean extensionFound = false;
for (int k = 0; k < results.length; ++k) {
String path = results[k].getPath();
for (final FileChooserFilter filter : filters) {
String[] filterExtensions = filter.getExtensions();
for (int t = 0; t < filterExtensions.length; ++t) {
if (filterExtensions[t].equals("") || path.endsWith("." + filterExtensions[t]))
extensionFound = true;
}
}
if (!extensionFound) {
JOptionPane.showMessageDialog(chooser, "Cytoscape does not recognize files with suffix '" + path.substring(path.lastIndexOf(".")) + "' . Please choose another file.", "File extension incorrect", JOptionPane.WARNING_MESSAGE);
return null;
}
extensionFound = false;
}
}
}
} else if (loadSaveCustom == SAVE) {
if (chooser.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION) {
if (multiselect) {
results = chooser.getSelectedFiles();
} else if ((tmp = chooser.getSelectedFile()) != null) {
results = new File[1];
results[0] = tmp;
}
// not, so we need to do so ourselves:
for (int k = 0; k < results.length; ++k) {
File file = results[k];
final String filePath = file.getAbsolutePath();
final String filePathWithExt = addFileExt(filters, filePath);
// Add an extension to the filename, if necessary and possible
if (!filePathWithExt.equals(filePath)) {
file = new File(filePathWithExt);
results[k] = file;
}
if (file.exists()) {
int answer = JOptionPane.showConfirmDialog(chooser, "The file '" + file.getName() + "' already exists. Are you sure you want to overwrite it?", "File exists", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (// Try again
answer == JOptionPane.NO_OPTION)
return getFiles(parent, title, loadSaveCustom, file.getParent(), customApproveText, multiselect, filters);
}
}
}
} else {
if (chooser.showDialog(parent, customApproveText) == JFileChooser.APPROVE_OPTION) {
if (multiselect)
results = chooser.getSelectedFiles();
else if ((tmp = chooser.getSelectedFile()) != null) {
results = new File[1];
results[0] = tmp;
}
}
}
if (results != null && chooser.getCurrentDirectory().getPath() != null)
applicationManager.setCurrentDirectory(chooser.getCurrentDirectory());
return results;
}
}
use of java.awt.FileDialog in project AlgorithmsSolutions by Allenskoo856.
the class Draw method actionPerformed.
/**
* This method cannot be called directly.
*/
@Override
public void actionPerformed(ActionEvent e) {
FileDialog chooser = new FileDialog(frame, "Use a .png or .jpg extension", FileDialog.SAVE);
chooser.setVisible(true);
String filename = chooser.getFile();
if (filename != null) {
save(chooser.getDirectory() + File.separator + chooser.getFile());
}
}
use of java.awt.FileDialog in project fql by CategoricalData.
the class GUI method getOpenDialog.
private static FileDialog getOpenDialog() {
if (openDialog != null) {
openDialog.setFile(AllNameFilter.getAllString());
return openDialog;
}
openDialog = new FileDialog((Dialog) null, "Open", FileDialog.LOAD);
openDialog.setFile(AllNameFilter.getAllString());
openDialog.setFilenameFilter(new AllNameFilter());
// if (!GlobalOptions.debug.general.file_path.isEmpty()) {
openDialog.setDirectory(IdeOptions.theCurrentOptions.getFile(IdeOption.FILE_PATH).getAbsolutePath());
// }
openDialog.setMultipleMode(true);
return openDialog;
}
Aggregations