use of java.awt.FileDialog in project org.alloytools.alloy by AlloyTools.
the class OurDialog method askFile.
public static File askFile(boolean isOpen, String dir, final String[] exts, final String description) {
if (dir == null)
dir = Util.getCurrentDirectory();
if (!(new File(dir).isDirectory()))
dir = System.getProperty("user.home");
dir = Util.canon(dir);
String ans;
if (useAWT) {
// this window is
Frame parent = new Frame("Alloy File Dialog");
// unused and not
// shown; needed by
// FileDialog and
// nothing more
FileDialog open = new FileDialog(parent, isOpen ? "Open..." : "Save...");
open.setAlwaysOnTop(true);
open.setMode(isOpen ? FileDialog.LOAD : FileDialog.SAVE);
open.setDirectory(dir);
if (exts != null && exts.length > 0)
open.setFilenameFilter(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
for (String ext : exts) {
if (name.toLowerCase(Locale.US).endsWith(ext))
return true;
}
return false;
}
});
// This method blocks until the user either
open.setVisible(true);
// chooses something or cancels the dialog.
parent.dispose();
if (open.getFile() == null)
return null;
else
ans = open.getDirectory() + File.separatorChar + open.getFile();
} else {
try {
JFileChooser open = new JFileChooser(dir) {
private static final long serialVersionUID = 0;
@Override
public JDialog createDialog(Component parent) throws HeadlessException {
JDialog dialog = super.createDialog(null);
dialog.setAlwaysOnTop(true);
return dialog;
}
};
open.setDialogTitle(isOpen ? "Open..." : "Save...");
open.setApproveButtonText(isOpen ? "Open" : "Save");
open.setDialogType(isOpen ? JFileChooser.OPEN_DIALOG : JFileChooser.SAVE_DIALOG);
if (exts != null && exts.length > 0)
open.setFileFilter(new FileFilter() {
@Override
public boolean accept(File file) {
for (String ext : exts) {
boolean result = !file.isFile() || file.getPath().toLowerCase(Locale.US).endsWith(ext);
if (result)
return true;
}
return false;
}
@Override
public String getDescription() {
return description;
}
});
if (open.showDialog(null, null) != JFileChooser.APPROVE_OPTION || open.getSelectedFile() == null)
return null;
ans = open.getSelectedFile().getPath();
} catch (Exception ex) {
// Some combination of Windows version and JDK version will
// trigger this failure.
// In such a case, we'll fall back to using the "AWT" file open
// dialog
useAWT = true;
return askFile(isOpen, dir, exts, description);
}
}
if (!isOpen) {
int lastSlash = ans.lastIndexOf(File.separatorChar);
int lastDot = (lastSlash >= 0) ? ans.indexOf('.', lastSlash) : ans.indexOf('.');
if (lastDot < 0 && exts != null && exts.length > 0)
ans = ans + exts[0];
}
return new File(Util.canon(ans));
}
use of java.awt.FileDialog in project opennars by opennars.
the class NARControls method actionPerformed.
/**
* Handling button click
*
* @param e The ActionEvent
*/
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj instanceof JButton) {
if (obj == stopButton) {
setSpeed(0);
updateGUI();
} else if (obj == walkButton) {
nar.stop();
nar.cycles(1);
updateGUI();
}
} else if (obj instanceof JMenuItem) {
String label = e.getActionCommand();
switch(label) {
case "Save Memory":
{
try {
FileDialog dialog = new FileDialog((Dialog) null, "Save memory", FileDialog.SAVE);
dialog.setVisible(true);
String directoryName = dialog.getDirectory();
String fileName = dialog.getFile();
String path = directoryName + fileName;
nar.SaveToFile(path);
} catch (IOException ex) {
Logger.getLogger(NARControls.class.getName()).log(Level.SEVERE, null, ex);
}
}
break;
case "Load Memory":
{
try {
FileDialog dialog = new FileDialog((Dialog) null, "Load memory", FileDialog.LOAD);
dialog.setVisible(true);
String directoryName = dialog.getDirectory();
String fileName = dialog.getFile();
String filePath = directoryName + fileName;
NAR loadedNAR = NAR.LoadFromFile(filePath);
new NARSwing(loadedNAR);
loadedNAR.memory.emit(OutputHandler.ECHO.class, "Memory file " + fileName + " loaded successfully.");
// new javax.swing.JOptionPane.showInputDialog(new javax.swing.JFrame(), "Memory loaded");
parent.mainWindow.setVisible(false);
parent.mainWindow.dispose();
} catch (IOException ex) {
Logger.getLogger(NARControls.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(NARControls.class.getName()).log(Level.SEVERE, null, ex);
}
break;
}
case "Load Experience":
openLoadFile();
break;
case "Save Experience":
if (savingExp) {
experienceWriter.closeSaveFile();
} else {
FileDialog dialog = new FileDialog((Dialog) null, "Save experience", FileDialog.SAVE);
dialog.setVisible(true);
String directoryName = dialog.getDirectory();
String fileName = dialog.getFile();
String path = directoryName + fileName;
experienceWriter.openSaveFile(path);
}
savingExp = !savingExp;
break;
case "Reset":
// / TODO mixture of modifier and reporting
// narsPlusItem.setEnabled(true);
// internalExperienceItem.setEnabled(true);
nar.reset();
break;
case "Related Information":
// MessageDialog web =
new MessageDialog(NAR.WEBSITE);
break;
case "About NARS":
// MessageDialog info =
new MessageDialog(NAR.VERSION + "\n\n" + NAR.WEBSITE);
break;
}
}
}
use of java.awt.FileDialog in project opennars by opennars.
the class NARControls method openLoadFile.
/**
* Open an addInput experience file with a FileDialog
*/
public void openLoadFile() {
FileDialog dialog = new FileDialog((Dialog) null, "Load experience", FileDialog.LOAD);
dialog.setVisible(true);
String directoryName = dialog.getDirectory();
String fileName = dialog.getFile();
String filePath = directoryName + fileName;
try {
System.out.println("TODO load file");
// nar.addInput(new TextInput(new File(filePath)));
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of java.awt.FileDialog in project hid-serial by rayshobby.
the class G4P method selectFolder.
/**
* Select a folder from the local file system.
*
* @param prompt the frame text for the chooser
* @return the absolute path name for the selected folder, or null if action
* cancelled.
*/
public static String selectFolder(String prompt) {
String selectedFolder = null;
Frame frame = (sketchApplet == null) ? null : sketchApplet.frame;
if (PApplet.platform == MACOSX && PApplet.useNativeSelect != false) {
FileDialog fileDialog = new FileDialog(frame, prompt, FileDialog.LOAD);
System.setProperty("apple.awt.fileDialogForDirectories", "true");
fileDialog.setVisible(true);
System.setProperty("apple.awt.fileDialogForDirectories", "false");
String filename = fileDialog.getFile();
if (filename != null) {
try {
selectedFolder = (new File(fileDialog.getDirectory(), fileDialog.getFile())).getCanonicalPath();
} catch (IOException e) {
selectedFolder = null;
}
}
} else {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle(prompt);
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = fileChooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
try {
selectedFolder = fileChooser.getSelectedFile().getCanonicalPath();
} catch (IOException e) {
selectedFolder = null;
}
}
}
return selectedFolder;
}
use of java.awt.FileDialog in project binnavi by google.
the class CFileChooser method showNativeFileDialog.
private static int showNativeFileDialog(final JFileChooser chooser) {
final FileDialog result = new FileDialog((Frame) chooser.getParent());
result.setDirectory(chooser.getCurrentDirectory().getPath());
final File selected = chooser.getSelectedFile();
result.setFile(selected == null ? "" : selected.getPath());
result.setFilenameFilter(new FilenameFilter() {
@Override
public boolean accept(final File dir, final String name) {
return chooser.getFileFilter().accept(new File(dir.getPath() + File.pathSeparator + name));
}
});
if (chooser.getDialogType() == SAVE_DIALOG) {
result.setMode(FileDialog.SAVE);
} else {
// The native dialog only support Open and Save
result.setMode(FileDialog.LOAD);
}
if (chooser.getFileSelectionMode() == DIRECTORIES_ONLY) {
System.setProperty("apple.awt.fileDialogForDirectories", "true");
}
// Display dialog
result.setVisible(true);
System.setProperty("apple.awt.fileDialogForDirectories", "false");
if (result.getFile() == null) {
return CANCEL_OPTION;
}
final String selectedDir = result.getDirectory();
chooser.setSelectedFile(new File(FileUtils.ensureTrailingSlash(selectedDir) + result.getFile()));
return APPROVE_OPTION;
}
Aggregations