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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1540 - (hide annotations) (download)
Mon Dec 3 23:22:02 2007 UTC (16 years, 4 months ago) by iliev
File size: 9520 byte(s)
* Fantasia: by default the volume values are now shown in decibels
* Implemented support for retrieving instrument information
  from instrument files
* Some bugfixes and enhancements

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.Dialog;
26     import java.awt.Dimension;
27     import java.awt.Frame;
28     import java.awt.Insets;
29    
30     import java.awt.event.ActionEvent;
31     import java.awt.event.ActionListener;
32     import java.awt.event.ItemEvent;
33     import java.awt.event.ItemListener;
34    
35     import javax.swing.BorderFactory;
36     import javax.swing.Box;
37     import javax.swing.BoxLayout;
38     import javax.swing.Icon;
39     import javax.swing.JButton;
40     import javax.swing.JCheckBox;
41 iliev 1308 import javax.swing.JComboBox;
42 iliev 1286 import javax.swing.JFileChooser;
43     import javax.swing.JPanel;
44     import javax.swing.SpinnerNumberModel;
45     import javax.swing.SwingUtilities;
46    
47     import javax.swing.event.DocumentEvent;
48     import javax.swing.event.DocumentListener;
49    
50     import net.sf.juife.OkCancelDialog;
51     import net.sf.juife.Task;
52    
53     import net.sf.juife.event.TaskEvent;
54     import net.sf.juife.event.TaskListener;
55    
56     import org.jsampler.CC;
57 iliev 1304 import org.jsampler.JSPrefs;
58 iliev 1286 import org.jsampler.task.InstrumentsDb;
59    
60     import static org.jsampler.view.std.StdI18n.i18n;
61 iliev 1352 import static org.linuxsampler.lscp.Parser.*;
62 iliev 1286
63     /**
64     *
65     * @author Grigor Iliev
66     */
67     public class JSAddDbInstrumentsFromDirDlg extends OkCancelDialog {
68 iliev 1540 private final JComboBox cbSource = StdUtils.createPathComboBox();
69 iliev 1286 private JButton btnBrowse;
70     private final JCheckBox checkScanSubdirs =
71     new JCheckBox(i18n.getLabel("JSAddDbInstrumentsFromDirDlg.checkScanSubdirs"));
72     private final JCheckBox checkFlat =
73     new JCheckBox(i18n.getLabel("JSAddDbInstrumentsFromDirDlg.checkFlat"));
74    
75 iliev 1540 private final JComboBox cbDest = StdUtils.createPathComboBox();
76 iliev 1286 private JButton btnBrowseDb;
77    
78     /**
79     * Creates a new instance of <code>AddDbInstrumentsFromFileDlg</code>
80     */
81     public
82     JSAddDbInstrumentsFromDirDlg(Frame owner, String dbDir, Icon iconBrowse) {
83     super(owner, i18n.getLabel("JSAddDbInstrumentsFromDirDlg.title"));
84    
85     initAddDbInstrumentsFromDirDlg(dbDir, iconBrowse);
86     }
87    
88     /**
89     * Creates a new instance of <code>AddDbInstrumentsFromFileDlg</code>
90     */
91     public
92     JSAddDbInstrumentsFromDirDlg(Dialog owner, String dbDir, Icon iconBrowse) {
93     super(owner, i18n.getLabel("JSAddDbInstrumentsFromDirDlg.title"));
94    
95     initAddDbInstrumentsFromDirDlg(dbDir, iconBrowse);
96     }
97    
98     private void
99     initAddDbInstrumentsFromDirDlg(String dbDir, Icon iconBrowse) {
100     btnBrowse = new JButton(iconBrowse);
101     btnBrowseDb = new JButton(iconBrowse);
102    
103     JPanel p = new JPanel();
104     p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
105    
106     JPanel p2 = new JPanel();
107     p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS));
108 iliev 1308 p2.add(cbSource);
109 iliev 1286 p2.add(Box.createRigidArea(new Dimension(6, 0)));
110     btnBrowse.setToolTipText(i18n.getButtonLabel("browse"));
111     btnBrowse.setMargin(new Insets(0, 0, 0, 0));
112     p2.add(btnBrowse);
113     p2.setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
114     p2.setMaximumSize(new Dimension(Short.MAX_VALUE, p2.getPreferredSize().height));
115     p2.setAlignmentX(LEFT_ALIGNMENT);
116    
117     p.add(p2);
118    
119     p2 = new JPanel();
120     p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS));
121     p2.add(checkScanSubdirs);
122     p2.setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
123     p2.setAlignmentX(LEFT_ALIGNMENT);
124     p.add(p2);
125    
126     p2 = new JPanel();
127     p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS));
128     p2.add(checkFlat);
129     p2.setBorder(BorderFactory.createEmptyBorder(0, 15, 3, 3));
130     p2.setAlignmentX(LEFT_ALIGNMENT);
131    
132     p.add(p2);
133    
134     String s = i18n.getLabel("JSAddDbInstrumentsFromDirDlg.source");
135     p.setBorder(BorderFactory.createTitledBorder(s));
136    
137     JPanel mainPane = new JPanel();
138     mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
139    
140     mainPane.add(p);
141     mainPane.add(Box.createRigidArea(new Dimension(0, 6)));
142    
143     p = new JPanel();
144     p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
145    
146     p2 = new JPanel();
147     p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS));
148 iliev 1308 p2.add(cbDest);
149 iliev 1286 p2.add(Box.createRigidArea(new Dimension(6, 0)));
150     btnBrowseDb.setToolTipText(i18n.getButtonLabel("browse"));
151     btnBrowseDb.setMargin(new Insets(0, 0, 0, 0));
152     p2.add(btnBrowseDb);
153     p2.setBorder(BorderFactory.createEmptyBorder(0, 3, 3, 3));
154     p2.setMaximumSize(new Dimension(Short.MAX_VALUE, p2.getPreferredSize().height));
155     p2.setAlignmentX(LEFT_ALIGNMENT);
156    
157     p.add(p2);
158    
159     s = i18n.getLabel("JSAddDbInstrumentsFromDirDlg.dest");
160     p.setBorder(BorderFactory.createTitledBorder(s));
161    
162     mainPane.add(p);
163     Dimension d = mainPane.getPreferredSize();
164     mainPane.setPreferredSize(new Dimension(d.width > 300 ? d.width : 300, d.height));
165    
166     setMainPane(mainPane);
167    
168     setMinimumSize(this.getPreferredSize());
169     setResizable(true);
170    
171     btnOk.setEnabled(false);
172     checkScanSubdirs.doClick(0);
173     checkFlat.doClick(0);
174    
175 iliev 1308 String[] dirs = preferences().getStringListProperty("recentDirectories");
176     for(String dir : dirs) cbSource.addItem(dir);
177     cbSource.setSelectedItem(null);
178 iliev 1286
179 iliev 1308 cbSource.setPreferredSize (
180     new Dimension(200, cbSource.getPreferredSize().height)
181     );
182    
183     cbSource.addActionListener(getHandler());
184    
185     dirs = preferences().getStringListProperty("recentDbDirectories");
186     for(String dir : dirs) cbDest.addItem(dir);
187     cbDest.setSelectedItem(dbDir);
188    
189    
190     cbDest.setPreferredSize (
191     new Dimension(200, cbDest.getPreferredSize().height)
192     );
193    
194     cbDest.addActionListener(getHandler());
195    
196 iliev 1286 btnBrowse.addActionListener(new ActionListener() {
197     public void
198     actionPerformed(ActionEvent e) { onBrowse(); }
199     });
200    
201     checkScanSubdirs.addItemListener(getHandler());
202    
203     btnBrowseDb.addActionListener(new ActionListener() {
204     public void
205 iliev 1308 actionPerformed(ActionEvent e) { onBrowseDb(); }
206 iliev 1286 });
207     }
208    
209 iliev 1357 protected JComboBox
210     createComboBox() { return new JComboBox(); }
211    
212 iliev 1304 protected JSPrefs
213     preferences() { return CC.getViewConfig().preferences(); }
214    
215 iliev 1286 private void
216     onBrowse() {
217 iliev 1304 String path = preferences().getStringProperty("lastInstrumentLocation");
218     JFileChooser fc = new JFileChooser(path);
219 iliev 1286 fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
220     int result = fc.showOpenDialog(this);
221     if(result != JFileChooser.APPROVE_OPTION) return;
222    
223 iliev 1480 path = fc.getSelectedFile().getAbsolutePath();
224     if(java.io.File.separatorChar == '\\') {
225 iliev 1483 path = path.replace('\\', '/');
226 iliev 1480 }
227     path = toEscapedString(path);
228     cbSource.setSelectedItem(path);
229 iliev 1304 path = fc.getCurrentDirectory().getAbsolutePath();
230     preferences().setStringProperty("lastInstrumentLocation", path);
231 iliev 1286 }
232    
233     private void
234 iliev 1308 onBrowseDb() {
235     JSDbDirectoryChooser dlg;
236     dlg = new JSDbDirectoryChooser(JSAddDbInstrumentsFromDirDlg.this);
237     Object o = cbDest.getSelectedItem();
238     if(o != null && o.toString().length() > 0) dlg.setSelectedDirectory(o.toString());
239     dlg.setVisible(true);
240     if(dlg.isCancelled()) return;
241     cbDest.setSelectedItem(dlg.getSelectedDirectory());
242     }
243    
244     private void
245 iliev 1286 updateState() {
246 iliev 1308 Object o = cbSource.getSelectedItem();
247     Object o2 = cbDest.getSelectedItem();
248     boolean b = o != null && o.toString().length() > 0;
249     b = b && o2 != null && o2.toString().length() > 0;
250 iliev 1286 btnOk.setEnabled(b);
251     }
252    
253     protected void
254     onOk() {
255     if(!btnOk.isEnabled()) return;
256    
257     btnOk.setEnabled(false);
258 iliev 1308 String dbDir = cbDest.getSelectedItem().toString();
259     String fsDir = cbSource.getSelectedItem().toString();
260 iliev 1286 boolean recursive = checkScanSubdirs.isSelected();
261     boolean flat = !checkFlat.isSelected();
262     if(recursive) runTask(new InstrumentsDb.AddInstruments(dbDir, fsDir, flat));
263     else runTask(new InstrumentsDb.AddInstrumentsNonrecursive(dbDir, fsDir));
264    
265 iliev 1308 StdUtils.updateRecentElements("recentDirectories", fsDir);
266     StdUtils.updateRecentElements("recentDbDirectories", dbDir);
267 iliev 1286 }
268    
269     protected void
270     onCancel() { setVisible(false); }
271    
272     private void
273     runTask(final Task<Integer> t) {
274     t.addTaskListener(new TaskListener() {
275     public void
276     taskPerformed(TaskEvent e) {
277     updateState();
278     if(t.doneWithErrors()) return;
279     showProgress(t.getResult());
280     }
281     });
282    
283     CC.getTaskQueue().add(t);
284     }
285    
286     private void
287     showProgress(int jobId) {
288     final JSAddDbInstrumentsProgressDlg dlg;
289     dlg = new JSAddDbInstrumentsProgressDlg(this, jobId);
290     SwingUtilities.invokeLater(new Runnable() {
291     public void
292     run() {
293     dlg.updateStatus();
294     dlg.setVisible(true);
295     }
296     });
297     }
298    
299     private final Handler eventHandler = new Handler();
300    
301     private Handler
302     getHandler() { return eventHandler; }
303    
304 iliev 1308 private class Handler implements DocumentListener, ActionListener, ItemListener {
305 iliev 1286 // DocumentListener
306     public void
307     insertUpdate(DocumentEvent e) { updateState(); }
308    
309     public void
310     removeUpdate(DocumentEvent e) { updateState(); }
311    
312     public void
313     changedUpdate(DocumentEvent e) { updateState(); }
314 iliev 1308 ///////
315 iliev 1286
316     public void
317 iliev 1308 actionPerformed(ActionEvent e) { updateState(); }
318    
319     public void
320 iliev 1286 itemStateChanged(ItemEvent e) {
321     checkFlat.setEnabled(checkScanSubdirs.isSelected());
322     }
323     }
324     }

  ViewVC Help
Powered by ViewVC