Improved File open/save dialogs, remembering last opened file/folder

Consistent UI across the IDE
Solves NPE on some linuxes #1384
Hopefully improves UX #559
This commit is contained in:
Federico Fissore 2013-10-22 15:34:42 +02:00
parent 5463cfb673
commit 8bcf02ac3b
4 changed files with 81 additions and 143 deletions

View File

@ -769,30 +769,23 @@ public class Base {
*/
public void handleOpenPrompt() throws Exception {
// get the frontmost window frame for placing file dialog
FileDialog fd = new FileDialog(activeEditor,
_("Open an Arduino sketch..."),
FileDialog.LOAD);
// This was annoying people, so disabled it in 0125.
//fd.setDirectory(Preferences.get("sketchbook.path"));
//fd.setDirectory(getSketchbookPath());
JFileChooser fd = new JFileChooser(Preferences.get("last.folder", Base.getSketchbookFolder().getAbsolutePath()));
fd.setDialogTitle(_("Open an Arduino sketch..."));
fd.setFileSelectionMode(JFileChooser.FILES_ONLY);
fd.setFileFilter(new FileNameExtensionFilter(_("Sketches (*.ino, *.pde)"), "ino", "pde"));
// Only show .pde files as eligible bachelors
fd.setFilenameFilter(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".ino")
|| name.toLowerCase().endsWith(".pde");
}
});
Dimension preferredSize = fd.getPreferredSize();
fd.setPreferredSize(new Dimension(preferredSize.width + 200, preferredSize.height + 200));
fd.setVisible(true);
int returnVal = fd.showOpenDialog(activeEditor);
String directory = fd.getDirectory();
String filename = fd.getFile();
if (returnVal != JFileChooser.APPROVE_OPTION) {
return;
}
// User canceled selection
if (filename == null) return;
File inputFile = fd.getSelectedFile();
File inputFile = new File(directory, filename);
Preferences.set("last.folder", inputFile.getAbsolutePath());
handleOpen(inputFile.getAbsolutePath());
}
@ -2185,43 +2178,17 @@ public class Base {
// .................................................................
/**
* Prompt for a fodler and return it as a File object (or null).
* Implementation for choosing directories that handles both the
* Mac OS X hack to allow the native AWT file dialog, or uses
* the JFileChooser on other platforms. Mac AWT trick obtained from
* <A HREF="http://lists.apple.com/archives/java-dev/2003/Jul/msg00243.html">this post</A>
* on the OS X Java dev archive which explains the cryptic note in
* Apple's Java 1.4 release docs about the special System property.
*/
static public File selectFolder(String prompt, File folder, Frame frame) {
if (Base.isMacOS()) {
if (frame == null) frame = new Frame(); //.pack();
FileDialog fd = new FileDialog(frame, prompt, FileDialog.LOAD);
if (folder != null) {
fd.setDirectory(folder.getParent());
//fd.setFile(folder.getName());
}
System.setProperty("apple.awt.fileDialogForDirectories", "true");
fd.setVisible(true);
System.setProperty("apple.awt.fileDialogForDirectories", "false");
if (fd.getFile() == null) {
return null;
}
return new File(fd.getDirectory(), fd.getFile());
JFileChooser fc = new JFileChooser();
fc.setDialogTitle(prompt);
if (folder != null) {
fc.setSelectedFile(folder);
}
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
} else {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle(prompt);
if (folder != null) {
fc.setSelectedFile(folder);
}
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returned = fc.showOpenDialog(new JDialog());
if (returned == JFileChooser.APPROVE_OPTION) {
return fc.getSelectedFile();
}
int returned = fc.showOpenDialog(new JDialog());
if (returned == JFileChooser.APPROVE_OPTION) {
return fc.getSelectedFile();
}
return null;
}

View File

@ -818,17 +818,15 @@ public class Preferences {
//static public String get(String attribute) {
//return get(attribute, null);
//}
static public String get(String attribute /*, String defaultValue */) {
return table.get(attribute);
/*
//String value = (properties != null) ?
//properties.getProperty(attribute) : applet.getParameter(attribute);
String value = properties.getProperty(attribute);
return (value == null) ?
defaultValue : value;
*/
static public String get(String attribute) {
return table.get(attribute);
}
static public String get(String attribute, String defaultValue) {
String value = get(attribute);
return (value == null) ? defaultValue : value;
}
public static boolean has(String key) {

View File

@ -797,58 +797,29 @@ public class Sketch {
* because they can cause trouble.
*/
protected boolean saveAs() throws IOException {
String newParentDir = null;
String newName = null;
JFileChooser fd = new JFileChooser();
fd.setDialogTitle(_("Save sketch folder as..."));
fd.setDialogType(JFileChooser.SAVE_DIALOG);
/*
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Save sketch folder as...");
if (isReadOnly() || isUntitled()) {
// default to the sketchbook folder
fc.setCurrentDirectory(new File(Preferences.get("sketchbook.path")));
} else {
// default to the parent folder of where this was
fc.setCurrentDirectory(folder.getParentFile());
}
// can't do this, will try to save into itself by default
//fc.setSelectedFile(folder);
int result = fc.showSaveDialog(editor);
if (result == JFileChooser.APPROVE_OPTION) {
File selection = fc.getSelectedFile();
newParentDir = selection.getParent();
newName = selection.getName();
}
*/
// get new name for folder
FileDialog fd = new FileDialog(editor,
_("Save sketch folder as..."),
FileDialog.SAVE);
if (isReadOnly() || isUntitled()) {
// default to the sketchbook folder
fd.setDirectory(Base.getSketchbookFolder().getAbsolutePath());
fd.setSelectedFile(new File(Base.getSketchbookFolder().getAbsolutePath(), folder.getName()));
} else {
// default to the parent folder of where this was
fd.setDirectory(folder.getParent());
fd.setSelectedFile(folder);
}
String oldName = folder.getName();
fd.setFile(oldName);
fd.setVisible(true);
newParentDir = fd.getDirectory();
newName = fd.getFile();
int returnVal = fd.showSaveDialog(editor);
// user canceled selection
if (newName == null) return false;
newName = Sketch.checkName(newName);
if (returnVal != JFileChooser.APPROVE_OPTION) {
return false;
}
File newFolder = new File(newParentDir, newName);
// String newPath = newFolder.getAbsolutePath();
// String oldPath = folder.getAbsolutePath();
File selectedFile = fd.getSelectedFile();
// if (newPath.equals(oldPath)) {
// return false; // Can't save a sketch over itself
// }
String newName = Sketch.checkName(selectedFile.getName());
File newFolder = new File(selectedFile.getParentFile(), newName);
// make sure there doesn't exist a .cpp file with that name already
// but ignore this situation for the first tab, since it's probably being
@ -973,23 +944,25 @@ public class Sketch {
// get a dialog, select a file to add to the sketch
String prompt =
_("Select an image or other data file to copy to your sketch");
//FileDialog fd = new FileDialog(new Frame(), prompt, FileDialog.LOAD);
FileDialog fd = new FileDialog(editor, prompt, FileDialog.LOAD);
fd.setVisible(true);
JFileChooser fd = new JFileChooser(Preferences.get("last.folder"));
fd.setDialogTitle(prompt);
String directory = fd.getDirectory();
String filename = fd.getFile();
if (filename == null) return;
int returnVal = fd.showOpenDialog(editor);
if (returnVal != JFileChooser.APPROVE_OPTION) {
return;
}
// copy the file into the folder. if people would rather
// it move instead of copy, they can do it by hand
File sourceFile = new File(directory, filename);
File sourceFile = fd.getSelectedFile();
// now do the work of adding the file
boolean result = addFile(sourceFile);
if (result) {
editor.statusNotice(_("One file added to the sketch."));
Preferences.set("last.folder", sourceFile.getAbsolutePath());
}
}

View File

@ -24,9 +24,11 @@
package processing.app.tools;
import processing.app.*;
import javax.swing.*;
import static processing.app.I18n._;
import java.awt.FileDialog;
import java.io.*;
import java.text.*;
import java.util.*;
@ -105,38 +107,36 @@ public class Archiver implements Tool {
} while (newbie.exists());
// open up a prompt for where to save this fella
FileDialog fd =
new FileDialog(editor, _("Archive sketch as:"), FileDialog.SAVE);
fd.setDirectory(parent.getAbsolutePath());
fd.setFile(newbie.getName());
fd.setVisible(true);
JFileChooser fd = new JFileChooser();
fd.setDialogTitle(_("Archive sketch as:"));
fd.setDialogType(JFileChooser.SAVE_DIALOG);
fd.setSelectedFile(newbie);
String directory = fd.getDirectory();
String filename = fd.getFile();
int returnVal = fd.showSaveDialog(editor);
// only write the file if not canceled
if (filename != null) {
newbie = new File(directory, filename);
try {
//System.out.println(newbie);
FileOutputStream zipOutputFile = new FileOutputStream(newbie);
ZipOutputStream zos = new ZipOutputStream(zipOutputFile);
// recursively fill the zip file
buildZip(location, name, zos);
// close up the jar file
zos.flush();
zos.close();
editor.statusNotice("Created archive " + newbie.getName() + ".");
} catch (IOException e) {
e.printStackTrace();
}
} else {
if (returnVal != JFileChooser.APPROVE_OPTION) {
editor.statusNotice(_("Archive sketch canceled."));
return;
}
newbie = fd.getSelectedFile();
try {
//System.out.println(newbie);
FileOutputStream zipOutputFile = new FileOutputStream(newbie);
ZipOutputStream zos = new ZipOutputStream(zipOutputFile);
// recursively fill the zip file
buildZip(location, name, zos);
// close up the jar file
zos.flush();
zos.close();
editor.statusNotice("Created archive " + newbie.getName() + ".");
} catch (IOException e) {
e.printStackTrace();
}
}