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

Contents of /jsampler/trunk/src/org/jsampler/view/std/JSOrchestraPane.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1737 - (show annotations) (download)
Thu May 8 17:26:19 2008 UTC (15 years, 11 months ago) by iliev
File size: 12636 byte(s)
* Major memory optimizations when too many sampler channels are present

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.BorderLayout;
26 import java.awt.Dialog;
27 import java.awt.Frame;
28 import java.awt.Window;
29
30 import java.awt.event.ActionEvent;
31 import java.awt.event.ActionListener;
32 import java.awt.event.MouseAdapter;
33 import java.awt.event.MouseEvent;
34
35 import javax.swing.AbstractAction;
36 import javax.swing.Action;
37 import javax.swing.JMenu;
38 import javax.swing.JMenuItem;
39 import javax.swing.JPanel;
40 import javax.swing.JPopupMenu;
41 import javax.swing.JScrollPane;
42
43 import javax.swing.event.ListSelectionEvent;
44 import javax.swing.event.ListSelectionListener;
45
46 import net.sf.juife.JuifeUtils;
47
48 import org.jsampler.CC;
49 import org.jsampler.DefaultOrchestraModel;
50 import org.jsampler.OrchestraInstrument;
51 import org.jsampler.MidiInstrumentMap;
52 import org.jsampler.OrchestraModel;
53 import org.jsampler.SamplerChannelModel;
54
55 import org.jsampler.event.ListEvent;
56 import org.jsampler.event.ListListener;
57 import org.jsampler.event.SamplerChannelListEvent;
58 import org.jsampler.event.SamplerChannelListListener;
59
60 import org.jsampler.view.InstrumentTable;
61 import org.jsampler.view.InstrumentTableModel;
62
63 import org.linuxsampler.lscp.MidiInstrumentEntry;
64 import org.linuxsampler.lscp.MidiInstrumentInfo;
65
66 import static org.jsampler.view.std.StdI18n.i18n;
67
68 /**
69 *
70 * @author Grigor Iliev
71 */
72 public class JSOrchestraPane extends JPanel {
73 protected final InstrumentTable instrumentTable;
74
75 protected final Action actionAddInstrument = new AddInstrumentAction();
76 protected final Action actionEditInstrument = new EditInstrumentAction();
77 protected final Action actionDeleteInstrument = new DeleteInstrumentAction();
78 protected final Action actionInstrumentUp = new InstrumentUpAction();
79 protected final Action actionInstrumentDown = new InstrumentDownAction();
80
81 private OrchestraModel orchestra;
82
83 /** Creates a new instance of <code>JSOrchestraPane</code> */
84 public
85 JSOrchestraPane() {
86 this(null);
87 }
88
89 /** Creates a new instance of <code>JSOrchestraPane</code> */
90 public
91 JSOrchestraPane(OrchestraModel orchestra) {
92 instrumentTable = new InstrumentTable();
93 setOrchestra(orchestra);
94
95 setLayout(new BorderLayout());
96 JScrollPane sp = new JScrollPane(instrumentTable);
97 add(sp);
98
99 installListeneres();
100 }
101
102 private void
103 installListeneres() {
104 InstrumentSelectionHandler l = new InstrumentSelectionHandler();
105 instrumentTable.getSelectionModel().addListSelectionListener(l);
106
107 instrumentTable.addMouseListener(new MouseAdapter() {
108 public void
109 mousePressed(MouseEvent e) {
110 if(e.getButton() != e.BUTTON3) return;
111
112 int i = instrumentTable.rowAtPoint(e.getPoint());
113 if(i == -1) return;
114
115 instrumentTable.getSelectionModel().setSelectionInterval(i, i);
116
117 }
118
119 public void
120 mouseClicked(MouseEvent e) {
121 if(e.getClickCount() < 2) return;
122
123 if(instrumentTable.getSelectedInstrument() == null) return;
124 editInstrument(instrumentTable.getSelectedInstrument());
125 }
126 });
127
128 ContextMenu contextMenu = new ContextMenu();
129 instrumentTable.addMouseListener(contextMenu);
130 }
131
132 public void
133 setOrchestra(OrchestraModel orchestra) {
134 this.orchestra = orchestra;
135 if(orchestra == null) {
136 orchestra = new DefaultOrchestraModel();
137 actionAddInstrument.setEnabled(false);
138 } else {
139 actionAddInstrument.setEnabled(true);
140 }
141 instrumentTable.getModel().setOrchestraModel(orchestra);
142 }
143
144 public OrchestraInstrument
145 getSelectedInstrument() { return instrumentTable.getSelectedInstrument(); }
146
147 /**
148 * Invoked when the user initiates the creation of new instrument.
149 * @return The instrument to add
150 * or <code>null</code> if the user cancelled the task.
151 */
152 public OrchestraInstrument
153 createInstrument() {
154 JSAddOrEditInstrumentDlg dlg = new JSAddOrEditInstrumentDlg();
155 dlg.setVisible(true);
156
157 if(dlg.isCancelled()) return null;
158 return dlg.getInstrument();
159 }
160
161 public void
162 editInstrument(OrchestraInstrument instr) {
163 JSAddOrEditInstrumentDlg dlg = new JSAddOrEditInstrumentDlg(instr);
164 dlg.setVisible(true);
165 }
166
167 private class InstrumentSelectionHandler implements ListSelectionListener {
168 public void
169 valueChanged(ListSelectionEvent e) {
170 if(e.getValueIsAdjusting()) return;
171
172 if(instrumentTable.getSelectedInstrument() == null) {
173 actionEditInstrument.setEnabled(false);
174 actionDeleteInstrument.setEnabled(false);
175 actionInstrumentUp.setEnabled(false);
176 actionInstrumentDown.setEnabled(false);
177 return;
178 }
179
180 actionEditInstrument.setEnabled(true);
181 actionDeleteInstrument.setEnabled(true);
182
183 int idx = instrumentTable.getSelectedRow();
184 actionInstrumentUp.setEnabled(idx != 0);
185 actionInstrumentDown.setEnabled(idx != instrumentTable.getRowCount() - 1);
186 }
187 }
188
189 private class AddInstrumentAction extends AbstractAction {
190 AddInstrumentAction() {
191 super("");
192
193 String s = i18n.getLabel("JSOrchestraPane.ttAddInstrument");
194 putValue(SHORT_DESCRIPTION, s);
195 }
196
197 public void
198 actionPerformed(ActionEvent e) {
199 OrchestraInstrument instr = createInstrument();
200 if(instr == null) return;
201 orchestra.addInstrument(instr);
202 }
203 }
204
205 private class EditInstrumentAction extends AbstractAction {
206 EditInstrumentAction() {
207 super("");
208
209 String s = i18n.getLabel("JSOrchestraPane.ttEditInstrument");
210 putValue(SHORT_DESCRIPTION, s);
211
212 setEnabled(false);
213 }
214
215 public void
216 actionPerformed(ActionEvent e) {
217 editInstrument(instrumentTable.getSelectedInstrument());
218 }
219 }
220
221 private class DeleteInstrumentAction extends AbstractAction {
222 DeleteInstrumentAction() {
223 super("");
224
225 String s = i18n.getLabel("JSOrchestraPane.ttDeleteInstrument");
226 putValue(SHORT_DESCRIPTION, s);
227
228 setEnabled(false);
229 }
230
231 public void
232 actionPerformed(ActionEvent e) {
233 OrchestraInstrument instr = instrumentTable.getSelectedInstrument();
234 if(instr == null) return;
235 int i = instrumentTable.getSelectedRow();
236 orchestra.removeInstrument(instr);
237
238 if(instrumentTable.getRowCount() > i) {
239 instrumentTable.getSelectionModel().setSelectionInterval(i, i);
240 }
241 }
242 }
243
244 private class InstrumentUpAction extends AbstractAction {
245 InstrumentUpAction() {
246 super("");
247
248 String s = i18n.getLabel("JSOrchestraPane.ttInstrumentUp");
249 putValue(SHORT_DESCRIPTION, s);
250
251 setEnabled(false);
252 }
253
254 public void
255 actionPerformed(ActionEvent e) {
256 OrchestraInstrument instr = instrumentTable.getSelectedInstrument();
257 instrumentTable.getModel().getOrchestraModel().moveInstrumentUp(instr);
258 instrumentTable.setSelectedInstrument(instr);
259 }
260 }
261
262 private class InstrumentDownAction extends AbstractAction {
263 InstrumentDownAction() {
264 super("");
265
266 String s = i18n.getLabel("JSOrchestraPane.ttInstrumentDown");
267 putValue(SHORT_DESCRIPTION, s);
268
269 setEnabled(false);
270 }
271
272 public void
273 actionPerformed(ActionEvent e) {
274 OrchestraInstrument instr = instrumentTable.getSelectedInstrument();
275 instrumentTable.getModel().getOrchestraModel().moveInstrumentDown(instr);
276 instrumentTable.setSelectedInstrument(instr);
277 }
278 }
279
280
281
282 private class LoadInstrumentAction extends AbstractAction {
283 private final SamplerChannelModel channelModel;
284
285 LoadInstrumentAction(SamplerChannelModel model) {
286 String s = "instrumentsdb.actions.loadInstrument.onChannel";
287 putValue(Action.NAME, i18n.getMenuLabel(s, model.getChannelId()));
288 channelModel = model;
289 }
290
291 public void
292 actionPerformed(ActionEvent e) {
293 OrchestraInstrument instr = instrumentTable.getSelectedInstrument();
294 if(instr == null) return;
295
296 int idx = instr.getInstrumentIndex();
297 channelModel.setBackendEngineType(instr.getEngine());
298 channelModel.loadBackendInstrument(instr.getFilePath(), idx);
299 }
300 }
301
302 class AddToMidiMapAction extends AbstractAction {
303 private final MidiInstrumentMap midiMap;
304
305 AddToMidiMapAction(MidiInstrumentMap map) {
306 super(map.getName());
307 midiMap = map;
308 }
309
310 public void
311 actionPerformed(ActionEvent e) {
312 OrchestraInstrument instr = instrumentTable.getSelectedInstrument();
313 if(instr == null) return;
314
315 JSAddMidiInstrumentDlg dlg;
316 Window w = JuifeUtils.getWindow(JSOrchestraPane.this);
317 if(w instanceof Dialog) {
318 dlg = new JSAddMidiInstrumentDlg((Dialog)w, midiMap, instr);
319 } else if(w instanceof Frame) {
320 dlg = new JSAddMidiInstrumentDlg((Frame)w, midiMap, instr);
321 } else {
322 dlg = new JSAddMidiInstrumentDlg((Frame)null, midiMap, instr);
323 }
324
325 dlg.setVisible(true);
326 }
327 }
328
329
330 class ContextMenu extends MouseAdapter
331 implements SamplerChannelListListener, ListSelectionListener {
332
333 private final JPopupMenu cmenu = new JPopupMenu();
334 JMenuItem miEdit = new JMenuItem(i18n.getMenuLabel("ContextMenu.edit"));
335
336 JMenu mLoadInstrument =
337 new JMenu(i18n.getMenuLabel("JSOrchestraPane.loadInstrument"));
338
339 JMenu mMapInstrument =
340 new JMenu(i18n.getMenuLabel("JSOrchestraPane.mapInstrument"));
341
342 ContextMenu() {
343 cmenu.add(miEdit);
344 miEdit.addActionListener(new ActionListener() {
345 public void
346 actionPerformed(ActionEvent e) {
347 actionEditInstrument.actionPerformed(null);
348 }
349 });
350
351 JMenuItem mi = new JMenuItem(i18n.getMenuLabel("ContextMenu.delete"));
352 cmenu.add(mi);
353 mi.addActionListener(new ActionListener() {
354 public void
355 actionPerformed(ActionEvent e) {
356 actionDeleteInstrument.actionPerformed(null);
357 }
358 });
359
360 cmenu.addSeparator();
361
362 cmenu.add(mLoadInstrument);
363 cmenu.add(mMapInstrument);
364
365 CC.getSamplerModel().addSamplerChannelListListener(this);
366 instrumentTable.getSelectionModel().addListSelectionListener(this);
367
368 ListListener<MidiInstrumentMap> l = new ListListener<MidiInstrumentMap>() {
369 public void
370 entryAdded(ListEvent<MidiInstrumentMap> e) {
371 updateAddToMidiMapMenu(mMapInstrument);
372 }
373
374 public void
375 entryRemoved(ListEvent<MidiInstrumentMap> e) {
376 updateAddToMidiMapMenu(mMapInstrument);
377 }
378 };
379
380 CC.getSamplerModel().addMidiInstrumentMapListListener(l);
381 }
382
383 private void
384 updateLoadInstrumentMenu(JMenu menu) {
385 menu.removeAll();
386 for(SamplerChannelModel m : CC.getSamplerModel().getChannels()) {
387 menu.add(new JMenuItem(new LoadInstrumentAction(m)));
388 }
389
390 updateLoadInstrumentMenuState(menu);
391 }
392
393 private void
394 updateLoadInstrumentMenuState(JMenu menu) {
395 OrchestraInstrument instr = instrumentTable.getSelectedInstrument();
396 boolean b = instr == null;
397 b = b || CC.getSamplerModel().getChannelCount() == 0;
398 menu.setEnabled(!b);
399 }
400
401 private void
402 updateAddToMidiMapMenu(JMenu menu) {
403 menu.removeAll();
404 for(int i = 0; i < CC.getSamplerModel().getMidiInstrumentMapCount(); i++) {
405 MidiInstrumentMap m = CC.getSamplerModel().getMidiInstrumentMap(i);
406 menu.add(new JMenuItem(new AddToMidiMapAction(m)));
407 }
408
409 updateAddToMidiMapMenuState(menu);
410 }
411
412 private void
413 updateAddToMidiMapMenuState(JMenu menu) {
414 OrchestraInstrument instr = instrumentTable.getSelectedInstrument();
415 boolean b = instr == null;
416 b = b || CC.getSamplerModel().getMidiInstrumentMapCount() == 0;
417 menu.setEnabled(!b);
418 }
419
420 public void
421 valueChanged(ListSelectionEvent e) {
422 updateLoadInstrumentMenuState(mLoadInstrument);
423 updateAddToMidiMapMenuState(mMapInstrument);
424 }
425
426 public void
427 channelAdded(SamplerChannelListEvent e) {
428 if(CC.getSamplerModel().getChannelListIsAdjusting()) return;
429 updateLoadInstrumentMenu(mLoadInstrument);
430 }
431
432 public void
433 channelRemoved(SamplerChannelListEvent e) {
434 updateLoadInstrumentMenu(mLoadInstrument);
435 }
436
437 public void
438 mousePressed(MouseEvent e) {
439 if(e.isPopupTrigger()) show(e);
440 }
441
442 public void
443 mouseReleased(MouseEvent e) {
444 if(e.isPopupTrigger()) show(e);
445 }
446
447 void
448 show(MouseEvent e) {
449 cmenu.show(e.getComponent(), e.getX(), e.getY());
450 }
451 }
452 }

  ViewVC Help
Powered by ViewVC