/[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 1785 - (show annotations) (download)
Tue Oct 7 00:07:14 2008 UTC (15 years, 6 months ago) by iliev
File size: 13355 byte(s)
* Fantasia: Implemented multiple channels panels
* Fantasia: Refactoring - all basic UI components moved to
  org.jsampler.view.fantasia.basic package

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

  ViewVC Help
Powered by ViewVC