Merge branch 'develop' into refactor_remote_operation_to_create_folder
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / EditNameDialog.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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
19 package com.owncloud.android.ui.dialog;
20
21 import android.app.AlertDialog;
22 import android.app.Dialog;
23 import android.content.DialogInterface;
24 import android.os.Bundle;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.WindowManager.LayoutParams;
28 import android.widget.EditText;
29 import android.widget.TextView;
30
31 import com.actionbarsherlock.app.SherlockDialogFragment;
32 import com.owncloud.android.R;
33
34
35
36 /**
37 * Dialog to request the user to input a name, optionally initialized with a former name.
38 *
39 * @author Bartek Przybylski
40 * @author David A. Velasco
41 */
42 public class EditNameDialog extends SherlockDialogFragment implements DialogInterface.OnClickListener {
43
44 public static final String TAG = EditNameDialog.class.getSimpleName();
45
46 protected static final String ARG_TITLE = "TITLE";
47 protected static final String ARG_NAME = "NAME";
48 protected static final String ARG_SELECTION_START = "SELECTION_START";
49 protected static final String ARG_SELECTION_END = "SELECTION_END";
50
51 private String mNewFilename;
52 private boolean mResult;
53 private EditNameDialogListener mListener;
54
55 /**
56 * Public factory method to get dialog instances.
57 *
58 * @param title Text to show as title in the dialog.
59 * @param name Optional text to include in the text input field when the dialog is shown.
60 * @param listener Instance to notify when the dialog is dismissed.
61 * @param selectionStart Index to the first character to be selected in the input field; negative value for none
62 * @param selectionEnd Index to the last character to be selected in the input field; negative value for none
63 * @return New dialog instance, ready to show.
64 */
65 static public EditNameDialog newInstance(String title, String name, int selectionStart, int selectionEnd, EditNameDialogListener listener) {
66 EditNameDialog f = new EditNameDialog();
67 Bundle args = new Bundle();
68 args.putString(ARG_TITLE, title);
69 args.putString(ARG_NAME, name);
70 args.putInt(ARG_SELECTION_START, selectionStart);
71 args.putInt(ARG_SELECTION_END, selectionEnd);
72 f.setArguments(args);
73 f.setOnDismissListener(listener);
74 return f;
75 }
76
77
78 /**
79 * {@inheritDoc}
80 */
81 @Override
82 public Dialog onCreateDialog(Bundle savedInstanceState) {
83 String currentName = getArguments().getString(ARG_NAME);
84 if (currentName == null)
85 currentName = "";
86 String title = getArguments().getString(ARG_TITLE);
87
88 // Inflate the layout for the dialog
89 LayoutInflater inflater = getSherlockActivity().getLayoutInflater();
90 View v = inflater.inflate(R.layout.edit_box_dialog, null); // null parent view because it will go in the dialog layout
91 EditText inputText = ((EditText)v.findViewById(R.id.user_input));
92 inputText.setText(currentName);
93
94 // Set it to the dialog
95 AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
96 builder.setView(v)
97 .setPositiveButton(R.string.common_ok, this)
98 .setNegativeButton(R.string.common_cancel, this);
99
100 if (title != null) {
101 builder.setTitle(title);
102 }
103
104 mResult = false;
105
106 Dialog d = builder.create();
107
108 inputText.requestFocus();
109 int selectionStart = getArguments().getInt(ARG_SELECTION_START, -1);
110 int selectionEnd = getArguments().getInt(ARG_SELECTION_END, -1);
111 if (selectionStart >= 0 && selectionEnd >= 0) {
112 inputText.setSelection(Math.min(selectionStart, selectionEnd), Math.max(selectionStart, selectionEnd));
113 }
114 d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
115 return d;
116 }
117
118
119 /**
120 * Performs the corresponding action when a dialog button is clicked.
121 *
122 * Saves the text in the input field to be accessed through {@link #getNewFilename()} when the positive
123 * button is clicked.
124 *
125 * Notify the current listener in any case.
126 */
127 @Override
128 public void onClick(DialogInterface dialog, int which) {
129 switch (which) {
130 case AlertDialog.BUTTON_POSITIVE: {
131 mNewFilename = ((TextView)(getDialog().findViewById(R.id.user_input))).getText().toString();
132 mResult = true;
133 }
134 case AlertDialog.BUTTON_NEGATIVE: { // fall through
135 dismiss();
136 if (mListener != null)
137 mListener.onDismiss(this);
138 }
139 }
140 }
141
142 protected void setOnDismissListener(EditNameDialogListener listener) {
143 mListener = listener;
144 }
145
146 /**
147 * Returns the text in the input field after the user clicked the positive button.
148 *
149 * @return Text in the input field.
150 */
151 public String getNewFilename() {
152 return mNewFilename;
153 }
154
155 /**
156 *
157 * @return True when the user clicked the positive button.
158 */
159 public boolean getResult() {
160 return mResult;
161 }
162
163
164 /**
165 * Interface to receive a notification when any button in the dialog is clicked.
166 */
167 public interface EditNameDialogListener {
168 public void onDismiss(EditNameDialog dialog);
169 }
170
171
172 }
173