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