diff --git a/src/select/files/SelectDialog.java b/src/select/files/SelectDialog.java index 7db3b09..80d0c69 100644 --- a/src/select/files/SelectDialog.java +++ b/src/select/files/SelectDialog.java @@ -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 simpleAdapter = null; private SelectMode selectMode = null; + private String extension = null; private final Intent intent; private PApplet parent; @@ -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(getContext(), android.R.layout.simple_list_item_2, android.R.id.text1) { @@ -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(); } diff --git a/src/select/files/SelectLibrary.java b/src/select/files/SelectLibrary.java index f18c57e..1195c4d 100644 --- a/src/select/files/SelectLibrary.java +++ b/src/select/files/SelectLibrary.java @@ -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); } /** @@ -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); } /** @@ -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. * @@ -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(); @@ -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() {