Skip to content

Commit 8bcf02a

Browse files
author
Federico Fissore
committed
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
1 parent 5463cfb commit 8bcf02a

File tree

4 files changed

+77
-139
lines changed

4 files changed

+77
-139
lines changed

app/src/processing/app/Base.java

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -769,30 +769,23 @@ public void handleOpenReplace(String path) {
769769
*/
770770
public void handleOpenPrompt() throws Exception {
771771
// get the frontmost window frame for placing file dialog
772-
FileDialog fd = new FileDialog(activeEditor,
773-
_("Open an Arduino sketch..."),
774-
FileDialog.LOAD);
775-
// This was annoying people, so disabled it in 0125.
776-
//fd.setDirectory(Preferences.get("sketchbook.path"));
777-
//fd.setDirectory(getSketchbookPath());
778-
779-
// Only show .pde files as eligible bachelors
780-
fd.setFilenameFilter(new FilenameFilter() {
781-
public boolean accept(File dir, String name) {
782-
return name.toLowerCase().endsWith(".ino")
783-
|| name.toLowerCase().endsWith(".pde");
784-
}
785-
});
772+
JFileChooser fd = new JFileChooser(Preferences.get("last.folder", Base.getSketchbookFolder().getAbsolutePath()));
773+
fd.setDialogTitle(_("Open an Arduino sketch..."));
774+
fd.setFileSelectionMode(JFileChooser.FILES_ONLY);
775+
fd.setFileFilter(new FileNameExtensionFilter(_("Sketches (*.ino, *.pde)"), "ino", "pde"));
786776

787-
fd.setVisible(true);
777+
Dimension preferredSize = fd.getPreferredSize();
778+
fd.setPreferredSize(new Dimension(preferredSize.width + 200, preferredSize.height + 200));
788779

789-
String directory = fd.getDirectory();
790-
String filename = fd.getFile();
780+
int returnVal = fd.showOpenDialog(activeEditor);
781+
782+
if (returnVal != JFileChooser.APPROVE_OPTION) {
783+
return;
784+
}
791785

792-
// User canceled selection
793-
if (filename == null) return;
786+
File inputFile = fd.getSelectedFile();
794787

795-
File inputFile = new File(directory, filename);
788+
Preferences.set("last.folder", inputFile.getAbsolutePath());
796789
handleOpen(inputFile.getAbsolutePath());
797790
}
798791

@@ -2185,43 +2178,17 @@ static public void openFolder(File file) {
21852178
// .................................................................
21862179

21872180

2188-
/**
2189-
* Prompt for a fodler and return it as a File object (or null).
2190-
* Implementation for choosing directories that handles both the
2191-
* Mac OS X hack to allow the native AWT file dialog, or uses
2192-
* the JFileChooser on other platforms. Mac AWT trick obtained from
2193-
* <A HREF="http://lists.apple.com/archives/java-dev/2003/Jul/msg00243.html">this post</A>
2194-
* on the OS X Java dev archive which explains the cryptic note in
2195-
* Apple's Java 1.4 release docs about the special System property.
2196-
*/
21972181
static public File selectFolder(String prompt, File folder, Frame frame) {
2198-
if (Base.isMacOS()) {
2199-
if (frame == null) frame = new Frame(); //.pack();
2200-
FileDialog fd = new FileDialog(frame, prompt, FileDialog.LOAD);
2201-
if (folder != null) {
2202-
fd.setDirectory(folder.getParent());
2203-
//fd.setFile(folder.getName());
2204-
}
2205-
System.setProperty("apple.awt.fileDialogForDirectories", "true");
2206-
fd.setVisible(true);
2207-
System.setProperty("apple.awt.fileDialogForDirectories", "false");
2208-
if (fd.getFile() == null) {
2209-
return null;
2210-
}
2211-
return new File(fd.getDirectory(), fd.getFile());
2212-
2213-
} else {
2214-
JFileChooser fc = new JFileChooser();
2215-
fc.setDialogTitle(prompt);
2216-
if (folder != null) {
2217-
fc.setSelectedFile(folder);
2218-
}
2219-
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
2182+
JFileChooser fc = new JFileChooser();
2183+
fc.setDialogTitle(prompt);
2184+
if (folder != null) {
2185+
fc.setSelectedFile(folder);
2186+
}
2187+
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
22202188

2221-
int returned = fc.showOpenDialog(new JDialog());
2222-
if (returned == JFileChooser.APPROVE_OPTION) {
2223-
return fc.getSelectedFile();
2224-
}
2189+
int returned = fc.showOpenDialog(new JDialog());
2190+
if (returned == JFileChooser.APPROVE_OPTION) {
2191+
return fc.getSelectedFile();
22252192
}
22262193
return null;
22272194
}

app/src/processing/app/Preferences.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -818,17 +818,15 @@ static protected void save() {
818818
//static public String get(String attribute) {
819819
//return get(attribute, null);
820820
//}
821-
822-
static public String get(String attribute /*, String defaultValue */) {
821+
822+
static public String get(String attribute) {
823823
return table.get(attribute);
824-
/*
825-
//String value = (properties != null) ?
826-
//properties.getProperty(attribute) : applet.getParameter(attribute);
827-
String value = properties.getProperty(attribute);
824+
}
828825

829-
return (value == null) ?
830-
defaultValue : value;
831-
*/
826+
static public String get(String attribute, String defaultValue) {
827+
String value = get(attribute);
828+
829+
return (value == null) ? defaultValue : value;
832830
}
833831

834832
public static boolean has(String key) {

app/src/processing/app/Sketch.java

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -797,58 +797,29 @@ protected boolean renameCodeToInoExtension(File pdeFile) {
797797
* because they can cause trouble.
798798
*/
799799
protected boolean saveAs() throws IOException {
800-
String newParentDir = null;
801-
String newName = null;
800+
JFileChooser fd = new JFileChooser();
801+
fd.setDialogTitle(_("Save sketch folder as..."));
802+
fd.setDialogType(JFileChooser.SAVE_DIALOG);
802803

803-
/*
804-
JFileChooser fc = new JFileChooser();
805-
fc.setDialogTitle("Save sketch folder as...");
806-
if (isReadOnly() || isUntitled()) {
807-
// default to the sketchbook folder
808-
fc.setCurrentDirectory(new File(Preferences.get("sketchbook.path")));
809-
} else {
810-
// default to the parent folder of where this was
811-
fc.setCurrentDirectory(folder.getParentFile());
812-
}
813-
// can't do this, will try to save into itself by default
814-
//fc.setSelectedFile(folder);
815-
int result = fc.showSaveDialog(editor);
816-
if (result == JFileChooser.APPROVE_OPTION) {
817-
File selection = fc.getSelectedFile();
818-
newParentDir = selection.getParent();
819-
newName = selection.getName();
820-
}
821-
*/
822-
823-
// get new name for folder
824-
FileDialog fd = new FileDialog(editor,
825-
_("Save sketch folder as..."),
826-
FileDialog.SAVE);
827804
if (isReadOnly() || isUntitled()) {
828805
// default to the sketchbook folder
829-
fd.setDirectory(Base.getSketchbookFolder().getAbsolutePath());
806+
fd.setSelectedFile(new File(Base.getSketchbookFolder().getAbsolutePath(), folder.getName()));
830807
} else {
831808
// default to the parent folder of where this was
832-
fd.setDirectory(folder.getParent());
809+
fd.setSelectedFile(folder);
833810
}
834-
String oldName = folder.getName();
835-
fd.setFile(oldName);
836811

837-
fd.setVisible(true);
838-
newParentDir = fd.getDirectory();
839-
newName = fd.getFile();
812+
int returnVal = fd.showSaveDialog(editor);
840813

841-
// user canceled selection
842-
if (newName == null) return false;
843-
newName = Sketch.checkName(newName);
814+
if (returnVal != JFileChooser.APPROVE_OPTION) {
815+
return false;
816+
}
844817

845-
File newFolder = new File(newParentDir, newName);
846-
// String newPath = newFolder.getAbsolutePath();
847-
// String oldPath = folder.getAbsolutePath();
818+
File selectedFile = fd.getSelectedFile();
848819

849-
// if (newPath.equals(oldPath)) {
850-
// return false; // Can't save a sketch over itself
851-
// }
820+
String newName = Sketch.checkName(selectedFile.getName());
821+
822+
File newFolder = new File(selectedFile.getParentFile(), newName);
852823

853824
// make sure there doesn't exist a .cpp file with that name already
854825
// but ignore this situation for the first tab, since it's probably being
@@ -973,23 +944,25 @@ public void handleAddFile() {
973944
// get a dialog, select a file to add to the sketch
974945
String prompt =
975946
_("Select an image or other data file to copy to your sketch");
976-
//FileDialog fd = new FileDialog(new Frame(), prompt, FileDialog.LOAD);
977-
FileDialog fd = new FileDialog(editor, prompt, FileDialog.LOAD);
978-
fd.setVisible(true);
947+
JFileChooser fd = new JFileChooser(Preferences.get("last.folder"));
948+
fd.setDialogTitle(prompt);
979949

980-
String directory = fd.getDirectory();
981-
String filename = fd.getFile();
982-
if (filename == null) return;
950+
int returnVal = fd.showOpenDialog(editor);
951+
952+
if (returnVal != JFileChooser.APPROVE_OPTION) {
953+
return;
954+
}
983955

984956
// copy the file into the folder. if people would rather
985957
// it move instead of copy, they can do it by hand
986-
File sourceFile = new File(directory, filename);
958+
File sourceFile = fd.getSelectedFile();
987959

988960
// now do the work of adding the file
989961
boolean result = addFile(sourceFile);
990962

991963
if (result) {
992964
editor.statusNotice(_("One file added to the sketch."));
965+
Preferences.set("last.folder", sourceFile.getAbsolutePath());
993966
}
994967
}
995968

app/src/processing/app/tools/Archiver.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
package processing.app.tools;
2525

2626
import processing.app.*;
27+
28+
import javax.swing.*;
29+
2730
import static processing.app.I18n._;
2831

29-
import java.awt.FileDialog;
3032
import java.io.*;
3133
import java.text.*;
3234
import java.util.*;
@@ -105,38 +107,36 @@ public void run() {
105107
} while (newbie.exists());
106108

107109
// open up a prompt for where to save this fella
108-
FileDialog fd =
109-
new FileDialog(editor, _("Archive sketch as:"), FileDialog.SAVE);
110-
fd.setDirectory(parent.getAbsolutePath());
111-
fd.setFile(newbie.getName());
112-
fd.setVisible(true);
110+
JFileChooser fd = new JFileChooser();
111+
fd.setDialogTitle(_("Archive sketch as:"));
112+
fd.setDialogType(JFileChooser.SAVE_DIALOG);
113+
fd.setSelectedFile(newbie);
114+
115+
int returnVal = fd.showSaveDialog(editor);
113116

114-
String directory = fd.getDirectory();
115-
String filename = fd.getFile();
117+
if (returnVal != JFileChooser.APPROVE_OPTION) {
118+
editor.statusNotice(_("Archive sketch canceled."));
119+
return;
120+
}
116121

117-
// only write the file if not canceled
118-
if (filename != null) {
119-
newbie = new File(directory, filename);
122+
newbie = fd.getSelectedFile();
120123

121-
try {
122-
//System.out.println(newbie);
123-
FileOutputStream zipOutputFile = new FileOutputStream(newbie);
124-
ZipOutputStream zos = new ZipOutputStream(zipOutputFile);
124+
try {
125+
//System.out.println(newbie);
126+
FileOutputStream zipOutputFile = new FileOutputStream(newbie);
127+
ZipOutputStream zos = new ZipOutputStream(zipOutputFile);
125128

126-
// recursively fill the zip file
127-
buildZip(location, name, zos);
129+
// recursively fill the zip file
130+
buildZip(location, name, zos);
128131

129-
// close up the jar file
130-
zos.flush();
131-
zos.close();
132+
// close up the jar file
133+
zos.flush();
134+
zos.close();
132135

133-
editor.statusNotice("Created archive " + newbie.getName() + ".");
136+
editor.statusNotice("Created archive " + newbie.getName() + ".");
134137

135-
} catch (IOException e) {
136-
e.printStackTrace();
137-
}
138-
} else {
139-
editor.statusNotice(_("Archive sketch canceled."));
138+
} catch (IOException e) {
139+
e.printStackTrace();
140140
}
141141
}
142142

0 commit comments

Comments
 (0)