1 |
iliev |
912 |
/* |
2 |
|
|
* JSampler - a java front-end for LinuxSampler |
3 |
|
|
* |
4 |
iliev |
1688 |
* Copyright (C) 2005-2008 Grigor Iliev <grigor@grigoriliev.com> |
5 |
iliev |
912 |
* |
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 |
iliev |
1467 |
import java.awt.Container; |
26 |
|
|
import java.awt.Rectangle; |
27 |
iliev |
912 |
|
28 |
|
|
import java.awt.event.ActionEvent; |
29 |
|
|
import java.awt.event.ActionListener; |
30 |
|
|
|
31 |
iliev |
1285 |
import java.beans.PropertyChangeEvent; |
32 |
|
|
import java.beans.PropertyChangeListener; |
33 |
|
|
|
34 |
iliev |
912 |
import javax.swing.BoxLayout; |
35 |
|
|
import javax.swing.DefaultListCellRenderer; |
36 |
|
|
import javax.swing.JButton; |
37 |
iliev |
1540 |
import javax.swing.JComponent; |
38 |
iliev |
912 |
import javax.swing.JPanel; |
39 |
iliev |
1467 |
import javax.swing.JScrollPane; |
40 |
iliev |
912 |
import javax.swing.JToggleButton; |
41 |
|
|
|
42 |
|
|
import net.sf.juife.TitleBar; |
43 |
|
|
|
44 |
iliev |
1285 |
import org.jdesktop.swingx.JXCollapsiblePane; |
45 |
|
|
|
46 |
iliev |
912 |
import org.jsampler.CC; |
47 |
iliev |
1540 |
import org.jsampler.HF; |
48 |
iliev |
912 |
import org.jsampler.SamplerChannelModel; |
49 |
|
|
|
50 |
|
|
import org.jsampler.event.SamplerChannelEvent; |
51 |
iliev |
1285 |
import org.jsampler.event.SamplerChannelListEvent; |
52 |
|
|
import org.jsampler.event.SamplerChannelListListener; |
53 |
iliev |
912 |
import org.jsampler.event.SamplerChannelListener; |
54 |
|
|
|
55 |
iliev |
1285 |
import static org.jsampler.view.fantasia.FantasiaPrefs.*; |
56 |
iliev |
912 |
|
57 |
|
|
|
58 |
|
|
/** |
59 |
|
|
* |
60 |
|
|
* @author Grigor Iliev |
61 |
|
|
*/ |
62 |
|
|
public class Channel extends org.jsampler.view.JSChannel { |
63 |
iliev |
1285 |
private final JXCollapsiblePane mainPane; |
64 |
iliev |
1730 |
private ChannelView channelView; |
65 |
|
|
private ChannelOptionsView channelOptionsView; |
66 |
|
|
private final ChannelOptionsPane optionsPane = new ChannelOptionsPane(); |
67 |
iliev |
912 |
|
68 |
iliev |
1730 |
private boolean selected = false; |
69 |
iliev |
912 |
|
70 |
iliev |
1730 |
private AnimatedPorpetyListener animatedPorpetyListener = new AnimatedPorpetyListener(); |
71 |
iliev |
912 |
|
72 |
iliev |
1730 |
class AnimatedPorpetyListener implements PropertyChangeListener { |
73 |
|
|
public void |
74 |
|
|
propertyChange(PropertyChangeEvent e) { |
75 |
|
|
mainPane.setAnimated(preferences().getBoolProperty(ANIMATED)); |
76 |
|
|
} |
77 |
|
|
} |
78 |
iliev |
912 |
|
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 |
iliev |
1318 |
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 |
iliev |
912 |
super(model); |
101 |
|
|
|
102 |
|
|
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
103 |
|
|
optionsPane.setAlignmentX(JPanel.CENTER_ALIGNMENT); |
104 |
|
|
|
105 |
iliev |
1285 |
mainPane = new JXCollapsiblePane(); |
106 |
|
|
mainPane.getContentPane().setLayout ( |
107 |
|
|
new BoxLayout(mainPane.getContentPane(), BoxLayout.Y_AXIS) |
108 |
|
|
); |
109 |
iliev |
912 |
|
110 |
iliev |
1730 |
channelView = new NormalChannelView(this); |
111 |
|
|
channelOptionsView = channelView.getChannelOptionsView(); |
112 |
|
|
|
113 |
|
|
optionsPane.setContentPane(channelOptionsView.getComponent()); |
114 |
|
|
|
115 |
|
|
mainPane.add(channelView.getComponent()); |
116 |
iliev |
1285 |
mainPane.add(optionsPane); |
117 |
|
|
|
118 |
|
|
setOpaque(false); |
119 |
|
|
|
120 |
iliev |
912 |
getModel().addSamplerChannelListener(getHandler()); |
121 |
|
|
|
122 |
|
|
updateChannelInfo(); |
123 |
iliev |
1285 |
|
124 |
|
|
add(mainPane); |
125 |
|
|
|
126 |
iliev |
1318 |
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 |
iliev |
1467 |
// TODO: this should be done regardles the listener != null? |
133 |
iliev |
1318 |
mainPane.removePropertyChangeListener(s, this); |
134 |
iliev |
1467 |
/////// |
135 |
iliev |
1318 |
listener.actionPerformed(null); |
136 |
iliev |
1467 |
ensureChannelIsVisible(); |
137 |
|
|
} else if(e.getNewValue() == "expanding/collapsing") { |
138 |
|
|
ensureChannelIsVisible(); |
139 |
iliev |
1318 |
} |
140 |
|
|
} |
141 |
|
|
}); |
142 |
|
|
} |
143 |
|
|
|
144 |
iliev |
1285 |
mainPane.setAnimated(false); |
145 |
|
|
mainPane.setCollapsed(true); |
146 |
|
|
mainPane.setAnimated(preferences().getBoolProperty(ANIMATED)); |
147 |
|
|
mainPane.setCollapsed(false); |
148 |
|
|
|
149 |
iliev |
1730 |
preferences().addPropertyChangeListener(ANIMATED, animatedPorpetyListener); |
150 |
iliev |
1318 |
|
151 |
|
|
if(listener != null) { |
152 |
|
|
javax.swing.SwingUtilities.invokeLater(new Runnable() { |
153 |
|
|
public void |
154 |
|
|
run() { listener.actionPerformed(null); } |
155 |
|
|
}); |
156 |
|
|
} |
157 |
iliev |
1341 |
|
158 |
|
|
CC.getSamplerModel().addSamplerChannelListListener(getHandler()); |
159 |
iliev |
912 |
} |
160 |
|
|
|
161 |
iliev |
1467 |
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 |
iliev |
912 |
/** |
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 |
iliev |
1285 |
expandChannel() { expandChannel(optionsPane.isAnimated()); } |
198 |
iliev |
912 |
|
199 |
iliev |
1285 |
/** Shows the channel properties. */ |
200 |
|
|
public void |
201 |
|
|
expandChannel(boolean animated) { |
202 |
|
|
boolean b = optionsPane.isAnimated(); |
203 |
|
|
optionsPane.setAnimated(animated); |
204 |
iliev |
1730 |
channelView.expandChannel(); |
205 |
iliev |
1285 |
optionsPane.setAnimated(b); |
206 |
|
|
} |
207 |
iliev |
912 |
|
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 |
iliev |
1730 |
channelView.updateChannelInfo(); |
215 |
|
|
channelOptionsView.updateChannelInfo(); |
216 |
iliev |
912 |
} |
217 |
|
|
|
218 |
iliev |
1341 |
protected void |
219 |
|
|
onDestroy() { |
220 |
|
|
CC.getSamplerModel().removeSamplerChannelListListener(getHandler()); |
221 |
iliev |
1730 |
preferences().removePropertyChangeListener(ANIMATED, animatedPorpetyListener); |
222 |
iliev |
1341 |
|
223 |
iliev |
1730 |
channelView.uninstallView(); |
224 |
|
|
channelOptionsView.uninstallView(); |
225 |
iliev |
1341 |
} |
226 |
iliev |
1730 |
|
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 |
iliev |
1341 |
|
239 |
iliev |
1730 |
public void |
240 |
|
|
showOptionsPane(boolean show) { optionsPane.showOptionsPane(show); } |
241 |
|
|
|
242 |
iliev |
912 |
private final EventHandler eventHandler = new EventHandler(); |
243 |
|
|
|
244 |
|
|
private EventHandler |
245 |
|
|
getHandler() { return eventHandler; } |
246 |
|
|
|
247 |
iliev |
1730 |
private class EventHandler implements SamplerChannelListener, |
248 |
|
|
SamplerChannelListListener, PropertyChangeListener { |
249 |
iliev |
912 |
/** |
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 |
iliev |
1730 |
channelView.updateStreamCount(getModel().getStreamCount()); |
265 |
iliev |
912 |
} |
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 |
iliev |
1730 |
channelView.updateVoiceCount(getModel().getVoiceCount()); |
275 |
iliev |
912 |
} |
276 |
iliev |
1341 |
|
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 |
iliev |
1285 |
|
298 |
iliev |
912 |
public void |
299 |
iliev |
1285 |
propertyChange(PropertyChangeEvent e) { |
300 |
|
|
if(e.getNewValue() == "collapsed") { |
301 |
|
|
CC.getSamplerModel().removeBackendChannel(getChannelId()); |
302 |
|
|
} |
303 |
|
|
} |
304 |
iliev |
912 |
} |
305 |
|
|
} |
306 |
|
|
|
307 |
iliev |
1730 |
class ChannelOptionsPane extends JXCollapsiblePane { |
308 |
|
|
ChannelOptionsPane() { |
309 |
iliev |
1285 |
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 |
iliev |
912 |
} |
320 |
|
|
|
321 |
iliev |
1730 |
public void |
322 |
|
|
showOptionsPane(boolean show) { setCollapsed(!show); } |
323 |
iliev |
912 |
} |