1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005, 2006 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.Dimension; |
26 |
|
27 |
import java.awt.datatransfer.DataFlavor; |
28 |
import java.awt.datatransfer.Transferable; |
29 |
|
30 |
import java.awt.event.ActionEvent; |
31 |
import java.awt.event.ActionListener; |
32 |
import java.awt.event.MouseAdapter; |
33 |
import java.awt.event.MouseEvent; |
34 |
|
35 |
import java.util.logging.Level; |
36 |
|
37 |
import javax.swing.BorderFactory; |
38 |
import javax.swing.Box; |
39 |
import javax.swing.BoxLayout; |
40 |
import javax.swing.JComboBox; |
41 |
import javax.swing.JComponent; |
42 |
import javax.swing.JScrollPane; |
43 |
import javax.swing.JSeparator; |
44 |
import javax.swing.TransferHandler; |
45 |
|
46 |
import net.sf.juife.LinkButton; |
47 |
import net.sf.juife.NavigationPage; |
48 |
|
49 |
import org.jsampler.CC; |
50 |
import org.jsampler.DefaultOrchestraModel; |
51 |
import org.jsampler.HF; |
52 |
import org.jsampler.Instrument; |
53 |
import org.jsampler.OrchestraModel; |
54 |
|
55 |
import org.jsampler.event.OrchestraAdapter; |
56 |
import org.jsampler.event.OrchestraEvent; |
57 |
import org.jsampler.event.OrchestraListEvent; |
58 |
import org.jsampler.event.OrchestraListListener; |
59 |
|
60 |
import org.jsampler.view.InstrumentTable; |
61 |
import org.jsampler.view.InstrumentTableModel; |
62 |
import org.jsampler.view.JSChannel; |
63 |
import org.jsampler.view.JSChannelsPane; |
64 |
|
65 |
import static org.jsampler.view.classic.ClassicI18n.i18n; |
66 |
|
67 |
|
68 |
/** |
69 |
* |
70 |
* @author Grigor Iliev |
71 |
*/ |
72 |
public class OrchestrasPage extends NavigationPage { |
73 |
private final JComboBox cbOrchestras = new JComboBox(); |
74 |
private final InstrumentTable instrumentTable = new InstrumentTable(new InstrTableModel()); |
75 |
|
76 |
private final LinkButton lnkManageOrchestras = |
77 |
new LinkButton(i18n.getButtonLabel("OrchestrasPage.lnkManageOrchestras")); |
78 |
|
79 |
|
80 |
/** Creates a new instance of OrchestrasPage */ |
81 |
public OrchestrasPage() { |
82 |
setTitle(i18n.getLabel("OrchestrasPage.title")); |
83 |
|
84 |
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
85 |
|
86 |
lnkManageOrchestras.setAlignmentX(LEFT_ALIGNMENT); |
87 |
add(lnkManageOrchestras); |
88 |
|
89 |
JSeparator sep = new JSeparator(); |
90 |
sep.setMaximumSize(new Dimension(Short.MAX_VALUE, sep.getPreferredSize().height)); |
91 |
add(sep); |
92 |
|
93 |
add(Box.createRigidArea(new Dimension(0, 12))); |
94 |
|
95 |
cbOrchestras.addActionListener(new ActionListener() { |
96 |
public void |
97 |
actionPerformed(ActionEvent e) { orchestraChanged(); } |
98 |
}); |
99 |
|
100 |
for(int i = 0; i < CC.getOrchestras().getOrchestraCount(); i++) { |
101 |
cbOrchestras.addItem(CC.getOrchestras().getOrchestra(i)); |
102 |
} |
103 |
|
104 |
CC.getOrchestras().addOrchestraListListener(getHandler()); |
105 |
cbOrchestras.setEnabled(cbOrchestras.getItemCount() != 0); |
106 |
|
107 |
Dimension d; |
108 |
d= new Dimension(Short.MAX_VALUE, cbOrchestras.getPreferredSize().height); |
109 |
cbOrchestras.setMaximumSize(d); |
110 |
cbOrchestras.setAlignmentX(LEFT_ALIGNMENT); |
111 |
add(cbOrchestras); |
112 |
|
113 |
add(Box.createRigidArea(new Dimension(0, 5))); |
114 |
|
115 |
JScrollPane sp = new DnDScrollPane(instrumentTable); |
116 |
sp.setAlignmentX(LEFT_ALIGNMENT); |
117 |
add(sp); |
118 |
|
119 |
setBorder(BorderFactory.createEmptyBorder(5, 3, 5, 3)); |
120 |
|
121 |
installListeners(); |
122 |
} |
123 |
|
124 |
private void |
125 |
installListeners() { |
126 |
lnkManageOrchestras.addActionListener(new ActionListener() { |
127 |
public void |
128 |
actionPerformed(ActionEvent e) { |
129 |
LeftPane.getLeftPane().showManageOrchestrasPage(); |
130 |
} |
131 |
}); |
132 |
|
133 |
instrumentTable.addMouseListener(new MouseAdapter() { |
134 |
public void |
135 |
mouseClicked(MouseEvent e) { |
136 |
if(e.getClickCount() < 2) return; |
137 |
|
138 |
Instrument instr = instrumentTable.getSelectedInstrument(); |
139 |
if(instr == null) return; |
140 |
loadInstrument(instr); |
141 |
} |
142 |
}); |
143 |
} |
144 |
|
145 |
private void |
146 |
loadInstrument(Instrument instr) { |
147 |
JSChannelsPane cp = CC.getMainFrame().getSelectedChannelsPane(); |
148 |
JSChannel chn = null; |
149 |
|
150 |
if(cp.hasSelectedChannel()) chn = cp.getSelectedChannels()[0]; |
151 |
if(chn == null) { |
152 |
for(JSChannel c : cp.getChannels()) { |
153 |
if( (c.getChannelInfo().getInstrumentName() == null || |
154 |
c.getChannelInfo().getInstrumentStatus() < -1) && |
155 |
c.getChannelInfo().getAudioOutputDevice() != -1 ) { |
156 |
chn = c; |
157 |
break; |
158 |
} |
159 |
} |
160 |
} |
161 |
|
162 |
if(chn == null) { |
163 |
HF.showErrorMessage("Select channel!"); |
164 |
return; |
165 |
} |
166 |
|
167 |
chn.getModel().loadInstrument(instr.getPath(), instr.getInstrumentIndex()); |
168 |
} |
169 |
|
170 |
/** |
171 |
* Gets the index of the orchestra whose instruments are shown. |
172 |
* @return The position of the currently selected orchestra, |
173 |
* and -1 if no orchestra is selected. |
174 |
*/ |
175 |
public int |
176 |
getCurrentOrchestraIndex() { |
177 |
OrchestraModel om = (OrchestraModel)cbOrchestras.getSelectedItem(); |
178 |
return CC.getOrchestras().getOrchestraIndex(om); |
179 |
} |
180 |
|
181 |
public void |
182 |
setSelectedOrchestra(OrchestraModel orchestra) { |
183 |
cbOrchestras.setSelectedItem(orchestra); |
184 |
} |
185 |
|
186 |
private void |
187 |
orchestraChanged() { |
188 |
OrchestraModel om = (OrchestraModel)cbOrchestras.getSelectedItem(); |
189 |
if(om == null) om = new DefaultOrchestraModel(); |
190 |
instrumentTable.getModel().setOrchestraModel(om); |
191 |
|
192 |
String s = om.getDescription(); |
193 |
if(s != null && s.length() == 0) s = null; |
194 |
cbOrchestras.setToolTipText(s); |
195 |
} |
196 |
|
197 |
public class DnDScrollPane extends JScrollPane { |
198 |
private InstrumentTable instrumentTable; |
199 |
|
200 |
private |
201 |
DnDScrollPane(InstrumentTable table) { |
202 |
instrumentTable = table; |
203 |
setViewport(new DnDViewPort()); |
204 |
setViewportView(instrumentTable); |
205 |
|
206 |
Dimension d; |
207 |
d = new Dimension(getMinimumSize().width, getPreferredSize().height); |
208 |
setPreferredSize(d); |
209 |
} |
210 |
|
211 |
public class DnDViewPort extends javax.swing.JViewport { |
212 |
private |
213 |
DnDViewPort() { |
214 |
setTransferHandler(new TransferHandler("instrument") { |
215 |
public boolean |
216 |
canImport(JComponent comp, DataFlavor[] flavors) { |
217 |
if(instrumentTable.isPerformingDnD()) return false; |
218 |
if(cbOrchestras.getSelectedItem() == null) { |
219 |
return false; |
220 |
} |
221 |
|
222 |
return super.canImport(comp, flavors); |
223 |
} |
224 |
}); |
225 |
|
226 |
addMouseListener(new MouseAdapter() { |
227 |
public void |
228 |
mouseClicked(MouseEvent e) { |
229 |
instrumentTable.clearSelection(); |
230 |
} |
231 |
}); |
232 |
} |
233 |
|
234 |
public String |
235 |
getInstrument() { return null; } |
236 |
|
237 |
public void |
238 |
setInstrument(String instr) { |
239 |
instrumentTable.setSelectedInstrument(null); |
240 |
instrumentTable.setInstrument(instr); |
241 |
} |
242 |
} |
243 |
|
244 |
/*class SPTransferHandler extends TransferHandler { |
245 |
public boolean |
246 |
canImport(JComponent comp, DataFlavor[] flavors) { |
247 |
if(instrumentTable.isPerformingDnD()) return false; |
248 |
if(cbOrchestras.getSelectedItem() == null) return false; |
249 |
|
250 |
for(int i = 0; i < flavors.length; i++) { |
251 |
if(DataFlavor.stringFlavor.equals(flavors[i])) { |
252 |
return true; |
253 |
} |
254 |
} |
255 |
|
256 |
return false; |
257 |
} |
258 |
|
259 |
public boolean |
260 |
importData(JComponent c, Transferable t) { |
261 |
if(!canImport(c, t.getTransferDataFlavors())) return false; |
262 |
String str; |
263 |
try { |
264 |
str = (String)t.getTransferData(DataFlavor.stringFlavor); |
265 |
instrumentTable.setInstrument(str); |
266 |
return true; |
267 |
} catch(Exception x) { |
268 |
CC.getLogger().log(Level.INFO, HF.getErrorMessage(x), x); |
269 |
} |
270 |
|
271 |
return false; |
272 |
} |
273 |
}*/ |
274 |
} |
275 |
|
276 |
|
277 |
private final Handler eventHandler = new Handler(); |
278 |
|
279 |
private Handler |
280 |
getHandler() { return eventHandler; } |
281 |
|
282 |
private class Handler extends OrchestraAdapter implements OrchestraListListener { |
283 |
/** Invoked when an orchestra is added to the orchestra list. */ |
284 |
public void |
285 |
orchestraAdded(OrchestraListEvent e) { |
286 |
if(cbOrchestras.getItemCount() == 0) cbOrchestras.setEnabled(true); |
287 |
cbOrchestras.addItem(e.getOrchestraModel()); |
288 |
} |
289 |
|
290 |
/** Invoked when an orchestra is removed from the orchestra list. */ |
291 |
public void |
292 |
orchestraRemoved(OrchestraListEvent e) { |
293 |
cbOrchestras.removeItem(e.getOrchestraModel()); |
294 |
if(cbOrchestras.getItemCount() == 0) cbOrchestras.setEnabled(false); |
295 |
} |
296 |
|
297 |
/** Invoked when the name of orchestra is changed. */ |
298 |
public void |
299 |
nameChanged(OrchestraEvent e) { |
300 |
|
301 |
} |
302 |
|
303 |
/** Invoked when the description of orchestra is changed. */ |
304 |
public void |
305 |
descriptionChanged(OrchestraEvent e) { } |
306 |
|
307 |
|
308 |
} |
309 |
|
310 |
private class InstrTableModel extends org.jsampler.view.InstrumentTableModel { |
311 |
/** |
312 |
* Returns <code>true</code> if the cell at |
313 |
* <code>row</code> and <code>col</code> is editable. |
314 |
*/ |
315 |
public boolean |
316 |
isCellEditable(int row, int col) { return false; } |
317 |
|
318 |
/** |
319 |
* Gets the name of the column at <code>columnIndex</code>. |
320 |
* @return The name of the column at <code>columnIndex</code>. |
321 |
*/ |
322 |
public String |
323 |
getColumnName(int col) { |
324 |
return i18n.getLabel("OrchestrasPage.instruments"); |
325 |
} |
326 |
} |
327 |
} |