RenameFileDialogFragment.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* ownCloud Android client application
  2. *
  3. * @author David A. Velasco
  4. * Copyright (C) 2014 ownCloud Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2,
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package com.owncloud.android.ui.dialog;
  20. /**
  21. * Dialog to input a new name for an {@link OCFile} being renamed.
  22. *
  23. * Triggers the rename operation.
  24. */
  25. import android.app.AlertDialog;
  26. import android.app.Dialog;
  27. import android.content.DialogInterface;
  28. import android.os.Bundle;
  29. import android.view.LayoutInflater;
  30. import android.view.View;
  31. import android.view.WindowManager.LayoutParams;
  32. import android.widget.EditText;
  33. import android.widget.TextView;
  34. import android.widget.Toast;
  35. import com.actionbarsherlock.app.SherlockDialogFragment;
  36. import com.owncloud.android.R;
  37. import com.owncloud.android.datamodel.OCFile;
  38. import com.owncloud.android.lib.resources.files.FileUtils;
  39. import com.owncloud.android.ui.activity.ComponentsGetter;
  40. /**
  41. * Dialog to input a new name for a file or folder to rename.
  42. *
  43. * Triggers the rename operation when name is confirmed.
  44. */
  45. public class RenameFileDialogFragment
  46. extends SherlockDialogFragment implements DialogInterface.OnClickListener {
  47. private static final String ARG_TARGET_FILE = "TARGET_FILE";
  48. /**
  49. * Public factory method to create new RenameFileDialogFragment instances.
  50. *
  51. * @param file File to rename.
  52. * @return Dialog ready to show.
  53. */
  54. public static RenameFileDialogFragment newInstance(OCFile file) {
  55. RenameFileDialogFragment frag = new RenameFileDialogFragment();
  56. Bundle args = new Bundle();
  57. args.putParcelable(ARG_TARGET_FILE, file);
  58. frag.setArguments(args);
  59. return frag;
  60. }
  61. private OCFile mTargetFile;
  62. @Override
  63. public Dialog onCreateDialog(Bundle savedInstanceState) {
  64. mTargetFile = getArguments().getParcelable(ARG_TARGET_FILE);
  65. // Inflate the layout for the dialog
  66. LayoutInflater inflater = getSherlockActivity().getLayoutInflater();
  67. View v = inflater.inflate(R.layout.edit_box_dialog, null);
  68. // Setup layout
  69. String currentName = mTargetFile.getFileName();
  70. EditText inputText = ((EditText)v.findViewById(R.id.user_input));
  71. inputText.setText(currentName);
  72. int selectionStart = 0;
  73. int extensionStart = mTargetFile.isFolder() ? -1 : currentName.lastIndexOf(".");
  74. int selectionEnd = (extensionStart >= 0) ? extensionStart : currentName.length();
  75. if (selectionStart >= 0 && selectionEnd >= 0) {
  76. inputText.setSelection(
  77. Math.min(selectionStart, selectionEnd),
  78. Math.max(selectionStart, selectionEnd));
  79. }
  80. inputText.requestFocus();
  81. // Build the dialog
  82. AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
  83. builder.setView(v)
  84. .setPositiveButton(R.string.common_ok, this)
  85. .setNegativeButton(R.string.common_cancel, this)
  86. .setTitle(R.string.rename_dialog_title);
  87. Dialog d = builder.create();
  88. d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  89. return d;
  90. }
  91. @Override
  92. public void onClick(DialogInterface dialog, int which) {
  93. if (which == AlertDialog.BUTTON_POSITIVE) {
  94. String newFileName =
  95. ((TextView)(getDialog().findViewById(R.id.user_input)))
  96. .getText().toString().trim();
  97. if (newFileName.length() <= 0) {
  98. Toast.makeText(
  99. getSherlockActivity(),
  100. R.string.filename_empty,
  101. Toast.LENGTH_LONG).show();
  102. return;
  103. }
  104. if (!FileUtils.isValidName(newFileName)) {
  105. Toast.makeText(
  106. getSherlockActivity(),
  107. R.string.filename_forbidden_characters,
  108. Toast.LENGTH_LONG).show();
  109. return;
  110. }
  111. ((ComponentsGetter)getSherlockActivity()).getFileOperationsHelper().renameFile(mTargetFile, newFileName);
  112. }
  113. }
  114. }