Skip to content
Open
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
20 changes: 19 additions & 1 deletion src/select/files/SelectDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ public class SelectDialog extends Dialog {
public static final String EX_PATH_RESULT = "pathResult";
public static final String EX_CALLBACK = "selectCallback";
public static final String EX_TITLE = "selectTitle";
public static final String EX_EXTENSION = "extension";


private String currentPath = "";
private ArrayAdapter<FileItem> simpleAdapter = null;

private SelectMode selectMode = null;
private String extension = null;
private final Intent intent;
private PApplet parent;

Expand Down Expand Up @@ -117,6 +119,8 @@ public void onItemClick(AdapterView parent, View v, int position, long id) {
selectMode = SelectMode.createSelectMode(getIntent().getIntExtra(EX_STYLE, SelectMode.SELECT_FILE), this);
selectMode.updateUI();

extension = getIntent().getStringExtra(EX_EXTENSION);

File f = new File(currentPath);

simpleAdapter = new ArrayAdapter<FileItem>(getContext(), android.R.layout.simple_list_item_2, android.R.id.text1) {
Expand Down Expand Up @@ -153,7 +157,21 @@ void updateCurrentList(File f) {
currentPath = f.getAbsolutePath();
simpleAdapter.clear();
for (FileItem item : newData) {
simpleAdapter.add(item);
if (extension == null) { // no extension filtering
simpleAdapter.add(item);
} else {
if (item.getFile().isDirectory()) { // display folders
simpleAdapter.add(item);
} else {
String name = item.getName();
String[] ss = name.split("\\.");
String ext = ss[ss.length - 1];
if (ext.equals(extension)) { // display file matching extension given
simpleAdapter.add(item);
}

}
}
}
simpleAdapter.notifyDataSetChanged();
}
Expand Down
60 changes: 57 additions & 3 deletions src/select/files/SelectLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public SelectLibrary(PApplet parent) {
* @param callback name of the method to be called when the selection is made
*/
public void selectInput(String prompt, String callback) {
selectImpl(prompt, callback, null, SelectMode.SELECT_FILE);
selectImpl(prompt, callback, null, null, SelectMode.SELECT_FILE);
}

/**
Expand All @@ -79,7 +79,7 @@ public void selectInput(String prompt, String callback) {
* @param callback name of the method to be called when the selection is made
*/
public void selectOutput(String prompt, String callback) {
selectImpl(prompt, callback, null, SelectMode.SAVE_FILE);
selectImpl(prompt, callback, null, null, SelectMode.SAVE_FILE);
}

/**
Expand All @@ -90,9 +90,61 @@ public void selectOutput(String prompt, String callback) {
* @param callback name of the method to be called when the selection is made
*/
public void selectFolder(String prompt, String callback) {
selectImpl(prompt, callback, null, SelectMode.SELECT_FOLDER);
selectImpl(prompt, callback, null, null, SelectMode.SELECT_FOLDER);
}

/**
* Select input with a specific path
* See selectInput() for details.
*
* @webref input:files
* @param prompt
* message to the user
* @param callback
* name of the method to be called when the selection is made
* @param path
* a java File object with the specific path
*/
public void customInput(String prompt, String callback, File path) {
selectImpl(prompt, callback, path, null, SelectMode.SELECT_FILE);
}

/**
* See customInput() for details.
*
* @webref output:files
* @param prompt
* message to the user
* @param callback
* name of the method to be called when the selection is made
* @param path
* a java File object with the specific path
*/
public void customOutput(String prompt, String callback, File path) {
selectImpl(prompt, callback, path, null, SelectMode.SAVE_FILE);
}


/**
* Filtering result with an extension name
* See selectInput() for details.
*
* @webref input:files
* @param prompt
* message to the user
* @param callback
* name of the method to be called when the selection is made
* @param path
* java File object with the specific path
* @param extension
* string for instance "mp3" or "csv".
*/
public void filteredInput(String prompt, String callback, File path,
String extension) {
selectImpl(prompt, callback, path, extension, SelectMode.SELECT_FILE);
}


/**
* Starts open/save dialog.
*
Expand All @@ -104,6 +156,7 @@ public void selectFolder(String prompt, String callback) {
protected void selectImpl(final String prompt,
final String callbackMethod,
File defaultSelection,
final String ext,
final int mode) {
if (defaultSelection == null) {
defaultSelection = Environment.getExternalStorageDirectory();
Expand All @@ -113,6 +166,7 @@ protected void selectImpl(final String prompt,
i.putExtra(SelectDialog.EX_PATH, defaultSelection.getAbsolutePath());
i.putExtra(SelectDialog.EX_STYLE, mode);
i.putExtra(SelectDialog.EX_CALLBACK, callbackMethod);
i.putExtra(SelectDialog.EX_EXTENSION, ext);
i.putExtra(SelectDialog.EX_TITLE, prompt);

parent.runOnUiThread(new Runnable() {
Expand Down