1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-02 13:24:12 +01:00

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 { public void handleOpenPrompt() throws Exception {
// get the frontmost window frame for placing file dialog // get the frontmost window frame for placing file dialog
FileDialog fd = new FileDialog(activeEditor, JFileChooser fd = new JFileChooser(Preferences.get("last.folder", Base.getSketchbookFolder().getAbsolutePath()));
_("Open an Arduino sketch..."), fd.setDialogTitle(_("Open an Arduino sketch..."));
FileDialog.LOAD); fd.setFileSelectionMode(JFileChooser.FILES_ONLY);
// This was annoying people, so disabled it in 0125. fd.setFileFilter(new FileNameExtensionFilter(_("Sketches (*.ino, *.pde)"), "ino", "pde"));
//fd.setDirectory(Preferences.get("sketchbook.path"));
//fd.setDirectory(getSketchbookPath());
// Only show .pde files as eligible bachelors Dimension preferredSize = fd.getPreferredSize();
fd.setFilenameFilter(new FilenameFilter() { fd.setPreferredSize(new Dimension(preferredSize.width + 200, preferredSize.height + 200));
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".ino") int returnVal = fd.showOpenDialog(activeEditor);
|| name.toLowerCase().endsWith(".pde");
if (returnVal != JFileChooser.APPROVE_OPTION) {
return;
} }
});
fd.setVisible(true); File inputFile = fd.getSelectedFile();
String directory = fd.getDirectory(); Preferences.set("last.folder", inputFile.getAbsolutePath());
String filename = fd.getFile();
// User canceled selection
if (filename == null) return;
File inputFile = new File(directory, filename);
handleOpen(inputFile.getAbsolutePath()); handleOpen(inputFile.getAbsolutePath());
} }
@ -2185,32 +2178,7 @@ 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) { 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());
} else {
JFileChooser fc = new JFileChooser(); JFileChooser fc = new JFileChooser();
fc.setDialogTitle(prompt); fc.setDialogTitle(prompt);
if (folder != null) { if (folder != null) {
@ -2222,7 +2190,6 @@ public class Base {
if (returned == JFileChooser.APPROVE_OPTION) { if (returned == JFileChooser.APPROVE_OPTION) {
return fc.getSelectedFile(); return fc.getSelectedFile();
} }
}
return null; return null;
} }

View File

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

View File

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

View File

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