1 /* ownCloud Android client application
2 * Copyright (C) 2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.dialog
;
21 * Dialog to input a new name for an {@link OCFile} being renamed.
23 * Triggers the rename operation.
25 import android
.app
.AlertDialog
;
26 import android
.app
.Dialog
;
27 import android
.content
.DialogInterface
;
28 import android
.os
.Bundle
;
29 import android
.view
.LayoutInflater
;
30 import android
.view
.View
;
31 import android
.view
.WindowManager
.LayoutParams
;
32 import android
.widget
.EditText
;
33 import android
.widget
.TextView
;
34 import android
.widget
.Toast
;
36 import com
.actionbarsherlock
.app
.SherlockDialogFragment
;
37 import com
.owncloud
.android
.R
;
38 import com
.owncloud
.android
.datamodel
.OCFile
;
39 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
40 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
44 * Dialog to input a new name for a file or folder to rename.
46 * Triggers the rename operation when name is confirmed.
48 * @author David A. Velasco
50 public class RenameFileDialogFragment
51 extends SherlockDialogFragment
implements DialogInterface
.OnClickListener
{
53 private static final String ARG_TARGET_FILE
= "TARGET_FILE";
56 * Public factory method to create new RenameFileDialogFragment instances.
58 * @param file File to rename.
59 * @return Dialog ready to show.
61 public static RenameFileDialogFragment
newInstance(OCFile file
) {
62 RenameFileDialogFragment frag
= new RenameFileDialogFragment();
63 Bundle args
= new Bundle();
64 args
.putParcelable(ARG_TARGET_FILE
, file
);
65 frag
.setArguments(args
);
70 private OCFile mTargetFile
;
73 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
74 mTargetFile
= getArguments().getParcelable(ARG_TARGET_FILE
);
76 // Inflate the layout for the dialog
77 LayoutInflater inflater
= getSherlockActivity().getLayoutInflater();
78 View v
= inflater
.inflate(R
.layout
.edit_box_dialog
, null
);
81 String currentName
= mTargetFile
.getFileName();
82 EditText inputText
= ((EditText
)v
.findViewById(R
.id
.user_input
));
83 inputText
.setText(currentName
);
84 int selectionStart
= 0;
85 int extensionStart
= mTargetFile
.isFolder() ?
-1 : currentName
.lastIndexOf(".");
86 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: currentName
.length();
87 if (selectionStart
>= 0 && selectionEnd
>= 0) {
88 inputText
.setSelection(
89 Math
.min(selectionStart
, selectionEnd
),
90 Math
.max(selectionStart
, selectionEnd
));
92 inputText
.requestFocus();
95 AlertDialog
.Builder builder
= new AlertDialog
.Builder(getSherlockActivity());
97 .setPositiveButton(R
.string
.common_ok
, this)
98 .setNegativeButton(R
.string
.common_cancel
, this)
99 .setTitle(R
.string
.rename_dialog_title
);
100 Dialog d
= builder
.create();
101 d
.getWindow().setSoftInputMode(LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
107 public void onClick(DialogInterface dialog
, int which
) {
108 if (which
== AlertDialog
.BUTTON_POSITIVE
) {
110 ((TextView
)(getDialog().findViewById(R
.id
.user_input
)))
111 .getText().toString().trim();
113 if (newFileName
.length() <= 0) {
115 getSherlockActivity(),
116 R
.string
.filename_empty
,
117 Toast
.LENGTH_LONG
).show();
121 if (!FileUtils
.isValidName(newFileName
)) {
123 getSherlockActivity(),
124 R
.string
.filename_forbidden_characters
,
125 Toast
.LENGTH_LONG
).show();
129 ((ComponentsGetter
)getSherlockActivity()).getFileOperationsHelper().renameFile(mTargetFile
, newFileName
);