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