Fixed bug in File browser where Select button gets disabled when browsing into a directory

This commit is contained in:
Ziver Koc 2015-01-15 14:14:00 +01:00
parent 20a120a07e
commit d9c924009d

View file

@ -9,7 +9,9 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable;
import android.text.InputType; import android.text.InputType;
import android.text.TextWatcher;
import android.view.ContextThemeWrapper; import android.view.ContextThemeWrapper;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View; import android.view.View;
@ -34,11 +36,11 @@ import java.util.Collections;
import java.util.List; import java.util.List;
/** Allow user to select destination directory and to enter filename. /**
* * Allow user to select destination directory and to enter filename.
* */ * */
public class FileBrowserDialog extends DialogFragment public class FileBrowserDialog extends DialogFragment
implements OnItemClickListener, DialogInterface.OnKeyListener, DialogInterface.OnClickListener{ implements OnItemClickListener, DialogInterface.OnKeyListener, DialogInterface.OnClickListener, TextWatcher {
public static final Logger log = Logger.getLogger(FileBrowserDialog.class); public static final Logger log = Logger.getLogger(FileBrowserDialog.class);
public static final int FOLDER_ICON = R.drawable.folder; public static final int FOLDER_ICON = R.drawable.folder;
public static final int FILE_ICON = R.drawable.file; public static final int FILE_ICON = R.drawable.file;
@ -131,6 +133,7 @@ public class FileBrowserDialog extends DialogFragment
LinearLayout.LayoutParams.WRAP_CONTENT)); LinearLayout.LayoutParams.WRAP_CONTENT));
fileNameView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); fileNameView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
fileNameView.setHint("File Name"); fileNameView.setHint("File Name");
fileNameView.addTextChangedListener(this);
rootView.addView(fileNameView); rootView.addView(fileNameView);
} }
@ -148,12 +151,13 @@ public class FileBrowserDialog extends DialogFragment
AlertDialog dialog = builder.create(); AlertDialog dialog = builder.create();
dialog.show(); dialog.show();
if(mode == BrowserMode.SELECT_FILE) dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
return dialog; return dialog;
} }
/**
* A file or folder has been clicked
*/
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
File file = (File)fileListView.getAdapter().getItem(position); File file = (File)fileListView.getAdapter().getItem(position);
@ -172,6 +176,9 @@ public class FileBrowserDialog extends DialogFragment
} }
} }
/**
* The "select" button has been pressed
*/
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
if(mode == BrowserMode.SELECT_FILE && selection != null) if(mode == BrowserMode.SELECT_FILE && selection != null)
@ -183,6 +190,9 @@ public class FileBrowserDialog extends DialogFragment
Toast.makeText(this.getActivity(), "No File Selected", Toast.LENGTH_SHORT).show(); Toast.makeText(this.getActivity(), "No File Selected", Toast.LENGTH_SHORT).show();
} }
/**
* Back button has been pressed
*/
@Override @Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) { if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
@ -195,6 +205,23 @@ public class FileBrowserDialog extends DialogFragment
return false; return false;
} }
/**
* Text input event for new file text field
*/
@Override
public void afterTextChanged(Editable editable) {
if(this.getDialog() != null) {
if (fileNameView.getText().toString().isEmpty())
((AlertDialog) this.getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
else
((AlertDialog) this.getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
}
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
private void updateFileList(File dir){ private void updateFileList(File dir){
if(!dir.isDirectory()) if(!dir.isDirectory())
@ -221,7 +248,9 @@ public class FileBrowserDialog extends DialogFragment
// Reset selection // Reset selection
selection = null; selection = null;
if(this.getDialog() != null) if(this.getDialog() != null &&
(mode == BrowserMode.SELECT_FILE ||
mode == BrowserMode.NEW_FILE && fileNameView.getText().toString().isEmpty()))
((AlertDialog)this.getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); ((AlertDialog)this.getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
} }