/[svn]/jsampler/trunk/src/org/jsampler/view/classic/ChannelsPane.java
ViewVC logotype

Contents of /jsampler/trunk/src/org/jsampler/view/classic/ChannelsPane.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2288 - (show annotations) (download)
Wed Nov 23 21:19:44 2011 UTC (12 years, 5 months ago) by iliev
File size: 13398 byte(s)
* Added option to select a sampler engine in Add/Edit Instrument dialog
* Moved all Swing dependent code outside the JSampler core

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

  ViewVC Help
Powered by ViewVC