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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

1 iliev 1286 /*
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 iliev 1540 import org.jsampler.OrchestraInstrument;
70 iliev 1286 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 iliev 1347 import static org.linuxsampler.lscp.Parser.*;
94    
95 iliev 1286 /**
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 iliev 1729 final String path = n.getInfo().getDirectoryPath();
426     instrumentsDbTree.refreshDirectoryContent(path);
427     CC.scheduleInTaskQueue(new Runnable() {
428     public void
429     run() { instrumentsDbTree.setSelectedDirectory(path); }
430     });
431 iliev 1286 }
432    
433     public void
434     valueChanged(TreeSelectionEvent e) {
435     DbDirectoryTreeNode n = instrumentsDbTree.getSelectedDirectoryNode();
436     setEnabled(n != null);
437     }
438     }
439    
440     class CreateDirectoryAction extends AbstractAction {
441     private String directoryName = null;
442    
443     CreateDirectoryAction() {
444     super(i18n.getMenuLabel("instrumentsdb.actions.createFolder"));
445    
446     String s = i18n.getMenuLabel("instrumentsdb.actions.createFolder.tt");
447     putValue(SHORT_DESCRIPTION, s);
448     }
449    
450     public void
451     actionPerformed(ActionEvent e) {
452     setDirectoryName(getUniqueDirectoryName());
453    
454     String path = instrumentsDbTree.getSelectedDirectoryPath();
455     if(path.length() > 1) path += "/";
456 iliev 1347 path += toEscapedFileName(getDirectoryName());
457 iliev 1286
458     final InstrumentsDb.CreateDirectory t =
459     new InstrumentsDb.CreateDirectory(path);
460    
461     setCreatedDirectoryName(directoryName);
462    
463     t.addTaskListener(new TaskListener() {
464     public void
465     taskPerformed(TaskEvent e) {
466     if(t.doneWithErrors()) {
467     setCreatedDirectoryName(null);
468     return;
469     }
470     }
471     });
472     CC.getTaskQueue().add(t);
473     }
474    
475     /**
476     * Gets the name of the directory to be created.
477     * @return The name of the directory to be created.
478     */
479     public String
480     getDirectoryName() { return directoryName; }
481    
482     /**
483     * Sets the name of the directory to be created.
484     * @param name The name of the directory to be created.
485     */
486     public void
487     setDirectoryName(String name) { directoryName = name; }
488     }
489    
490     class DeleteAction extends AbstractAction {
491     DeleteAction() {
492     super(i18n.getMenuLabel("instrumentsdb.actions.delete"));
493    
494     String s;
495     s = i18n.getMenuLabel("instrumentsdb.actions.delete.tt");
496     putValue(SHORT_DESCRIPTION, s);
497     setEnabled(false);
498     }
499    
500     public void
501     actionPerformed(ActionEvent e) {
502     final DbDirectoryInfo[] dirs = getSelectedDirectories();
503    
504     if(dirs.length > 0) {
505     String s = i18n.getMessage("JSInstrumentsDbTable.confirmDeletion");
506     if(!showYesNoDialog(s)) return;
507    
508     final Task t = new InstrumentsDb.RemoveDirectories(dirs);
509     t.addTaskListener(new TaskListener() {
510     public void
511     taskPerformed(TaskEvent e) {
512     if(instrumentsDbTree.getSelectionCount() == 0) {
513     // update search results
514     // TODO: lazily implemented
515     deleteDirectories(dirs);
516     }
517     }
518     });
519     CC.getTaskQueue().add(t);
520    
521    
522     }
523    
524     final DbInstrumentInfo[] instrs = getSelectedInstruments();
525     if(instrs.length > 0) {
526     final Task t = new InstrumentsDb.RemoveInstruments(instrs);
527     t.addTaskListener(new TaskListener() {
528     public void
529     taskPerformed(TaskEvent e) {
530     if(instrumentsDbTree.getSelectionCount() == 0) {
531     // update search results
532     // TODO: lazily implemented
533     deleteInstruments(instrs);
534     }
535     }
536     });
537     CC.getTaskQueue().add(t);
538     }
539     }
540    
541     /** Deletes the specified directories from the model */
542     private void
543     deleteDirectories(DbDirectoryInfo[] dirs) {
544     for(DbDirectoryInfo info : dirs) {
545     String path = info.getDirectoryPath();
546     getParentDirectoryNode().removeDirectoryByPathName(path);
547     getModel().fireTableDataChanged();
548     }
549     }
550    
551     /** Deletes the specified instruments from the model */
552     private void
553     deleteInstruments(DbInstrumentInfo[] instrs) {
554     for(DbInstrumentInfo info : instrs) {
555     String path = info.getInstrumentPath();
556     getParentDirectoryNode().removeInstrumentByPathName(path);
557     getModel().fireTableDataChanged();
558     }
559     }
560     }
561    
562     class AddInstrumentsFromFileAction extends AbstractAction {
563     AddInstrumentsFromFileAction() {
564     super(i18n.getMenuLabel("instrumentsdb.actions.addInstruments.fromFile"));
565    
566     String s = "instrumentsdb.actions.addInstruments.fromFile.tt";
567     putValue(SHORT_DESCRIPTION, i18n.getMenuLabel(s));
568     }
569    
570     public void
571     actionPerformed(ActionEvent e) {
572     String s;
573     DbDirectoryTreeNode node = getParentDirectoryNode();
574     if(node == null || node.getInfo() == null) s = null;
575     else s = node.getInfo().getDirectoryPath();
576    
577     JSAddDbInstrumentsFromFileDlg dlg;
578     Icon ico = instrumentsDbTree.getView().getOpenIcon();
579     Window w = JuifeUtils.getWindow(JSInstrumentsDbTable.this);
580     if(w instanceof Dialog) {
581     dlg = new JSAddDbInstrumentsFromFileDlg((Dialog)w, s, ico);
582     } else if(w instanceof Frame) {
583     dlg = new JSAddDbInstrumentsFromFileDlg((Frame)w, s, ico);
584     } else {
585     dlg = new JSAddDbInstrumentsFromFileDlg((Frame)null, s, ico);
586     }
587    
588     dlg.setVisible(true);
589     if(w != null) w.toFront();
590     }
591     }
592    
593     class AddInstrumentsFromDirAction extends AbstractAction {
594     AddInstrumentsFromDirAction() {
595     super(i18n.getMenuLabel("instrumentsdb.actions.addInstruments.fromDir"));
596    
597     String s = "instrumentsdb.actions.addInstruments.fromDir.tt";
598     putValue(SHORT_DESCRIPTION, i18n.getMenuLabel(s));
599     }
600    
601     public void
602     actionPerformed(ActionEvent e) {
603     String s;
604     DbDirectoryTreeNode node = getParentDirectoryNode();
605     if(node == null || node.getInfo() == null) s = null;
606     else s = node.getInfo().getDirectoryPath();
607    
608     JSAddDbInstrumentsFromDirDlg dlg;
609     Icon ico = instrumentsDbTree.getView().getOpenIcon();
610     Window w = JuifeUtils.getWindow(JSInstrumentsDbTable.this);
611     if(w instanceof Dialog) {
612     dlg = new JSAddDbInstrumentsFromDirDlg((Dialog)w, s, ico);
613     } else if(w instanceof Frame) {
614     dlg = new JSAddDbInstrumentsFromDirDlg((Frame)w, s, ico);
615     } else {
616     dlg = new JSAddDbInstrumentsFromDirDlg((Frame)null, s, ico);
617     }
618    
619     dlg.setVisible(true);
620     if(w != null) w.toFront();
621     }
622     }
623    
624     class LoadInstrumentAction extends AbstractAction {
625     private final SamplerChannelModel channelModel;
626    
627     LoadInstrumentAction(SamplerChannelModel model) {
628     String s = "instrumentsdb.actions.loadInstrument.onChannel";
629     putValue(Action.NAME, i18n.getMenuLabel(s, model.getChannelId()));
630     channelModel = model;
631     }
632    
633     public void
634     actionPerformed(ActionEvent e) {
635     Object obj = getLeadObject();
636     if(obj == null || !(obj instanceof DbInstrumentInfo)) return;
637     DbInstrumentInfo info = (DbInstrumentInfo)obj;
638     int idx = info.getInstrumentIndex();
639     channelModel.setBackendEngineType(info.getFormatFamily()); // TODO: fix this
640     channelModel.loadBackendInstrument(info.getFilePath(), idx);
641     }
642     }
643    
644     class AddToMidiMapAction extends AbstractAction {
645     private final MidiInstrumentMap midiMap;
646    
647     AddToMidiMapAction(MidiInstrumentMap map) {
648     super(map.getName());
649     midiMap = map;
650     }
651    
652     public void
653     actionPerformed(ActionEvent e) {
654 iliev 1540 DbInstrumentInfo[] instruments = getSelectedInstruments();
655     int l = instruments.length;
656     if(l == 0) return;
657 iliev 1286
658 iliev 1540 if(l > 4) {
659     String s = "JSInstrumentsDbTable.confirmAddToMidiMap";
660     s = i18n.getMessage(s, l, midiMap.getName());
661     if(!HF.showYesNoDialog(JSInstrumentsDbTable.this, s)) return;
662     }
663 iliev 1286
664     JSAddMidiInstrumentDlg dlg;
665     Window w = JuifeUtils.getWindow(JSInstrumentsDbTable.this);
666 iliev 1540
667     for(DbInstrumentInfo i : instruments) {
668     if(w instanceof Dialog) {
669     dlg = new JSAddMidiInstrumentDlg((Dialog)w, midiMap, i);
670     } else if(w instanceof Frame) {
671     dlg = new JSAddMidiInstrumentDlg((Frame)w, midiMap, i);
672     } else {
673     dlg = new JSAddMidiInstrumentDlg((Frame)null, midiMap, i);
674     }
675    
676     dlg.setVisible(true);
677 iliev 1286 }
678     }
679     }
680    
681     class AddToOrchestraAction extends AbstractAction {
682     private final OrchestraModel orchestraModel;
683    
684     AddToOrchestraAction(OrchestraModel model) {
685     super(model.getName());
686     orchestraModel = model;
687     }
688    
689     public void
690     actionPerformed(ActionEvent e) {
691 iliev 1540 DbInstrumentInfo[] instruments = getSelectedInstruments();
692     int l = instruments.length;
693     if(l == 0) return;
694    
695     if(l > 1) {
696     String s = "JSInstrumentsDbTable.confirmAddToOrchestra";
697     s = i18n.getMessage(s, l, orchestraModel.getName());
698     if(!HF.showYesNoDialog(JSInstrumentsDbTable.this, s)) return;
699     }
700    
701     for(DbInstrumentInfo i : instruments) {
702     OrchestraInstrument instr = new OrchestraInstrument();
703     instr.setFilePath(i.getFilePath());
704     instr.setInstrumentIndex(i.getInstrumentIndex());
705     instr.setName(i.getName());
706     instr.setDescription(i.getDescription());
707     instr.setEngine(i.getFormatFamily()); // TODO: this should be fixed
708     orchestraModel.addInstrument(instr);
709     }
710 iliev 1286 }
711     }
712    
713     class PropertiesAction extends AbstractAction {
714     PropertiesAction() {
715     super(i18n.getMenuLabel("instrumentsdb.actions.properties"));
716    
717     String s;
718     s = i18n.getMenuLabel("instrumentsdb.actions.properties.tt");
719     putValue(SHORT_DESCRIPTION, s);
720     setEnabled(false);
721     }
722    
723     public void
724     actionPerformed(ActionEvent e) {
725     Object obj = getLeadObject();
726     if(obj == null) {
727     DbDirectoryTreeNode node = getParentDirectoryNode();
728     if(node == null || node.getInfo() == null) return;
729     showDirectoryProperties(node.getInfo());
730     return;
731     }
732    
733     if(obj instanceof DbDirectoryInfo) {
734     showDirectoryProperties((DbDirectoryInfo)obj);
735     } else if(obj instanceof DbInstrumentInfo) {
736     showInstrumentProperties((DbInstrumentInfo)obj);
737     }
738     }
739    
740     private void
741     showInstrumentProperties(DbInstrumentInfo instr) {
742     JPanel p = new JSDbInstrumentPropsPane(instr);
743     String s = i18n.getLabel("JSInstrumentsDbTable.instrProps");
744     showDialog(s, p);
745     }
746    
747     private void
748     showDirectoryProperties(DbDirectoryInfo dir) {
749     JPanel p = new JSDbDirectoryPropsPane(dir);
750     String s = i18n.getLabel("JSInstrumentsDbTable.dirProps");
751     showDialog(s, p);
752     }
753    
754     private void
755     showDialog(String title, JPanel mainPane) {
756     InformationDialog dlg;
757     Window w = JuifeUtils.getWindow(JSInstrumentsDbTable.this);
758     if(w instanceof Dialog) {
759     dlg = new InformationDialog((Dialog)w, title, mainPane);
760     } else if(w instanceof Frame) {
761     dlg = new InformationDialog((Frame)w, title, mainPane);
762     } else {
763     dlg = new InformationDialog((Frame)null, title, mainPane);
764     }
765    
766     dlg.setMinimumSize(dlg.getPreferredSize());
767     dlg.setVisible(true);
768     }
769     }
770    
771     class RenameAction extends AbstractAction {
772     private String directoryPath = null;
773    
774     RenameAction() {
775     super(i18n.getMenuLabel("instrumentsdb.edit.rename"));
776    
777     String s = i18n.getMenuLabel("instrumentsdb.edit.rename.tt");
778     putValue(SHORT_DESCRIPTION, s);
779     setEnabled(false);
780     }
781    
782     public void
783     actionPerformed(ActionEvent e) {
784     int i = getSelectionModel().getLeadSelectionIndex();
785     if(i == -1) return;
786     editCellAt(i, 0);
787     }
788     }
789    
790     class ChangeDescriptionAction extends AbstractAction {
791     private String directoryPath = null;
792    
793     ChangeDescriptionAction() {
794     super(i18n.getMenuLabel("instrumentsdb.edit.description"));
795    
796     String s = i18n.getMenuLabel("instrumentsdb.edit.description.tt");
797     putValue(SHORT_DESCRIPTION, s);
798     setEnabled(false);
799     }
800    
801     public void
802     actionPerformed(ActionEvent e) {
803     Object obj = getLeadObject();
804     if(obj == null) return;
805    
806     if(obj instanceof DbDirectoryInfo) {
807     DbDirectoryInfo info = (DbDirectoryInfo)obj;
808     String s = editDescription(info.getDescription());
809     if(s == null) return;
810     String path = info.getDirectoryPath();
811     Task t = new InstrumentsDb.SetDirectoryDescription(path, s);
812     CC.getTaskQueue().add(t);
813     } else if(obj instanceof DbInstrumentInfo) {
814     DbInstrumentInfo info = (DbInstrumentInfo)obj;
815     String s = editDescription(info.getDescription());
816     if(s == null) return;
817     String path = info.getInstrumentPath();
818     Task t = new InstrumentsDb.SetInstrumentDescription(path, s);
819     CC.getTaskQueue().add(t);
820     }
821     }
822    
823     private String
824     editDescription(String s) {
825     JSDbDescriptionDlg dlg;
826     Window w = JuifeUtils.getWindow(JSInstrumentsDbTable.this);
827     if(w instanceof Dialog) {
828     dlg = new JSDbDescriptionDlg((Dialog)w);
829     } else if(w instanceof Frame) {
830     dlg = new JSDbDescriptionDlg((Frame)w);
831     } else {
832     dlg = new JSDbDescriptionDlg((Frame)null);
833     }
834    
835     dlg.setDescription(s);
836     dlg.setVisible(true);
837     if(dlg.isCancelled()) return null;
838     return dlg.getDescription();
839     }
840     }
841    
842     class CutAction extends AbstractAction {
843     CutAction() {
844     super(i18n.getMenuLabel("instrumentsdb.edit.cut"));
845    
846     String s = i18n.getMenuLabel("instrumentsdb.edit.cut.tt");
847     putValue(SHORT_DESCRIPTION, s);
848     setEnabled(false);
849     }
850    
851     public void
852     actionPerformed(ActionEvent e) {
853     getDbClipboard().setDirectories(getSelectedDirectories());
854     getDbClipboard().setInstruments(getSelectedInstruments());
855     getDbClipboard().setOperation(DbClipboard.Operation.CUT);
856     }
857     }
858    
859     class CopyAction extends AbstractAction {
860     CopyAction() {
861     super(i18n.getMenuLabel("instrumentsdb.edit.copy"));
862    
863     String s = i18n.getMenuLabel("instrumentsdb.edit.copy.tt");
864     putValue(SHORT_DESCRIPTION, s);
865     setEnabled(false);
866     }
867    
868     public void
869     actionPerformed(ActionEvent e) {
870     getDbClipboard().setDirectories(getSelectedDirectories());
871     getDbClipboard().setInstruments(getSelectedInstruments());
872     getDbClipboard().setOperation(DbClipboard.Operation.COPY);
873     }
874     }
875    
876     class PasteAction extends AbstractAction implements TreeSelectionListener, ChangeListener {
877     PasteAction() {
878     super(i18n.getMenuLabel("instrumentsdb.edit.paste"));
879    
880     String s = i18n.getMenuLabel("instrumentsdb.edit.paste.tt");
881     putValue(SHORT_DESCRIPTION, s);
882     setEnabled(false);
883     getDbClipboard().addChangeListener(this);
884     }
885    
886     public void
887     actionPerformed(ActionEvent e) {
888     DbDirectoryInfo[] dirs = getDbClipboard().getDirectories();
889     DbInstrumentInfo[] instrs = getDbClipboard().getInstruments();
890     String dest = instrumentsDbTree.getSelectedDirectoryPath();
891    
892     Task t;
893     if(getDbClipboard().getOperation() == DbClipboard.Operation.CUT) {
894     t = new InstrumentsDb.Move(dirs, instrs, dest);
895     getDbClipboard().setDirectories(new DbDirectoryInfo[0]);
896     getDbClipboard().setInstruments(new DbInstrumentInfo[0]);
897     } else if(getDbClipboard().getOperation() == DbClipboard.Operation.COPY) {
898     t = new InstrumentsDb.Copy(dirs, instrs, dest);
899     } else {
900     return;
901     }
902    
903     CC.getTaskQueue().add(t);
904     }
905    
906     public void
907     valueChanged(TreeSelectionEvent e) { updateState(); }
908    
909     public void
910     stateChanged(ChangeEvent e) { updateState(); }
911    
912     private void
913     updateState() {
914     DbDirectoryTreeNode n = instrumentsDbTree.getSelectedDirectoryNode();
915     if(n == null) {
916     setEnabled(false);
917     return;
918     }
919    
920     int dirs = getDbClipboard().getDirectories().length;
921     setEnabled(dirs > 0 || getDbClipboard().getInstruments().length > 0);
922     }
923     }
924    
925     class InstrumentsDbCellRenderer extends JLabel implements TableCellRenderer {
926    
927     InstrumentsDbCellRenderer() {
928     setOpaque(true);
929     setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
930     }
931    
932     public Component
933     getTableCellRendererComponent (
934     JTable table,
935     Object value,
936     boolean isSelected,
937     boolean hasFocus,
938     int row,
939     int column
940     ) {
941     if(column == 0 && value != null) {
942     String s;
943     if(value instanceof DbDirectoryInfo) {
944     setIcon(getView().getFolderIcon());
945     s = ((DbDirectoryInfo)value).getDescription();
946     setToolTipText(s.length() == 0 ? null : s);
947     } else if(value instanceof String) {
948     setIcon(getView().getFolderIcon());
949     setToolTipText(null);
950     } else if(value instanceof DbInstrumentInfo) {
951     DbInstrumentInfo info = (DbInstrumentInfo)value;
952     if("GIG".equals(info.getFormatFamily())) { // TODO: fix it!
953     setIcon(getView().getGigInstrumentIcon());
954     } else {
955     setIcon(getView().getInstrumentIcon());
956     }
957    
958     s = info.getDescription();
959     setToolTipText(s.length() == 0 ? null : s);
960     } else {
961     setIcon(null);
962     setToolTipText(null);
963     }
964     } else {
965     setIcon(null);
966     setToolTipText(null);
967     }
968    
969     if(value != null) setText(value.toString());
970     else setText("");
971    
972     if (isSelected) {
973     setBackground(table.getSelectionBackground());
974     setForeground(table.getSelectionForeground());
975     } else {
976     setBackground(table.getBackground());
977     setForeground(table.getForeground());
978     }
979    
980     ColumnType ct =
981     ((InstrumentsDbTableModel)table.getModel()).getColumnType(column);
982    
983     if(ct == ColumnType.IS_DRUM || ct == ColumnType.FORMAT_FAMILY) {
984     setHorizontalAlignment(CENTER);
985     } else if ( ct == ColumnType.SIZE ||
986     ct == ColumnType.INSTRUMENT_NR ||
987     ct == ColumnType.FORMAT_VERSION
988     ) {
989     setHorizontalAlignment(RIGHT);
990     } else {
991     setHorizontalAlignment(LEFT);
992     }
993    
994     return this;
995     }
996     }
997    
998     private final EventHandler eventHandler = new EventHandler();
999    
1000     private EventHandler
1001     getHandler() { return eventHandler; }
1002    
1003     private class EventHandler implements ListSelectionListener, TreeSelectionListener,
1004     SamplerChannelListListener, ListListener<OrchestraModel> {
1005    
1006     public void
1007     valueChanged(ListSelectionEvent e) {
1008     boolean b = !getSelectionModel().isSelectionEmpty();
1009     deleteAction.setEnabled(b);
1010     propertiesAction.setEnabled(b || instrumentsDbTree.getSelectionCount() > 0);
1011     renameAction.setEnabled(b);
1012     changeDescriptionAction.setEnabled(b);
1013     cutAction.setEnabled(b);
1014     copyAction.setEnabled(b);
1015     updateLoadInstrumentMenuStates();
1016     updateAddToMidiMapMenuStates();
1017     updateAddToOrchestraMenuStates();
1018     }
1019    
1020     public void
1021     valueChanged(TreeSelectionEvent e) {
1022     DbDirectoryTreeNode n = instrumentsDbTree.getSelectedDirectoryNode();
1023     setParentDirectoryNode(n);
1024     reloadAction.setEnabled(n != null);
1025     createDirectoryAction.setEnabled(n != null);
1026     propertiesAction.setEnabled(n != null || getLeadObject() != null);
1027     }
1028    
1029     public void
1030     channelAdded(SamplerChannelListEvent e) {
1031 iliev 1737 if(CC.getSamplerModel().getChannelListIsAdjusting()) return;
1032 iliev 1286 updateLoadInstrumentMenus();
1033     }
1034    
1035     public void
1036     channelRemoved(SamplerChannelListEvent e) {
1037     updateLoadInstrumentMenus();
1038     }
1039    
1040     public void
1041     entryAdded(ListEvent<OrchestraModel> e) { updateAddToOrchestraMenus(); }
1042    
1043     public void
1044     entryRemoved(ListEvent<OrchestraModel> e) { updateAddToOrchestraMenus(); }
1045     }
1046    
1047     class ContextMenu extends MouseAdapter {
1048     private final JPopupMenu instrumentMenu = new JPopupMenu();
1049     private final JPopupMenu directoryMenu = new JPopupMenu();
1050     private final JPopupMenu menu = new JPopupMenu();
1051    
1052     private JMenu loadInstrumentMenu;
1053     private JMenu addToMidiMapMenu;
1054     private JMenu addToOrchestraMenu;
1055    
1056     class MenuItem extends JMenuItem {
1057     MenuItem(Action a) { super(a); }
1058    
1059     public Icon
1060     getIcon() { return null; }
1061     }
1062    
1063     ContextMenu() {
1064     JMenuItem mi = new JMenuItem(pasteAction);
1065     mi.setIcon(null);
1066     menu.add(mi);
1067    
1068     menu.addSeparator();
1069    
1070     mi = new MenuItem(createDirectoryAction);
1071     mi.setIcon(null);
1072     menu.add(mi);
1073    
1074     String s = i18n.getMenuLabel("instrumentsdb.actions.addInstruments");
1075     JMenu addInstrumentsMenu = new JMenu(s);
1076     menu.add(addInstrumentsMenu);
1077    
1078     mi = new JMenuItem(addInstrumentsFromFileAction);
1079     mi.setIcon(null);
1080     addInstrumentsMenu.add(mi);
1081    
1082     mi = new JMenuItem(addInstrumentsFromDirAction);
1083     mi.setIcon(null);
1084     addInstrumentsMenu.add(mi);
1085    
1086     menu.addSeparator();
1087    
1088     mi = new MenuItem(reloadAction);
1089     mi.setIcon(null);
1090     menu.add(mi);
1091    
1092     menu.addSeparator();
1093    
1094     mi = new JMenuItem(propertiesAction);
1095     mi.setIcon(null);
1096     menu.add(mi);
1097    
1098     // Instrument's context menu
1099     mi = new JMenuItem(cutAction);
1100     mi.setIcon(null);
1101     instrumentMenu.add(mi);
1102    
1103     mi = new JMenuItem(copyAction);
1104     mi.setIcon(null);
1105     instrumentMenu.add(mi);
1106    
1107     instrumentMenu.addSeparator();
1108    
1109     mi = new JMenuItem(deleteAction);
1110     mi.setIcon(null);
1111     instrumentMenu.add(mi);
1112    
1113     mi = new JMenuItem(renameAction);
1114     mi.setIcon(null);
1115     instrumentMenu.add(mi);
1116    
1117     mi = new JMenuItem(changeDescriptionAction);
1118     mi.setIcon(null);
1119     instrumentMenu.add(mi);
1120    
1121     instrumentMenu.addSeparator();
1122    
1123     s = i18n.getMenuLabel("instrumentsdb.actions.loadInstrument");
1124     loadInstrumentMenu = new JMenu(s);
1125     instrumentMenu.add(loadInstrumentMenu);
1126     registerLoadInstrumentMenus(loadInstrumentMenu);
1127    
1128     addToMidiMapMenu =
1129     new JMenu(i18n.getMenuLabel("instrumentsdb.actions.addToMidiMap"));
1130     instrumentMenu.add(addToMidiMapMenu);
1131     registerAddToMidiMapMenu(addToMidiMapMenu);
1132    
1133     s = i18n.getMenuLabel("instrumentsdb.actions.addToOrchestra");
1134     addToOrchestraMenu = new JMenu(s);
1135     instrumentMenu.add(addToOrchestraMenu);
1136     registerAddToOrchestraMenu(addToOrchestraMenu);
1137    
1138     instrumentMenu.addSeparator();
1139    
1140     mi = new JMenuItem(propertiesAction);
1141     mi.setIcon(null);
1142     instrumentMenu.add(mi);
1143    
1144     // Directory's context menu
1145     mi = new JMenuItem(cutAction);
1146     mi.setIcon(null);
1147     directoryMenu.add(mi);
1148    
1149     mi = new JMenuItem(copyAction);
1150     mi.setIcon(null);
1151     directoryMenu.add(mi);
1152    
1153     directoryMenu.addSeparator();
1154    
1155     mi = new JMenuItem(deleteAction);
1156     mi.setIcon(null);
1157     directoryMenu.add(mi);
1158    
1159     mi = new JMenuItem(renameAction);
1160     mi.setIcon(null);
1161     directoryMenu.add(mi);
1162    
1163     mi = new JMenuItem(changeDescriptionAction);
1164     mi.setIcon(null);
1165     directoryMenu.add(mi);
1166    
1167     directoryMenu.addSeparator();
1168    
1169     mi = new JMenuItem(propertiesAction);
1170     mi.setIcon(null);
1171     directoryMenu.add(mi);
1172     }
1173    
1174     public void
1175     mousePressed(MouseEvent e) {
1176     if(e.isPopupTrigger()) show(e);
1177     }
1178    
1179     public void
1180     mouseReleased(MouseEvent e) {
1181     if(e.isPopupTrigger()) show(e);
1182     }
1183    
1184     void
1185     show(MouseEvent e) {
1186     Object obj = getLeadObject();
1187     if(obj == null) {
1188     menu.show(e.getComponent(), e.getX(), e.getY());
1189     return;
1190     }
1191    
1192     if(obj instanceof DbInstrumentInfo) {
1193     instrumentMenu.show(e.getComponent(), e.getX(), e.getY());
1194     return;
1195     }
1196    
1197     if(obj instanceof DbDirectoryInfo) {
1198     directoryMenu.show(e.getComponent(), e.getX(), e.getY());
1199     return;
1200     }
1201     }
1202     }
1203     }

  ViewVC Help
Powered by ViewVC