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.classic; |
24 |
|
25 |
import java.awt.Dialog; |
26 |
import java.awt.Dimension; |
27 |
import java.awt.Frame; |
28 |
import java.awt.GridBagConstraints; |
29 |
import java.awt.GridBagLayout; |
30 |
import java.awt.Insets; |
31 |
|
32 |
import javax.swing.JComboBox; |
33 |
import javax.swing.JLabel; |
34 |
import javax.swing.JPanel; |
35 |
import javax.swing.JSlider; |
36 |
import javax.swing.JSpinner; |
37 |
import javax.swing.JTextField; |
38 |
import javax.swing.SpinnerNumberModel; |
39 |
|
40 |
import javax.swing.event.DocumentEvent; |
41 |
import javax.swing.event.DocumentListener; |
42 |
|
43 |
import net.sf.juife.OkCancelDialog; |
44 |
|
45 |
import org.linuxsampler.lscp.MidiInstrumentInfo; |
46 |
|
47 |
import static org.jsampler.view.classic.ClassicI18n.i18n; |
48 |
|
49 |
|
50 |
/** |
51 |
* |
52 |
* @author Grigor Iliev |
53 |
*/ |
54 |
public class AddMidiInstrumentDlg extends OkCancelDialog { |
55 |
private final JLabel lName = new JLabel(i18n.getLabel("AddMidiInstrumentDlg.lName")); |
56 |
private final JLabel lBank = new JLabel(i18n.getLabel("AddMidiInstrumentDlg.lBank")); |
57 |
private final JLabel lProgram = new JLabel(i18n.getLabel("AddMidiInstrumentDlg.lProgram")); |
58 |
private final JLabel lVolume = new JLabel(i18n.getLabel("AddMidiInstrumentDlg.lVolume")); |
59 |
private final JLabel lLoadMode = |
60 |
new JLabel(i18n.getLabel("AddMidiInstrumentDlg.lLoadMode")); |
61 |
|
62 |
private final JTextField tfName = new JTextField(); |
63 |
private final JSpinner spinnerBank = new JSpinner(new SpinnerNumberModel(0, 0, 16383, 1)); |
64 |
private final JComboBox cbProgram = new JComboBox(); |
65 |
private final JSlider slVolume = new JSlider(0, 100, 100); |
66 |
private final JComboBox cbLoadMode = new JComboBox(); |
67 |
|
68 |
/** Creates a new instance of <code>AddMidiInstrumentDlg</code> */ |
69 |
public |
70 |
AddMidiInstrumentDlg(Frame owner) { |
71 |
super(owner, i18n.getLabel("AddMidiInstrumentDlg.title")); |
72 |
initAddMidiInstrumentDlg(); |
73 |
} |
74 |
|
75 |
/** Creates a new instance of <code>AddMidiInstrumentDlg</code> */ |
76 |
public |
77 |
AddMidiInstrumentDlg(Dialog owner) { |
78 |
super(owner, i18n.getLabel("AddMidiInstrumentDlg.title")); |
79 |
initAddMidiInstrumentDlg(); |
80 |
} |
81 |
|
82 |
private void |
83 |
initAddMidiInstrumentDlg() { |
84 |
JPanel mainPane = new JPanel(); |
85 |
GridBagLayout gridbag = new GridBagLayout(); |
86 |
GridBagConstraints c = new GridBagConstraints(); |
87 |
|
88 |
mainPane.setLayout(gridbag); |
89 |
|
90 |
c.fill = GridBagConstraints.NONE; |
91 |
|
92 |
c.gridx = 0; |
93 |
c.gridy = 0; |
94 |
c.anchor = GridBagConstraints.EAST; |
95 |
c.insets = new Insets(3, 3, 3, 3); |
96 |
gridbag.setConstraints(lName, c); |
97 |
mainPane.add(lName); |
98 |
|
99 |
c.gridx = 0; |
100 |
c.gridy = 2; |
101 |
gridbag.setConstraints(lBank, c); |
102 |
mainPane.add(lBank); |
103 |
|
104 |
c.gridx = 0; |
105 |
c.gridy = 3; |
106 |
gridbag.setConstraints(lProgram, c); |
107 |
mainPane.add(lProgram); |
108 |
|
109 |
c.gridx = 0; |
110 |
c.gridy = 4; |
111 |
gridbag.setConstraints(lLoadMode, c); |
112 |
mainPane.add(lLoadMode); |
113 |
|
114 |
c.gridx = 0; |
115 |
c.gridy = 1; |
116 |
c.insets = new Insets(3, 3, 24, 3); |
117 |
gridbag.setConstraints(lVolume, c); |
118 |
mainPane.add(lVolume); |
119 |
|
120 |
c.fill = GridBagConstraints.HORIZONTAL; |
121 |
c.gridx = 1; |
122 |
c.gridy = 0; |
123 |
c.weightx = 1.0; |
124 |
c.insets = new Insets(3, 3, 3, 3); |
125 |
c.anchor = GridBagConstraints.WEST; |
126 |
gridbag.setConstraints(tfName, c); |
127 |
mainPane.add(tfName); |
128 |
|
129 |
c.gridx = 1; |
130 |
c.gridy = 2; |
131 |
gridbag.setConstraints(spinnerBank, c); |
132 |
mainPane.add(spinnerBank); |
133 |
|
134 |
c.gridx = 1; |
135 |
c.gridy = 3; |
136 |
gridbag.setConstraints(cbProgram, c); |
137 |
mainPane.add(cbProgram); |
138 |
|
139 |
c.gridx = 1; |
140 |
c.gridy = 4; |
141 |
gridbag.setConstraints(cbLoadMode, c); |
142 |
mainPane.add(cbLoadMode); |
143 |
|
144 |
c.gridx = 1; |
145 |
c.gridy = 1; |
146 |
c.insets = new Insets(3, 3, 24, 3); |
147 |
gridbag.setConstraints(slVolume, c); |
148 |
mainPane.add(slVolume); |
149 |
|
150 |
setMainPane(mainPane); |
151 |
|
152 |
setResizable(true); |
153 |
setMinimumSize(getPreferredSize()); |
154 |
|
155 |
for(int i = 0; i < 128; i++) cbProgram.addItem(new Integer(i)); |
156 |
|
157 |
cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.DEFAULT); |
158 |
cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.ON_DEMAND); |
159 |
cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.ON_DEMAND_HOLD); |
160 |
cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.PERSISTENT); |
161 |
|
162 |
tfName.getDocument().addDocumentListener(getHandler()); |
163 |
}/** |
164 |
* Gets the selected MIDI bank. |
165 |
*/ |
166 |
public int |
167 |
getMidiBank() { return Integer.parseInt(spinnerBank.getValue().toString()); } |
168 |
|
169 |
/** |
170 |
* Gets the selected MIDI program. |
171 |
*/ |
172 |
public int |
173 |
getMidiProgram() { return cbProgram.getSelectedIndex(); } |
174 |
|
175 |
/** |
176 |
* Gets the chosen name for the new MIDI instrument. |
177 |
* @return The chosen name for the new MIDI instrument. |
178 |
*/ |
179 |
public String |
180 |
getInstrumentName() { return tfName.getText(); } |
181 |
|
182 |
/** |
183 |
* Sets the name for the new MIDI instrument. |
184 |
* @param name The name for the new MIDI instrument. |
185 |
*/ |
186 |
public void |
187 |
setInstrumentName(String name) { tfName.setText(name); } |
188 |
|
189 |
/** |
190 |
* Returns the volume level of the new MIDI instrument. |
191 |
* @return The volume level of the new MIDI instrument. |
192 |
*/ |
193 |
public float |
194 |
getVolume() { |
195 |
float f = slVolume.getValue(); |
196 |
f /= 100; |
197 |
return f; |
198 |
} |
199 |
|
200 |
/** Gets the selected load mode. */ |
201 |
public MidiInstrumentInfo.LoadMode |
202 |
getLoadMode() { return (MidiInstrumentInfo.LoadMode) cbLoadMode.getSelectedItem(); } |
203 |
|
204 |
protected void |
205 |
onOk() { |
206 |
if(!btnOk.isEnabled()) return; |
207 |
setCancelled(false); |
208 |
setVisible(false); |
209 |
} |
210 |
|
211 |
protected void |
212 |
onCancel() { setVisible(false); } |
213 |
|
214 |
private void |
215 |
updateState() { |
216 |
boolean b = tfName.getText().length() > 0; |
217 |
b = b && cbProgram.getSelectedItem() != null; |
218 |
btnOk.setEnabled(b); |
219 |
} |
220 |
|
221 |
private final Handler eventHandler = new Handler(); |
222 |
|
223 |
private Handler |
224 |
getHandler() { return eventHandler; } |
225 |
|
226 |
private class Handler implements DocumentListener { |
227 |
// DocumentListener |
228 |
public void |
229 |
insertUpdate(DocumentEvent e) { updateState(); } |
230 |
|
231 |
public void |
232 |
removeUpdate(DocumentEvent e) { updateState(); } |
233 |
|
234 |
public void |
235 |
changedUpdate(DocumentEvent e) { updateState(); } |
236 |
} |
237 |
} |