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 java.beans.PropertyChangeEvent; |
26 |
import java.beans.PropertyChangeListener; |
27 |
|
28 |
import org.jsampler.HF; |
29 |
import org.jsampler.android.AHF; |
30 |
import org.jsampler.android.R; |
31 |
import org.linuxsampler.lscp.SamplerChannel; |
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.TextView; |
38 |
|
39 |
import static org.jsampler.android.view.AndroidI18n.i18n; |
40 |
|
41 |
public class SamplerChannelListAdapter extends AbstractListAdapter { |
42 |
private AndroidChannelsPane channelLane; |
43 |
|
44 |
public SamplerChannelListAdapter(AndroidChannelsPane channelLane) { |
45 |
this.channelLane = channelLane; |
46 |
installListeners(); |
47 |
} |
48 |
|
49 |
private void |
50 |
installListeners() { |
51 |
channelLane.addPropertyChangeListener(getHandler()); |
52 |
} |
53 |
|
54 |
private void |
55 |
uninstallListeners() { |
56 |
channelLane.removePropertyChangeListener(getHandler()); |
57 |
} |
58 |
|
59 |
public void |
60 |
uninstall() { uninstallListeners(); } |
61 |
|
62 |
@Override |
63 |
public int |
64 |
getCount() { return channelLane.getChannelCount(); } |
65 |
|
66 |
@Override |
67 |
public Object |
68 |
getItem(int position) { return channelLane.getChannel(position); } |
69 |
|
70 |
@Override |
71 |
public View |
72 |
getView(int position, View convertView, ViewGroup parent) { |
73 |
View view; |
74 |
if(convertView != null) { |
75 |
view = convertView; |
76 |
} else { |
77 |
LayoutInflater inflater = |
78 |
(LayoutInflater)AHF.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
79 |
view = inflater.inflate(R.layout.sampler_channel_list_item, null); |
80 |
} |
81 |
|
82 |
TextView text = (TextView)view.findViewById(R.id.sampler_channel_list_item_text_num); |
83 |
text.setText(String.valueOf(position + 1) + ":"); |
84 |
|
85 |
SamplerChannel sc = channelLane.getChannel(position).getChannelInfo(); |
86 |
text = (TextView)view.findViewById(R.id.sampler_channel_list_item_text_instr); |
87 |
text.setText(getInstrumentText(sc)); |
88 |
|
89 |
text = (TextView)view.findViewById(R.id.sampler_channel_list_item_text_volume); |
90 |
text.setText( HF.getVolumeString((int)(sc.getVolume() * 100)) ); |
91 |
|
92 |
return view; |
93 |
} |
94 |
|
95 |
private String |
96 |
getInstrumentText(SamplerChannel sc) { |
97 |
StringBuffer sb = new StringBuffer(); |
98 |
int status = sc.getInstrumentStatus(); |
99 |
if(status >= 0 && status < 100) { |
100 |
sb.append(i18n.getLabel("SamplerChannelListAdapter.loadingInstrument", status)); |
101 |
} else if(status == -1) { |
102 |
sb.append(i18n.getLabel("SamplerChannelListAdapter.noInstrument")); |
103 |
} else if(status < -1) { |
104 |
sb.append(i18n.getLabel("SamplerChannelListAdapter.errorLoadingInstrument")); |
105 |
} else { |
106 |
if(sc.getInstrumentName() != null) sb.append(sc.getInstrumentName()); |
107 |
else sb.append(i18n.getButtonLabel("SamplerChannelListAdapter.noInstrument")); |
108 |
} |
109 |
|
110 |
return sb.toString(); |
111 |
} |
112 |
|
113 |
EventHandler handler = new EventHandler(); |
114 |
|
115 |
private EventHandler |
116 |
getHandler() { return handler; } |
117 |
|
118 |
private class EventHandler implements PropertyChangeListener { |
119 |
@Override |
120 |
public void |
121 |
propertyChange(PropertyChangeEvent e) { |
122 |
if ( "channelRemoved".equals(e.getPropertyName()) || |
123 |
"channelAdded" .equals(e.getPropertyName()) || |
124 |
"channelsAdded" .equals(e.getPropertyName()) |
125 |
) { |
126 |
notifyDataSetChanged(); |
127 |
} |
128 |
|
129 |
} |
130 |
} |
131 |
} |