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

Contents of /jsampler/trunk/src/org/jsampler/view/std/JSDbInstrumentChooser.java

Parent Directory Parent Directory | Revision Log Revision Log


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

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005-2011 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.BorderLayout;
26 import java.awt.Dialog;
27 import java.awt.Frame;
28
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31 import java.awt.event.KeyEvent;
32 import java.awt.event.MouseAdapter;
33 import java.awt.event.MouseEvent;
34
35 import javax.swing.Action;
36 import javax.swing.AbstractAction;
37 import javax.swing.JButton;
38 import javax.swing.JComponent;
39 import javax.swing.JPanel;
40 import javax.swing.JScrollPane;
41 import javax.swing.JSplitPane;
42 import javax.swing.JToolBar;
43 import javax.swing.KeyStroke;
44 import javax.swing.ListSelectionModel;
45
46 import javax.swing.event.ListSelectionEvent;
47 import javax.swing.event.ListSelectionListener;
48
49 import net.sf.juife.swing.OkCancelDialog;
50
51 import org.jsampler.CC;
52
53 import org.jsampler.view.swing.DbDirectoryTreeNode;
54 import org.jsampler.view.swing.InstrumentsDbTreeModel;
55 import org.jsampler.view.swing.SHF;
56
57 import org.linuxsampler.lscp.DbInstrumentInfo;
58 import org.linuxsampler.lscp.Parser;
59
60 import static org.jsampler.view.std.StdI18n.i18n;
61
62
63 /**
64 *
65 * @author Grigor Iliev
66 */
67 public class JSDbInstrumentChooser extends OkCancelDialog implements ListSelectionListener {
68 protected JSInstrumentsDbTree instrumentsDbTree =
69 createInstrumentsDbTree(SHF.getInstrumentsDbTreeModel());
70
71 protected JSInstrumentsDbTable instrumentsDbTable =
72 new JSInstrumentsDbTable(instrumentsDbTree, "DbInstrumentChooser.");
73
74 protected final ToolBar toolBar = new ToolBar();
75
76
77 /**
78 * Creates a new instance of <code>JSDbInstrumentChooser</code>
79 */
80 public JSDbInstrumentChooser(Frame owner) {
81 super(owner, i18n.getLabel("JSDbInstrumentChooser.title"));
82 initDbInstrumentChooser();
83 }
84
85 /**
86 * Creates a new instance of <code>JSDbInstrumentChooser</code>
87 */
88 public JSDbInstrumentChooser(Dialog owner) {
89 super(owner, i18n.getLabel("JSDbInstrumentChooser.title"));
90 initDbInstrumentChooser();
91 }
92
93 private void
94 initDbInstrumentChooser() {
95 btnOk.setEnabled(false);
96 JScrollPane sp = new JScrollPane(instrumentsDbTree);
97
98 instrumentsDbTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
99 instrumentsDbTable.getSelectionModel().addListSelectionListener(this);
100 JScrollPane sp2 = new JScrollPane(instrumentsDbTable);
101
102 JSplitPane splitPane = new JSplitPane (
103 JSplitPane.HORIZONTAL_SPLIT,
104 true, // continuousLayout
105 sp,
106 sp2
107 );
108
109 splitPane.setDividerSize(3);
110 splitPane.setDividerLocation(200);
111
112 JPanel p = new JPanel();
113 p.setLayout(new BorderLayout());
114 p.add(new ToolBar(), BorderLayout.NORTH);
115 p.add(splitPane);
116
117 setMainPane(p);
118
119 instrumentsDbTable.addMouseListener(new MouseAdapter() {
120 public void
121 mouseClicked(MouseEvent e) {
122 if(e.getButton() != e.BUTTON1 || e.getClickCount() < 2) return;
123 DbInstrumentInfo infos[];
124 infos = instrumentsDbTable.getSelectedInstruments();
125 if(infos.length == 0) return;
126 onOk();
127 }
128 });
129
130 installKeyboardListeners();
131
132 setMinimumSize(getPreferredSize());
133 setResizable(true);
134 }
135
136 private void
137 installKeyboardListeners() {
138 getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put (
139 KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0),
140 "goUp"
141 );
142
143 getRootPane().getActionMap().put ("goUp", instrumentsDbTree.actionGoUp);
144
145 instrumentsDbTable.getInputMap().put (
146 KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
147 "Select"
148 );
149
150 instrumentsDbTable.getActionMap().put ("Select", new AbstractAction() {
151 public void
152 actionPerformed(ActionEvent e) {
153 DbDirectoryTreeNode node;
154 node = instrumentsDbTable.getSelectedDirectoryNode();
155 if(node != null) {
156 instrumentsDbTree.setSelectedDirectoryNode(node);
157 return;
158 }
159
160 if(instrumentsDbTable.getSelectedInstruments().length > 0) {
161 onOk();
162 }
163
164 }
165 });
166 }
167
168 protected JSInstrumentsDbTree
169 createInstrumentsDbTree(InstrumentsDbTreeModel m) { return new JSInstrumentsDbTree(m); }
170
171 public String
172 getSelectedInstrument() {
173 DbInstrumentInfo[] infos = instrumentsDbTable.getSelectedInstruments();
174 if(infos.length == 0) return null;
175 return infos[0].getInstrumentPath();
176 }
177
178 public void
179 setSelectedInstrument(String instr) {
180 String parentDir = Parser.getParentDirectory(instr);
181 if(parentDir == null) return;
182 instrumentsDbTree.setSelectedDirectory(parentDir);
183 if(instrumentsDbTable.getParentDirectoryNode() == null) return;
184
185 instr = instr.substring(parentDir.length() + 1, instr.length());
186 instrumentsDbTable.setSelectedInstrument(instr);
187 }
188
189 public void
190 setSelectedDirectory(String dir) {
191 instrumentsDbTree.setSelectedDirectory(dir);
192 }
193
194 protected void
195 onOk() {
196 if(!btnOk.isEnabled()) return;
197
198 instrumentsDbTable.saveColumnsVisibleState();
199 instrumentsDbTable.saveColumnWidths();
200
201 setCancelled(false);
202 setVisible(false);
203 }
204
205 protected void
206 onCancel() { setVisible(false); }
207
208 public void
209 valueChanged(ListSelectionEvent e) {
210 boolean b = instrumentsDbTable.getSelectedInstruments().length > 0;
211 btnOk.setEnabled(b);
212 }
213
214 protected JButton
215 createToolbarButton(Action a) { return new JButton(a); }
216
217 class ToolBar extends JToolBar {
218 protected final JButton btnGoUp = createToolbarButton(instrumentsDbTree.actionGoUp);
219 protected final JButton btnGoBack = createToolbarButton(instrumentsDbTree.actionGoBack);
220 protected final JButton btnGoForward = createToolbarButton(instrumentsDbTree.actionGoForward);
221 protected final JButton btnReload = createToolbarButton(instrumentsDbTable.reloadAction);
222 protected final JButton btnPreferences = createToolbarButton(null);
223
224 public ToolBar() {
225 super("");
226 setFloatable(false);
227
228 add(btnGoBack);
229 add(btnGoForward);
230 add(btnGoUp);
231
232 javax.swing.Icon i = SHF.getViewConfig().getBasicIconSet().getReload16Icon();
233 instrumentsDbTable.reloadAction.putValue(Action.SMALL_ICON, i);
234 add(btnReload);
235
236 addSeparator();
237
238 i = SHF.getViewConfig().getBasicIconSet().getPreferences16Icon();
239 btnPreferences.setIcon(i);
240 add(btnPreferences);
241
242 btnPreferences.addActionListener(new ActionListener() {
243 public void
244 actionPerformed(ActionEvent e) {
245 new PreferencesDlg().setVisible(true);
246 }
247 });
248 }
249 }
250
251 class PreferencesDlg extends JSInstrumentsDbColumnPreferencesDlg {
252 PreferencesDlg() {
253 super(JSDbInstrumentChooser.this, instrumentsDbTable);
254 }
255 }
256 }

  ViewVC Help
Powered by ViewVC