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.classic; |
24 |
|
25 |
import java.util.ArrayDeque; |
26 |
|
27 |
import org.jsampler.CC; |
28 |
import org.jsampler.android.AHF; |
29 |
import org.jsampler.event.SamplerChannelListEvent; |
30 |
import org.jsampler.event.SamplerChannelListListener; |
31 |
|
32 |
import android.app.Activity; |
33 |
import android.os.Bundle; |
34 |
import android.os.Parcelable; |
35 |
import android.support.v4.view.PagerAdapter; |
36 |
import android.support.v4.view.ViewPager; |
37 |
import android.view.View; |
38 |
|
39 |
public class ChannelPagerActivity extends Activity { |
40 |
private ViewPager channelPager; |
41 |
|
42 |
/** Called when the activity is first created. */ |
43 |
@Override |
44 |
public void onCreate(Bundle savedInstanceState) { |
45 |
super.onCreate(savedInstanceState); |
46 |
|
47 |
channelPager = new ViewPager(this); |
48 |
ChannelPagerAdapter adapter = new ChannelPagerAdapter(); |
49 |
channelPager.setAdapter(adapter); |
50 |
CC.getSamplerModel().addSamplerChannelListListener(adapter); |
51 |
int idx = getIntent().getExtras().getInt("org.jsampler.android.SelectedChannelID", 0); |
52 |
channelPager.setCurrentItem(idx); |
53 |
setContentView(channelPager); |
54 |
} |
55 |
|
56 |
@Override |
57 |
protected void |
58 |
onDestroy() { |
59 |
ChannelPagerAdapter adapter = (ChannelPagerAdapter)channelPager.getAdapter(); |
60 |
CC.getSamplerModel().removeSamplerChannelListListener(adapter); |
61 |
super.onDestroy(); |
62 |
|
63 |
} |
64 |
|
65 |
private static class ChannelViewPair { |
66 |
public Channel channel; |
67 |
public View view; |
68 |
|
69 |
ChannelViewPair(Channel channel, View view) { |
70 |
this.channel = channel; |
71 |
this.view = view; |
72 |
} |
73 |
} |
74 |
|
75 |
private class ChannelPagerAdapter extends PagerAdapter implements SamplerChannelListListener { |
76 |
|
77 |
|
78 |
private ArrayDeque<View> recycledViews = new ArrayDeque<View>(); |
79 |
private ChannelViewPair primaryItem = null; |
80 |
|
81 |
private ChannelsPane |
82 |
getLane() { return (ChannelsPane)AHF.getMainFrame().getSelectedChannelsPane(); } |
83 |
|
84 |
@Override |
85 |
public int |
86 |
getCount() { return getLane().getChannelCount(); } |
87 |
|
88 |
@Override |
89 |
public void startUpdate(View container) { } |
90 |
|
91 |
@Override |
92 |
public void finishUpdate(View container) { |
93 |
if(channelPager.getCurrentItem() >= getCount()) { |
94 |
// workaround for bug in ViewPager when the |
95 |
// current item is the last one and a previous item is removed |
96 |
channelPager.setCurrentItem(getCount() - 1, true); |
97 |
} |
98 |
} |
99 |
|
100 |
@Override |
101 |
public int |
102 |
getItemPosition(Object object) { return POSITION_NONE; } |
103 |
|
104 |
@Override |
105 |
public Object |
106 |
instantiateItem(View container, int position) { |
107 |
Channel chn = getLane().getChannel(position); |
108 |
View view = recycledViews.pollLast(); |
109 |
chn.installView(view); |
110 |
view = chn.getView(); |
111 |
|
112 |
((ViewPager)container).addView(view, 0); |
113 |
return new ChannelViewPair(chn, view); |
114 |
} |
115 |
|
116 |
@Override |
117 |
public void |
118 |
destroyItem(View container, int position, Object object) { |
119 |
ChannelViewPair pair = (ChannelViewPair)object; |
120 |
((ViewPager)container).removeView(pair.view); |
121 |
recycledViews.add(pair.view); |
122 |
pair.channel.uninstallView(); |
123 |
} |
124 |
|
125 |
@Override |
126 |
public void |
127 |
setPrimaryItem(View container, int position, Object object) { |
128 |
primaryItem = (ChannelViewPair)object; |
129 |
} |
130 |
|
131 |
@Override |
132 |
public boolean |
133 |
isViewFromObject(View view, Object object) { |
134 |
return view == ((ChannelViewPair)object).view; |
135 |
} |
136 |
|
137 |
@Override |
138 |
public Parcelable |
139 |
saveState() { return null; } |
140 |
|
141 |
@Override |
142 |
public void |
143 |
restoreState(Parcelable state, ClassLoader loader) { } |
144 |
|
145 |
@Override |
146 |
public void |
147 |
channelAdded(SamplerChannelListEvent e) { notifyDataSetChanged(); } |
148 |
|
149 |
@Override |
150 |
public void |
151 |
channelRemoved(SamplerChannelListEvent e) { notifyDataSetChanged(); } |
152 |
} |
153 |
} |