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.List;
22  
23  import org.exoplatform.R;
24  import org.exoplatform.model.ExoFile;
25  import org.exoplatform.ui.DocumentActionDialog;
26  import org.exoplatform.ui.DocumentActivity;
27  import org.exoplatform.utils.CompatibleFileOpen.FileOpenRequest;
28  import org.exoplatform.utils.CompatibleFileOpen.FileOpenRequestResult;
29  import org.exoplatform.utils.ExoConstants;
30  import org.exoplatform.utils.ExoDocumentUtils;
31  import org.exoplatform.widget.UnreadableFileDialog;
32  
33  import android.content.Context;
34  import android.view.LayoutInflater;
35  import android.view.View;
36  import android.view.ViewGroup;
37  import android.widget.BaseAdapter;
38  import android.widget.ImageButton;
39  import android.widget.ImageView;
40  import android.widget.TextView;
41  
42  public class DocumentAdapter extends BaseAdapter {
43  
44    private List<ExoFile>       _documentList;
45  
46    private DocumentActivity    _mContext;
47  
48    public DocumentActionDialog _documentActionDialog;
49  
50    public DocumentAdapter(DocumentActivity context, List<ExoFile> list) {
51  
52      _mContext = context;
53      _documentList = list;
54  
55    }
56  
57    @Override
58    public int getCount() {
59      return _documentList.size();
60    }
61  
62    @Override
63    public Object getItem(int pos) {
64      return pos;
65    }
66  
67    @Override
68    public long getItemId(int pos) {
69      return pos;
70    }
71  
72    @Override
73    public View getView(int position, View convertView, ViewGroup parent) {
74  
75      final int pos = position;
76      // TODO use ViewHolder pattern
77      LayoutInflater inflater = (LayoutInflater) _mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
78  
79      final ExoFile myFile = _documentList.get(pos);
80  
81      if ("".equals(myFile.name) && "".equals(myFile.path)) {
82        convertView = inflater.inflate(R.layout.gadget_tab_layout, parent, false);
83        TextView textViewTabTitle = (TextView) convertView.findViewById(R.id.textView_Tab_Title);
84        if (myFile.driveName.equals(ExoConstants.DOCUMENT_PERSONAL_DRIVER))
85          textViewTabTitle.setText(_mContext.getResources().getString(R.string.Personal));
86        else if (myFile.driveName.equals(ExoConstants.DOCUMENT_GROUP_DRIVER))
87          textViewTabTitle.setText(_mContext.getResources().getString(R.string.Group));
88        else if (myFile.driveName.equals(ExoConstants.DOCUMENT_GENERAL_DRIVER))
89          textViewTabTitle.setText(_mContext.getResources().getString(R.string.General));
90        return (convertView);
91      } else {
92        convertView = inflater.inflate(R.layout.fileitem, parent, false);
93  
94        ImageButton btnAction = (ImageButton) convertView.findViewById(R.id.Button_FileAction);
95        ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
96        TextView lb = (TextView) convertView.findViewById(R.id.label);
97        // using the natural name here is possible
98        lb.setText(myFile.getName());
99  
100       if ("".equals(myFile.currentFolder)) {
101         /*
102          * If current folder is null, make the action button is invisible
103          */
104         btnAction.setVisibility(View.INVISIBLE);
105 
106       }
107 
108       if (position == 0) {
109         if (_documentList.size() == 1)
110           convertView.setBackgroundResource(R.drawable.dashboard_single_background_shape);
111         else {
112           convertView.setBackgroundResource(R.drawable.dashboard_top_background_shape);
113         }
114       } else {
115         if (position + 1 == _documentList.size())
116           convertView.setBackgroundResource(R.drawable.dasboard_bottom_background_shape);
117         else {
118           ExoFile previousItem = _documentList.get(position - 1);
119           ExoFile nextItem = _documentList.get(position + 1);
120 
121           if ("".equals(previousItem.name) && "".equals(previousItem.path) && "".equals(nextItem.name)
122               && "".equals(nextItem.path)) {
123             convertView.setBackgroundResource(R.drawable.dashboard_single_background_shape);
124           } else if ("".equals(previousItem.name) && "".equals(previousItem.path)) {
125             convertView.setBackgroundResource(R.drawable.dashboard_top_background_shape);
126           } else if ("".equals(nextItem.name) && "".equals(nextItem.path))
127             convertView.setBackgroundResource(R.drawable.dasboard_bottom_background_shape);
128           else
129             convertView.setBackgroundResource(R.drawable.dashboard_middle_background_shape);
130         }
131       }
132 
133       if (!myFile.isFolder) {
134         btnAction.setVisibility(View.VISIBLE);
135 
136         icon.setImageResource(ExoDocumentUtils.getIconFromType(myFile.nodeType));
137 
138       } else {
139         icon.setImageResource(R.drawable.documenticonforfolder);
140       }
141 
142       convertView.setOnClickListener(new View.OnClickListener() {
143         public void onClick(View v) {
144           if (!myFile.isFolder) {
145             /*
146              * Open file with compatible application
147              */
148             if (ExoDocumentUtils.isForbidden(myFile.nodeType)) {
149               new UnreadableFileDialog(_mContext, null).show();
150             } else {
151               FileOpenRequest fileOpenReq = ExoDocumentUtils.fileOpen(_mContext, myFile.nodeType, myFile.path, myFile.name);
152               if (fileOpenReq.mResult == FileOpenRequestResult.EXTERNAL) {
153                 DocumentActivity._documentActivityInstance.mFileOpenController = fileOpenReq.mFileOpenController;
154               }
155             }
156           } else {
157             DocumentActivity._documentActivityInstance.loadFolderContent(myFile);
158           }
159         }
160       });
161 
162       btnAction.setOnClickListener(new View.OnClickListener() {
163         public void onClick(View v) {
164           ExoFile file = _documentList.get(pos);
165           _documentActionDialog = new DocumentActionDialog(_mContext, file, false);
166           _documentActionDialog.myFile = file;
167           _documentActionDialog._documentActionAdapter.setSelectedFile(file);
168           _documentActionDialog._documentActionAdapter.notifyDataSetChanged();
169           _documentActionDialog.setTileForDialog(file.name);
170           _documentActionDialog.show();
171         }
172       });
173       return convertView;
174     }
175   }
176 }