1 | /*
|
---|
2 | *
|
---|
3 | * This program is free software: you can redistribute it and/or modify
|
---|
4 | * it under the terms of the GNU General Public License as published by
|
---|
5 | * the Free Software Foundation, either version 3 of the License, or
|
---|
6 | * (at your option) any later version.
|
---|
7 | *
|
---|
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.
|
---|
12 | *
|
---|
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/>.
|
---|
15 | *
|
---|
16 | * Based on FileBrowserIconAdapter.java from mapsforge.org
|
---|
17 | *
|
---|
18 | */
|
---|
19 | package org.greenstone.android.tipple;
|
---|
20 |
|
---|
21 | import java.io.File;
|
---|
22 |
|
---|
23 | import android.content.Context;
|
---|
24 | import android.view.Gravity;
|
---|
25 | import android.view.View;
|
---|
26 | import android.view.ViewGroup;
|
---|
27 | import android.widget.BaseAdapter;
|
---|
28 | import android.widget.TextView;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * An adapter for the FileBrowser GridView.
|
---|
32 | */
|
---|
33 | class TippleLogFileBrowserIconAdapter extends BaseAdapter
|
---|
34 | {
|
---|
35 | private Context context;
|
---|
36 | private File currentFile;
|
---|
37 | private File[] files;
|
---|
38 | private boolean hasParentFolder;
|
---|
39 | private TextView textView;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Create a new FileBrowserIconAdapter with the given context.
|
---|
43 | *
|
---|
44 | * @param context
|
---|
45 | * the context of this adapter, through which new Views can be created.
|
---|
46 | */
|
---|
47 | TippleLogFileBrowserIconAdapter(Context context) {
|
---|
48 | this.context = context;
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public int getCount() {
|
---|
53 | if (this.files == null) {
|
---|
54 | return 0;
|
---|
55 | }
|
---|
56 | return this.files.length;
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public Object getItem(int index) {
|
---|
61 | return this.files[index];
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public long getItemId(int index) {
|
---|
66 | return index;
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public View getView(int index, View convertView, ViewGroup parent) {
|
---|
71 | if (convertView instanceof TextView) {
|
---|
72 | // recycle the old view
|
---|
73 | this.textView = (TextView) convertView;
|
---|
74 | } else {
|
---|
75 | // create a new view object
|
---|
76 | this.textView = new TextView(this.context);
|
---|
77 | this.textView.setLines(2);
|
---|
78 | this.textView.setGravity(Gravity.CENTER_HORIZONTAL);
|
---|
79 | this.textView.setPadding(5, 10, 5, 10);
|
---|
80 | }
|
---|
81 |
|
---|
82 | if (index == 0 && this.hasParentFolder) {
|
---|
83 | // the parent directory of the current folder
|
---|
84 | this.textView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_menu_back,
|
---|
85 | 0, 0);
|
---|
86 | this.textView.setText("..");
|
---|
87 | } else {
|
---|
88 | this.currentFile = this.files[index];
|
---|
89 | if (this.currentFile.isDirectory()) {
|
---|
90 | this.textView.setCompoundDrawablesWithIntrinsicBounds(0,
|
---|
91 | R.drawable.ic_menu_archive, 0, 0);
|
---|
92 | } else {
|
---|
93 | this.textView.setCompoundDrawablesWithIntrinsicBounds(0,
|
---|
94 | R.drawable.ic_menu_mapmode, 0, 0);
|
---|
95 | }
|
---|
96 | this.textView.setText(this.currentFile.getName());
|
---|
97 | }
|
---|
98 | return this.textView;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Call this method to change the data of the adapter.
|
---|
103 | *
|
---|
104 | * @param newFiles
|
---|
105 | * the new files for this adapter.
|
---|
106 | * @param newHasParentFolder
|
---|
107 | * true if the files array has a parent folder at index 0, false otherwise.
|
---|
108 | */
|
---|
109 | void updateFiles(File[] newFiles, boolean newHasParentFolder) {
|
---|
110 | this.files = newFiles;
|
---|
111 | this.hasParentFolder = newHasParentFolder;
|
---|
112 | }
|
---|
113 | } |
---|