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