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.std; |
24 |
|
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.awt.Component; |
28 |
import java.awt.Rectangle; |
29 |
|
30 |
import java.awt.event.KeyEvent; |
31 |
|
32 |
import java.util.Vector; |
33 |
|
34 |
import javax.swing.BorderFactory; |
35 |
import javax.swing.JScrollPane; |
36 |
import javax.swing.KeyStroke; |
37 |
import javax.swing.ListSelectionModel; |
38 |
|
39 |
import javax.swing.event.ListSelectionEvent; |
40 |
import javax.swing.event.ListSelectionListener; |
41 |
|
42 |
import net.sf.juife.ComponentList; |
43 |
import net.sf.juife.ComponentListModel; |
44 |
import net.sf.juife.DefaultComponentListModel; |
45 |
|
46 |
import org.jsampler.CC; |
47 |
import org.jsampler.SamplerChannelModel; |
48 |
|
49 |
import org.jsampler.view.JSChannel; |
50 |
import org.jsampler.view.JSChannelsPane; |
51 |
|
52 |
import static javax.swing.KeyStroke.*; |
53 |
|
54 |
|
55 |
/** |
56 |
* |
57 |
* @author Grigor Iliev |
58 |
*/ |
59 |
public abstract class StdChannelsPane extends JSChannelsPane implements ListSelectionListener { |
60 |
protected final ChannelList chnList = createChannelList(); |
61 |
protected final ChannelListModel listModel = createChannelListModel(); |
62 |
|
63 |
protected final JScrollPane scrollPane; |
64 |
|
65 |
protected static class ChannelList extends ComponentList { } |
66 |
|
67 |
protected static class ChannelListModel extends DefaultComponentListModel<JSChannel> { } |
68 |
|
69 |
private final Vector<ListSelectionListener> selListeners = new Vector<ListSelectionListener>(); |
70 |
|
71 |
/** |
72 |
* Creates a new instance of <code>StdChannelsPane</code> with |
73 |
* the specified <code>title</code>. |
74 |
* @param title The title of this <code>StdChannelsPane</code> |
75 |
*/ |
76 |
public |
77 |
StdChannelsPane(String title) { |
78 |
super(title); |
79 |
|
80 |
setLayout(new BorderLayout()); |
81 |
|
82 |
chnList.setOpaque(false); |
83 |
//chnList.setLayoutOrientation(JList.HORIZONTAL_WRAP); |
84 |
chnList.setModel(listModel); |
85 |
chnList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
86 |
|
87 |
scrollPane = new JScrollPane(chnList); |
88 |
scrollPane.setBorder(BorderFactory.createEmptyBorder()); |
89 |
add(scrollPane); |
90 |
|
91 |
setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); |
92 |
installListeners(); |
93 |
} |
94 |
|
95 |
protected void |
96 |
installListeners() { |
97 |
chnList.addListSelectionListener(this); |
98 |
|
99 |
KeyStroke k = getKeyStroke(KeyEvent.VK_UP, KeyEvent.ALT_MASK | KeyEvent.SHIFT_MASK); |
100 |
getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(k, "a4n.moveChannelsOnTop"); |
101 |
getActionMap().put("a4n.moveChannelsOnTop", getA4n().moveChannelsOnTop); |
102 |
|
103 |
k = getKeyStroke(KeyEvent.VK_UP, KeyEvent.ALT_MASK); |
104 |
getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(k, "a4n.moveChannelsUp"); |
105 |
getActionMap().put("a4n.moveChannelsUp", getA4n().moveChannelsUp); |
106 |
|
107 |
k = getKeyStroke(KeyEvent.VK_DOWN, KeyEvent.ALT_MASK); |
108 |
getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(k, "a4n.moveChannelsDown"); |
109 |
getActionMap().put("a4n.moveChannelsDown", getA4n().moveChannelsDown); |
110 |
|
111 |
k = getKeyStroke(KeyEvent.VK_DOWN, KeyEvent.ALT_MASK | KeyEvent.SHIFT_MASK); |
112 |
getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(k, "a4n.moveChannelsAtBottom"); |
113 |
getActionMap().put("a4n.moveChannelsAtBottom", getA4n().moveChannelsAtBottom); |
114 |
|
115 |
} |
116 |
|
117 |
public StdA4n |
118 |
getA4n() { return ((StdMainFrame)CC.getMainFrame()).getA4n(); } |
119 |
|
120 |
/** |
121 |
* Registers the specified listener for receiving list selection events. |
122 |
* @param listener The <code>ListSelectionListener</code> to register. |
123 |
*/ |
124 |
@Override |
125 |
public void |
126 |
addListSelectionListener(ListSelectionListener listener) { |
127 |
selListeners.add(listener); |
128 |
} |
129 |
|
130 |
/** |
131 |
* Removes the specified listener. |
132 |
* @param listener The <code>ListSelectionListener</code> to remove. |
133 |
*/ |
134 |
@Override |
135 |
public void |
136 |
removeListSelectionListener(ListSelectionListener listener) { |
137 |
selListeners.remove(listener); |
138 |
} |
139 |
|
140 |
/** |
141 |
* Invoked when the selection has changed. |
142 |
* This method implements <code>valueChanged</code> |
143 |
* method of the <code>ListSelectionListener</code> interface. |
144 |
* @param e A <code>ListSelectionEvent</code> |
145 |
* instance providing the event information. |
146 |
*/ |
147 |
@Override |
148 |
public void |
149 |
valueChanged(ListSelectionEvent e) { |
150 |
ListSelectionEvent e2 = null; |
151 |
for(ListSelectionListener l : selListeners) { |
152 |
if(e2 == null) e2 = new ListSelectionEvent ( |
153 |
this, |
154 |
e.getFirstIndex(), |
155 |
e.getLastIndex(), |
156 |
e.getValueIsAdjusting() |
157 |
); |
158 |
l.valueChanged(e2); |
159 |
} |
160 |
} |
161 |
|
162 |
protected ChannelList |
163 |
createChannelList() { return new ChannelList(); } |
164 |
|
165 |
protected ChannelListModel |
166 |
createChannelListModel() { return new ChannelListModel(); } |
167 |
|
168 |
@Override |
169 |
public void |
170 |
setSelectedChannel(JSChannel channel) { |
171 |
chnList.setSelectedComponent(channel, true); |
172 |
} |
173 |
|
174 |
/** |
175 |
* Adds new channel to this channels pane. |
176 |
* @param channelModel The sampler channel model to be used by the new channel. |
177 |
*/ |
178 |
@Override |
179 |
public void |
180 |
addChannel(SamplerChannelModel channelModel) { |
181 |
JSChannel channel = createChannel(channelModel); |
182 |
listModel.add(channel); |
183 |
chnList.setSelectedComponent(channel, true); |
184 |
scrollToBottom(); |
185 |
} |
186 |
|
187 |
protected abstract JSChannel createChannel(SamplerChannelModel channelModel); |
188 |
|
189 |
/** |
190 |
* Adds the specified channels to this channels pane. |
191 |
* @param chns The channels to be added. |
192 |
*/ |
193 |
@Override |
194 |
public void |
195 |
addChannels(JSChannel[] chns) { |
196 |
if(chns == null || chns.length == 0) return; |
197 |
|
198 |
for(JSChannel c : chns) listModel.add(c); |
199 |
chnList.setSelectionInterval ( |
200 |
listModel.getSize() - chns.length, listModel.getSize() - 1 |
201 |
); |
202 |
|
203 |
chnList.ensureIndexIsVisible(listModel.getSize() - 1); |
204 |
} |
205 |
|
206 |
/** |
207 |
* Removes the specified channel from this channels pane. |
208 |
* This method is invoked when a sampler channel is removed in the back-end. |
209 |
* @param chn The channel to be removed from this channels pane. |
210 |
*/ |
211 |
@Override |
212 |
public void |
213 |
removeChannel(JSChannel chn) { listModel.remove(chn); } |
214 |
|
215 |
/** |
216 |
* Gets the first channel in this channels pane. |
217 |
* @return The first channel in this channels pane or <code>null</code> if |
218 |
* the channels pane is empty. |
219 |
*/ |
220 |
@Override |
221 |
public JSChannel |
222 |
getFirstChannel() { return listModel.size() == 0 ? null : listModel.get(0); } |
223 |
|
224 |
/** |
225 |
* Gets the last channel in this channels pane. |
226 |
* @return The last channel in this channels pane or <code>null</code> if |
227 |
* the channels pane is empty. |
228 |
*/ |
229 |
@Override |
230 |
public JSChannel |
231 |
getLastChannel() { |
232 |
return listModel.size() == 0 ? null : listModel.get(listModel.size() - 1); |
233 |
} |
234 |
|
235 |
/** |
236 |
* Gets the channel at the specified index. |
237 |
* @return The channel at the specified index. |
238 |
* @throws ArrayIndexOutOfBoundsException If the index is out of range. |
239 |
*/ |
240 |
@Override |
241 |
public JSChannel |
242 |
getChannel(int idx) { return listModel.get(idx); } |
243 |
|
244 |
/** |
245 |
* Gets an array with all channels in this channels pane. |
246 |
* @return An array with all channels in this channels pane. |
247 |
*/ |
248 |
@Override |
249 |
public JSChannel[] |
250 |
getChannels() { |
251 |
JSChannel[] chns = new JSChannel[listModel.size()]; |
252 |
for(int i = 0; i < listModel.size(); i++) chns[i] = listModel.get(i); |
253 |
return chns; |
254 |
} |
255 |
|
256 |
/** |
257 |
* Gets the number of channels in this channels pane. |
258 |
* @return The number of channels in this channels pane. |
259 |
*/ |
260 |
@Override |
261 |
public int |
262 |
getChannelCount() { return listModel.size(); } |
263 |
|
264 |
/** |
265 |
* Determines whether there is at least one selected channel. |
266 |
* @return <code>true</code> if there is at least one selected channel, |
267 |
* <code>false</code> otherwise. |
268 |
*/ |
269 |
@Override |
270 |
public boolean |
271 |
hasSelectedChannel() { return !chnList.isSelectionEmpty(); } |
272 |
|
273 |
/** |
274 |
* Gets the number of the selected channels. |
275 |
* @return The number of the selected channels. |
276 |
*/ |
277 |
@Override |
278 |
public int |
279 |
getSelectedChannelCount() { return chnList.getSelectedIndices().length; } |
280 |
|
281 |
/** |
282 |
* Gets an array with all selected channels. |
283 |
* The channels are sorted in increasing index order. |
284 |
* @return The selected channels or an empty array if nothing is selected. |
285 |
*/ |
286 |
@Override |
287 |
public JSChannel[] |
288 |
getSelectedChannels() { |
289 |
Component[] cS = chnList.getSelectedComponents(); |
290 |
JSChannel[] chns = new JSChannel[cS.length]; |
291 |
for(int i = 0; i < cS.length; i++) chns[i] = (JSChannel)cS[i]; |
292 |
return chns; |
293 |
} |
294 |
|
295 |
/** |
296 |
* Removes all selected channels in this channels pane. |
297 |
* Notice that this method does not remove any channels in the back-end. |
298 |
* It is invoked after the channels are already removed in the back-end. |
299 |
* @return The number of removed channels. |
300 |
*/ |
301 |
@Override |
302 |
public int |
303 |
removeSelectedChannels() { |
304 |
int[] l = chnList.getSelectedIndices(); |
305 |
ComponentListModel model = chnList.getModel(); |
306 |
|
307 |
for(;;) { |
308 |
int i = chnList.getMinSelectionIndex(); |
309 |
if(i == -1) break; |
310 |
model.remove(i); |
311 |
} |
312 |
|
313 |
return l.length; |
314 |
} |
315 |
|
316 |
/** Selects all channels. */ |
317 |
@Override |
318 |
public void |
319 |
selectAll() { chnList.selectAll(); } |
320 |
|
321 |
/** Deselects all selected channels. */ |
322 |
@Override |
323 |
public void |
324 |
clearSelection() { chnList.clearSelection(); } |
325 |
|
326 |
/** |
327 |
* Determines whether the channel list UI should be automatically updated |
328 |
* when channel is added/removed. The default value is <code>true</code>. |
329 |
* @see updateChannelListUI |
330 |
*/ |
331 |
@Override |
332 |
public boolean |
333 |
getAutoUpdate() { return chnList.getAutoUpdate(); } |
334 |
|
335 |
/** |
336 |
* Determines whether the channel list UI should be automatically updated |
337 |
* when channel is added/removed. |
338 |
* @see updateChannelListUI |
339 |
*/ |
340 |
@Override |
341 |
public void |
342 |
setAutoUpdate(boolean b) { chnList.setAutoUpdate(b); } |
343 |
|
344 |
/** |
345 |
* Updates the channel list UI. |
346 |
* @see setAutoUpdate |
347 |
*/ |
348 |
@Override |
349 |
public void |
350 |
updateChannelListUI() { chnList.updateList(); } |
351 |
|
352 |
|
353 |
@Override |
354 |
public void |
355 |
moveSelectedChannelsOnTop() { |
356 |
JSChannel[] chns = getSelectedChannels(); |
357 |
|
358 |
if(chns.length == 0) { |
359 |
CC.getLogger().info("Can't move channel(s) at the beginning"); |
360 |
return; |
361 |
} |
362 |
|
363 |
for(int i = chns.length - 1; i >= 0; i--) { |
364 |
listModel.remove(chns[i]); |
365 |
listModel.insert(chns[i], 0); |
366 |
} |
367 |
|
368 |
chnList.setSelectionInterval(0, chns.length - 1); |
369 |
chnList.ensureIndexIsVisible(0); |
370 |
} |
371 |
|
372 |
@Override |
373 |
public void |
374 |
moveSelectedChannelsUp() { |
375 |
JSChannel[] chns = getSelectedChannels(); |
376 |
|
377 |
if(chns.length == 0 || chns[0] == getFirstChannel()) { |
378 |
CC.getLogger().info("Can't move channel(s) up"); |
379 |
return; |
380 |
} |
381 |
|
382 |
for(int i = 0; i < chns.length; i++) listModel.moveUp(chns[i]); |
383 |
|
384 |
int[] si = chnList.getSelectedIndices(); |
385 |
|
386 |
for(int i = 0; i < si.length; i++) si[i] -= 1; |
387 |
|
388 |
chnList.setSelectedIndices(si); |
389 |
chnList.ensureIndexIsVisible(si[0]); |
390 |
} |
391 |
|
392 |
@Override |
393 |
public void |
394 |
moveSelectedChannelsDown() { |
395 |
JSChannel[] chns = getSelectedChannels(); |
396 |
|
397 |
if(chns.length == 0 || chns[chns.length - 1] == getLastChannel()) { |
398 |
CC.getLogger().info("Can't move channel(s) down"); |
399 |
return; |
400 |
} |
401 |
|
402 |
for(int i = chns.length - 1; i >= 0; i--) listModel.moveDown(chns[i]); |
403 |
|
404 |
int[] si = chnList.getSelectedIndices(); |
405 |
for(int i = 0; i < si.length; i++) si[i] += 1; |
406 |
chnList.setSelectedIndices(si); |
407 |
chnList.ensureIndexIsVisible(si[si.length - 1]); |
408 |
} |
409 |
|
410 |
@Override |
411 |
public void |
412 |
moveSelectedChannelsAtBottom() { |
413 |
JSChannel[] chns = getSelectedChannels(); |
414 |
|
415 |
if(chns.length == 0) { |
416 |
CC.getLogger().info("Can't move channel(s) at the end"); |
417 |
return; |
418 |
} |
419 |
|
420 |
for(int i = 0; i < chns.length; i++) { |
421 |
listModel.remove(chns[i]); |
422 |
listModel.add(chns[i]); |
423 |
} |
424 |
|
425 |
chnList.setSelectionInterval ( |
426 |
listModel.getSize() - chns.length, listModel.getSize() - 1 |
427 |
); |
428 |
chnList.ensureIndexIsVisible(listModel.getSize() - 1); |
429 |
} |
430 |
|
431 |
@Override |
432 |
public void |
433 |
processChannelSelection(JSChannel c, boolean controlDown, boolean shiftDown) { |
434 |
chnList.getUI().processSelectionEvent(c, controlDown, shiftDown); |
435 |
} |
436 |
|
437 |
private void |
438 |
scrollToBottom() { |
439 |
int h = scrollPane.getViewport().getView().getHeight(); |
440 |
scrollPane.getViewport().scrollRectToVisible(new Rectangle(0, h - 2, 1, 1)); |
441 |
} |
442 |
} |