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