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