1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2008 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.view.fantasia; |
24 |
|
25 |
import java.awt.Container; |
26 |
import java.awt.Rectangle; |
27 |
|
28 |
import java.awt.event.ActionEvent; |
29 |
import java.awt.event.ActionListener; |
30 |
|
31 |
import java.beans.PropertyChangeEvent; |
32 |
import java.beans.PropertyChangeListener; |
33 |
|
34 |
import javax.swing.BoxLayout; |
35 |
import javax.swing.DefaultListCellRenderer; |
36 |
import javax.swing.JButton; |
37 |
import javax.swing.JComponent; |
38 |
import javax.swing.JPanel; |
39 |
import javax.swing.JScrollPane; |
40 |
import javax.swing.JToggleButton; |
41 |
|
42 |
import net.sf.juife.TitleBar; |
43 |
|
44 |
import org.jdesktop.swingx.JXCollapsiblePane; |
45 |
|
46 |
import org.jsampler.CC; |
47 |
import org.jsampler.HF; |
48 |
import org.jsampler.SamplerChannelModel; |
49 |
|
50 |
import org.jsampler.event.SamplerChannelEvent; |
51 |
import org.jsampler.event.SamplerChannelListEvent; |
52 |
import org.jsampler.event.SamplerChannelListListener; |
53 |
import org.jsampler.event.SamplerChannelListener; |
54 |
|
55 |
import static org.jsampler.view.fantasia.FantasiaPrefs.*; |
56 |
|
57 |
|
58 |
/** |
59 |
* |
60 |
* @author Grigor Iliev |
61 |
*/ |
62 |
public class Channel extends org.jsampler.view.JSChannel { |
63 |
private final JXCollapsiblePane mainPane; |
64 |
private ChannelView channelView; |
65 |
private ChannelOptionsView channelOptionsView; |
66 |
private final ChannelOptionsPane optionsPane = new ChannelOptionsPane(); |
67 |
|
68 |
private boolean selected = false; |
69 |
|
70 |
private AnimatedPorpetyListener animatedPorpetyListener = new AnimatedPorpetyListener(); |
71 |
|
72 |
class AnimatedPorpetyListener implements PropertyChangeListener { |
73 |
public void |
74 |
propertyChange(PropertyChangeEvent e) { |
75 |
mainPane.setAnimated(preferences().getBoolProperty(ANIMATED)); |
76 |
} |
77 |
} |
78 |
|
79 |
/** |
80 |
* Creates a new instance of <code>Channel</code> using the specified |
81 |
* non-<code>null</code> channel model. |
82 |
* @param model The model to be used by this channel. |
83 |
* @throws IllegalArgumentException If the model is <code>null</code>. |
84 |
*/ |
85 |
public |
86 |
Channel(SamplerChannelModel model) { |
87 |
this(model, null); |
88 |
} |
89 |
|
90 |
/** |
91 |
* Creates a new instance of <code>Channel</code> using the specified |
92 |
* non-<code>null</code> channel model. |
93 |
* @param model The model to be used by this channel. |
94 |
* @param listener A listener which is notified when the newly created |
95 |
* channel is fully expanded on the screen. |
96 |
* @throws IllegalArgumentException If the model is <code>null</code>. |
97 |
*/ |
98 |
public |
99 |
Channel(SamplerChannelModel model, final ActionListener listener) { |
100 |
super(model); |
101 |
|
102 |
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
103 |
optionsPane.setAlignmentX(JPanel.CENTER_ALIGNMENT); |
104 |
|
105 |
mainPane = new JXCollapsiblePane(); |
106 |
mainPane.getContentPane().setLayout ( |
107 |
new BoxLayout(mainPane.getContentPane(), BoxLayout.Y_AXIS) |
108 |
); |
109 |
|
110 |
channelView = new NormalChannelView(this); |
111 |
channelOptionsView = channelView.getChannelOptionsView(); |
112 |
|
113 |
optionsPane.setContentPane(channelOptionsView.getComponent()); |
114 |
|
115 |
mainPane.add(channelView.getComponent()); |
116 |
mainPane.add(optionsPane); |
117 |
|
118 |
setOpaque(false); |
119 |
|
120 |
getModel().addSamplerChannelListener(getHandler()); |
121 |
|
122 |
updateChannelInfo(); |
123 |
|
124 |
add(mainPane); |
125 |
|
126 |
if(listener != null) { |
127 |
final String s = JXCollapsiblePane.ANIMATION_STATE_KEY; |
128 |
mainPane.addPropertyChangeListener(s, new PropertyChangeListener() { |
129 |
public void |
130 |
propertyChange(PropertyChangeEvent e) { |
131 |
if(e.getNewValue() == "expanded") { |
132 |
// TODO: this should be done regardles the listener != null? |
133 |
mainPane.removePropertyChangeListener(s, this); |
134 |
/////// |
135 |
listener.actionPerformed(null); |
136 |
ensureChannelIsVisible(); |
137 |
} else if(e.getNewValue() == "expanding/collapsing") { |
138 |
ensureChannelIsVisible(); |
139 |
} |
140 |
} |
141 |
}); |
142 |
} |
143 |
|
144 |
mainPane.setAnimated(false); |
145 |
mainPane.setCollapsed(true); |
146 |
mainPane.setAnimated(preferences().getBoolProperty(ANIMATED)); |
147 |
mainPane.setCollapsed(false); |
148 |
|
149 |
preferences().addPropertyChangeListener(ANIMATED, animatedPorpetyListener); |
150 |
|
151 |
if(listener != null) { |
152 |
javax.swing.SwingUtilities.invokeLater(new Runnable() { |
153 |
public void |
154 |
run() { listener.actionPerformed(null); } |
155 |
}); |
156 |
} |
157 |
|
158 |
CC.getSamplerModel().addSamplerChannelListListener(getHandler()); |
159 |
} |
160 |
|
161 |
private void |
162 |
ensureChannelIsVisible() { |
163 |
Container p = getParent(); |
164 |
JScrollPane sp = null; |
165 |
while(p != null) { |
166 |
if(p instanceof JScrollPane) { |
167 |
sp = (JScrollPane)p; |
168 |
break; |
169 |
} |
170 |
p = p.getParent(); |
171 |
} |
172 |
if(sp == null) return; |
173 |
int h = sp.getViewport().getView().getHeight(); |
174 |
sp.getViewport().scrollRectToVisible(new Rectangle(0, h - 2, 1, 1)); |
175 |
} |
176 |
|
177 |
/** |
178 |
* Determines whether the channel is selected. |
179 |
* @return <code>true</code> if the channel is selected, <code>false</code> otherwise. |
180 |
*/ |
181 |
public boolean isSelected() { return selected; } |
182 |
|
183 |
/** |
184 |
* Sets the selection state of this channel. |
185 |
* This method is invoked when the selection state of the channel has changed. |
186 |
* @param select Specifies the new selection state of this channel; |
187 |
* <code>true</code> to select the channel, <code>false</code> otherwise. |
188 |
*/ |
189 |
public void |
190 |
setSelected(boolean select) { |
191 |
|
192 |
selected = select; |
193 |
} |
194 |
|
195 |
/** Shows the channel properties. */ |
196 |
public void |
197 |
expandChannel() { expandChannel(optionsPane.isAnimated()); } |
198 |
|
199 |
/** Shows the channel properties. */ |
200 |
public void |
201 |
expandChannel(boolean animated) { |
202 |
boolean b = optionsPane.isAnimated(); |
203 |
optionsPane.setAnimated(animated); |
204 |
channelView.expandChannel(); |
205 |
optionsPane.setAnimated(b); |
206 |
} |
207 |
|
208 |
/** |
209 |
* Updates the channel settings. This method is invoked when changes to the |
210 |
* channel were made. |
211 |
*/ |
212 |
private void |
213 |
updateChannelInfo() { |
214 |
channelView.updateChannelInfo(); |
215 |
channelOptionsView.updateChannelInfo(); |
216 |
} |
217 |
|
218 |
protected void |
219 |
onDestroy() { |
220 |
CC.getSamplerModel().removeSamplerChannelListListener(getHandler()); |
221 |
preferences().removePropertyChangeListener(ANIMATED, animatedPorpetyListener); |
222 |
|
223 |
channelView.uninstallView(); |
224 |
channelOptionsView.uninstallView(); |
225 |
} |
226 |
|
227 |
public void |
228 |
remove() { |
229 |
if(!mainPane.isAnimated()) { |
230 |
CC.getSamplerModel().removeBackendChannel(getChannelId()); |
231 |
return; |
232 |
} |
233 |
|
234 |
String s = JXCollapsiblePane.ANIMATION_STATE_KEY; |
235 |
mainPane.addPropertyChangeListener(s, getHandler()); |
236 |
mainPane.setCollapsed(true); |
237 |
} |
238 |
|
239 |
public void |
240 |
showOptionsPane(boolean show) { optionsPane.showOptionsPane(show); } |
241 |
|
242 |
private final EventHandler eventHandler = new EventHandler(); |
243 |
|
244 |
private EventHandler |
245 |
getHandler() { return eventHandler; } |
246 |
|
247 |
private class EventHandler implements SamplerChannelListener, |
248 |
SamplerChannelListListener, PropertyChangeListener { |
249 |
/** |
250 |
* Invoked when changes are made to a sampler channel. |
251 |
* @param e A <code>SamplerChannelEvent</code> instance |
252 |
* containing event information. |
253 |
*/ |
254 |
public void |
255 |
channelChanged(SamplerChannelEvent e) { updateChannelInfo(); } |
256 |
|
257 |
/** |
258 |
* Invoked when the number of active disk streams has changed. |
259 |
* @param e A <code>SamplerChannelEvent</code> instance |
260 |
* containing event information. |
261 |
*/ |
262 |
public void |
263 |
streamCountChanged(SamplerChannelEvent e) { |
264 |
channelView.updateStreamCount(getModel().getStreamCount()); |
265 |
} |
266 |
|
267 |
/** |
268 |
* Invoked when the number of active voices has changed. |
269 |
* @param e A <code>SamplerChannelEvent</code> instance |
270 |
* containing event information. |
271 |
*/ |
272 |
public void |
273 |
voiceCountChanged(SamplerChannelEvent e) { |
274 |
channelView.updateVoiceCount(getModel().getVoiceCount()); |
275 |
} |
276 |
|
277 |
/** |
278 |
* Invoked when a new sampler channel is created. |
279 |
* @param e A <code>SamplerChannelListEvent</code> |
280 |
* instance providing the event information. |
281 |
*/ |
282 |
public void |
283 |
channelAdded(SamplerChannelListEvent e) { } |
284 |
|
285 |
/** |
286 |
* Invoked when a sampler channel is removed. |
287 |
* @param e A <code>SamplerChannelListEvent</code> |
288 |
* instance providing the event information. |
289 |
*/ |
290 |
public void |
291 |
channelRemoved(SamplerChannelListEvent e) { |
292 |
// Some cleanup when the channel is removed. |
293 |
if(e.getChannelModel().getChannelId() == getChannelId()) { |
294 |
onDestroy(); |
295 |
} |
296 |
} |
297 |
|
298 |
public void |
299 |
propertyChange(PropertyChangeEvent e) { |
300 |
if(e.getNewValue() == "collapsed") { |
301 |
CC.getSamplerModel().removeBackendChannel(getChannelId()); |
302 |
} |
303 |
} |
304 |
} |
305 |
} |
306 |
|
307 |
class ChannelOptionsPane extends JXCollapsiblePane { |
308 |
ChannelOptionsPane() { |
309 |
setAnimated(false); |
310 |
setCollapsed(true); |
311 |
setAnimated(preferences().getBoolProperty(ANIMATED)); |
312 |
|
313 |
preferences().addPropertyChangeListener(ANIMATED, new PropertyChangeListener() { |
314 |
public void |
315 |
propertyChange(PropertyChangeEvent e) { |
316 |
setAnimated(preferences().getBoolProperty(ANIMATED)); |
317 |
} |
318 |
}); |
319 |
} |
320 |
|
321 |
public void |
322 |
showOptionsPane(boolean show) { setCollapsed(!show); } |
323 |
} |