/[svn]/jsampler/trunk/src/org/jsampler/view/fantasia/MidiDevicePane.java
ViewVC logotype

Contents of /jsampler/trunk/src/org/jsampler/view/fantasia/MidiDevicePane.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1688 - (show annotations) (download)
Thu Feb 14 16:52:36 2008 UTC (16 years, 2 months ago) by iliev
File size: 9405 byte(s)
* Implemented a backend list with option to manually choose a backend
  to connect on startup(Edit/Preferences, then click the `Backend' tab)
  and option to change the backend without restarting JSampler
  (Actions/Change Backend or Ctrl + B)

* Added confirmation messages for removing sampler channels and
  audio/MIDI devices (Edit/Preferences, then click the `View' tab)

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.fantasia;
24
25 import java.awt.Dimension;
26 import java.awt.Insets;
27
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.ItemEvent;
31 import java.awt.event.ItemListener;
32
33 import java.beans.PropertyChangeEvent;
34 import java.beans.PropertyChangeListener;
35
36 import javax.swing.BorderFactory;
37 import javax.swing.Box;
38 import javax.swing.BoxLayout;
39 import javax.swing.JCheckBox;
40 import javax.swing.JComboBox;
41 import javax.swing.JLabel;
42 import javax.swing.JPanel;
43 import javax.swing.JScrollPane;
44 import javax.swing.JSpinner;
45 import javax.swing.SpinnerNumberModel;
46
47 import javax.swing.event.ChangeEvent;
48 import javax.swing.event.ChangeListener;
49
50 import net.sf.juife.Task;
51 import net.sf.juife.event.TaskEvent;
52 import net.sf.juife.event.TaskListener;
53
54 import org.jsampler.CC;
55 import org.jsampler.MidiDeviceModel;
56
57 import org.jsampler.event.MidiDeviceEvent;
58 import org.jsampler.event.MidiDeviceListener;
59 import org.jsampler.event.ParameterEvent;
60 import org.jsampler.event.ParameterListener;
61
62 import org.jsampler.task.Midi;
63
64 import org.jsampler.view.ParameterTable;
65
66 import org.linuxsampler.lscp.MidiInputDevice;
67 import org.linuxsampler.lscp.MidiPort;
68 import org.linuxsampler.lscp.Parameter;
69 import org.linuxsampler.lscp.ParameterFactory;
70
71 import static org.jsampler.view.fantasia.FantasiaI18n.i18n;
72 import static org.jsampler.view.fantasia.FantasiaPrefs.preferences;
73
74
75 /**
76 *
77 * @author Grigor Iliev
78 */
79 public class MidiDevicePane extends DevicePane {
80 private final OptionsPane optionsPane;
81 private final ParameterTable portParamTable = new ParameterTable();
82
83 private MidiDeviceModel midiDeviceModel;
84
85 /** Creates a new instance of <code>MidiDevicePane</code> */
86 public
87 MidiDevicePane(MidiDeviceModel model) {
88 midiDeviceModel = model;
89 optionsPane = new OptionsPane();
90 setOptionsPane(optionsPane);
91
92 int id = model.getDeviceId();
93 String s = model.getDeviceInfo().getDriverName();
94 setDeviceName(i18n.getLabel("MidiDevicePane.lDevName", id, s));
95 }
96
97 protected void
98 destroyDevice() {
99 final Task t = new Midi.DestroyDevice(midiDeviceModel.getDeviceId());
100 t.addTaskListener(new TaskListener() {
101 public void
102 taskPerformed(TaskEvent e) {
103 if(t.doneWithErrors()) restoreDevice();
104 }
105 });
106
107 CC.getTaskQueue().add(t);
108 }
109
110 public int
111 getDeviceId() { return midiDeviceModel.getDeviceId(); }
112
113 class OptionsPane extends PixmapPane implements ActionListener, ItemListener,
114 ChangeListener, MidiDeviceListener, ParameterListener {
115
116 private final JCheckBox checkActive =
117 new JCheckBox(i18n.getLabel("MidiDevicePane.checkActive"));
118
119 private final JLabel lPorts = new JLabel(i18n.getLabel("MidiDevicePane.lPorts"));
120 private final JSpinner spinnerPorts;
121 private final JLabel lPort = new JLabel(i18n.getLabel("MidiDevicePane.lPort"));
122 private final JComboBox cbPort = new JComboBox();
123
124 private final ParameterTable additionalParamsTable = new ParameterTable();
125 private final JPanel additionalParamsPane = new JPanel();
126
127 OptionsPane() {
128 super(Res.gfxChannelOptions);
129
130 setAlignmentX(LEFT_ALIGNMENT);
131
132 setPixmapInsets(new Insets(1, 1, 1, 1));
133 setLayout(new java.awt.BorderLayout());
134 setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
135 setOpaque(false);
136
137 PixmapPane mainPane = new PixmapPane(Res.gfxRoundBg7);
138 mainPane.setPixmapInsets(new Insets(3, 3, 3, 3));
139 mainPane.setOpaque(false);
140
141 mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
142 mainPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
143 mainPane.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
144
145 JPanel p = new JPanel();
146 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
147
148 p.add(checkActive);
149 p.add(Box.createRigidArea(new Dimension(12, 0)));
150 p.add(lPorts);
151 p.add(Box.createRigidArea(new Dimension(5, 0)));
152
153 Parameter<Integer> prm = midiDeviceModel.getDeviceInfo().getPortsParameter();
154 int min = 1;
155 if(prm.getRangeMin() != null) min = prm.getRangeMin().intValue();
156 int max = 50;
157 if(prm.getRangeMax() != null) max = prm.getRangeMax().intValue();
158 spinnerPorts = new JSpinner(new SpinnerNumberModel(1, min, max, 1));
159 if(prm.isFixed()) spinnerPorts.setEnabled(false);
160
161 p.add(spinnerPorts);
162 p.setOpaque(false);
163
164 mainPane.add(p);
165 mainPane.add(Box.createRigidArea(new Dimension(0, 5)));
166
167 JPanel p2 = additionalParamsPane;
168 p2.setLayout(new BoxLayout(p2, BoxLayout.Y_AXIS));
169 p2.setOpaque(false);
170
171 p2.add(createHSeparator());
172 p2.add(Box.createRigidArea(new Dimension(0, 5)));
173
174 JScrollPane sp = new JScrollPane(additionalParamsTable);
175
176 sp.setPreferredSize(new Dimension(77, 90));
177 p2.add(sp);
178 mainPane.add(p2);
179
180 mainPane.add(Box.createRigidArea(new Dimension(0, 5)));
181
182 mainPane.add(createHSeparator());
183 mainPane.add(Box.createRigidArea(new Dimension(0, 5)));
184
185 p = new JPanel();
186 p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
187 p.setOpaque(false);
188
189 p2 = new JPanel();
190 p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS));
191 p2.add(lPort);
192 p2.add(Box.createRigidArea(new Dimension(5, 0)));
193 p2.add(cbPort);
194 p2.setOpaque(false);
195 p.add(p2);
196
197 p.add(Box.createRigidArea(new Dimension(0, 5)));
198
199 sp = new JScrollPane(portParamTable);
200 sp.setPreferredSize(new Dimension(77, 90));
201 p.add(sp);
202
203 mainPane.add(p);
204 add(mainPane);
205
206 checkActive.setSelected(midiDeviceModel.isActive());
207 spinnerPorts.setValue(midiDeviceModel.getDeviceInfo().getMidiPortCount());
208
209 cbPort.addActionListener(this);
210 checkActive.addItemListener(this);
211 spinnerPorts.addChangeListener(this);
212 midiDeviceModel.addMidiDeviceListener(this);
213 portParamTable.getModel().addParameterListener(this);
214
215 for(MidiPort port : midiDeviceModel.getDeviceInfo().getMidiPorts()) {
216 cbPort.addItem(port);
217 }
218
219 Parameter[] pS = midiDeviceModel.getDeviceInfo().getAdditionalParameters();
220 additionalParamsTable.getModel().setParameters(pS);
221 additionalParamsTable.getModel().addParameterListener(new ParameterListener() {
222 public void
223 parameterChanged(ParameterEvent e) {
224 midiDeviceModel.setBackendDeviceParameter(e.getParameter());
225 }
226 });
227
228 updateAdditionalParamsViewState();
229 String s = "MidiDevice.showAdditionalParameters";
230 preferences().addPropertyChangeListener(s, new PropertyChangeListener() {
231 public void
232 propertyChange(PropertyChangeEvent e) {
233 updateAdditionalParamsViewState();
234 }
235 });
236 }
237
238 private void
239 updateAdditionalParamsViewState() {
240 String s = "MidiDevice.showAdditionalParameters";
241 additionalParamsPane.setVisible(preferences().getBoolProperty(s));
242 validate();
243 }
244
245 public void
246 actionPerformed(ActionEvent e) {
247 Object obj = cbPort.getSelectedItem();
248 if(obj == null) {
249 portParamTable.getModel().setParameters(new Parameter[0]);
250 return;
251 }
252
253 MidiPort port = (MidiPort)obj;
254
255 portParamTable.getModel().setParameters(port.getAllParameters());
256 }
257
258 public void
259 itemStateChanged(ItemEvent e) {
260 boolean a = checkActive.isSelected();
261 if(a != midiDeviceModel.isActive()) midiDeviceModel.setBackendActive(a);
262 }
263
264 public void
265 stateChanged(ChangeEvent e) {
266 int p = (Integer)spinnerPorts.getValue();
267 if(p != midiDeviceModel.getDeviceInfo().getMidiPortCount()) {
268 midiDeviceModel.setBackendPortCount(p);
269 }
270 }
271
272 public void
273 settingsChanged(MidiDeviceEvent e) {
274 int p = (Integer)spinnerPorts.getValue();
275 int np = midiDeviceModel.getDeviceInfo().getMidiPortCount();
276 if(p != np) spinnerPorts.setValue(np);
277
278 boolean a = checkActive.isSelected();
279 boolean na = midiDeviceModel.isActive();
280 if(a != na) checkActive.setSelected(na);
281
282 MidiInputDevice d = e.getMidiDeviceModel().getDeviceInfo();
283
284 Parameter[] params = d.getAdditionalParameters();
285 additionalParamsTable.getModel().setParameters(params);
286
287 int idx = cbPort.getSelectedIndex();
288 cbPort.removeAllItems();
289 for(MidiPort port : d.getMidiPorts()) cbPort.addItem(port);
290
291 if(idx >= cbPort.getModel().getSize()) idx = 0;
292
293 if(cbPort.getModel().getSize() > 0) cbPort.setSelectedIndex(idx);
294 }
295
296 /** Invoked when when the value of a particular parameter is changed. */
297 public void
298 parameterChanged(ParameterEvent e) {
299 int port = cbPort.getSelectedIndex();
300 if(port == -1) {
301 CC.getLogger().warning("There is no MIDI port selected!");
302 return;
303 }
304
305 midiDeviceModel.setBackendPortParameter(port, e.getParameter());
306 }
307 }
308 }

  ViewVC Help
Powered by ViewVC