/[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 1752 - (show annotations) (download)
Mon Aug 11 22:51:24 2008 UTC (15 years, 8 months ago) by iliev
File size: 34487 byte(s)
* Added toolbar to the Database Instrument Chooser dialog
* Instrument Chooser and Database Instrument Chooser dialogs
  are now resizable
* Fantasia: Added toolbar to the Right-Side Pane's Instruments Database

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

  ViewVC Help
Powered by ViewVC