EditNameDialog.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011 Bartek Przybylski
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.ui.dialog;
  19. import android.app.AlertDialog;
  20. import android.app.Dialog;
  21. import android.content.DialogInterface;
  22. import android.os.Bundle;
  23. import android.view.LayoutInflater;
  24. import android.view.View;
  25. import android.view.WindowManager.LayoutParams;
  26. import android.widget.TextView;
  27. import com.actionbarsherlock.app.SherlockDialogFragment;
  28. import com.owncloud.android.R;
  29. /**
  30. * Dialog to request the user to input a name, optionally initialized with a former name.
  31. *
  32. * @author Bartek Przybylski
  33. * @author David A. Velasco
  34. */
  35. public class EditNameDialog extends SherlockDialogFragment implements DialogInterface.OnClickListener {
  36. public static final String TAG = EditNameDialog.class.getSimpleName();
  37. protected static final String ARG_TITLE = "title";
  38. protected static final String ARG_NAME = "name";
  39. private String mNewFilename;
  40. private boolean mResult;
  41. private EditNameDialogListener mListener;
  42. /**
  43. * Public factory method to get dialog instances.
  44. *
  45. * @param title Text to show as title in the dialog.
  46. * @param name Optional text to include in the text input field when the dialog is shown.
  47. * @param listener Instance to notify when the dialog is dismissed.
  48. * @return New dialog instance, ready to show.
  49. */
  50. static public EditNameDialog newInstance(String title, String name, EditNameDialogListener listener) {
  51. EditNameDialog f = new EditNameDialog();
  52. Bundle args = new Bundle();
  53. args.putString(ARG_TITLE, title);
  54. args.putString(ARG_NAME, name);
  55. f.setArguments(args);
  56. f.setOnDismissListener(listener);
  57. return f;
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. @Override
  63. public Dialog onCreateDialog(Bundle savedInstanceState) {
  64. String currentName = getArguments().getString(ARG_NAME);
  65. if (currentName == null)
  66. currentName = "";
  67. String title = getArguments().getString(ARG_TITLE);
  68. // Inflate the layout for the dialog
  69. LayoutInflater inflater = getSherlockActivity().getLayoutInflater();
  70. View v = inflater.inflate(R.layout.edit_box_dialog, null); // null parent view because it will go in the dialog layout
  71. TextView inputText = ((TextView)v.findViewById(R.id.user_input));
  72. inputText.setText(currentName);
  73. // Set it to the dialog
  74. AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
  75. builder.setView(v)
  76. .setPositiveButton(R.string.common_ok, this)
  77. .setNegativeButton(R.string.common_cancel, this);
  78. if (title != null) {
  79. builder.setTitle(title);
  80. }
  81. mResult = false;
  82. Dialog d = builder.create();
  83. inputText.requestFocus();
  84. d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  85. return d;
  86. }
  87. /**
  88. * Performs the corresponding action when a dialog button is clicked.
  89. *
  90. * Saves the text in the input field to be accessed through {@link #getNewFilename()} when the positive
  91. * button is clicked.
  92. *
  93. * Notify the current listener in any case.
  94. */
  95. @Override
  96. public void onClick(DialogInterface dialog, int which) {
  97. switch (which) {
  98. case AlertDialog.BUTTON_POSITIVE: {
  99. mNewFilename = ((TextView)(getDialog().findViewById(R.id.user_input))).getText().toString();
  100. mResult = true;
  101. }
  102. case AlertDialog.BUTTON_NEGATIVE: { // fall through
  103. dismiss();
  104. if (mListener != null)
  105. mListener.onDismiss(this);
  106. }
  107. }
  108. }
  109. protected void setOnDismissListener(EditNameDialogListener listener) {
  110. mListener = listener;
  111. }
  112. /**
  113. * Returns the text in the input field after the user clicked the positive button.
  114. *
  115. * @return Text in the input field.
  116. */
  117. public String getNewFilename() {
  118. return mNewFilename;
  119. }
  120. /**
  121. *
  122. * @return True when the user clicked the positive button.
  123. */
  124. public boolean getResult() {
  125. return mResult;
  126. }
  127. /**
  128. * Interface to receive a notification when any button in the dialog is clicked.
  129. */
  130. public interface EditNameDialogListener {
  131. public void onDismiss(EditNameDialog dialog);
  132. }
  133. }