1 |
iliev |
912 |
/* |
2 |
|
|
* JSampler - a java front-end for LinuxSampler |
3 |
|
|
* |
4 |
iliev |
1285 |
* Copyright (C) 2005-2007 Grigor Iliev <grigor@grigoriliev.com> |
5 |
iliev |
912 |
* |
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.fantasia; |
24 |
|
|
|
25 |
|
|
import java.awt.BorderLayout; |
26 |
|
|
import java.awt.Component; |
27 |
|
|
|
28 |
iliev |
1285 |
import javax.swing.BoxLayout; |
29 |
iliev |
912 |
import javax.swing.ListSelectionModel; |
30 |
|
|
|
31 |
|
|
import javax.swing.event.ListSelectionEvent; |
32 |
|
|
import javax.swing.event.ListSelectionListener; |
33 |
|
|
|
34 |
|
|
import net.sf.juife.ComponentList; |
35 |
|
|
import net.sf.juife.ComponentListModel; |
36 |
|
|
import net.sf.juife.DefaultComponentListModel; |
37 |
|
|
|
38 |
|
|
import org.jsampler.CC; |
39 |
|
|
import org.jsampler.SamplerChannelModel; |
40 |
|
|
|
41 |
|
|
import org.jsampler.view.JSChannel; |
42 |
|
|
import org.jsampler.view.JSChannelsPane; |
43 |
|
|
|
44 |
|
|
|
45 |
|
|
/** |
46 |
|
|
* |
47 |
|
|
* @author Grigor Iliev |
48 |
|
|
*/ |
49 |
|
|
public class ChannelsPane extends JSChannelsPane { |
50 |
iliev |
1285 |
private final ChannelListPane chnList = new ChannelListPane(); |
51 |
iliev |
912 |
private final DefaultComponentListModel listModel = new DefaultComponentListModel(); |
52 |
|
|
|
53 |
|
|
|
54 |
|
|
/** |
55 |
|
|
* Creates a new instance of <code>ChannelsPane</code> with |
56 |
|
|
* the specified <code>title</code>. |
57 |
|
|
* @param title The title of this <code>ChannelsPane</code> |
58 |
|
|
*/ |
59 |
|
|
public |
60 |
|
|
ChannelsPane(String title) { |
61 |
|
|
super(title); |
62 |
|
|
|
63 |
iliev |
1285 |
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
64 |
iliev |
912 |
|
65 |
|
|
chnList.setModel(listModel); |
66 |
|
|
chnList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
67 |
|
|
|
68 |
|
|
add(chnList); |
69 |
|
|
} |
70 |
|
|
|
71 |
iliev |
1285 |
class ChannelListPane extends ComponentList { |
72 |
|
|
public java.awt.Dimension |
73 |
|
|
getMaximumSize() { return getPreferredSize(); } |
74 |
|
|
} |
75 |
|
|
|
76 |
iliev |
912 |
/** |
77 |
|
|
* Adds new channel to this channels pane. |
78 |
|
|
* @param channelModel The sampler channel model to be used by the new channel. |
79 |
|
|
*/ |
80 |
|
|
public void |
81 |
|
|
addChannel(SamplerChannelModel channelModel) { |
82 |
|
|
Channel channel = new Channel(channelModel); |
83 |
|
|
listModel.add(channel); |
84 |
iliev |
1285 |
if(channel.getChannelInfo().getEngine() == null) channel.expandChannel(false); |
85 |
iliev |
912 |
chnList.setSelectedComponent(channel, true); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
/** |
89 |
|
|
* Adds the specified channels to this channels pane. |
90 |
|
|
* @param chns The channels to be added. |
91 |
|
|
*/ |
92 |
|
|
public void |
93 |
|
|
addChannels(JSChannel[] chns) { |
94 |
|
|
if(chns == null || chns.length == 0) return; |
95 |
|
|
|
96 |
|
|
for(JSChannel c : chns) listModel.add(c); |
97 |
|
|
|
98 |
|
|
chnList.setSelectedIndex(listModel.getSize() - 1); |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
/** |
102 |
|
|
* Removes the specified channel from this channels pane. |
103 |
|
|
* This method is invoked when a sampler channel is removed in the back-end. |
104 |
|
|
* @param chn The channel to be removed from this channels pane. |
105 |
|
|
*/ |
106 |
|
|
public void |
107 |
|
|
removeChannel(JSChannel chn) { |
108 |
|
|
listModel.remove(chn); |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
/** |
112 |
|
|
* Determines whether there is at least one selected channel. |
113 |
|
|
* @return <code>true</code> if there is at least one selected channel, |
114 |
|
|
* <code>false</code> otherwise. |
115 |
|
|
*/ |
116 |
|
|
public boolean |
117 |
|
|
hasSelectedChannel() { return !chnList.isSelectionEmpty(); } |
118 |
|
|
|
119 |
|
|
/** |
120 |
|
|
* Gets the first channel in this channels pane. |
121 |
|
|
* @return The first channel in this channels pane or <code>null</code> if |
122 |
|
|
* the channels pane is empty. |
123 |
|
|
*/ |
124 |
|
|
public JSChannel |
125 |
|
|
getFirstChannel() { return listModel.size() == 0 ? null : (JSChannel)listModel.get(0); } |
126 |
|
|
|
127 |
|
|
/** |
128 |
|
|
* Gets the last channel in this channels pane. |
129 |
|
|
* @return The last channel in this channels pane or <code>null</code> if |
130 |
|
|
* the channels pane is empty. |
131 |
|
|
*/ |
132 |
|
|
public JSChannel |
133 |
|
|
getLastChannel() { |
134 |
|
|
return listModel.size() == 0 ? null : (JSChannel)listModel.get(listModel.size()-1); |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
/** |
138 |
|
|
* Gets the channel at the specified index. |
139 |
|
|
* @return The channel at the specified index. |
140 |
|
|
* @throws ArrayIndexOutOfBoundsException If the index is out of range. |
141 |
|
|
*/ |
142 |
|
|
public JSChannel |
143 |
|
|
getChannel(int idx) { return (JSChannel)listModel.get(idx); } |
144 |
|
|
|
145 |
|
|
/** |
146 |
|
|
* Gets an array of all channels in this channels pane. |
147 |
|
|
* @return An array of all channels in this channels pane. |
148 |
|
|
*/ |
149 |
|
|
public JSChannel[] |
150 |
|
|
getChannels() { |
151 |
|
|
JSChannel[] chns = new JSChannel[listModel.size()]; |
152 |
|
|
for(int i = 0; i < listModel.size(); i++) chns[i] = (JSChannel)listModel.get(i); |
153 |
|
|
return chns; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
/** |
157 |
|
|
* Gets the number of channels in this channels pane. |
158 |
|
|
* @return The number of channels in this channels pane. |
159 |
|
|
*/ |
160 |
|
|
public int |
161 |
|
|
getChannelCount() { return listModel.size(); } |
162 |
|
|
|
163 |
|
|
/** |
164 |
|
|
* Gets an array of all selected channels. |
165 |
|
|
* The channels are sorted in increasing index order. |
166 |
|
|
* @return The selected channels or an empty array if nothing is selected. |
167 |
|
|
*/ |
168 |
|
|
public JSChannel[] |
169 |
|
|
getSelectedChannels() { |
170 |
|
|
Component[] cS = chnList.getSelectedComponents(); |
171 |
|
|
JSChannel[] chns = new JSChannel[cS.length]; |
172 |
|
|
for(int i = 0; i < cS.length; i++) chns[i] = (JSChannel)cS[i]; |
173 |
|
|
return chns; |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
/** |
177 |
|
|
* Gets the number of the selected channels. |
178 |
|
|
* @return The number of the selected channels. |
179 |
|
|
*/ |
180 |
|
|
public int |
181 |
|
|
getSelectedChannelCount() { return chnList.getSelectedIndices().length; } |
182 |
|
|
|
183 |
|
|
/** |
184 |
|
|
* Removes all selected channels in this channels pane. |
185 |
|
|
* Notice that this method does not remove any channels in the back-end. |
186 |
|
|
* It is invoked after the channels are already removed in the back-end. |
187 |
|
|
* @return The number of removed channels. |
188 |
|
|
*/ |
189 |
|
|
public int |
190 |
|
|
removeSelectedChannels() { |
191 |
|
|
int[] l = chnList.getSelectedIndices(); |
192 |
|
|
ComponentListModel model = chnList.getModel(); |
193 |
|
|
|
194 |
|
|
for(;;) { |
195 |
|
|
int i = chnList.getMinSelectionIndex(); |
196 |
|
|
if(i == -1) break; |
197 |
|
|
model.remove(i); |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
return l.length; |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
/** |
204 |
|
|
* Registers the specified listener for receiving list selection events. |
205 |
|
|
* @param listener The <code>ListSelectionListener</code> to register. |
206 |
|
|
*/ |
207 |
|
|
public void |
208 |
|
|
addListSelectionListener(ListSelectionListener listener) { |
209 |
|
|
listenerList.add(ListSelectionListener.class, listener); |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
/** |
213 |
|
|
* Removes the specified listener. |
214 |
|
|
* @param listener The <code>ListSelectionListener</code> to remove. |
215 |
|
|
*/ |
216 |
|
|
public void |
217 |
|
|
removeListSelectionListener(ListSelectionListener listener) { |
218 |
|
|
listenerList.remove(ListSelectionListener.class, listener); |
219 |
|
|
} |
220 |
|
|
} |