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

  ViewVC Help
Powered by ViewVC