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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1540 - (show annotations) (download)
Mon Dec 3 23:22:02 2007 UTC (16 years, 4 months ago) by iliev
File size: 33849 byte(s)
* Fantasia: by default the volume values are now shown in decibels
* Implemented support for retrieving instrument information
  from instrument files
* Some bugfixes and enhancements

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.Component;
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 java.util.Vector;
37
38 import javax.swing.AbstractAction;
39 import javax.swing.Action;
40 import javax.swing.BorderFactory;
41 import javax.swing.Icon;
42 import javax.swing.JComponent;
43 import javax.swing.JLabel;
44 import javax.swing.JMenu;
45 import javax.swing.JMenuItem;
46 import javax.swing.JPanel;
47 import javax.swing.JPopupMenu;
48 import javax.swing.JTable;
49 import javax.swing.KeyStroke;
50
51 import javax.swing.event.ChangeEvent;
52 import javax.swing.event.ChangeListener;
53 import javax.swing.event.ListSelectionEvent;
54 import javax.swing.event.ListSelectionListener;
55 import javax.swing.event.TreeSelectionEvent;
56 import javax.swing.event.TreeSelectionListener;
57
58 import javax.swing.table.TableCellRenderer;
59
60 import net.sf.juife.InformationDialog;
61 import net.sf.juife.JuifeUtils;
62 import net.sf.juife.Task;
63
64 import net.sf.juife.event.TaskEvent;
65 import net.sf.juife.event.TaskListener;
66
67 import org.jsampler.CC;
68 import org.jsampler.HF;
69 import org.jsampler.OrchestraInstrument;
70 import org.jsampler.MidiInstrumentMap;
71 import org.jsampler.OrchestraModel;
72 import org.jsampler.SamplerChannelModel;
73 import org.jsampler.SamplerModel;
74
75 import org.jsampler.event.ListEvent;
76 import org.jsampler.event.ListListener;
77 import org.jsampler.event.SamplerChannelListEvent;
78 import org.jsampler.event.SamplerChannelListListener;
79
80 import org.jsampler.task.InstrumentsDb;
81
82 import org.jsampler.view.DbClipboard;
83 import org.jsampler.view.DbDirectoryTreeNode;
84 import org.jsampler.view.InstrumentsDbTableModel;
85
86 import org.linuxsampler.lscp.DbDirectoryInfo;
87 import org.linuxsampler.lscp.DbInstrumentInfo;
88 import org.linuxsampler.lscp.MidiInstrumentInfo;
89
90 import static org.jsampler.view.InstrumentsDbTableModel.ColumnType;
91 import static org.jsampler.view.std.StdI18n.i18n;
92
93 import static org.linuxsampler.lscp.Parser.*;
94
95 /**
96 *
97 * @author Grigor Iliev
98 */
99 public class JSInstrumentsDbTable extends org.jsampler.view.AbstractInstrumentsDbTable {
100 private JSInstrumentsDbTree instrumentsDbTree;
101 private InstrumentsDbCellRenderer cellRenderer = new InstrumentsDbCellRenderer();
102
103 public final Action reloadAction = new ReloadAction();
104 public final Action createDirectoryAction = new CreateDirectoryAction();
105 public final Action deleteAction = new DeleteAction();
106 public final AddInstrumentsFromFileAction addInstrumentsFromFileAction =
107 new AddInstrumentsFromFileAction();
108 public final AddInstrumentsFromDirAction addInstrumentsFromDirAction =
109 new AddInstrumentsFromDirAction();
110 public final Action propertiesAction = new PropertiesAction();
111 public final Action renameAction = new RenameAction();
112 public final Action changeDescriptionAction = new ChangeDescriptionAction();
113 public final Action cutAction = new CutAction();
114 public final Action copyAction = new CopyAction();
115 public final Action pasteAction;
116
117 private static final DbClipboard dbClipboard = new DbClipboard();
118
119 /**
120 * Creates a new instance of <code>JSInstrumentsDbTable</code>
121 */
122 public JSInstrumentsDbTable(JSInstrumentsDbTree tree) {
123 instrumentsDbTree = tree;
124
125 /*for(int i = 0; i < getColumnModel().getColumnCount(); i++) {
126 getColumnModel().getColumn(i).setMinWidth(50);
127 }*/
128
129 setShowGrid(false);
130 getColumnModel().setColumnMargin(0);
131 getTableHeader().setReorderingAllowed(false);
132
133 setFillsViewportHeight(true);
134
135 addMouseListener(new MouseAdapter() {
136 public void
137 mouseClicked(MouseEvent e) {
138 if(e.getButton() != e.BUTTON1) return;
139 int r = rowAtPoint(e.getPoint());
140 if(r == -1) {
141 clearSelection();
142 return;
143 }
144
145 if(e.getClickCount() < 2) return;
146
147 DbDirectoryTreeNode node = getSelectedDirectoryNode();
148 if(node == null) return;
149 if(!node.isDetached()) {
150 instrumentsDbTree.setSelectedDirectoryNode(node);
151 } else {
152 String s = node.getInfo().getDirectoryPath();
153 instrumentsDbTree.setSelectedDirectory(s);
154 }
155 }
156 });
157
158 addMouseListener(new MouseAdapter() {
159 public void
160 mousePressed(MouseEvent e) {
161 int r = rowAtPoint(e.getPoint());
162
163 if(e.getButton() != e.BUTTON1 && e.getButton() != e.BUTTON3) return;
164 if(r == -1) {
165 clearSelection();
166 return;
167 }
168
169 if(e.getButton() != e.BUTTON3) return;
170 if(getSelectionModel().isSelectedIndex(r)) {
171 getSelectionModel().addSelectionInterval(r, r);
172 } else {
173 getSelectionModel().setSelectionInterval(r, r);
174 }
175 }
176 });
177
178 getSelectionModel().addListSelectionListener(getHandler());
179
180 instrumentsDbTree.addTreeSelectionListener(getHandler());
181
182 PasteAction pasteAction = new PasteAction();
183 instrumentsDbTree.addTreeSelectionListener(pasteAction);
184 this.pasteAction = pasteAction;
185
186 ContextMenu contextMenu = new ContextMenu();
187 addMouseListener(contextMenu);
188
189 CC.getOrchestras().addOrchestraListListener(getHandler());
190 CC.getSamplerModel().addSamplerChannelListListener(getHandler());
191
192 ListListener<MidiInstrumentMap> l = new ListListener<MidiInstrumentMap>() {
193 public void
194 entryAdded(ListEvent<MidiInstrumentMap> e) { updateAddToMidiMapMenus(); }
195
196 public void
197 entryRemoved(ListEvent<MidiInstrumentMap> e) { updateAddToMidiMapMenus(); }
198 };
199
200 CC.getSamplerModel().addMidiInstrumentMapListListener(l);
201
202 installKeyboardListeners();
203 }
204
205 public static DbClipboard
206 getDbClipboard() { return dbClipboard; }
207
208 public TableCellRenderer
209 getCellRenderer(int row, int column) {
210 return cellRenderer;
211 }
212
213 private void
214 installKeyboardListeners() {
215 AbstractAction a = new AbstractAction() {
216 public void
217 actionPerformed(ActionEvent e) { }
218 };
219 a.setEnabled(false);
220 getActionMap().put("none", a);
221
222 getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put (
223 KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK),
224 "none"
225 );
226
227 getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put (
228 KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK),
229 "none"
230 );
231
232 getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put (
233 KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_MASK),
234 "none"
235 );
236
237 getInputMap(JComponent.WHEN_FOCUSED).put (
238 KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK),
239 "none"
240 );
241
242 getInputMap(JComponent.WHEN_FOCUSED).put (
243 KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK),
244 "none"
245 );
246
247 getInputMap(JComponent.WHEN_FOCUSED).put (
248 KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_MASK),
249 "none"
250 );
251
252 getInputMap().put (
253 KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
254 "OpenDirectory"
255 );
256
257 getActionMap().put ("OpenDirectory", new AbstractAction() {
258 public void
259 actionPerformed(ActionEvent e) {
260 DbDirectoryTreeNode node = getSelectedDirectoryNode();
261 if(node == null) return;
262 instrumentsDbTree.setSelectedDirectoryNode(node);
263 }
264 });
265 }
266
267 public String
268 getUniqueDirectoryName() {
269 DbDirectoryTreeNode node = getParentDirectoryNode();
270 if(node == null || node.isDetached()) return null;
271 if(node != instrumentsDbTree.getSelectedDirectoryNode()) return null;
272
273 boolean b = false;
274 int c = 2;
275 String dir = "New Folder";
276
277 while(true) {
278 for(int i = 0; i < node.getChildCount(); i++) {
279
280 if(dir.equals(node.getChildAt(i).getInfo().getName())) {
281 b = true;
282 break;
283 }
284 }
285
286 if(!b) break;
287
288 b = false;
289 dir = "New Folder[" + c++ + "]";
290 }
291
292 return dir;
293 }
294
295 private final Vector<JMenu> loadInstrumentMenus = new Vector<JMenu>();
296 private final Vector<JMenu> addToMidiMapMenus = new Vector<JMenu>();
297 private final Vector<JMenu> addToOrchestraMenus = new Vector<JMenu>();
298
299 public void
300 registerLoadInstrumentMenus(JMenu menu) {
301 loadInstrumentMenus.add(menu);
302 updateLoadInstrumentMenu(menu);
303 }
304
305 public void
306 registerAddToMidiMapMenu(JMenu menu) {
307 addToMidiMapMenus.add(menu);
308 updateAddToMidiMapMenu(menu);
309 }
310
311 public void
312 registerAddToOrchestraMenu(JMenu menu) {
313 addToOrchestraMenus.add(menu);
314 updateAddToOrchestraMenu(menu);
315 }
316
317 private void
318 updateLoadInstrumentMenus() {
319 for(JMenu menu : loadInstrumentMenus) updateLoadInstrumentMenu(menu);
320 }
321
322 private void
323 updateLoadInstrumentMenu(JMenu menu) {
324 menu.removeAll();
325 for(SamplerChannelModel m : CC.getSamplerModel().getChannels()) {
326 menu.add(new JMenuItem(new LoadInstrumentAction(m)));
327 }
328
329 updateLoadInstrumentMenuState(menu);
330 }
331
332 private void
333 updateLoadInstrumentMenuStates() {
334 for(JMenu menu : loadInstrumentMenus) updateLoadInstrumentMenuState(menu);
335 }
336
337 private void
338 updateLoadInstrumentMenuState(JMenu menu) {
339 Object obj = getLeadObject();
340 boolean b = obj == null || !(obj instanceof DbInstrumentInfo);
341 b = b || CC.getSamplerModel().getChannelCount() == 0;
342 menu.setEnabled(!b);
343 }
344
345 private void
346 updateAddToMidiMapMenus() {
347 for(JMenu menu : addToMidiMapMenus) updateAddToMidiMapMenu(menu);
348 }
349
350 private void
351 updateAddToMidiMapMenu(JMenu menu) {
352 menu.removeAll();
353 for(int i = 0; i < CC.getSamplerModel().getMidiInstrumentMapCount(); i++) {
354 MidiInstrumentMap m = CC.getSamplerModel().getMidiInstrumentMap(i);
355 menu.add(new JMenuItem(new AddToMidiMapAction(m)));
356 }
357
358 updateAddToMidiMapMenuState(menu);
359 }
360
361 private void
362 updateAddToMidiMapMenuStates() {
363 for(JMenu menu : addToMidiMapMenus) updateAddToMidiMapMenuState(menu);
364 }
365
366 private void
367 updateAddToMidiMapMenuState(JMenu menu) {
368 Object obj = getLeadObject();
369 boolean b = obj == null || !(obj instanceof DbInstrumentInfo);
370 b = b || CC.getSamplerModel().getMidiInstrumentMapCount() == 0;
371 menu.setEnabled(!b);
372 }
373
374 private void
375 updateAddToOrchestraMenus() {
376 for(JMenu menu : addToOrchestraMenus) updateAddToOrchestraMenu(menu);
377 }
378
379 private void
380 updateAddToOrchestraMenu(JMenu menu) {
381 menu.removeAll();
382 for(int i = 0; i < CC.getOrchestras().getOrchestraCount(); i++) {
383 OrchestraModel m = CC.getOrchestras().getOrchestra(i);
384 Action a = new AddToOrchestraAction(m);
385 menu.add(new JMenuItem(a));
386 }
387
388 updateAddToOrchestraMenuState(menu);
389 }
390
391 private void
392 updateAddToOrchestraMenuStates() {
393 for(JMenu menu : addToOrchestraMenus) updateAddToOrchestraMenuState(menu);
394 }
395
396 private void
397 updateAddToOrchestraMenuState(JMenu menu) {
398 Object obj = getLeadObject();
399 boolean b = obj == null || !(obj instanceof DbInstrumentInfo);
400 b = b || CC.getOrchestras().getOrchestraCount() == 0;
401 menu.setEnabled(!b);
402 }
403
404 private boolean
405 showYesNoDialog(String s) {
406 Window w = JuifeUtils.getWindow(this);
407 if(w instanceof Dialog) return HF.showYesNoDialog((Dialog)w, s);
408 if(w instanceof Frame) return HF.showYesNoDialog((Frame)w, s);
409 return HF.showYesNoDialog((Frame)null, s);
410 }
411
412 private class ReloadAction extends AbstractAction implements TreeSelectionListener {
413 ReloadAction() {
414 super(i18n.getMenuLabel("instrumentsdb.actions.reload"));
415
416 String s = i18n.getMenuLabel("instrumentsdb.actions.reload.tt");
417 putValue(SHORT_DESCRIPTION, s);
418 setEnabled(false);
419 }
420
421 public void
422 actionPerformed(ActionEvent e) {
423 DbDirectoryTreeNode n = instrumentsDbTree.getSelectedDirectoryNode();
424 if(n == null) return;
425 instrumentsDbTree.refreshDirectoryContent(n.getInfo().getDirectoryPath());
426 }
427
428 public void
429 valueChanged(TreeSelectionEvent e) {
430 DbDirectoryTreeNode n = instrumentsDbTree.getSelectedDirectoryNode();
431 setEnabled(n != null);
432 }
433 }
434
435 class CreateDirectoryAction extends AbstractAction {
436 private String directoryName = null;
437
438 CreateDirectoryAction() {
439 super(i18n.getMenuLabel("instrumentsdb.actions.createFolder"));
440
441 String s = i18n.getMenuLabel("instrumentsdb.actions.createFolder.tt");
442 putValue(SHORT_DESCRIPTION, s);
443 }
444
445 public void
446 actionPerformed(ActionEvent e) {
447 setDirectoryName(getUniqueDirectoryName());
448
449 String path = instrumentsDbTree.getSelectedDirectoryPath();
450 if(path.length() > 1) path += "/";
451 path += toEscapedFileName(getDirectoryName());
452
453 final InstrumentsDb.CreateDirectory t =
454 new InstrumentsDb.CreateDirectory(path);
455
456 setCreatedDirectoryName(directoryName);
457
458 t.addTaskListener(new TaskListener() {
459 public void
460 taskPerformed(TaskEvent e) {
461 if(t.doneWithErrors()) {
462 setCreatedDirectoryName(null);
463 return;
464 }
465 }
466 });
467 CC.getTaskQueue().add(t);
468 }
469
470 /**
471 * Gets the name of the directory to be created.
472 * @return The name of the directory to be created.
473 */
474 public String
475 getDirectoryName() { return directoryName; }
476
477 /**
478 * Sets the name of the directory to be created.
479 * @param name The name of the directory to be created.
480 */
481 public void
482 setDirectoryName(String name) { directoryName = name; }
483 }
484
485 class DeleteAction extends AbstractAction {
486 DeleteAction() {
487 super(i18n.getMenuLabel("instrumentsdb.actions.delete"));
488
489 String s;
490 s = i18n.getMenuLabel("instrumentsdb.actions.delete.tt");
491 putValue(SHORT_DESCRIPTION, s);
492 setEnabled(false);
493 }
494
495 public void
496 actionPerformed(ActionEvent e) {
497 final DbDirectoryInfo[] dirs = getSelectedDirectories();
498
499 if(dirs.length > 0) {
500 String s = i18n.getMessage("JSInstrumentsDbTable.confirmDeletion");
501 if(!showYesNoDialog(s)) return;
502
503 final Task t = new InstrumentsDb.RemoveDirectories(dirs);
504 t.addTaskListener(new TaskListener() {
505 public void
506 taskPerformed(TaskEvent e) {
507 if(instrumentsDbTree.getSelectionCount() == 0) {
508 // update search results
509 // TODO: lazily implemented
510 deleteDirectories(dirs);
511 }
512 }
513 });
514 CC.getTaskQueue().add(t);
515
516
517 }
518
519 final DbInstrumentInfo[] instrs = getSelectedInstruments();
520 if(instrs.length > 0) {
521 final Task t = new InstrumentsDb.RemoveInstruments(instrs);
522 t.addTaskListener(new TaskListener() {
523 public void
524 taskPerformed(TaskEvent e) {
525 if(instrumentsDbTree.getSelectionCount() == 0) {
526 // update search results
527 // TODO: lazily implemented
528 deleteInstruments(instrs);
529 }
530 }
531 });
532 CC.getTaskQueue().add(t);
533 }
534 }
535
536 /** Deletes the specified directories from the model */
537 private void
538 deleteDirectories(DbDirectoryInfo[] dirs) {
539 for(DbDirectoryInfo info : dirs) {
540 String path = info.getDirectoryPath();
541 getParentDirectoryNode().removeDirectoryByPathName(path);
542 getModel().fireTableDataChanged();
543 }
544 }
545
546 /** Deletes the specified instruments from the model */
547 private void
548 deleteInstruments(DbInstrumentInfo[] instrs) {
549 for(DbInstrumentInfo info : instrs) {
550 String path = info.getInstrumentPath();
551 getParentDirectoryNode().removeInstrumentByPathName(path);
552 getModel().fireTableDataChanged();
553 }
554 }
555 }
556
557 class AddInstrumentsFromFileAction extends AbstractAction {
558 AddInstrumentsFromFileAction() {
559 super(i18n.getMenuLabel("instrumentsdb.actions.addInstruments.fromFile"));
560
561 String s = "instrumentsdb.actions.addInstruments.fromFile.tt";
562 putValue(SHORT_DESCRIPTION, i18n.getMenuLabel(s));
563 }
564
565 public void
566 actionPerformed(ActionEvent e) {
567 String s;
568 DbDirectoryTreeNode node = getParentDirectoryNode();
569 if(node == null || node.getInfo() == null) s = null;
570 else s = node.getInfo().getDirectoryPath();
571
572 JSAddDbInstrumentsFromFileDlg dlg;
573 Icon ico = instrumentsDbTree.getView().getOpenIcon();
574 Window w = JuifeUtils.getWindow(JSInstrumentsDbTable.this);
575 if(w instanceof Dialog) {
576 dlg = new JSAddDbInstrumentsFromFileDlg((Dialog)w, s, ico);
577 } else if(w instanceof Frame) {
578 dlg = new JSAddDbInstrumentsFromFileDlg((Frame)w, s, ico);
579 } else {
580 dlg = new JSAddDbInstrumentsFromFileDlg((Frame)null, s, ico);
581 }
582
583 dlg.setVisible(true);
584 if(w != null) w.toFront();
585 }
586 }
587
588 class AddInstrumentsFromDirAction extends AbstractAction {
589 AddInstrumentsFromDirAction() {
590 super(i18n.getMenuLabel("instrumentsdb.actions.addInstruments.fromDir"));
591
592 String s = "instrumentsdb.actions.addInstruments.fromDir.tt";
593 putValue(SHORT_DESCRIPTION, i18n.getMenuLabel(s));
594 }
595
596 public void
597 actionPerformed(ActionEvent e) {
598 String s;
599 DbDirectoryTreeNode node = getParentDirectoryNode();
600 if(node == null || node.getInfo() == null) s = null;
601 else s = node.getInfo().getDirectoryPath();
602
603 JSAddDbInstrumentsFromDirDlg dlg;
604 Icon ico = instrumentsDbTree.getView().getOpenIcon();
605 Window w = JuifeUtils.getWindow(JSInstrumentsDbTable.this);
606 if(w instanceof Dialog) {
607 dlg = new JSAddDbInstrumentsFromDirDlg((Dialog)w, s, ico);
608 } else if(w instanceof Frame) {
609 dlg = new JSAddDbInstrumentsFromDirDlg((Frame)w, s, ico);
610 } else {
611 dlg = new JSAddDbInstrumentsFromDirDlg((Frame)null, s, ico);
612 }
613
614 dlg.setVisible(true);
615 if(w != null) w.toFront();
616 }
617 }
618
619 class LoadInstrumentAction extends AbstractAction {
620 private final SamplerChannelModel channelModel;
621
622 LoadInstrumentAction(SamplerChannelModel model) {
623 String s = "instrumentsdb.actions.loadInstrument.onChannel";
624 putValue(Action.NAME, i18n.getMenuLabel(s, model.getChannelId()));
625 channelModel = model;
626 }
627
628 public void
629 actionPerformed(ActionEvent e) {
630 Object obj = getLeadObject();
631 if(obj == null || !(obj instanceof DbInstrumentInfo)) return;
632 DbInstrumentInfo info = (DbInstrumentInfo)obj;
633 int idx = info.getInstrumentIndex();
634 channelModel.setBackendEngineType(info.getFormatFamily()); // TODO: fix this
635 channelModel.loadBackendInstrument(info.getFilePath(), idx);
636 }
637 }
638
639 class AddToMidiMapAction extends AbstractAction {
640 private final MidiInstrumentMap midiMap;
641
642 AddToMidiMapAction(MidiInstrumentMap map) {
643 super(map.getName());
644 midiMap = map;
645 }
646
647 public void
648 actionPerformed(ActionEvent e) {
649 DbInstrumentInfo[] instruments = getSelectedInstruments();
650 int l = instruments.length;
651 if(l == 0) return;
652
653 if(l > 4) {
654 String s = "JSInstrumentsDbTable.confirmAddToMidiMap";
655 s = i18n.getMessage(s, l, midiMap.getName());
656 if(!HF.showYesNoDialog(JSInstrumentsDbTable.this, s)) return;
657 }
658
659 JSAddMidiInstrumentDlg dlg;
660 Window w = JuifeUtils.getWindow(JSInstrumentsDbTable.this);
661
662 for(DbInstrumentInfo i : instruments) {
663 if(w instanceof Dialog) {
664 dlg = new JSAddMidiInstrumentDlg((Dialog)w, midiMap, i);
665 } else if(w instanceof Frame) {
666 dlg = new JSAddMidiInstrumentDlg((Frame)w, midiMap, i);
667 } else {
668 dlg = new JSAddMidiInstrumentDlg((Frame)null, midiMap, i);
669 }
670
671 dlg.setVisible(true);
672 }
673 }
674 }
675
676 class AddToOrchestraAction extends AbstractAction {
677 private final OrchestraModel orchestraModel;
678
679 AddToOrchestraAction(OrchestraModel model) {
680 super(model.getName());
681 orchestraModel = model;
682 }
683
684 public void
685 actionPerformed(ActionEvent e) {
686 DbInstrumentInfo[] instruments = getSelectedInstruments();
687 int l = instruments.length;
688 if(l == 0) return;
689
690 if(l > 1) {
691 String s = "JSInstrumentsDbTable.confirmAddToOrchestra";
692 s = i18n.getMessage(s, l, orchestraModel.getName());
693 if(!HF.showYesNoDialog(JSInstrumentsDbTable.this, s)) return;
694 }
695
696 for(DbInstrumentInfo i : instruments) {
697 OrchestraInstrument instr = new OrchestraInstrument();
698 instr.setFilePath(i.getFilePath());
699 instr.setInstrumentIndex(i.getInstrumentIndex());
700 instr.setName(i.getName());
701 instr.setDescription(i.getDescription());
702 instr.setEngine(i.getFormatFamily()); // TODO: this should be fixed
703 orchestraModel.addInstrument(instr);
704 }
705 }
706 }
707
708 class PropertiesAction extends AbstractAction {
709 PropertiesAction() {
710 super(i18n.getMenuLabel("instrumentsdb.actions.properties"));
711
712 String s;
713 s = i18n.getMenuLabel("instrumentsdb.actions.properties.tt");
714 putValue(SHORT_DESCRIPTION, s);
715 setEnabled(false);
716 }
717
718 public void
719 actionPerformed(ActionEvent e) {
720 Object obj = getLeadObject();
721 if(obj == null) {
722 DbDirectoryTreeNode node = getParentDirectoryNode();
723 if(node == null || node.getInfo() == null) return;
724 showDirectoryProperties(node.getInfo());
725 return;
726 }
727
728 if(obj instanceof DbDirectoryInfo) {
729 showDirectoryProperties((DbDirectoryInfo)obj);
730 } else if(obj instanceof DbInstrumentInfo) {
731 showInstrumentProperties((DbInstrumentInfo)obj);
732 }
733 }
734
735 private void
736 showInstrumentProperties(DbInstrumentInfo instr) {
737 JPanel p = new JSDbInstrumentPropsPane(instr);
738 String s = i18n.getLabel("JSInstrumentsDbTable.instrProps");
739 showDialog(s, p);
740 }
741
742 private void
743 showDirectoryProperties(DbDirectoryInfo dir) {
744 JPanel p = new JSDbDirectoryPropsPane(dir);
745 String s = i18n.getLabel("JSInstrumentsDbTable.dirProps");
746 showDialog(s, p);
747 }
748
749 private void
750 showDialog(String title, JPanel mainPane) {
751 InformationDialog dlg;
752 Window w = JuifeUtils.getWindow(JSInstrumentsDbTable.this);
753 if(w instanceof Dialog) {
754 dlg = new InformationDialog((Dialog)w, title, mainPane);
755 } else if(w instanceof Frame) {
756 dlg = new InformationDialog((Frame)w, title, mainPane);
757 } else {
758 dlg = new InformationDialog((Frame)null, title, mainPane);
759 }
760
761 dlg.setMinimumSize(dlg.getPreferredSize());
762 dlg.setVisible(true);
763 }
764 }
765
766 class RenameAction extends AbstractAction {
767 private String directoryPath = null;
768
769 RenameAction() {
770 super(i18n.getMenuLabel("instrumentsdb.edit.rename"));
771
772 String s = i18n.getMenuLabel("instrumentsdb.edit.rename.tt");
773 putValue(SHORT_DESCRIPTION, s);
774 setEnabled(false);
775 }
776
777 public void
778 actionPerformed(ActionEvent e) {
779 int i = getSelectionModel().getLeadSelectionIndex();
780 if(i == -1) return;
781 editCellAt(i, 0);
782 }
783 }
784
785 class ChangeDescriptionAction extends AbstractAction {
786 private String directoryPath = null;
787
788 ChangeDescriptionAction() {
789 super(i18n.getMenuLabel("instrumentsdb.edit.description"));
790
791 String s = i18n.getMenuLabel("instrumentsdb.edit.description.tt");
792 putValue(SHORT_DESCRIPTION, s);
793 setEnabled(false);
794 }
795
796 public void
797 actionPerformed(ActionEvent e) {
798 Object obj = getLeadObject();
799 if(obj == null) return;
800
801 if(obj instanceof DbDirectoryInfo) {
802 DbDirectoryInfo info = (DbDirectoryInfo)obj;
803 String s = editDescription(info.getDescription());
804 if(s == null) return;
805 String path = info.getDirectoryPath();
806 Task t = new InstrumentsDb.SetDirectoryDescription(path, s);
807 CC.getTaskQueue().add(t);
808 } else if(obj instanceof DbInstrumentInfo) {
809 DbInstrumentInfo info = (DbInstrumentInfo)obj;
810 String s = editDescription(info.getDescription());
811 if(s == null) return;
812 String path = info.getInstrumentPath();
813 Task t = new InstrumentsDb.SetInstrumentDescription(path, s);
814 CC.getTaskQueue().add(t);
815 }
816 }
817
818 private String
819 editDescription(String s) {
820 JSDbDescriptionDlg dlg;
821 Window w = JuifeUtils.getWindow(JSInstrumentsDbTable.this);
822 if(w instanceof Dialog) {
823 dlg = new JSDbDescriptionDlg((Dialog)w);
824 } else if(w instanceof Frame) {
825 dlg = new JSDbDescriptionDlg((Frame)w);
826 } else {
827 dlg = new JSDbDescriptionDlg((Frame)null);
828 }
829
830 dlg.setDescription(s);
831 dlg.setVisible(true);
832 if(dlg.isCancelled()) return null;
833 return dlg.getDescription();
834 }
835 }
836
837 class CutAction extends AbstractAction {
838 CutAction() {
839 super(i18n.getMenuLabel("instrumentsdb.edit.cut"));
840
841 String s = i18n.getMenuLabel("instrumentsdb.edit.cut.tt");
842 putValue(SHORT_DESCRIPTION, s);
843 setEnabled(false);
844 }
845
846 public void
847 actionPerformed(ActionEvent e) {
848 getDbClipboard().setDirectories(getSelectedDirectories());
849 getDbClipboard().setInstruments(getSelectedInstruments());
850 getDbClipboard().setOperation(DbClipboard.Operation.CUT);
851 }
852 }
853
854 class CopyAction extends AbstractAction {
855 CopyAction() {
856 super(i18n.getMenuLabel("instrumentsdb.edit.copy"));
857
858 String s = i18n.getMenuLabel("instrumentsdb.edit.copy.tt");
859 putValue(SHORT_DESCRIPTION, s);
860 setEnabled(false);
861 }
862
863 public void
864 actionPerformed(ActionEvent e) {
865 getDbClipboard().setDirectories(getSelectedDirectories());
866 getDbClipboard().setInstruments(getSelectedInstruments());
867 getDbClipboard().setOperation(DbClipboard.Operation.COPY);
868 }
869 }
870
871 class PasteAction extends AbstractAction implements TreeSelectionListener, ChangeListener {
872 PasteAction() {
873 super(i18n.getMenuLabel("instrumentsdb.edit.paste"));
874
875 String s = i18n.getMenuLabel("instrumentsdb.edit.paste.tt");
876 putValue(SHORT_DESCRIPTION, s);
877 setEnabled(false);
878 getDbClipboard().addChangeListener(this);
879 }
880
881 public void
882 actionPerformed(ActionEvent e) {
883 DbDirectoryInfo[] dirs = getDbClipboard().getDirectories();
884 DbInstrumentInfo[] instrs = getDbClipboard().getInstruments();
885 String dest = instrumentsDbTree.getSelectedDirectoryPath();
886
887 Task t;
888 if(getDbClipboard().getOperation() == DbClipboard.Operation.CUT) {
889 t = new InstrumentsDb.Move(dirs, instrs, dest);
890 getDbClipboard().setDirectories(new DbDirectoryInfo[0]);
891 getDbClipboard().setInstruments(new DbInstrumentInfo[0]);
892 } else if(getDbClipboard().getOperation() == DbClipboard.Operation.COPY) {
893 t = new InstrumentsDb.Copy(dirs, instrs, dest);
894 } else {
895 return;
896 }
897
898 CC.getTaskQueue().add(t);
899 }
900
901 public void
902 valueChanged(TreeSelectionEvent e) { updateState(); }
903
904 public void
905 stateChanged(ChangeEvent e) { updateState(); }
906
907 private void
908 updateState() {
909 DbDirectoryTreeNode n = instrumentsDbTree.getSelectedDirectoryNode();
910 if(n == null) {
911 setEnabled(false);
912 return;
913 }
914
915 int dirs = getDbClipboard().getDirectories().length;
916 setEnabled(dirs > 0 || getDbClipboard().getInstruments().length > 0);
917 }
918 }
919
920 class InstrumentsDbCellRenderer extends JLabel implements TableCellRenderer {
921
922 InstrumentsDbCellRenderer() {
923 setOpaque(true);
924 setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
925 }
926
927 public Component
928 getTableCellRendererComponent (
929 JTable table,
930 Object value,
931 boolean isSelected,
932 boolean hasFocus,
933 int row,
934 int column
935 ) {
936 if(column == 0 && value != null) {
937 String s;
938 if(value instanceof DbDirectoryInfo) {
939 setIcon(getView().getFolderIcon());
940 s = ((DbDirectoryInfo)value).getDescription();
941 setToolTipText(s.length() == 0 ? null : s);
942 } else if(value instanceof String) {
943 setIcon(getView().getFolderIcon());
944 setToolTipText(null);
945 } else if(value instanceof DbInstrumentInfo) {
946 DbInstrumentInfo info = (DbInstrumentInfo)value;
947 if("GIG".equals(info.getFormatFamily())) { // TODO: fix it!
948 setIcon(getView().getGigInstrumentIcon());
949 } else {
950 setIcon(getView().getInstrumentIcon());
951 }
952
953 s = info.getDescription();
954 setToolTipText(s.length() == 0 ? null : s);
955 } else {
956 setIcon(null);
957 setToolTipText(null);
958 }
959 } else {
960 setIcon(null);
961 setToolTipText(null);
962 }
963
964 if(value != null) setText(value.toString());
965 else setText("");
966
967 if (isSelected) {
968 setBackground(table.getSelectionBackground());
969 setForeground(table.getSelectionForeground());
970 } else {
971 setBackground(table.getBackground());
972 setForeground(table.getForeground());
973 }
974
975 ColumnType ct =
976 ((InstrumentsDbTableModel)table.getModel()).getColumnType(column);
977
978 if(ct == ColumnType.IS_DRUM || ct == ColumnType.FORMAT_FAMILY) {
979 setHorizontalAlignment(CENTER);
980 } else if ( ct == ColumnType.SIZE ||
981 ct == ColumnType.INSTRUMENT_NR ||
982 ct == ColumnType.FORMAT_VERSION
983 ) {
984 setHorizontalAlignment(RIGHT);
985 } else {
986 setHorizontalAlignment(LEFT);
987 }
988
989 return this;
990 }
991 }
992
993 private final EventHandler eventHandler = new EventHandler();
994
995 private EventHandler
996 getHandler() { return eventHandler; }
997
998 private class EventHandler implements ListSelectionListener, TreeSelectionListener,
999 SamplerChannelListListener, ListListener<OrchestraModel> {
1000
1001 public void
1002 valueChanged(ListSelectionEvent e) {
1003 boolean b = !getSelectionModel().isSelectionEmpty();
1004 deleteAction.setEnabled(b);
1005 propertiesAction.setEnabled(b || instrumentsDbTree.getSelectionCount() > 0);
1006 renameAction.setEnabled(b);
1007 changeDescriptionAction.setEnabled(b);
1008 cutAction.setEnabled(b);
1009 copyAction.setEnabled(b);
1010 updateLoadInstrumentMenuStates();
1011 updateAddToMidiMapMenuStates();
1012 updateAddToOrchestraMenuStates();
1013 }
1014
1015 public void
1016 valueChanged(TreeSelectionEvent e) {
1017 DbDirectoryTreeNode n = instrumentsDbTree.getSelectedDirectoryNode();
1018 setParentDirectoryNode(n);
1019 reloadAction.setEnabled(n != null);
1020 createDirectoryAction.setEnabled(n != null);
1021 propertiesAction.setEnabled(n != null || getLeadObject() != null);
1022 }
1023
1024 public void
1025 channelAdded(SamplerChannelListEvent e) {
1026 updateLoadInstrumentMenus();
1027 }
1028
1029 public void
1030 channelRemoved(SamplerChannelListEvent e) {
1031 updateLoadInstrumentMenus();
1032 }
1033
1034 public void
1035 entryAdded(ListEvent<OrchestraModel> e) { updateAddToOrchestraMenus(); }
1036
1037 public void
1038 entryRemoved(ListEvent<OrchestraModel> e) { updateAddToOrchestraMenus(); }
1039 }
1040
1041 class ContextMenu extends MouseAdapter {
1042 private final JPopupMenu instrumentMenu = new JPopupMenu();
1043 private final JPopupMenu directoryMenu = new JPopupMenu();
1044 private final JPopupMenu menu = new JPopupMenu();
1045
1046 private JMenu loadInstrumentMenu;
1047 private JMenu addToMidiMapMenu;
1048 private JMenu addToOrchestraMenu;
1049
1050 class MenuItem extends JMenuItem {
1051 MenuItem(Action a) { super(a); }
1052
1053 public Icon
1054 getIcon() { return null; }
1055 }
1056
1057 ContextMenu() {
1058 JMenuItem mi = new JMenuItem(pasteAction);
1059 mi.setIcon(null);
1060 menu.add(mi);
1061
1062 menu.addSeparator();
1063
1064 mi = new MenuItem(createDirectoryAction);
1065 mi.setIcon(null);
1066 menu.add(mi);
1067
1068 String s = i18n.getMenuLabel("instrumentsdb.actions.addInstruments");
1069 JMenu addInstrumentsMenu = new JMenu(s);
1070 menu.add(addInstrumentsMenu);
1071
1072 mi = new JMenuItem(addInstrumentsFromFileAction);
1073 mi.setIcon(null);
1074 addInstrumentsMenu.add(mi);
1075
1076 mi = new JMenuItem(addInstrumentsFromDirAction);
1077 mi.setIcon(null);
1078 addInstrumentsMenu.add(mi);
1079
1080 menu.addSeparator();
1081
1082 mi = new MenuItem(reloadAction);
1083 mi.setIcon(null);
1084 menu.add(mi);
1085
1086 menu.addSeparator();
1087
1088 mi = new JMenuItem(propertiesAction);
1089 mi.setIcon(null);
1090 menu.add(mi);
1091
1092 // Instrument's context menu
1093 mi = new JMenuItem(cutAction);
1094 mi.setIcon(null);
1095 instrumentMenu.add(mi);
1096
1097 mi = new JMenuItem(copyAction);
1098 mi.setIcon(null);
1099 instrumentMenu.add(mi);
1100
1101 instrumentMenu.addSeparator();
1102
1103 mi = new JMenuItem(deleteAction);
1104 mi.setIcon(null);
1105 instrumentMenu.add(mi);
1106
1107 mi = new JMenuItem(renameAction);
1108 mi.setIcon(null);
1109 instrumentMenu.add(mi);
1110
1111 mi = new JMenuItem(changeDescriptionAction);
1112 mi.setIcon(null);
1113 instrumentMenu.add(mi);
1114
1115 instrumentMenu.addSeparator();
1116
1117 s = i18n.getMenuLabel("instrumentsdb.actions.loadInstrument");
1118 loadInstrumentMenu = new JMenu(s);
1119 instrumentMenu.add(loadInstrumentMenu);
1120 registerLoadInstrumentMenus(loadInstrumentMenu);
1121
1122 addToMidiMapMenu =
1123 new JMenu(i18n.getMenuLabel("instrumentsdb.actions.addToMidiMap"));
1124 instrumentMenu.add(addToMidiMapMenu);
1125 registerAddToMidiMapMenu(addToMidiMapMenu);
1126
1127 s = i18n.getMenuLabel("instrumentsdb.actions.addToOrchestra");
1128 addToOrchestraMenu = new JMenu(s);
1129 instrumentMenu.add(addToOrchestraMenu);
1130 registerAddToOrchestraMenu(addToOrchestraMenu);
1131
1132 instrumentMenu.addSeparator();
1133
1134 mi = new JMenuItem(propertiesAction);
1135 mi.setIcon(null);
1136 instrumentMenu.add(mi);
1137
1138 // Directory's context menu
1139 mi = new JMenuItem(cutAction);
1140 mi.setIcon(null);
1141 directoryMenu.add(mi);
1142
1143 mi = new JMenuItem(copyAction);
1144 mi.setIcon(null);
1145 directoryMenu.add(mi);
1146
1147 directoryMenu.addSeparator();
1148
1149 mi = new JMenuItem(deleteAction);
1150 mi.setIcon(null);
1151 directoryMenu.add(mi);
1152
1153 mi = new JMenuItem(renameAction);
1154 mi.setIcon(null);
1155 directoryMenu.add(mi);
1156
1157 mi = new JMenuItem(changeDescriptionAction);
1158 mi.setIcon(null);
1159 directoryMenu.add(mi);
1160
1161 directoryMenu.addSeparator();
1162
1163 mi = new JMenuItem(propertiesAction);
1164 mi.setIcon(null);
1165 directoryMenu.add(mi);
1166 }
1167
1168 public void
1169 mousePressed(MouseEvent e) {
1170 if(e.isPopupTrigger()) show(e);
1171 }
1172
1173 public void
1174 mouseReleased(MouseEvent e) {
1175 if(e.isPopupTrigger()) show(e);
1176 }
1177
1178 void
1179 show(MouseEvent e) {
1180 Object obj = getLeadObject();
1181 if(obj == null) {
1182 menu.show(e.getComponent(), e.getX(), e.getY());
1183 return;
1184 }
1185
1186 if(obj instanceof DbInstrumentInfo) {
1187 instrumentMenu.show(e.getComponent(), e.getX(), e.getY());
1188 return;
1189 }
1190
1191 if(obj instanceof DbDirectoryInfo) {
1192 directoryMenu.show(e.getComponent(), e.getX(), e.getY());
1193 return;
1194 }
1195 }
1196 }
1197 }

  ViewVC Help
Powered by ViewVC