OSDN Git Service

Fix InputNameDialog actions
authorjruesga <jorge@ruesga.com>
Sat, 6 Oct 2012 23:33:52 +0000 (01:33 +0200)
committerjruesga <jorge@ruesga.com>
Sat, 6 Oct 2012 23:33:52 +0000 (01:33 +0200)
* Fix Pass the files no selection
* New action: rename

res/values/strings.xml
src/com/cyanogenmod/explorer/listeners/OnSelectionListener.java
src/com/cyanogenmod/explorer/ui/dialogs/ActionsDialog.java
src/com/cyanogenmod/explorer/ui/dialogs/InputNameDialog.java
src/com/cyanogenmod/explorer/ui/policy/ActionsPolicy.java
src/com/cyanogenmod/explorer/ui/widgets/NavigationView.java
src/com/cyanogenmod/explorer/util/FileHelper.java

index 8f81a57..ea3831d 100644 (file)
   <string name="input_name_dialog_message_invalid_name">Invalid name. The names \'.\' and
     \'..\' are not allowed.</string>
   <!-- Enter Name Dialog * Message * Name exists -->
-  <string name="input_name_dialog_message_name_exists">The namealready exists.</string>
+  <string name="input_name_dialog_message_name_exists">The name already exists.</string>
 
   <!-- Associations Dialog * Title -->
   <string name="associations_dialog_title">Associations</string>
index e599df2..3c52ab6 100644 (file)
@@ -54,6 +54,14 @@ public interface OnSelectionListener {
     List<FileSystemObject> onRequestSelectedFiles();
 
     /**
+     * Method that request the current directory items
+     *
+     * @return List<FileSystemObject> The array of {@link FileSystemObject} objects of the
+     * current directory.
+     */
+    List<FileSystemObject> onRequestCurrentItems();
+
+    /**
      * Method that request the current directory of the selection info.
      *
      * @return String The current directory of the selection info.
index 53b3379..cc01678 100644 (file)
@@ -48,7 +48,10 @@ import com.cyanogenmod.explorer.util.SelectionHelper;
  */
 public class ActionsDialog implements OnItemClickListener, OnItemLongClickListener {
 
-    private final Context mContext;
+    /**
+     * @hide
+     */
+    final Context mContext;
     private final boolean mGlobal;
 
     /**
@@ -56,9 +59,15 @@ public class ActionsDialog implements OnItemClickListener, OnItemLongClickListen
      */
     AlertDialog mDialog;
     private ListView mListView;
-    private final FileSystemObject mFso;
+    /**
+     * @hide
+     */
+    final FileSystemObject mFso;
 
-    private OnRequestRefreshListener mOnRequestRefreshListener;
+    /**
+     * @hide
+     */
+    OnRequestRefreshListener mOnRequestRefreshListener;
 
     private OnSelectionListener mOnSelectionListener;
 
@@ -158,13 +167,22 @@ public class ActionsDialog implements OnItemClickListener, OnItemLongClickListen
                 }
                 break;
 
+            //- Rename
+            case R.id.mnu_actions_rename:
+                // Dialog is dismissed inside showInputNameDialog
+                if (this.mOnSelectionListener != null) {
+                    showFsoInputNameDialog(menuItem, this.mFso);
+                    return;
+                }
+                break;
+
             //- Delete
             case R.id.mnu_actions_delete:
                 ActionsPolicy.removeFileSystemObject(
                         this.mContext, this.mFso, this.mOnRequestRefreshListener);
                 break;
 
-            //- Delete
+            //- Refresh
             case R.id.mnu_actions_refresh:
                 if (this.mOnRequestRefreshListener != null) {
                     this.mOnRequestRefreshListener.onRequestRefresh(null); //Refresh all
@@ -236,7 +254,7 @@ public class ActionsDialog implements OnItemClickListener, OnItemLongClickListen
         final InputNameDialog inputNameDialog =
                 new InputNameDialog(
                         this.mContext,
-                        this.mOnSelectionListener.onRequestSelectedFiles(),
+                        this.mOnSelectionListener.onRequestCurrentItems(),
                         menuItem.getTitle().toString());
         inputNameDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
             @Override
@@ -262,6 +280,56 @@ public class ActionsDialog implements OnItemClickListener, OnItemLongClickListen
     }
 
     /**
+     * Method that show a new dialog for input a name for an existing fso.
+     *
+     * @param menuItem The item menu associated
+     * @param fso The file system object
+     */
+    private void showFsoInputNameDialog(final MenuItem menuItem, final FileSystemObject fso) {
+        //Hide the dialog
+        this.mDialog.hide();
+
+        //Show the input name dialog
+        final InputNameDialog inputNameDialog =
+                new InputNameDialog(
+                        this.mContext,
+                        this.mOnSelectionListener.onRequestCurrentItems(),
+                        fso,
+                        menuItem.getTitle().toString());
+        inputNameDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
+            @Override
+            public void onCancel(DialogInterface dialog) {
+                //Show the menu again
+                ActionsDialog.this.mDialog.show();
+            }
+        });
+        inputNameDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
+            @Override
+            public void onDismiss(DialogInterface dialog) {
+                //Retrieve the name an execute the action
+                try {
+                    String name = inputNameDialog.getName();
+                    switch (menuItem.getItemId()) {
+                        case R.id.mnu_actions_rename:
+                            ActionsPolicy.renameFileSystemObject(
+                                    ActionsDialog.this.mContext,
+                                    ActionsDialog.this.mFso,
+                                    name,
+                                    ActionsDialog.this.mOnRequestRefreshListener);
+                            break;
+                        default:
+                            break;
+                    }
+
+                } finally {
+                    ActionsDialog.this.mDialog.dismiss();
+                }
+            }
+        });
+        inputNameDialog.show();
+    }
+
+    /**
      * {@inheritDoc}
      */
     @Override
index d952e90..ebb5018 100644 (file)
@@ -30,6 +30,7 @@ import android.widget.TextView;
 import com.cyanogenmod.explorer.R;
 import com.cyanogenmod.explorer.model.FileSystemObject;
 import com.cyanogenmod.explorer.util.DialogHelper;
+import com.cyanogenmod.explorer.util.FileHelper;
 
 import java.io.File;
 import java.util.List;
@@ -41,13 +42,17 @@ public class InputNameDialog
     implements TextWatcher, DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
 
     private final Context mContext;
-    private final AlertDialog mDialog;
+    /**
+     * @hide
+     */
+    final AlertDialog mDialog;
     private final TextView mMsg;
     /**
      * @hide
      */
     final EditText mEditText;
     private final List<FileSystemObject> mFiles;
+    private final FileSystemObject mFso;
 
     private DialogInterface.OnCancelListener mOnCancelListener;
     private DialogInterface.OnDismissListener mOnDismissListener;
@@ -65,6 +70,20 @@ public class InputNameDialog
      */
     public InputNameDialog(
             final Context context, List<FileSystemObject> files, String dialogTitle) {
+        this(context, files, null, dialogTitle);
+    }
+
+    /**
+     * Constructor of <code>InputNameDialog</code>.
+     *
+     * @param context The current context
+     * @param files The files of the current directory (used to validate the name)
+     * @param fso The original file system object. null if not needed.
+     * @param dialogTitle The dialog title
+     */
+    public InputNameDialog(
+            final Context context, List<FileSystemObject> files,
+            FileSystemObject fso, String dialogTitle) {
         super();
 
         //Save the context
@@ -72,6 +91,7 @@ public class InputNameDialog
 
         //Save the files
         this.mFiles = files;
+        this.mFso = fso;
         this.mCancelled = true;
 
         //Create the
@@ -81,7 +101,11 @@ public class InputNameDialog
         TextView title = (TextView)v.findViewById(R.id.input_name_dialog_label);
         title.setText(R.string.input_name_dialog_label);
         this.mEditText = (EditText)v.findViewById(R.id.input_name_dialog_edit);
-        this.mEditText.setText(dialogTitle);
+        if (this.mFso != null) {
+            this.mEditText.setText(this.mFso.getName());
+        } else {
+            this.mEditText.setText(dialogTitle);
+        }
         this.mEditText.selectAll();
         this.mEditText.addTextChangedListener(this);
         this.mMsg = (TextView)v.findViewById(R.id.input_name_dialog_message);
@@ -115,6 +139,17 @@ public class InputNameDialog
                 (DialogInterface.OnClickListener)null);
         this.mDialog.setOnCancelListener(this);
         this.mDialog.setOnDismissListener(this);
+
+        // Disable accept button, because name is the same as fso
+        if (this.mFso != null) {
+            this.mEditText.post(new Runnable() {
+                @Override
+                public void run() {
+                    InputNameDialog.this.mDialog.getButton(
+                            DialogInterface.BUTTON_POSITIVE).setEnabled(false);
+                }
+            });
+        }
     }
 
     /**
@@ -184,19 +219,24 @@ public class InputNameDialog
                       R.string.input_name_dialog_message_empty_name), false);
             return;
         }
-        if (txt.indexOf("/") != -1) { //$NON-NLS-1$
+        if (txt.indexOf(File.separator) != -1) {
             setMsg(
                 InputNameDialog.this.mContext.getString(
                       R.string.input_name_dialog_message_invalid_path_name,
                       File.separator), false);
             return;
         }
-        if (txt.compareTo(".") == 0 || txt.compareTo("..") == 0) { //$NON-NLS-1$ //$NON-NLS-2$
+        if (txt.compareTo(FileHelper.CURRENT_DIRECTORY) == 0 ||
+            txt.compareTo(FileHelper.PARENT_DIRECTORY) == 0) {
             setMsg(
                 InputNameDialog.this.mContext.getString(
                         R.string.input_name_dialog_message_invalid_name), false);
             return;
         }
+        if (this.mFso != null && txt.compareTo(this.mFso.getName()) == 0) {
+            setMsg(null, false);
+            return;
+        }
         if (isNameExists(txt)) {
             setMsg(
                 InputNameDialog.this.mContext.getString(
index dc73042..042d4a5 100644 (file)
@@ -451,4 +451,40 @@ public final class ActionsPolicy {
            });
         dialog.show();
     }
+
+    /**
+     * Method that remove an existing file system object.
+     *
+     * @param ctx The current context
+     * @param fso The file system object
+     * @param newName The new name of the object
+     * @param onRequestRefreshListener The listener for request a refresh after the new
+     * folder was created (option)
+     */
+    public static void renameFileSystemObject(
+            final Context ctx, final FileSystemObject fso, final String newName,
+            final OnRequestRefreshListener onRequestRefreshListener) {
+        try {
+            File newFile = new File(fso.getParent(), newName);
+            CommandHelper.move(ctx, fso.getFullPath(), newFile.getAbsolutePath(), null);
+
+            // Check that the operation was completed retrieving the fso modified
+            CommandHelper.getFileInfo(ctx, newFile.getAbsolutePath(), null);
+
+            //Operation complete. Refresh
+            if (onRequestRefreshListener != null) {
+                // The reference is not the same, so refresh the complete navigation view
+                onRequestRefreshListener.onRequestRefresh(null);
+            }
+
+            //Operation complete. Refresh
+            if (onRequestRefreshListener != null) {
+                onRequestRefreshListener.onRequestRemove(fso);
+            }
+
+        } catch (Throwable ex) {
+            // Capture the exception
+            ExceptionUtil.translateException(ctx, ex, false, true, null);
+        }
+    }
 }
\ No newline at end of file
index 7e479e1..deff821 100644 (file)
@@ -916,6 +916,14 @@ public class NavigationView extends RelativeLayout implements
      * {@inheritDoc}
      */
     @Override
+    public List<FileSystemObject> onRequestCurrentItems() {
+        return this.getFiles();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
     public String onRequestCurrentDirOfSelectionData() {
         return this.mCurrentDir;
     }
index f864e33..80112a6 100644 (file)
@@ -585,4 +585,28 @@ public final class FileHelper {
         return o1.compareTo(fso2);
     }
 
+    /**
+     * Method that add to the path the trailing slash
+     *
+     * @param path The path
+     * @return String The path with the trailing slash
+     */
+    public static String addTrailingSlash(String path) {
+        return path.endsWith(File.separator) ? path : path + File.separator;
+    }
+
+    /**
+     * Method that cleans the path and removes the trailing slash
+     *
+     * @param path The path to clean
+     * @return String The path without the trailing slash
+     */
+    public static String removeTrailingSlash(String path) {
+        if (path.trim().compareTo(ROOT_DIRECTORY) == 0) return path;
+        if (path.endsWith(File.separator)) {
+            return path.substring(0, path.length()-1);
+        }
+        return path;
+    }
+
 }