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