Skip to content

Improved selectInput/Output/Folder #1818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/src/processing/app/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public Preferences(Base base) {
public void actionPerformed(ActionEvent e) {
File dflt = new File(sketchbookLocationField.getText());
PApplet.selectFolder("Select new sketchbook location",
"sketchbookCallback", dflt,
"sketchbookCallback", null, dflt,
Preferences.this, dialog);
// File file =
// Base.selectFolder("Select new sketchbook location", dflt, dialog);
Expand Down
2 changes: 1 addition & 1 deletion app/src/processing/app/tools/Archiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void run() {
} while (newbie.exists());

// open up a prompt for where to save this fella
PApplet.selectOutput("Archive sketch as...", "fileSelected", newbie, this, editor);
PApplet.selectOutput("Archive sketch as...", "fileSelected", null, newbie, this, editor);
}


Expand Down
163 changes: 142 additions & 21 deletions core/src/processing/core/PApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.util.*;
import java.util.regex.*;
import java.util.zip.*;
import java.util.concurrent.SynchronousQueue;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
Expand Down Expand Up @@ -6460,6 +6461,41 @@ private Frame selectFrame() {
}


/**
* Synchronized quque to pass file object from callbacks to
* selectInput/Output/Folder.
*/
private SynchronousQueue<String> pathQueue = new SynchronousQueue<String>();
private Object chooserLock = new Object();

/**
* Open a platform-specific file chooser dialog to select a file for input.
* This simulates the previous behaviour of selectInput(). It returns a path
* string: there is no need to use callback mechanism.
*
* @webref input:files
* @param prompt message to the user
* @return an absolute path to the selected flie
*/
public String selectInput() {
return selectInput("Select a file...");
}

public String selectInput(String prompt) {
String str;
synchronized(chooserLock) {
selectInput(prompt, "_selectFileCallback");
try {
str = pathQueue.take();
} catch (InterruptedException e) {
str = "";
}
}
if(str.equals("")) return null;

return str;
}

/**
* Open a platform-specific file chooser dialog to select a file for input.
* After the selection is made, the selected File will be passed to the
Expand Down Expand Up @@ -6493,23 +6529,56 @@ public void selectInput(String prompt, String callback) {
selectInput(prompt, callback, null);
}

public void selectInput(String prompt, String callback, Object callerData) {
selectInput(prompt, callback, callerData, null);
}


public void selectInput(String prompt, String callback, File file) {
selectInput(prompt, callback, file, this);
public void selectInput(String prompt, String callback, Object callerData, File file) {
selectInput(prompt, callback, callerData, file, this);
}


public void selectInput(String prompt, String callback,
public void selectInput(String prompt, String callback, Object callerData,
File file, Object callbackObject) {
selectInput(prompt, callback, file, callbackObject, selectFrame());
selectInput(prompt, callback, callerData, file, callbackObject, selectFrame());
}


static public void selectInput(String prompt, String callbackMethod,
static public void selectInput(String prompt, String callbackMethod, Object callerData,
File file, Object callbackObject, Frame parent) {
selectImpl(prompt, callbackMethod, file, callbackObject, parent, FileDialog.LOAD);
selectImpl(prompt, callbackMethod, callerData, file, callbackObject, parent, FileDialog.LOAD);
}


/**
* Open a platform-specific file chooser dialog to select a file for output.
* This simulates the previous behaviour of selectOutput(). It returns a path
* string: there is no need to use callback mechanism.
*
* @webref output:files
* @param prompt message to the user
* @return an absolute path to the selected flie
*/

public String selectOutput() {
return selectOutput("Save as...");
}

public String selectOutput(String prompt) {
String str;
synchronized(chooserLock) {
selectOutput(prompt, "_selectFileCallback");
try {
str = pathQueue.take();
} catch (InterruptedException e) {
str = "";
}
}
if(str.equals("")) return null;

return str;
}

/**
* See selectInput() for details.
Expand All @@ -6522,25 +6591,30 @@ public void selectOutput(String prompt, String callback) {
selectOutput(prompt, callback, null);
}

public void selectOutput(String prompt, String callback, File file) {
selectOutput(prompt, callback, file, this);
public void selectOutput(String prompt, String callback, Object callerData) {
selectOutput(prompt, callback, callerData, null);
}

public void selectOutput(String prompt, String callback, Object callerData, File file) {
selectOutput(prompt, callback, callerData, file, this);
}

public void selectOutput(String prompt, String callback,

public void selectOutput(String prompt, String callback, Object callerData,
File file, Object callbackObject) {
selectOutput(prompt, callback, file, callbackObject, selectFrame());
selectOutput(prompt, callback, callerData, file, callbackObject, selectFrame());
}


static public void selectOutput(String prompt, String callbackMethod,
static public void selectOutput(String prompt, String callbackMethod, Object callerData,
File file, Object callbackObject, Frame parent) {
selectImpl(prompt, callbackMethod, file, callbackObject, parent, FileDialog.SAVE);
selectImpl(prompt, callbackMethod, callerData, file, callbackObject, parent, FileDialog.SAVE);
}


static protected void selectImpl(final String prompt,
final String callbackMethod,
final Object callerData,
final File defaultSelection,
final Object callbackObject,
final Frame parentFrame,
Expand Down Expand Up @@ -6579,12 +6653,40 @@ public void run() {
selectedFile = chooser.getSelectedFile();
}
}
selectCallback(selectedFile, callbackMethod, callbackObject);
selectCallback(selectedFile, callbackMethod, callbackObject, callerData);
}
});
}


/**
* Open a platform-specific file chooser dialog to select a file for input.
* This simulates the previous behaviour of selectInput(). It returns a path
* string: there is no need to use callback mechanism.
*
* @webref output:files
* @param prompt message to the user
* @return an absolute path to the selected flie
*/

public String selectFolder() {
return selectFolder("Select a folder...");
}

public String selectFolder(String prompt) {
String str;
synchronized(chooserLock) {
selectFolder(prompt, "_selectFileCallback");
try {
str = pathQueue.take();
} catch (InterruptedException e) {
str = "";
}
}
if(str.equals("")) return null;

return str;
}
/**
* See selectInput() for details.
*
Expand All @@ -6596,20 +6698,25 @@ public void selectFolder(String prompt, String callback) {
selectFolder(prompt, callback, null);
}

public void selectFolder(String prompt, String callback, Object callerData) {
selectFolder(prompt, callback, callerData, null);
}


public void selectFolder(String prompt, String callback, File file) {
selectFolder(prompt, callback, file, this);
public void selectFolder(String prompt, String callback, Object callerData, File file) {
selectFolder(prompt, callback, callerData, file, this);
}


public void selectFolder(String prompt, String callback,
public void selectFolder(String prompt, String callback, Object callerData,
File file, Object callbackObject) {
selectFolder(prompt, callback, file, callbackObject, selectFrame());
selectFolder(prompt, callback, callerData, file, callbackObject, selectFrame());
}


static public void selectFolder(final String prompt,
final String callbackMethod,
final Object callerData,
final File defaultSelection,
final Object callbackObject,
final Frame parentFrame) {
Expand Down Expand Up @@ -6640,20 +6747,21 @@ public void run() {
selectedFile = fileChooser.getSelectedFile();
}
}
selectCallback(selectedFile, callbackMethod, callbackObject);
selectCallback(selectedFile, callbackMethod, callbackObject, callerData);
}
});
}


static private void selectCallback(File selectedFile,
String callbackMethod,
Object callbackObject) {
Object callbackObject,
Object callerData) {
try {
Class<?> callbackClass = callbackObject.getClass();
Method selectMethod =
callbackClass.getMethod(callbackMethod, new Class[] { File.class });
selectMethod.invoke(callbackObject, new Object[] { selectedFile });
callbackClass.getMethod(callbackMethod, new Class[] { File.class, Object.class });
selectMethod.invoke(callbackObject, new Object[] { selectedFile, callerData });

} catch (IllegalAccessException iae) {
System.err.println(callbackMethod + "() must be public");
Expand All @@ -6666,6 +6774,19 @@ static private void selectCallback(File selectedFile,
}
}

public void _selectFileCallback(File selection, Object data)
{
try{
if(selection != null) {
pathQueue.put(selection.getAbsolutePath());
} else {
pathQueue.put("");
}
} catch (InterruptedException e) {
System.err.println("Failed to return a path.");
}
}



//////////////////////////////////////////////////////////////
Expand Down