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.controller.document;
20  
21  import java.util.ArrayList;
22  
23  import org.exoplatform.R;
24  import org.exoplatform.model.DocumentActionDescription;
25  import org.exoplatform.model.ExoFile;
26  import org.exoplatform.singleton.DocumentHelper;
27  import org.exoplatform.ui.DocumentActionDialog;
28  import org.exoplatform.ui.DocumentActivity;
29  import org.exoplatform.utils.ExoDocumentUtils;
30  import org.exoplatform.widget.AddPhotoDialog;
31  import org.exoplatform.widget.DocumentExtendDialog;
32  
33  import android.app.AlertDialog;
34  import android.content.Context;
35  import android.content.DialogInterface;
36  import android.content.res.Resources;
37  import android.view.LayoutInflater;
38  import android.view.View;
39  import android.view.ViewGroup;
40  import android.widget.BaseAdapter;
41  import android.widget.ImageView;
42  import android.widget.TextView;
43  
44  public class DocumentActionAdapter extends BaseAdapter {
45  
46    public ExoFile                               _selectedFile;
47  
48    private DocumentActivity                     mDocumentActivity;
49  
50    private DocumentActionDialog                 _delegate;
51  
52    private DocumentActionDescription[]          fileActions = null;
53  
54    private ArrayList<DocumentActionDescription> fileActionList;
55  
56    private DocumentExtendDialog                 extendDialog;
57  
58    public DocumentActionAdapter(DocumentActivity context, DocumentActionDialog parent, ExoFile file, boolean isActionBar) {
59  
60      mDocumentActivity = context;
61      _delegate = parent;
62      _selectedFile = file;
63      changeLanguage(isActionBar);
64  
65    }
66  
67    public void setSelectedFile(ExoFile file) {
68      _selectedFile = file;
69    }
70  
71    public void initCamera() {
72      mDocumentActivity.takePicture();
73    }
74  
75    public View getView(int position, View convertView, ViewGroup parent) {
76  
77      final int pos = position;
78      LayoutInflater inflater = (LayoutInflater) mDocumentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
79      // TODO use ViewHolder pattern
80      View rowView = inflater.inflate(R.layout.fileactionitem, parent, false);
81      rowView.setOnClickListener(new View.OnClickListener() {
82  
83        public void onClick(View v) {
84  
85          _delegate.dismiss();
86  
87          switch (pos) {
88          case DocumentActivity.ACTION_ADD_PHOTO:
89            new AddPhotoDialog(mDocumentActivity, _delegate).show();
90            break;
91          case DocumentActivity.ACTION_COPY:
92            DocumentHelper.getInstance()._fileCopied = _selectedFile;
93            DocumentHelper.getInstance()._fileMoved = new ExoFile();
94            break;
95  
96          case DocumentActivity.ACTION_MOVE:
97            DocumentHelper.getInstance()._fileMoved = _selectedFile;
98            DocumentHelper.getInstance()._fileCopied = new ExoFile();
99            break;
100 
101         case DocumentActivity.ACTION_PASTE:
102           ExoFile _fileCopied = DocumentHelper.getInstance()._fileCopied;
103           if (!"".equals(_fileCopied.path)) {
104             String lastPathComponent = ExoDocumentUtils.getLastPathComponent(_fileCopied.path);
105             String destinationUrl = _selectedFile.path + "/" + lastPathComponent;
106             DocumentActivity._documentActivityInstance.pasteFile(_fileCopied, destinationUrl, DocumentActivity.ACTION_COPY);
107 
108           }
109           ExoFile _fileMoved = DocumentHelper.getInstance()._fileMoved;
110           if (!"".equals(_fileMoved.path)) {
111             String lastPathComponent = ExoDocumentUtils.getLastPathComponent(_fileMoved.path);
112             String destinationUrl = _selectedFile.path + "/" + lastPathComponent;
113             DocumentActivity._documentActivityInstance.pasteFile(_fileMoved, destinationUrl, DocumentActivity.ACTION_MOVE);
114           }
115           DocumentHelper.getInstance()._fileCopied = new ExoFile();
116           DocumentHelper.getInstance()._fileMoved = new ExoFile();
117 
118           break;
119         case DocumentActivity.ACTION_DELETE:
120           Context ctx = v.getContext();
121           AlertDialog.Builder bld = new AlertDialog.Builder(ctx);
122           int selectedTypeStrId = R.string.File;
123           if (_selectedFile.isFolder) {
124             bld.setTitle(R.string.DeleteConfirmFolderTitle);
125             selectedTypeStrId = R.string.Folder;
126           } else {
127             bld.setTitle(R.string.DeleteConfirmFileTitle);
128           }
129           bld.setMessage(ctx.getString(R.string.DeleteConfirmMessage, ctx.getString(selectedTypeStrId), _selectedFile.name));
130           bld.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
131 
132             @Override
133             public void onClick(DialogInterface dialog, int which) {
134               String currentFolder = DocumentActivity._documentActivityInstance._fileForCurrentActionBar.currentFolder;
135               if (currentFolder.equalsIgnoreCase(_selectedFile.currentFolder) && _selectedFile.isFolder) {
136                 DocumentActivity._documentActivityInstance._fileForCurrentActionBar = 
137                     DocumentHelper.getInstance().folderToParentMap.getParcelable(DocumentActivity._documentActivityInstance._fileForCurrentActionBar.path);
138               }
139 
140               DocumentActivity._documentActivityInstance.deleteFile(_selectedFile);
141             }
142           });
143           bld.setNegativeButton(android.R.string.no, null);
144           bld.show();
145 
146           break;
147         case DocumentActivity.ACTION_RENAME:
148           extendDialog = new DocumentExtendDialog(mDocumentActivity, _selectedFile, DocumentActivity.ACTION_RENAME);
149           extendDialog.show();
150 
151           break;
152         case DocumentActivity.ACTION_CREATE:
153           extendDialog = new DocumentExtendDialog(mDocumentActivity, _selectedFile, DocumentActivity.ACTION_CREATE);
154 
155           extendDialog.show();
156           break;
157         case DocumentActivity.ACTION_OPEN_IN:
158           ExoDocumentUtils.fileOpen(mDocumentActivity, _selectedFile.nodeType, _selectedFile.path, _selectedFile.name);
159           break;
160 
161         }
162 
163       }
164 
165     });
166 
167     bindView(rowView, fileActionList.get(position), position);
168     return (rowView);
169 
170   }
171 
172   private void bindView(View view, DocumentActionDescription fileAction, int position) {
173     TextView label = (TextView) view.findViewById(R.id.label);
174     label.setText(fileAction.actionName);
175     ImageView icon = (ImageView) view.findViewById(R.id.icon);
176     icon.setImageResource(fileAction.imageID);
177     /*
178      * Disable action view if it can not be removed || position ==
179      * DocumentActivity.ACTION_COPY
180      */
181     if (!_selectedFile.canRemove && (position == DocumentActivity.ACTION_MOVE || position == DocumentActivity.ACTION_DELETE
182         || position == DocumentActivity.ACTION_RENAME
183         || (position == DocumentActivity.ACTION_PASTE && ("".equals(DocumentHelper.getInstance()._fileCopied.path)
184             && "".equals(DocumentHelper.getInstance()._fileMoved.path))))) {
185       label.setTextColor(android.graphics.Color.GRAY);
186       view.setEnabled(false);
187       return;
188     }
189 
190     if (_selectedFile.isFolder) {
191       if (position == DocumentActivity.ACTION_OPEN_IN
192           || (position == DocumentActivity.ACTION_PASTE && ("".equals(DocumentHelper.getInstance()._fileCopied.path)
193               && "".equals(DocumentHelper.getInstance()._fileMoved.path)))) {
194 
195         label.setTextColor(android.graphics.Color.GRAY);
196         view.setEnabled(false);
197       }
198     } else {
199       if (position == DocumentActivity.ACTION_ADD_PHOTO || position == DocumentActivity.ACTION_PASTE
200           || position == DocumentActivity.ACTION_RENAME || position == DocumentActivity.ACTION_CREATE) {
201 
202         label.setTextColor(android.graphics.Color.GRAY);
203         view.setEnabled(false);
204       }
205 
206     }
207 
208   }
209 
210   public long getItemId(int position) {
211 
212     return position;
213   }
214 
215   public Object getItem(int position) {
216 
217     return position;
218   }
219 
220   public int getCount() {
221 
222     return fileActionList.size();
223   }
224 
225   // Set language
226   public void changeLanguage(boolean isAct) {
227     Resources resource = mDocumentActivity.getResources();
228 
229     String strAddAPhoto = resource.getString(R.string.AddAPhoto);
230     String strCopy = resource.getString(R.string.Copy);
231     String strMove = resource.getString(R.string.Move);
232     String strDelete = resource.getString(R.string.Delete);
233     String strRename = resource.getString(R.string.Rename);
234     String strPaste = resource.getString(R.string.Paste);
235     String strCreateFolder = resource.getString(R.string.CreateFolder);
236     String strOpenIn = resource.getString(R.string.OpenIn);
237 
238     fileActions = new DocumentActionDescription[] {
239         new DocumentActionDescription(strAddAPhoto, R.drawable.documentactionpopupphotoicon),
240         new DocumentActionDescription(strCopy, R.drawable.documentactionpopupcopyicon),
241         new DocumentActionDescription(strMove, R.drawable.documentactionpopupcuticon),
242         new DocumentActionDescription(strPaste, R.drawable.documentactionpopuppasteicon),
243         new DocumentActionDescription(strDelete, R.drawable.documentactionpopupdeleteicon),
244         new DocumentActionDescription(strRename, R.drawable.documentactionpopuprenameicon),
245         new DocumentActionDescription(strCreateFolder, R.drawable.documentactionpopupaddfoldericon),
246         new DocumentActionDescription(strOpenIn, R.drawable.documenticonforfolder) };
247     int size = fileActions.length;
248     int maxChildren = 0;
249     if (isAct) {
250       maxChildren = size - 1;
251     } else {
252       if (_selectedFile.isFolder) {
253         maxChildren = size - 1;
254       } else
255         maxChildren = size;
256     }
257     fileActionList = new ArrayList<DocumentActionDescription>();
258     for (int i = 0; i < maxChildren; i++) {
259       fileActionList.add(fileActions[i]);
260     }
261 
262   }
263 }