/[svn]/jsampler/trunk/src/org/jsampler/view/std/JSAddMidiInstrumentDlg.java
ViewVC logotype

Annotation of /jsampler/trunk/src/org/jsampler/view/std/JSAddMidiInstrumentDlg.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1753 - (hide annotations) (download)
Tue Aug 12 14:18:51 2008 UTC (15 years, 8 months ago) by iliev
File size: 9026 byte(s)
* fixed bug #98

1 iliev 1286 /*
2     * JSampler - a java front-end for LinuxSampler
3     *
4 iliev 1753 * Copyright (C) 2005-2008 Grigor Iliev <grigor@grigoriliev.com>
5 iliev 1286 *
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.Frame;
28     import java.awt.GridBagConstraints;
29     import java.awt.GridBagLayout;
30     import java.awt.Insets;
31    
32 iliev 1329 import java.awt.event.ActionEvent;
33     import java.awt.event.ActionListener;
34    
35 iliev 1286 import javax.swing.JComboBox;
36     import javax.swing.JLabel;
37     import javax.swing.JPanel;
38     import javax.swing.JSlider;
39     import javax.swing.JSpinner;
40     import javax.swing.JTextField;
41     import javax.swing.SpinnerNumberModel;
42    
43     import javax.swing.event.DocumentEvent;
44     import javax.swing.event.DocumentListener;
45    
46     import net.sf.juife.OkCancelDialog;
47    
48 iliev 1540 import net.sf.juife.event.TaskEvent;
49     import net.sf.juife.event.TaskListener;
50    
51 iliev 1329 import org.jsampler.CC;
52     import org.jsampler.JSPrefs;
53 iliev 1540 import org.jsampler.MidiInstrumentMap;
54 iliev 1329
55 iliev 1540 import org.jsampler.task.Midi;
56    
57     import org.linuxsampler.lscp.Instrument;
58     import org.linuxsampler.lscp.MidiInstrumentEntry;
59 iliev 1286 import org.linuxsampler.lscp.MidiInstrumentInfo;
60    
61     import static org.jsampler.view.std.StdI18n.i18n;
62    
63    
64     /**
65     *
66     * @author Grigor Iliev
67     */
68     public class JSAddMidiInstrumentDlg extends OkCancelDialog {
69     private final JLabel lName = new JLabel(i18n.getLabel("JSAddMidiInstrumentDlg.lName"));
70     private final JLabel lBank = new JLabel(i18n.getLabel("JSAddMidiInstrumentDlg.lBank"));
71     private final JLabel lProgram = new JLabel(i18n.getLabel("JSAddMidiInstrumentDlg.lProgram"));
72     private final JLabel lVolume = new JLabel(i18n.getLabel("JSAddMidiInstrumentDlg.lVolume"));
73     private final JLabel lLoadMode =
74     new JLabel(i18n.getLabel("JSAddMidiInstrumentDlg.lLoadMode"));
75    
76     private final JTextField tfName = new JTextField();
77     private final JSpinner spinnerBank = new JSpinner(new SpinnerNumberModel(0, 0, 16383, 1));
78     private final JComboBox cbProgram = new JComboBox();
79     private final JSlider slVolume = new JSlider(0, 100, 100);
80     private final JComboBox cbLoadMode = new JComboBox();
81    
82 iliev 1540 private MidiInstrumentMap map;
83     private Instrument instr;
84    
85 iliev 1286 /**
86     * Creates a new instance of <code>JSAddMidiInstrumentDlg</code>
87     */
88     public
89 iliev 1540 JSAddMidiInstrumentDlg(Frame owner, MidiInstrumentMap map, Instrument instr) {
90 iliev 1286 super(owner, i18n.getLabel("JSAddMidiInstrumentDlg.title"));
91 iliev 1540 initAddMidiInstrumentDlg(map, instr);
92 iliev 1286 }
93    
94     /**
95     * Creates a new instance of <code>JSAddMidiInstrumentDlg</code>
96     */
97     public
98 iliev 1540 JSAddMidiInstrumentDlg(Dialog owner, MidiInstrumentMap map, Instrument instr) {
99 iliev 1286 super(owner, i18n.getLabel("JSAddMidiInstrumentDlg.title"));
100 iliev 1540 initAddMidiInstrumentDlg(map, instr);
101 iliev 1286 }
102    
103     private void
104 iliev 1753 initAddMidiInstrumentDlg(final MidiInstrumentMap map, Instrument instr) {
105 iliev 1540 this.map = map;
106     this.instr = instr;
107    
108 iliev 1286 JPanel mainPane = new JPanel();
109     GridBagLayout gridbag = new GridBagLayout();
110     GridBagConstraints c = new GridBagConstraints();
111    
112     mainPane.setLayout(gridbag);
113    
114     c.fill = GridBagConstraints.NONE;
115    
116     c.gridx = 0;
117     c.gridy = 0;
118     c.anchor = GridBagConstraints.EAST;
119     c.insets = new Insets(3, 3, 3, 3);
120     gridbag.setConstraints(lName, c);
121     mainPane.add(lName);
122    
123     c.gridx = 0;
124     c.gridy = 2;
125     gridbag.setConstraints(lBank, c);
126     mainPane.add(lBank);
127    
128     c.gridx = 0;
129     c.gridy = 3;
130     gridbag.setConstraints(lProgram, c);
131     mainPane.add(lProgram);
132    
133     c.gridx = 0;
134     c.gridy = 4;
135     gridbag.setConstraints(lLoadMode, c);
136     mainPane.add(lLoadMode);
137    
138     c.gridx = 0;
139     c.gridy = 1;
140     c.insets = new Insets(3, 3, 24, 3);
141     gridbag.setConstraints(lVolume, c);
142     mainPane.add(lVolume);
143    
144     c.fill = GridBagConstraints.HORIZONTAL;
145     c.gridx = 1;
146     c.gridy = 0;
147     c.weightx = 1.0;
148     c.insets = new Insets(3, 3, 3, 3);
149     c.anchor = GridBagConstraints.WEST;
150     gridbag.setConstraints(tfName, c);
151     mainPane.add(tfName);
152    
153     c.gridx = 1;
154     c.gridy = 2;
155     gridbag.setConstraints(spinnerBank, c);
156     mainPane.add(spinnerBank);
157    
158     c.gridx = 1;
159     c.gridy = 3;
160     gridbag.setConstraints(cbProgram, c);
161     mainPane.add(cbProgram);
162    
163     c.gridx = 1;
164     c.gridy = 4;
165     gridbag.setConstraints(cbLoadMode, c);
166     mainPane.add(cbLoadMode);
167    
168     c.gridx = 1;
169     c.gridy = 1;
170     c.insets = new Insets(3, 3, 24, 3);
171     gridbag.setConstraints(slVolume, c);
172     mainPane.add(slVolume);
173    
174     setMainPane(mainPane);
175    
176     setResizable(true);
177     setMinimumSize(getPreferredSize());
178    
179     for(int i = 0; i < 128; i++) cbProgram.addItem(new Integer(i));
180    
181     cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.DEFAULT);
182     cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.ON_DEMAND);
183     cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.ON_DEMAND_HOLD);
184     cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.PERSISTENT);
185    
186 iliev 1329 int i = preferences().getIntProperty("std.midiInstrument.loadMode", 0);
187     if(cbLoadMode.getItemCount() > i) cbLoadMode.setSelectedIndex(i);
188    
189     cbLoadMode.addActionListener(new ActionListener() {
190     public void
191     actionPerformed(ActionEvent e) {
192     int j = cbLoadMode.getSelectedIndex();
193     if(j < 0) return;
194     preferences().setIntProperty("std.midiInstrument.loadMode", j);
195     }
196     });
197    
198 iliev 1286 tfName.getDocument().addDocumentListener(getHandler());
199 iliev 1540
200     setInstrumentName(instr.getName());
201 iliev 1753
202     CC.scheduleInTaskQueue(new Runnable() {
203     public void
204     run() { setMidiInstrumentEntry(map.getAvailableEntry()); }
205     });
206    
207 iliev 1329 }
208    
209     protected JSPrefs
210     preferences() { return CC.getViewConfig().preferences(); }
211    
212     /**
213 iliev 1540 * Sets the MIDI bank and MIDI program of the dialog.
214     */
215     public void
216     setMidiInstrumentEntry(MidiInstrumentEntry entry) {
217     if(entry == null) return;
218     setMidiBank(entry.getMidiBank());
219     setMidiProgram(entry.getMidiProgram());
220     }
221    
222     /**
223 iliev 1286 * Gets the selected MIDI bank.
224     */
225     public int
226     getMidiBank() { return Integer.parseInt(spinnerBank.getValue().toString()); }
227    
228     /**
229 iliev 1329 * Sets the selected MIDI bank.
230     */
231     public void
232     setMidiBank(int bank) { spinnerBank.setValue(bank); }
233    
234     /**
235 iliev 1286 * Gets the selected MIDI program.
236     */
237     public int
238     getMidiProgram() { return cbProgram.getSelectedIndex(); }
239    
240     /**
241 iliev 1329 * Sets the selected MIDI program.
242     */
243     public void
244     setMidiProgram(int program) { cbProgram.setSelectedIndex(program); }
245    
246     /**
247 iliev 1286 * Gets the chosen name for the new MIDI instrument.
248     * @return The chosen name for the new MIDI instrument.
249     */
250     public String
251     getInstrumentName() { return tfName.getText(); }
252    
253     /**
254     * Sets the name for the new MIDI instrument.
255     * @param name The name for the new MIDI instrument.
256     */
257     public void
258     setInstrumentName(String name) { tfName.setText(name); }
259    
260     /**
261     * Returns the volume level of the new MIDI instrument.
262     * @return The volume level of the new MIDI instrument.
263     */
264     public float
265     getVolume() {
266     float f = slVolume.getValue();
267     f /= 100;
268     return f;
269     }
270    
271     /** Gets the selected load mode. */
272     public MidiInstrumentInfo.LoadMode
273     getLoadMode() { return (MidiInstrumentInfo.LoadMode) cbLoadMode.getSelectedItem(); }
274    
275     protected void
276     onOk() {
277     if(!btnOk.isEnabled()) return;
278 iliev 1540
279     btnOk.setEnabled(false);
280 iliev 1329 preferences().setIntProperty("lastUsedMidiBank", getMidiBank());
281 iliev 1540
282     MidiInstrumentInfo instrInfo = new MidiInstrumentInfo();
283     instrInfo.setName(getInstrumentName());
284     instrInfo.setFilePath(instr.getFilePath());
285     instrInfo.setInstrumentIndex(instr.getInstrumentIndex());
286     instrInfo.setEngine(instr.getEngine());
287     instrInfo.setVolume(getVolume());
288     instrInfo.setLoadMode(getLoadMode());
289    
290     int id = map.getMapId();
291     int b = getMidiBank();
292     int p = getMidiProgram();
293     final Midi.MapInstrument t = new Midi.MapInstrument(id, b, p, instrInfo);
294    
295     t.addTaskListener(new TaskListener() {
296     public void
297     taskPerformed(TaskEvent e) {
298     btnOk.setEnabled(true);
299     if(t.doneWithErrors()) return;
300     setCancelled(false);
301     setVisible(false);
302     }
303     });
304    
305     CC.getTaskQueue().add(t);
306 iliev 1286 }
307    
308     protected void
309     onCancel() { setVisible(false); }
310    
311     private void
312     updateState() {
313     boolean b = tfName.getText().length() > 0;
314     b = b && cbProgram.getSelectedItem() != null;
315     btnOk.setEnabled(b);
316     }
317    
318     private final Handler eventHandler = new Handler();
319    
320     private Handler
321     getHandler() { return eventHandler; }
322    
323     private class Handler implements DocumentListener {
324     // DocumentListener
325     public void
326     insertUpdate(DocumentEvent e) { updateState(); }
327    
328     public void
329     removeUpdate(DocumentEvent e) { updateState(); }
330    
331     public void
332     changedUpdate(DocumentEvent e) { updateState(); }
333     }
334     }

  ViewVC Help
Powered by ViewVC