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.std; |
24 |
|
25 |
import java.awt.Dialog; |
26 |
import java.awt.Dimension; |
27 |
import java.awt.GridBagConstraints; |
28 |
import java.awt.GridBagLayout; |
29 |
import java.awt.Insets; |
30 |
|
31 |
import java.awt.event.ActionEvent; |
32 |
import java.awt.event.ActionListener; |
33 |
|
34 |
import java.text.NumberFormat; |
35 |
|
36 |
import javax.swing.BorderFactory; |
37 |
import javax.swing.Box; |
38 |
import javax.swing.BoxLayout; |
39 |
import javax.swing.JComboBox; |
40 |
import javax.swing.JDialog; |
41 |
import javax.swing.JLabel; |
42 |
import javax.swing.JSlider; |
43 |
import javax.swing.JPanel; |
44 |
|
45 |
import javax.swing.event.ChangeEvent; |
46 |
import javax.swing.event.ChangeListener; |
47 |
|
48 |
import net.sf.juife.InformationDialog; |
49 |
import net.sf.juife.JuifeUtils; |
50 |
|
51 |
import org.linuxsampler.lscp.SamplerEngine; |
52 |
|
53 |
import org.jsampler.CC; |
54 |
import org.jsampler.HF; |
55 |
import org.jsampler.JSPrefs; |
56 |
|
57 |
import static org.jsampler.view.std.StdI18n.i18n; |
58 |
import static org.jsampler.view.std.StdPrefs.*; |
59 |
|
60 |
|
61 |
/** |
62 |
* |
63 |
* @author Grigor Iliev |
64 |
*/ |
65 |
public class JSChannelsDefaultSettingsPane extends JPanel { |
66 |
private final JLabel lDefaultEngine = |
67 |
new JLabel(i18n.getLabel("JSChannelsDefaultSettingsPane.lDefaultEngine")); |
68 |
|
69 |
private final JLabel lMidiInput = |
70 |
new JLabel(i18n.getLabel("JSChannelsDefaultSettingsPane.lMidiInput")); |
71 |
|
72 |
private final JLabel lAudioOutput = |
73 |
new JLabel(i18n.getLabel("JSChannelsDefaultSettingsPane.lAudioOutput")); |
74 |
|
75 |
private final JLabel lChannelVolume = |
76 |
new JLabel(i18n.getLabel("JSChannelsDefaultSettingsPane.lChannelVolume")); |
77 |
|
78 |
private final JLabel lMidiMap = |
79 |
new JLabel(i18n.getLabel("JSChannelsDefaultSettingsPane.lMidiMap")); |
80 |
|
81 |
private final JComboBox cbDefaultEngine = new JComboBox(); |
82 |
private final JComboBox cbMidiInput = new JComboBox(); |
83 |
private final JComboBox cbAudioOutput = new JComboBox(); |
84 |
private final JSlider slChannelVolume = new JSlider(0, 100); |
85 |
private final JComboBox cbMidiMap = new JComboBox(); |
86 |
|
87 |
private final JLabel lVolume = new JLabel(); |
88 |
|
89 |
private final static String strFirstDevice = |
90 |
i18n.getLabel("JSChannelsDefaultSettingsPane.strFirstDevice"); |
91 |
|
92 |
private final static String strFirstDeviceNextChannel = |
93 |
i18n.getLabel("JSChannelsDefaultSettingsPane.strFirstDeviceNextChannel"); |
94 |
|
95 |
private class NoMap { |
96 |
public String |
97 |
toString() { return "[None]"; } |
98 |
} |
99 |
|
100 |
private NoMap noMap = new NoMap(); |
101 |
|
102 |
private class DefaultMap { |
103 |
public String |
104 |
toString() { return "[Default]"; } |
105 |
} |
106 |
|
107 |
private DefaultMap defaultMap = new DefaultMap(); |
108 |
|
109 |
private static NumberFormat numberFormat = NumberFormat.getInstance(); |
110 |
|
111 |
|
112 |
/** Creates a new instance of <code>JSChannelsDefaultSettingsPane</code> */ |
113 |
public |
114 |
JSChannelsDefaultSettingsPane() { |
115 |
numberFormat.setMaximumFractionDigits(1); |
116 |
|
117 |
GridBagLayout gridbag = new GridBagLayout(); |
118 |
GridBagConstraints c = new GridBagConstraints(); |
119 |
|
120 |
setLayout(gridbag); |
121 |
|
122 |
c.fill = GridBagConstraints.NONE; |
123 |
|
124 |
c.gridx = 0; |
125 |
c.gridy = 0; |
126 |
c.anchor = GridBagConstraints.EAST; |
127 |
c.insets = new Insets(3, 3, 3, 3); |
128 |
gridbag.setConstraints(lDefaultEngine, c); |
129 |
add(lDefaultEngine); |
130 |
|
131 |
c.gridx = 0; |
132 |
c.gridy = 1; |
133 |
gridbag.setConstraints(lMidiInput, c); |
134 |
add(lMidiInput); |
135 |
|
136 |
c.gridx = 0; |
137 |
c.gridy = 2; |
138 |
gridbag.setConstraints(lAudioOutput, c); |
139 |
add(lAudioOutput); |
140 |
|
141 |
c.gridx = 0; |
142 |
c.gridy = 3; |
143 |
gridbag.setConstraints(lMidiMap, c); |
144 |
add(lMidiMap); |
145 |
|
146 |
c.gridx = 0; |
147 |
c.gridy = 4; |
148 |
gridbag.setConstraints(lChannelVolume, c); |
149 |
add(lChannelVolume); |
150 |
|
151 |
c.fill = GridBagConstraints.HORIZONTAL; |
152 |
c.gridx = 1; |
153 |
c.gridy = 0; |
154 |
c.weightx = 1.0; |
155 |
c.anchor = GridBagConstraints.WEST; |
156 |
gridbag.setConstraints(cbDefaultEngine, c); |
157 |
add(cbDefaultEngine); |
158 |
|
159 |
c.gridx = 1; |
160 |
c.gridy = 1; |
161 |
gridbag.setConstraints(cbMidiInput, c); |
162 |
add(cbMidiInput); |
163 |
|
164 |
c.gridx = 1; |
165 |
c.gridy = 2; |
166 |
gridbag.setConstraints(cbAudioOutput, c); |
167 |
add(cbAudioOutput); |
168 |
|
169 |
c.gridx = 1; |
170 |
c.gridy = 3; |
171 |
gridbag.setConstraints(cbMidiMap, c); |
172 |
add(cbMidiMap); |
173 |
|
174 |
JPanel volumePane = new JPanel(); |
175 |
volumePane.setOpaque(false); |
176 |
volumePane.setLayout(new BoxLayout(volumePane, BoxLayout.X_AXIS)); |
177 |
|
178 |
Dimension d = slChannelVolume.getPreferredSize(); |
179 |
slChannelVolume.setMaximumSize(new Dimension(d.width > 300 ? d.width : 300, d.height)); |
180 |
slChannelVolume.setOpaque(false); |
181 |
volumePane.add(slChannelVolume); |
182 |
|
183 |
volumePane.add(Box.createRigidArea(new Dimension(6, 0))); |
184 |
|
185 |
lVolume.setHorizontalAlignment(lVolume.RIGHT); |
186 |
|
187 |
// We use this to set the size of the lVolume |
188 |
// to prevent the frequent resizing of lVolume |
189 |
lVolume.setText("100000%"); |
190 |
lVolume.setPreferredSize(lVolume.getPreferredSize()); |
191 |
lVolume.setMinimumSize(lVolume.getPreferredSize()); |
192 |
|
193 |
volumePane.add(lVolume); |
194 |
|
195 |
slChannelVolume.addChangeListener(new ChangeListener() { |
196 |
public void |
197 |
stateChanged(ChangeEvent e) { updateVolume(); } |
198 |
}); |
199 |
|
200 |
int v = preferences().getIntProperty(DEFAULT_CHANNEL_VOLUME); |
201 |
slChannelVolume.setValue(v); |
202 |
|
203 |
c.gridx = 1; |
204 |
c.gridy = 4; |
205 |
gridbag.setConstraints(volumePane, c); |
206 |
add(volumePane); |
207 |
|
208 |
for(SamplerEngine e : CC.getSamplerModel().getEngines()) { |
209 |
cbDefaultEngine.addItem(e); |
210 |
} |
211 |
|
212 |
String defaultEngine = preferences().getStringProperty(DEFAULT_ENGINE); |
213 |
for(SamplerEngine e : CC.getSamplerModel().getEngines()) { |
214 |
if(e.getName().equals(defaultEngine)) cbDefaultEngine.setSelectedItem(e); |
215 |
} |
216 |
|
217 |
cbDefaultEngine.addActionListener(new ActionListener() { |
218 |
public void |
219 |
actionPerformed(ActionEvent e) { changeDefaultEngine(); } |
220 |
}); |
221 |
|
222 |
cbMidiInput.addItem(strFirstDevice); |
223 |
cbMidiInput.addItem(strFirstDeviceNextChannel); |
224 |
|
225 |
String s = preferences().getStringProperty(DEFAULT_MIDI_INPUT); |
226 |
|
227 |
if(s.equals("firstDevice")) { |
228 |
cbMidiInput.setSelectedItem(strFirstDevice); |
229 |
} else if(s.equals("firstDeviceNextChannel")) { |
230 |
cbMidiInput.setSelectedItem(strFirstDeviceNextChannel); |
231 |
} |
232 |
|
233 |
cbMidiInput.addActionListener(new ActionListener() { |
234 |
public void |
235 |
actionPerformed(ActionEvent e) { changeDefaultMidiInput(); } |
236 |
}); |
237 |
|
238 |
cbAudioOutput.addItem(strFirstDevice); |
239 |
|
240 |
cbAudioOutput.addActionListener(new ActionListener() { |
241 |
public void |
242 |
actionPerformed(ActionEvent e) { changeDefaultAudioOutput(); } |
243 |
}); |
244 |
|
245 |
cbMidiMap.addItem(noMap); |
246 |
cbMidiMap.addItem(defaultMap); |
247 |
|
248 |
String midiMap = preferences().getStringProperty(DEFAULT_MIDI_INSTRUMENT_MAP); |
249 |
if(midiMap.equals("midiInstrumentMap.none")) { |
250 |
cbMidiMap.setSelectedItem(noMap); |
251 |
} else if(midiMap.equals("midiInstrumentMap.default")) { |
252 |
cbMidiMap.setSelectedItem(defaultMap); |
253 |
} |
254 |
|
255 |
cbMidiMap.addActionListener(new ActionListener() { |
256 |
public void |
257 |
actionPerformed(ActionEvent e) { changeDefaultMidiMap(); } |
258 |
}); |
259 |
} |
260 |
|
261 |
public JDialog |
262 |
createDialog(Dialog owner) { |
263 |
String s = i18n.getLabel("JSChannelsDefaultSettingsPane.title"); |
264 |
InformationDialog dlg = new InformationDialog(owner, s, this); |
265 |
return dlg; |
266 |
} |
267 |
|
268 |
private void |
269 |
changeDefaultEngine() { |
270 |
Object o = cbDefaultEngine.getSelectedItem(); |
271 |
if(o == null) return; |
272 |
String s = ((SamplerEngine) o).getName(); |
273 |
preferences().setStringProperty(DEFAULT_ENGINE, s); |
274 |
} |
275 |
|
276 |
private void |
277 |
changeDefaultMidiInput() { |
278 |
Object o = cbMidiInput.getSelectedItem(); |
279 |
if(o == null) return; |
280 |
|
281 |
if(o == strFirstDevice) { |
282 |
preferences().setStringProperty(DEFAULT_MIDI_INPUT, "firstDevice"); |
283 |
} else if(o == strFirstDeviceNextChannel) { |
284 |
preferences().setStringProperty(DEFAULT_MIDI_INPUT, "firstDeviceNextChannel"); |
285 |
} |
286 |
} |
287 |
|
288 |
private void |
289 |
changeDefaultAudioOutput() { |
290 |
Object o = cbAudioOutput.getSelectedItem(); |
291 |
if(o == null) return; |
292 |
|
293 |
if(o == strFirstDevice) { |
294 |
preferences().setStringProperty(DEFAULT_AUDIO_OUTPUT, "firstDevice"); |
295 |
} |
296 |
} |
297 |
|
298 |
private void |
299 |
changeDefaultMidiMap() { |
300 |
Object o = cbMidiMap.getSelectedItem(); |
301 |
if(o == null) return; |
302 |
String s = DEFAULT_MIDI_INSTRUMENT_MAP; |
303 |
if(o == noMap) { |
304 |
preferences().setStringProperty(s, "midiInstrumentMap.none"); |
305 |
} else if(o == defaultMap) { |
306 |
preferences().setStringProperty(s, "midiInstrumentMap.default"); |
307 |
} |
308 |
} |
309 |
|
310 |
private void |
311 |
updateVolume() { |
312 |
int volume = slChannelVolume.getValue(); |
313 |
if(CC.getViewConfig().isMeasurementUnitDecibel()) { |
314 |
double dB = HF.percentsToDecibels(volume); |
315 |
lVolume.setText(numberFormat.format(dB) + "dB"); |
316 |
} else { |
317 |
lVolume.setText(String.valueOf(volume) + '%'); |
318 |
} |
319 |
|
320 |
if(slChannelVolume.getValueIsAdjusting()) return; |
321 |
preferences().setIntProperty(DEFAULT_CHANNEL_VOLUME, slChannelVolume.getValue()); |
322 |
} |
323 |
|
324 |
private static JSPrefs |
325 |
preferences() { return CC.getViewConfig().preferences(); } |
326 |
} |