069eedb775936eb1ae7f440b592bbb3758168562
[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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
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
20 package com.owncloud.android.ui.dialog;
21
22 import android.app.AlertDialog;
23 import android.app.Dialog;
24 import android.content.DialogInterface;
25 import android.os.Bundle;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.WindowManager.LayoutParams;
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
48 private String mNewFilename;
49 private boolean mResult;
50 private EditNameDialogListener mListener;
51
52 /**
53 * Public factory method to get dialog instances.
54 *
55 * @param title Text to show as title in the dialog.
56 * @param name Optional text to include in the text input field when the dialog is shown.
57 * @param listener Instance to notify when the dialog is dismissed.
58 * @return New dialog instance, ready to show.
59 */
60 static public EditNameDialog newInstance(String title, String name, EditNameDialogListener listener) {
61 EditNameDialog f = new EditNameDialog();
62 Bundle args = new Bundle();
63 args.putString(ARG_TITLE, title);
64 args.putString(ARG_NAME, name);
65 f.setArguments(args);
66 f.setOnDismissListener(listener);
67 return f;
68 }
69
70
71 /**
72 * {@inheritDoc}
73 */
74 @Override
75 public Dialog onCreateDialog(Bundle savedInstanceState) {
76 String currentName = getArguments().getString(ARG_NAME);
77 if (currentName == null)
78 currentName = "";
79 String title = getArguments().getString(ARG_TITLE);
80
81 // Inflate the layout for the dialog
82 LayoutInflater inflater = getSherlockActivity().getLayoutInflater();
83 View v = inflater.inflate(R.layout.edit_box_dialog, null); // null parent view because it will go in the dialog layout
84 TextView inputText = ((TextView)v.findViewById(R.id.user_input));
85 inputText.setText(currentName);
86
87 // Set it to the dialog
88 AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
89 builder.setView(v)
90 .setPositiveButton(R.string.common_ok, this)
91 .setNegativeButton(R.string.common_cancel, this);
92
93 if (title != null) {
94 builder.setTitle(title);
95 }
96
97 mResult = false;
98
99 Dialog d = builder.create();
100
101 inputText.requestFocus();
102 d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
103 return d;
104 }
105
106
107 /**
108 * Performs the corresponding action when a dialog button is clicked.
109 *
110 * Saves the text in the input field to be accessed through {@link #getNewFilename()} when the positive
111 * button is clicked.
112 *
113 * Notify the current listener in any case.
114 */
115 @Override
116 public void onClick(DialogInterface dialog, int which) {
117 switch (which) {
118 case AlertDialog.BUTTON_POSITIVE: {
119 mNewFilename = ((TextView)(getDialog().findViewById(R.id.user_input))).getText().toString();
120 mResult = true;
121 }
122 case AlertDialog.BUTTON_NEGATIVE: { // fall through
123 dismiss();
124 if (mListener != null)
125 mListener.onDismiss(this);
126 }
127 }
128 }
129
130 protected void setOnDismissListener(EditNameDialogListener listener) {
131 mListener = listener;
132 }
133
134 /**
135 * Returns the text in the input field after the user clicked the positive button.
136 *
137 * @return Text in the input field.
138 */
139 public String getNewFilename() {
140 return mNewFilename;
141 }
142
143 /**
144 *
145 * @return True when the user clicked the positive button.
146 */
147 public boolean getResult() {
148 return mResult;
149 }
150
151
152 /**
153 * Interface to receive a notification when any button in the dialog is clicked.
154 */
155 public interface EditNameDialogListener {
156 public void onDismiss(EditNameDialog dialog);
157 }
158
159
160 }
161