/[svn]/jsampler/trunk/src/org/jsampler/view/fantasia/InstrumentsDbFrame.java
ViewVC logotype

Annotation of /jsampler/trunk/src/org/jsampler/view/fantasia/InstrumentsDbFrame.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2288 - (hide annotations) (download)
Wed Nov 23 21:19:44 2011 UTC (12 years, 6 months ago) by iliev
File size: 16392 byte(s)
* Added option to select a sampler engine in Add/Edit Instrument dialog
* Moved all Swing dependent code outside the JSampler core

1 iliev 1286 /*
2     * JSampler - a java front-end for LinuxSampler
3     *
4 iliev 2288 * Copyright (C) 2005-2011 Grigor Iliev <grigor@grigoriliev.com>
5 iliev 1286 *
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.fantasia;
24    
25     import java.awt.BorderLayout;
26     import java.awt.Dimension;
27 iliev 1786 import java.awt.Rectangle;
28 iliev 1286
29     import java.awt.event.ActionEvent;
30     import java.awt.event.ActionListener;
31     import java.awt.event.KeyEvent;
32     import java.awt.event.WindowAdapter;
33     import java.awt.event.WindowEvent;
34    
35     import javax.swing.AbstractAction;
36     import javax.swing.Action;
37     import javax.swing.JComponent;
38     import javax.swing.JFrame;
39     import javax.swing.JMenu;
40     import javax.swing.JMenuBar;
41     import javax.swing.JMenuItem;
42     import javax.swing.JPanel;
43     import javax.swing.JScrollPane;
44     import javax.swing.JSplitPane;
45     import javax.swing.JToggleButton;
46     import javax.swing.JToolBar;
47     import javax.swing.KeyStroke;
48    
49     import javax.swing.event.ListSelectionEvent;
50     import javax.swing.event.ListSelectionListener;
51    
52 iliev 2288 import net.sf.juife.swing.NavigationPage;
53     import net.sf.juife.swing.NavigationPane;
54 iliev 1286
55     import org.jsampler.CC;
56     import org.jsampler.task.InstrumentsDb;
57 iliev 1752 import org.jsampler.view.std.JSInstrumentsDbColumnPreferencesDlg;
58 iliev 1286 import org.jsampler.view.std.JSInstrumentsDbTable;
59 iliev 1729 import org.jsampler.view.std.JSLostFilesDlg;
60 iliev 1786 import org.jsampler.view.std.StdUtils;
61 iliev 2288 import org.jsampler.view.swing.DbDirectoryTreeNode;
62     import org.jsampler.view.swing.SHF;
63 iliev 1743
64 iliev 1286 import org.linuxsampler.lscp.DbDirectoryInfo;
65     import org.linuxsampler.lscp.DbInstrumentInfo;
66    
67     import static org.jsampler.view.fantasia.FantasiaI18n.i18n;
68     import static org.jsampler.view.fantasia.FantasiaPrefs.preferences;
69    
70     /**
71     *
72     * @author Grigor Iliev
73     */
74     public class InstrumentsDbFrame extends JFrame {
75 iliev 1729 private final ToolBar toolbar;
76 iliev 1286 private final JMenuBar menuBar = new JMenuBar();
77 iliev 1743 private final FantasiaInstrumentsDbTree instrumentsDbTree;
78 iliev 1286 private final SidePane sidePane;
79     private final JSplitPane splitPane;
80     private final MainPane mainPane;
81    
82     private JMenu loadInstrumentMenu;
83     private JMenu addToMidiMapMenu;
84     private JMenu addToOrchestraMenu;
85    
86     /**
87     * Creates a new instance of <code>InstrumentsDbFrame</code>
88     */
89     public
90     InstrumentsDbFrame() {
91     setTitle(i18n.getLabel("InstrumentsDbFrame.title"));
92     if(Res.iconAppIcon != null) setIconImage(Res.iconAppIcon.getImage());
93 iliev 1878
94     ((ViewConfig)CC.getViewConfig()).restoreMenuProperties();
95 iliev 1286
96 iliev 2288 instrumentsDbTree = new FantasiaInstrumentsDbTree(SHF.getInstrumentsDbTreeModel());
97 iliev 1286
98     sidePane = new SidePane();
99     mainPane = new MainPane();
100    
101     instrumentsDbTree.setSelectedDirectory("/");
102    
103 iliev 1729 toolbar = new ToolBar();
104     getContentPane().add(toolbar, BorderLayout.NORTH);
105 iliev 1286
106     splitPane = new JSplitPane (
107     JSplitPane.HORIZONTAL_SPLIT,
108     true, // continuousLayout
109     sidePane,
110     mainPane
111     );
112    
113     splitPane.setDividerSize(3);
114     splitPane.setDividerLocation(200);
115    
116 iliev 1878 // fix for moving the menu bar on top of the screen
117     // when running on Mac OS and third party plugin is used
118     ((ViewConfig)CC.getViewConfig()).setNativeMenuProperties();
119    
120 iliev 1286 addMenu();
121    
122     pack();
123     setSavedSize();
124    
125     getContentPane().add(splitPane);
126     mainPane.getInstrumentsTable().loadColumnWidths();
127    
128     addWindowListener(new WindowAdapter() {
129     public void
130     windowClosing(WindowEvent we) { onWindowClose(); }
131     });
132    
133     installKeyboardListeners();
134     }
135    
136     private void
137     addMenu() {
138     JMenu m;
139     JMenuItem mi;
140    
141     setJMenuBar(menuBar);
142    
143     // Actions
144     m = new JMenu(i18n.getMenuLabel("instrumentsdb.actions"));
145     menuBar.add(m);
146    
147     mi = new JMenuItem(mainPane.getInstrumentsTable().createDirectoryAction);
148     mi.setIcon(null);
149     m.add(mi);
150    
151     mi = new JMenuItem(mainPane.getInstrumentsTable().deleteAction);
152     mi.setIcon(null);
153     mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
154     m.add(mi);
155    
156     JMenu addInstrumentsMenu =
157     new JMenu(i18n.getMenuLabel("instrumentsdb.actions.addInstruments"));
158     m.add(addInstrumentsMenu);
159    
160     mi = new JMenuItem(mainPane.getInstrumentsTable().addInstrumentsFromFileAction);
161     mi.setIcon(null);
162     addInstrumentsMenu.add(mi);
163    
164     mi = new JMenuItem(mainPane.getInstrumentsTable().addInstrumentsFromDirAction);
165     mi.setIcon(null);
166     addInstrumentsMenu.add(mi);
167    
168     m.addSeparator();
169    
170 iliev 1355 mi = new JMenuItem(i18n.getMenuLabel("instrumentsdb.actions.format"));
171     m.add(mi);
172     mi.addActionListener(new ActionListener() {
173     public void
174     actionPerformed(ActionEvent e) {
175     String s = i18n.getMessage("InstrumentsDbFrame.formatDatabase?");
176 iliev 2288 if(!SHF.showYesNoDialog(InstrumentsDbFrame.this, s)) return;
177 iliev 1355 CC.getTaskQueue().add(new InstrumentsDb.Format());
178     }
179     });
180    
181 iliev 1729 mi = new JMenuItem(i18n.getMenuLabel("instrumentsdb.actions.checkForLostFiles"));
182     m.add(mi);
183     mi.addActionListener(new ActionListener() {
184     public void
185     actionPerformed(ActionEvent e) {
186     new JSLostFilesDlg(InstrumentsDbFrame.this).setVisible(true);
187     }
188     });
189    
190 iliev 1355 m.addSeparator();
191    
192 iliev 1286 loadInstrumentMenu =
193     new JMenu(i18n.getMenuLabel("instrumentsdb.actions.loadInstrument"));
194     m.add(loadInstrumentMenu);
195     mainPane.getInstrumentsTable().registerLoadInstrumentMenus(loadInstrumentMenu);
196    
197     addToMidiMapMenu =
198     new JMenu(i18n.getMenuLabel("instrumentsdb.actions.addToMidiMap"));
199     m.add(addToMidiMapMenu);
200     mainPane.getInstrumentsTable().registerAddToMidiMapMenu(addToMidiMapMenu);
201    
202     addToOrchestraMenu =
203     new JMenu(i18n.getMenuLabel("instrumentsdb.actions.addToOrchestra"));
204     m.add(addToOrchestraMenu);
205     mainPane.getInstrumentsTable().registerAddToOrchestraMenu(addToOrchestraMenu);
206    
207     m.addSeparator();
208    
209     mi = new JMenuItem(mainPane.getInstrumentsTable().reloadAction);
210     mi.setIcon(null);
211     mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0));
212     m.add(mi);
213    
214     m.addSeparator();
215    
216     mi = new JMenuItem(mainPane.getInstrumentsTable().propertiesAction);
217     mi.setIcon(null);
218     mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.ALT_MASK));
219     m.add(mi);
220    
221     m = new JMenu(i18n.getMenuLabel("instrumentsdb.edit"));
222     menuBar.add(m);
223 iliev 1864
224     int modKey = CC.getViewConfig().getDefaultModKey();
225    
226 iliev 1286 mi = new JMenuItem(mainPane.getInstrumentsTable().cutAction);
227 iliev 1864 mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, modKey));
228 iliev 1286 mi.setIcon(null);
229     m.add(mi);
230    
231     mi = new JMenuItem(mainPane.getInstrumentsTable().copyAction);
232 iliev 1864 mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, modKey));
233 iliev 1286 mi.setIcon(null);
234     m.add(mi);
235    
236     mi = new JMenuItem(mainPane.getInstrumentsTable().pasteAction);
237 iliev 1864 mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, modKey));
238 iliev 1286 mi.setIcon(null);
239     m.add(mi);
240    
241     m.addSeparator();
242    
243 iliev 1729 mi = new JMenuItem(i18n.getMenuLabel("instrumentsdb.edit.find"));
244 iliev 1864 mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, modKey));
245 iliev 1729 m.add(mi);
246     mi.addActionListener(new ActionListener() {
247     public void
248     actionPerformed(ActionEvent e) {
249     if(toolbar.btnFind.isSelected()) return;
250    
251     String path = instrumentsDbTree.getSelectedDirectoryPath();
252     if(path != null) sidePane.searchPage.setSearchPath(path);
253     toolbar.btnFind.doClick(0);
254     }
255     });
256    
257     m.addSeparator();
258    
259 iliev 1286 mi = new JMenuItem(mainPane.getInstrumentsTable().renameAction);
260     mi.setIcon(null);
261     m.add(mi);
262    
263     mi = new JMenuItem(mainPane.getInstrumentsTable().changeDescriptionAction);
264     mi.setIcon(null);
265     m.add(mi);
266    
267     m = new JMenu(i18n.getMenuLabel("instrumentsdb.go"));
268     menuBar.add(m);
269    
270 iliev 1752 instrumentsDbTree.actionGoUp.putValue(Action.SMALL_ICON, Res.iconGoUp22);
271     mi = new JMenuItem(instrumentsDbTree.actionGoUp);
272 iliev 1286 mi.setIcon(null);
273     mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_UP, KeyEvent.ALT_MASK));
274     m.add(mi);
275    
276 iliev 1752 instrumentsDbTree.actionGoBack.putValue(Action.SMALL_ICON, Res.iconGoBack22);
277     mi = new JMenuItem(instrumentsDbTree.actionGoBack);
278 iliev 1286 mi.setIcon(null);
279     mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, KeyEvent.ALT_MASK));
280     m.add(mi);
281    
282 iliev 1752 instrumentsDbTree.actionGoForward.putValue(Action.SMALL_ICON, Res.iconGoForward22);
283     mi = new JMenuItem(instrumentsDbTree.actionGoForward);
284 iliev 1286 mi.setIcon(null);
285     mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, KeyEvent.ALT_MASK));
286     m.add(mi);
287     }
288    
289     private void
290     installKeyboardListeners() {
291     getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put (
292     KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0),
293     "goUp"
294     );
295    
296     getRootPane().getActionMap().put ("goUp", new AbstractAction() {
297     public void
298     actionPerformed(ActionEvent e) {
299 iliev 1752 if(!instrumentsDbTree.actionGoUp.isEnabled()) return;
300     instrumentsDbTree.actionGoUp.actionPerformed(null);
301 iliev 1286 }
302     });
303     }
304    
305     /** Invoked when this window is about to close. */
306     private void
307     onWindowClose() {
308     boolean b = (getExtendedState() & MAXIMIZED_BOTH) == MAXIMIZED_BOTH;
309     preferences().setBoolProperty("InstrumentsDbFrame.windowMaximized", b);
310     if(b) return;
311    
312 iliev 1786 StdUtils.saveWindowBounds("InstrumentsDbFrame", getBounds());
313 iliev 1286
314     mainPane.getInstrumentsTable().saveColumnsVisibleState();
315     mainPane.getInstrumentsTable().saveColumnWidths();
316     }
317    
318 iliev 1786 @Override
319     public void
320     setVisible(boolean b) {
321     if(b == isVisible()) return;
322    
323     super.setVisible(b);
324    
325     if(b && preferences().getBoolProperty("InstrumentsDbFrame.windowMaximized")) {
326     setExtendedState(getExtendedState() | MAXIMIZED_BOTH);
327     }
328     }
329    
330 iliev 1286 private void
331     setDefaultSize() {
332     Dimension dimension = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
333     double width = dimension.getWidth();
334     double height = dimension.getHeight();
335     setBounds(100, 100, (int) width - 200, (int) height - 200);
336     }
337    
338     private void
339     setSavedSize() {
340 iliev 1786 Rectangle r = StdUtils.getWindowBounds("InstrumentsDbFrame");
341     if(r == null) {
342 iliev 1286 setDefaultSize();
343     return;
344     }
345    
346 iliev 1786 setBounds(r);
347 iliev 1286 }
348    
349     private void
350     addSidePane() {
351     getContentPane().remove(mainPane);
352     splitPane.setRightComponent(mainPane);
353     getContentPane().add(splitPane);
354     int i = preferences().getIntProperty("InstrumentsDbFrame.dividerLocation");
355     if(i != 0) splitPane.setDividerLocation(i);
356     }
357    
358     private void
359     removeSidePane() {
360     int i = splitPane.getDividerLocation();
361     preferences().setIntProperty("InstrumentsDbFrame.dividerLocation", i);
362     getContentPane().remove(splitPane);
363     getContentPane().add(mainPane);
364     }
365    
366 iliev 1729 class ToolBar extends JToolBar {
367 iliev 1286 private final ToggleButton btnFolders = new ToggleButton();
368     private final ToggleButton btnFind = new ToggleButton();
369    
370 iliev 1752 private final ToolbarButton btnGoUp = new ToolbarButton(instrumentsDbTree.actionGoUp);
371     private final ToolbarButton btnGoBack = new ToolbarButton(instrumentsDbTree.actionGoBack);
372     private final ToolbarButton btnGoForward = new ToolbarButton(instrumentsDbTree.actionGoForward);
373     private final ToolbarButton btnReload = new ToolbarButton(mainPane.getInstrumentsTable().reloadAction);
374 iliev 1286
375     private final ToolbarButton btnPreferences = new ToolbarButton();
376    
377 iliev 1729 public ToolBar() {
378 iliev 1286 super(i18n.getLabel("InstrumentsDbFrame.ToolbarBar.name"));
379     setFloatable(false);
380    
381     btnFolders.setIcon(Res.iconFolderOpen22);
382     btnFolders.doClick(0);
383     btnFind.setIcon(Res.iconFind22);
384     btnPreferences.setIcon(Res.iconPreferences22);
385    
386     add(btnFolders);
387     add(btnFind);
388     addSeparator();
389     add(btnGoBack);
390     add(btnGoForward);
391     add(btnGoUp);
392     add(btnReload);
393     addSeparator();
394     add(btnPreferences);
395    
396    
397     btnFolders.addActionListener(new ActionListener() {
398     public void
399     actionPerformed(ActionEvent e) {
400     if(!btnFolders.isSelected() && !btnFind.isSelected()) {
401     removeSidePane();
402     }
403    
404     if(!btnFolders.isSelected()) return;
405    
406     if(btnFind.isSelected()) btnFind.doClick(0);
407     else addSidePane();
408    
409     sidePane.showFoldersPage();
410     }
411     });
412    
413     btnFind.addActionListener(new ActionListener() {
414     public void
415     actionPerformed(ActionEvent e) {
416     if(!btnFolders.isSelected() && !btnFind.isSelected()) {
417     removeSidePane();
418     }
419    
420     if(!btnFind.isSelected()) return;
421    
422     if(btnFolders.isSelected()) btnFolders.doClick(0);
423     else addSidePane();
424    
425     sidePane.showSearchPage();
426     }
427     });
428    
429     btnPreferences.addActionListener(new ActionListener() {
430     public void
431     actionPerformed(ActionEvent e) {
432     new PreferencesDlg().setVisible(true);
433     }
434     });
435     }
436     }
437    
438     class ToggleButton extends JToggleButton {
439     ToggleButton() {
440     setFocusPainted(false);
441     }
442     }
443    
444 iliev 1743 public FantasiaInstrumentsDbTree
445 iliev 1286 getInstrumentsDbTree() { return instrumentsDbTree; }
446    
447     public void
448     setSearchResults(DbDirectoryInfo[] directories) {
449     setSearchResults(directories, null);
450     }
451    
452     public void
453     setSearchResults(DbInstrumentInfo[] instruments) {
454     setSearchResults(null, instruments);
455     }
456    
457     public void
458     setSearchResults(DbDirectoryInfo[] directories, DbInstrumentInfo[] instruments) {
459     DbDirectoryTreeNode node = mainPane.getSearchResultsNode();
460     node.removeAllDirectories();
461     node.removeAllInstruments();
462    
463     if(instruments != null) {
464     for(DbInstrumentInfo i : instruments) i.setShowAbsolutePath(true);
465     node.addInstruments(instruments);
466     }
467    
468     if(directories != null) {
469     DbDirectoryTreeNode[] nodeS = new DbDirectoryTreeNode[directories.length];
470     for(int i = 0; i < directories.length; i++) {
471     DbDirectoryInfo d = directories[i];
472     d.setShowAbsolutePath(true);
473     nodeS[i] = new DbDirectoryTreeNode(d);
474    
475    
476     }
477     node.addDirectories(nodeS);
478     }
479    
480     mainPane.showSearchResultsNode();
481     }
482    
483     class MainPane extends JPanel {
484     private final JSInstrumentsDbTable instrumentsTable =
485 iliev 1752 new JSInstrumentsDbTable(instrumentsDbTree, "InstrumentsDbFrame.");
486 iliev 1286
487     private final DbDirectoryTreeNode searchResultsNode = new DbDirectoryTreeNode(null);
488    
489     MainPane() {
490     setLayout(new BorderLayout());
491     JScrollPane sp = new JScrollPane(instrumentsTable);
492 iliev 1743 sp.setOpaque(false);
493     sp.getViewport().setOpaque(false);
494 iliev 1286 add(sp);
495    
496     //instrumentsTable.setBackground(new java.awt.Color(0x626262));
497     instrumentsTable.getModel().setShowDummyColumn(false);
498     instrumentsTable.reloadAction.putValue(Action.SMALL_ICON, Res.iconReload22);
499    
500     instrumentsTable.createDirectoryAction.putValue (
501     Action.SMALL_ICON, Res.iconNew16
502     );
503    
504     instrumentsTable.getParent().setBackground(instrumentsTable.getBackground());
505 iliev 1705
506 iliev 1286 searchResultsNode.setDetached(true);
507     }
508    
509     public JSInstrumentsDbTable
510     getInstrumentsTable() { return instrumentsTable; }
511    
512     public DbDirectoryTreeNode
513     getSearchResultsNode() { return searchResultsNode; }
514    
515     public void
516     showSearchResultsNode() {
517     instrumentsDbTree.clearSelection();
518     instrumentsTable.setParentDirectoryNode(searchResultsNode);
519     }
520     }
521    
522    
523     class SidePane extends NavigationPane {
524     private final NavigationPage foldersPage = new NavigationPage();
525     private final DbSearchPage searchPage = new DbSearchPage(InstrumentsDbFrame.this);
526    
527     SidePane() {
528     setTitlebarVisiblie(false);
529    
530     foldersPage.setTitle(i18n.getLabel("InstrumentsDbFrame.folders"));
531     foldersPage.setLayout(new BorderLayout());
532     foldersPage.add(new JScrollPane(instrumentsDbTree));
533    
534     NavigationPage[] pages = { foldersPage, searchPage };
535     setPages(pages);
536     showFoldersPage();
537     }
538    
539     /** Shows the folders page in the left pane. */
540     public void
541     showFoldersPage() { getModel().addPage(foldersPage); }
542    
543     /** Shows the search page in the left pane. */
544     public void
545     showSearchPage() { getModel().addPage(searchPage); }
546     }
547    
548     private final EventHandler eventHandler = new EventHandler();
549    
550     private EventHandler
551     getHandler() { return eventHandler; }
552    
553     private class EventHandler implements ListSelectionListener {
554 iliev 1786 @Override
555 iliev 1286 public void
556     valueChanged(ListSelectionEvent e) {
557    
558     }
559     }
560    
561 iliev 1752 class PreferencesDlg extends JSInstrumentsDbColumnPreferencesDlg {
562 iliev 1286 PreferencesDlg() {
563 iliev 1752 super(InstrumentsDbFrame.this, mainPane.instrumentsTable);
564 iliev 1286 }
565     }
566     }

  ViewVC Help
Powered by ViewVC