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