1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2011 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of JSampler. |
7 |
* |
8 |
* JSampler is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* JSampler is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with JSampler; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
package org.jsampler.android.view; |
24 |
|
25 |
import android.view.View; |
26 |
import android.view.ViewGroup; |
27 |
import android.widget.SpinnerAdapter; |
28 |
import android.widget.TextView; |
29 |
|
30 |
public abstract class AbstractSpinnerAdapter<I> extends AbstractListAdapter implements SpinnerAdapter { |
31 |
protected boolean useEmptyItem = true; |
32 |
|
33 |
@Override |
34 |
public View |
35 |
getDropDownView(int position, View convertView, ViewGroup parent) { |
36 |
return getView(position, convertView, parent); |
37 |
} |
38 |
|
39 |
public abstract int size(); |
40 |
|
41 |
public abstract Object get(int position); |
42 |
|
43 |
@Override |
44 |
public int |
45 |
getCount() { return size() + (useEmptyItem ? 1 : 0); } |
46 |
|
47 |
@Override |
48 |
public Object |
49 |
getItem(int position) { |
50 |
return useEmptyItem ? (position == 0 ? null : get(position - 1)) : get(position); |
51 |
} |
52 |
|
53 |
public void |
54 |
setUseEmptyItem(boolean b) { |
55 |
if(b == useEmptyItem) return; |
56 |
useEmptyItem = b; |
57 |
notifyDataSetChanged(); |
58 |
} |
59 |
|
60 |
/** |
61 |
* Prepares adapter for the new selection. If the item is <code>null</code> |
62 |
* an extra (empty) element will be used to represent no selection, otherwise |
63 |
* the extra element will be removed. |
64 |
* @param item The item to be selected. |
65 |
* @return |
66 |
*/ |
67 |
public int |
68 |
prepareForSelection(I item) { |
69 |
if(item == null) { |
70 |
setUseEmptyItem(true); |
71 |
return 0; |
72 |
} |
73 |
|
74 |
setUseEmptyItem(false); |
75 |
return getItemPosition(item); |
76 |
} |
77 |
|
78 |
public int |
79 |
getItemPosition(I item) { |
80 |
int idx = -1; |
81 |
for(int i = 0; i < size(); i++) { |
82 |
if(compare((I)get(i), item)) { |
83 |
idx = i; |
84 |
break; |
85 |
} |
86 |
} |
87 |
|
88 |
return idx; |
89 |
} |
90 |
|
91 |
public boolean |
92 |
compare(I item1, I item2) { return item1 == item2; } |
93 |
|
94 |
/** Gets the text to be displayed when the empty item is selected. */ |
95 |
public abstract String getEmptyItemText(); |
96 |
|
97 |
public String |
98 |
getItemText(int position) { return getItem(position).toString(); } |
99 |
|
100 |
@Override |
101 |
public View |
102 |
getView(int position, View convertView, ViewGroup parent) { |
103 |
TextView text; |
104 |
if(convertView != null && convertView instanceof TextView) { |
105 |
text = (TextView) convertView; |
106 |
|
107 |
} else { |
108 |
text = new TextView(parent.getContext()); |
109 |
text.setFocusable(false); |
110 |
text.setFocusableInTouchMode(false); |
111 |
text.setClickable(false); |
112 |
} |
113 |
|
114 |
if(useEmptyItem && position == 0) text.setText(getEmptyItemText()); |
115 |
else text.setText(getItemText(position)); |
116 |
|
117 |
return text; |
118 |
} |
119 |
} |