use of java.awt.FileDialog in project AlgorithmsSolutions by Allenskoo856.
the class Picture method actionPerformed.
/**
* Opens a save dialog box when the user selects "Save As" from the menu.
*/
@Override
public void actionPerformed(ActionEvent e) {
FileDialog chooser = new FileDialog(frame, "Use a .png or .jpg extension", FileDialog.SAVE);
chooser.setVisible(true);
if (chooser.getFile() != null) {
save(chooser.getDirectory() + File.separator + chooser.getFile());
}
}
use of java.awt.FileDialog in project triplea by triplea-game.
the class FileProperty method getFileUsingDialog.
/**
* Prompts the user to select a file.
*/
private static File getFileUsingDialog(final String... acceptableSuffixes) {
// is to use an AWT FileDialog instead of a Swing JDialog
if (SystemProperties.isMac()) {
final FileDialog fileDialog = GameRunner.newFileDialog();
fileDialog.setMode(FileDialog.LOAD);
fileDialog.setFilenameFilter((dir, name) -> {
if (acceptableSuffixes == null || acceptableSuffixes.length == 0) {
return true;
}
for (final String suffix : acceptableSuffixes) {
if (name.toLowerCase().endsWith(suffix)) {
return true;
}
}
return false;
});
fileDialog.setVisible(true);
final String fileName = fileDialog.getFile();
final String dirName = fileDialog.getDirectory();
if (fileName == null) {
return null;
}
return new File(dirName, fileName);
}
final Optional<File> selectedFile = GameRunner.showFileChooser(new FileFilter() {
@Override
public boolean accept(final File file) {
if (file == null) {
return false;
} else if (file.isDirectory()) {
return true;
} else {
final String name = file.getAbsolutePath().toLowerCase();
for (final String suffix : acceptableSuffixes) {
if (name.endsWith(suffix)) {
return true;
}
}
return false;
}
}
@Override
public String getDescription() {
return Arrays.toString(acceptableSuffixes);
}
});
return selectedFile.orElse(null);
}
use of java.awt.FileDialog in project triplea by triplea-game.
the class GameSelectorPanel method selectGameFile.
public static File selectGameFile() {
if (SystemProperties.isMac()) {
final FileDialog fileDialog = GameRunner.newFileDialog();
fileDialog.setMode(FileDialog.LOAD);
fileDialog.setDirectory(new File(ClientSetting.SAVE_GAMES_FOLDER_PATH.value()).getPath());
fileDialog.setFilenameFilter((dir, name) -> GameDataFileUtils.isCandidateFileName(name));
fileDialog.setVisible(true);
final String fileName = fileDialog.getFile();
final String dirName = fileDialog.getDirectory();
return (fileName == null) ? null : new File(dirName, fileName);
}
return GameRunner.showSaveGameFileChooser().orElse(null);
}
use of java.awt.FileDialog in project triplea by triplea-game.
the class TripleAMenuBar method getSaveGameLocation.
/**
* Displays a file chooser dialog for the user to select the file to which the current game should be saved.
*
* @param frame The owner of the file chooser dialog; may be {@code null}.
*
* @return The file to which the current game should be saved or {@code null} if the user cancelled the operation.
*/
public static File getSaveGameLocation(final Frame frame) {
// is to use an AWT FileDialog instead of a Swing JDialog
if (SystemProperties.isMac()) {
final FileDialog fileDialog = new FileDialog(frame);
fileDialog.setMode(FileDialog.SAVE);
fileDialog.setDirectory(ClientSetting.SAVE_GAMES_FOLDER_PATH.value());
fileDialog.setFilenameFilter((dir, name) -> GameDataFileUtils.isCandidateFileName(name));
fileDialog.setVisible(true);
final String fileName = fileDialog.getFile();
if (fileName == null) {
return null;
}
// the AWT Dialog on Mac OS X will ask the user for confirmation
return new File(fileDialog.getDirectory(), GameDataFileUtils.addExtensionIfAbsent(fileName));
}
// Non-Mac platforms should use the normal Swing JFileChooser
final JFileChooser fileChooser = SaveGameFileChooser.getInstance();
final int selectedOption = fileChooser.showSaveDialog(frame);
if (selectedOption != JFileChooser.APPROVE_OPTION) {
return null;
}
File f = fileChooser.getSelectedFile();
// disallow sub directories to be entered (in the form directory/name) for Windows boxes
if (SystemProperties.isWindows()) {
final int slashIndex = Math.min(f.getPath().lastIndexOf("\\"), f.getPath().length());
final String filePath = f.getPath().substring(0, slashIndex);
if (!fileChooser.getCurrentDirectory().toString().equals(filePath)) {
JOptionPane.showConfirmDialog(frame, "Sub directories are not allowed in the file name. Please rename it.", "Cancel?", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
return null;
}
}
f = new File(f.getParent(), GameDataFileUtils.addExtensionIfAbsent(f.getName()));
// A small warning so users will not over-write a file
if (f.exists()) {
final int choice = JOptionPane.showConfirmDialog(frame, "A file by that name already exists. Do you wish to over write it?", "Over-write?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (choice != JOptionPane.OK_OPTION) {
return null;
}
}
return f;
}
use of java.awt.FileDialog in project opennars by opennars.
the class GUI method selectImpl.
/**
* The implementation of the select input and output methods.
* @param prompt
* @param mode
* @param types
* @param typeDesc
* @return the absolute path name for the selected folder, or null if action
* cancelled.
*/
private static String selectImpl(String prompt, int mode, String types, String typeDesc) {
// If no initial selection made then use last selection
// Assume that a file will not be selected
String selectedFile = null;
// Get the owner
Frame owner = (applet == null) ? null : applet.frame;
// Create a file filter
if (PApplet.useNativeSelect) {
FileDialog dialog = new FileDialog(owner, prompt, mode);
FilenameFilter filter = null;
if (types != null && types.length() > 0) {
filter = new FilenameChooserFilter(types);
dialog.setFilenameFilter(filter);
}
dialog.setVisible(true);
String directory = dialog.getDirectory();
if (directory != null) {
selectedFile = dialog.getFile();
if (selectedFile != null) {
try {
selectedFile = (new File(directory, selectedFile)).getCanonicalPath();
} catch (IOException e) {
selectedFile = null;
}
}
}
} else {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(prompt);
FileFilter filter = null;
if (types != null && types.length() > 0) {
filter = new FileChooserFilter(types, typeDesc);
chooser.setFileFilter(filter);
}
int result = JFileChooser.ERROR_OPTION;
if (mode == FileDialog.SAVE) {
result = chooser.showSaveDialog(owner);
} else if (mode == FileDialog.LOAD) {
result = chooser.showOpenDialog(owner);
}
if (result == JFileChooser.APPROVE_OPTION) {
try {
selectedFile = chooser.getSelectedFile().getCanonicalPath();
} catch (IOException e) {
selectedFile = null;
}
}
}
return selectedFile;
}
Aggregations