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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1878 - (show annotations) (download)
Sat Mar 28 13:39:02 2009 UTC (15 years, 1 month ago) by iliev
File size: 16364 byte(s)
* Mac OS integration, work in progress: screen menu bar fix

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

  ViewVC Help
Powered by ViewVC