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