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