View Javadoc
1   /*
2    * Copyright (C) 2003-2014 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 3 of
7    * the License, or (at your option) any later version.
8    *
9    * This software 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 GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18   */
19  package org.exoplatform.widget;
20  
21  import org.exoplatform.R;
22  import org.exoplatform.model.ExoFile;
23  import org.exoplatform.ui.DocumentActivity;
24  import org.exoplatform.utils.ExoDocumentUtils;
25  import org.exoplatform.utils.ExoUtils;
26  
27  import android.app.Dialog;
28  import android.content.Context;
29  import android.content.res.Resources;
30  import android.view.Gravity;
31  import android.view.View;
32  import android.view.Window;
33  import android.widget.Button;
34  import android.widget.EditText;
35  import android.widget.TextView;
36  import android.widget.Toast;
37  
38  public class DocumentExtendDialog extends Dialog implements android.view.View.OnClickListener {
39  
40    private String   renameTitleStr;
41  
42    private String   createTitleStr;
43  
44    private String   renameActionTitle;
45  
46    private String   createActionTitle;
47  
48    private String   okStr;
49  
50    private String   cancelStr;
51  
52    private String   inputTextWarning;
53  
54    private String   inputNameURLInvalid;
55  
56    private int      actionId;
57  
58    private TextView titleTextView;
59  
60    private TextView actionTitleView;
61  
62    private EditText actionEditText;
63  
64    private String   folderName;
65  
66    private Button   okButton;
67  
68    private Button   cancelButton;
69  
70    private ExoFile  selectedFile;
71  
72    public DocumentExtendDialog(Context context, ExoFile file, int id) {
73      super(context);
74      requestWindowFeature(Window.FEATURE_NO_TITLE);
75      setContentView(R.layout.document_extend_dialog_layout);
76      actionId = id;
77      selectedFile = file;
78      onChangeLanguage();
79      titleTextView = (TextView) findViewById(R.id.document_extend_dialog_title);
80      actionTitleView = (TextView) findViewById(R.id.document_extend_textview);
81      actionEditText = (EditText) findViewById(R.id.document_extend_edittext);
82      okButton = (Button) findViewById(R.id.document_extend_ok);
83      okButton.setText(okStr);
84      okButton.setOnClickListener(this);
85      cancelButton = (Button) findViewById(R.id.document_extend_cancel);
86      cancelButton.setText(cancelStr);
87      cancelButton.setOnClickListener(this);
88      initInformation();
89    }
90  
91    private void initInformation() {
92      if (actionId == DocumentActivity.ACTION_RENAME) {
93        titleTextView.setText(renameTitleStr);
94        actionTitleView.setText(renameActionTitle);
95        actionEditText.setText(selectedFile.name);
96      } else if (actionId == DocumentActivity.ACTION_CREATE) {
97        titleTextView.setText(createTitleStr);
98        actionTitleView.setText(createActionTitle);
99      }
100 
101   }
102 
103   public void onClick(View view) {
104     if (view.equals(okButton)) {
105       folderName = actionEditText.getText().toString();
106 
107       if ((folderName != null) && (folderName.length() > 0)) {
108 
109         if (actionId == DocumentActivity.ACTION_RENAME) {
110           if (DocumentActivity._documentActivityInstance._fileForCurrentActionBar != null) {
111             String currentFolder = DocumentActivity._documentActivityInstance._fileForCurrentActionBar.currentFolder;
112             String destinationUrl = ExoDocumentUtils.getParentUrl(selectedFile.path) + "/" + folderName;
113             if (currentFolder.equalsIgnoreCase(selectedFile.currentFolder)) {
114               DocumentActivity._documentActivityInstance._fileForCurrentActionBar.name = folderName;
115               currentFolder = ExoDocumentUtils.getParentUrl(currentFolder) + "/" + folderName;
116               DocumentActivity._documentActivityInstance._fileForCurrentActionBar.currentFolder = currentFolder;
117             }
118             if (ExoUtils.isDocumentUrlValid(destinationUrl)) {
119               DocumentActivity._documentActivityInstance.renameFile(selectedFile, destinationUrl);
120             } else {
121               Toast toast = Toast.makeText(getContext(), inputNameURLInvalid, Toast.LENGTH_SHORT);
122               toast.setGravity(Gravity.CENTER, 0, 0);
123               toast.show();
124             }
125 
126           }
127 
128         } else {
129           String desUrl = selectedFile.path + "/" + folderName;
130           if (ExoUtils.isDocumentUrlValid(desUrl)) {
131             DocumentActivity._documentActivityInstance.createFile(selectedFile, desUrl);
132           } else {
133             Toast toast = Toast.makeText(getContext(), inputNameURLInvalid, Toast.LENGTH_SHORT);
134             toast.setGravity(Gravity.CENTER, 0, 0);
135             toast.show();
136           }
137 
138         }
139         dismiss();
140       } else {
141         Toast toast = Toast.makeText(getContext(), inputTextWarning, Toast.LENGTH_SHORT);
142         toast.setGravity(Gravity.BOTTOM, 0, 0);
143         toast.show();
144       }
145     }
146     if (view.equals(cancelButton)) {
147       dismiss();
148     }
149 
150   }
151 
152   private void onChangeLanguage() {
153     Resources res = getContext().getResources();
154     renameTitleStr = res.getString(R.string.DocumentRenameTitle);
155     renameActionTitle = res.getString(R.string.DocumentRenameContent);
156     createTitleStr = res.getString(R.string.DocumentCreateTitle);
157     createActionTitle = res.getString(R.string.DocumentCreateContent);
158     okStr = res.getString(R.string.OK);
159     cancelStr = res.getString(R.string.Cancel);
160     inputTextWarning = res.getString(R.string.DocumentFolderNameEmpty);
161     inputNameURLInvalid = res.getString(R.string.SpecialCharacters);
162 
163   }
164 
165 }