1 |
iliev |
2302 |
/* |
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 static org.jsampler.android.view.AndroidI18n.i18n; |
26 |
|
|
|
27 |
|
|
import org.jsampler.CC; |
28 |
|
|
import org.jsampler.MidiDeviceModel; |
29 |
|
|
import org.jsampler.event.MidiDeviceEvent; |
30 |
|
|
import org.jsampler.event.MidiDeviceListener; |
31 |
|
|
|
32 |
|
|
import android.util.Log; |
33 |
|
|
|
34 |
|
|
public class MidiChannelSpinnerAdapter extends AbstractSpinnerAdapter<String> implements MidiDeviceListener { |
35 |
|
|
public static final String[] channels = new String[17]; |
36 |
|
|
|
37 |
|
|
static { |
38 |
|
|
channels[0] = i18n.getLabel("MidiChannelSpinnerAdapter.allChannels"); |
39 |
|
|
for(int i = 1; i < channels.length; i++) { |
40 |
|
|
channels[i] = i18n.getLabel("MidiChannelSpinnerAdapter.channel", i); |
41 |
|
|
} |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
private MidiDeviceModel midiDev = null; |
45 |
|
|
|
46 |
|
|
@Override |
47 |
|
|
public int |
48 |
|
|
size() { return midiDev == null ? 0 : channels.length; } |
49 |
|
|
|
50 |
|
|
@Override |
51 |
|
|
public Object |
52 |
|
|
get(int position) { return channels[position]; } |
53 |
|
|
|
54 |
|
|
public String |
55 |
|
|
getEmptyItemText() { return i18n.getLabel("MidiChannelSpinnerAdapter.selectChannel"); } |
56 |
|
|
|
57 |
|
|
public String |
58 |
|
|
getItemText(int position) { |
59 |
|
|
return getItem(position).toString(); |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
public void |
63 |
|
|
setMidiDevice(int devId) { |
64 |
|
|
if(midiDev == null) { |
65 |
|
|
if(devId == -1) return; |
66 |
|
|
changeMidiDevice(CC.getSamplerModel().getMidiDeviceById(devId)); |
67 |
|
|
} else { |
68 |
|
|
MidiDeviceModel newDev; |
69 |
|
|
if(devId == -1) { |
70 |
|
|
newDev = null; |
71 |
|
|
} else { |
72 |
|
|
newDev = CC.getSamplerModel().getMidiDeviceById(devId); |
73 |
|
|
if(newDev == null) Log.w("MidiPortSpinnerAdapter", "Unknown MIDI device - this is a bug!"); |
74 |
|
|
} |
75 |
|
|
if(newDev != null && midiDev.getDeviceId() == newDev.getDeviceId()) return; |
76 |
|
|
|
77 |
|
|
changeMidiDevice(newDev); |
78 |
|
|
} |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
private void |
82 |
|
|
changeMidiDevice(MidiDeviceModel newDev) { |
83 |
|
|
if(midiDev != null) midiDev.removeMidiDeviceListener(this); |
84 |
|
|
midiDev = newDev; |
85 |
|
|
if(midiDev != null) midiDev.addMidiDeviceListener(this); |
86 |
|
|
notifyDataSetChanged(); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
@Override |
90 |
|
|
public void |
91 |
|
|
settingsChanged(MidiDeviceEvent e) { |
92 |
|
|
notifyDataSetChanged(); |
93 |
|
|
} |
94 |
|
|
} |