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