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.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 |
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 = new JComboBox(); |
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 |
|
75 |
private final JComboBox cbDest = new JComboBox(); |
76 |
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 |
p2.add(cbSource); |
109 |
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 |
p2.add(cbDest); |
149 |
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 |
cbSource.setEditable(true); |
176 |
String[] dirs = preferences().getStringListProperty("recentDirectories"); |
177 |
for(String dir : dirs) cbSource.addItem(dir); |
178 |
cbSource.setSelectedItem(null); |
179 |
|
180 |
cbSource.setPreferredSize ( |
181 |
new Dimension(200, cbSource.getPreferredSize().height) |
182 |
); |
183 |
|
184 |
cbSource.addActionListener(getHandler()); |
185 |
|
186 |
cbDest.setEditable(true); |
187 |
dirs = preferences().getStringListProperty("recentDbDirectories"); |
188 |
for(String dir : dirs) cbDest.addItem(dir); |
189 |
cbDest.setSelectedItem(dbDir); |
190 |
|
191 |
|
192 |
cbDest.setPreferredSize ( |
193 |
new Dimension(200, cbDest.getPreferredSize().height) |
194 |
); |
195 |
|
196 |
cbDest.addActionListener(getHandler()); |
197 |
|
198 |
btnBrowse.addActionListener(new ActionListener() { |
199 |
public void |
200 |
actionPerformed(ActionEvent e) { onBrowse(); } |
201 |
}); |
202 |
|
203 |
checkScanSubdirs.addItemListener(getHandler()); |
204 |
|
205 |
btnBrowseDb.addActionListener(new ActionListener() { |
206 |
public void |
207 |
actionPerformed(ActionEvent e) { onBrowseDb(); } |
208 |
}); |
209 |
} |
210 |
|
211 |
protected JComboBox |
212 |
createComboBox() { return new JComboBox(); } |
213 |
|
214 |
protected JSPrefs |
215 |
preferences() { return CC.getViewConfig().preferences(); } |
216 |
|
217 |
private void |
218 |
onBrowse() { |
219 |
String path = preferences().getStringProperty("lastInstrumentLocation"); |
220 |
JFileChooser fc = new JFileChooser(path); |
221 |
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
222 |
int result = fc.showOpenDialog(this); |
223 |
if(result != JFileChooser.APPROVE_OPTION) return; |
224 |
|
225 |
String s = toEscapedString(fc.getSelectedFile().getPath()); |
226 |
cbSource.setSelectedItem(s); |
227 |
path = fc.getCurrentDirectory().getAbsolutePath(); |
228 |
preferences().setStringProperty("lastInstrumentLocation", path); |
229 |
} |
230 |
|
231 |
private void |
232 |
onBrowseDb() { |
233 |
JSDbDirectoryChooser dlg; |
234 |
dlg = new JSDbDirectoryChooser(JSAddDbInstrumentsFromDirDlg.this); |
235 |
Object o = cbDest.getSelectedItem(); |
236 |
if(o != null && o.toString().length() > 0) dlg.setSelectedDirectory(o.toString()); |
237 |
dlg.setVisible(true); |
238 |
if(dlg.isCancelled()) return; |
239 |
cbDest.setSelectedItem(dlg.getSelectedDirectory()); |
240 |
} |
241 |
|
242 |
private void |
243 |
updateState() { |
244 |
Object o = cbSource.getSelectedItem(); |
245 |
Object o2 = cbDest.getSelectedItem(); |
246 |
boolean b = o != null && o.toString().length() > 0; |
247 |
b = b && o2 != null && o2.toString().length() > 0; |
248 |
btnOk.setEnabled(b); |
249 |
} |
250 |
|
251 |
protected void |
252 |
onOk() { |
253 |
if(!btnOk.isEnabled()) return; |
254 |
|
255 |
btnOk.setEnabled(false); |
256 |
String dbDir = cbDest.getSelectedItem().toString(); |
257 |
String fsDir = cbSource.getSelectedItem().toString(); |
258 |
boolean recursive = checkScanSubdirs.isSelected(); |
259 |
boolean flat = !checkFlat.isSelected(); |
260 |
if(recursive) runTask(new InstrumentsDb.AddInstruments(dbDir, fsDir, flat)); |
261 |
else runTask(new InstrumentsDb.AddInstrumentsNonrecursive(dbDir, fsDir)); |
262 |
|
263 |
StdUtils.updateRecentElements("recentDirectories", fsDir); |
264 |
StdUtils.updateRecentElements("recentDbDirectories", dbDir); |
265 |
} |
266 |
|
267 |
protected void |
268 |
onCancel() { setVisible(false); } |
269 |
|
270 |
private void |
271 |
runTask(final Task<Integer> t) { |
272 |
t.addTaskListener(new TaskListener() { |
273 |
public void |
274 |
taskPerformed(TaskEvent e) { |
275 |
updateState(); |
276 |
if(t.doneWithErrors()) return; |
277 |
showProgress(t.getResult()); |
278 |
} |
279 |
}); |
280 |
|
281 |
CC.getTaskQueue().add(t); |
282 |
} |
283 |
|
284 |
private void |
285 |
showProgress(int jobId) { |
286 |
final JSAddDbInstrumentsProgressDlg dlg; |
287 |
dlg = new JSAddDbInstrumentsProgressDlg(this, jobId); |
288 |
SwingUtilities.invokeLater(new Runnable() { |
289 |
public void |
290 |
run() { |
291 |
dlg.updateStatus(); |
292 |
dlg.setVisible(true); |
293 |
} |
294 |
}); |
295 |
} |
296 |
|
297 |
private final Handler eventHandler = new Handler(); |
298 |
|
299 |
private Handler |
300 |
getHandler() { return eventHandler; } |
301 |
|
302 |
private class Handler implements DocumentListener, ActionListener, ItemListener { |
303 |
// DocumentListener |
304 |
public void |
305 |
insertUpdate(DocumentEvent e) { updateState(); } |
306 |
|
307 |
public void |
308 |
removeUpdate(DocumentEvent e) { updateState(); } |
309 |
|
310 |
public void |
311 |
changedUpdate(DocumentEvent e) { updateState(); } |
312 |
/////// |
313 |
|
314 |
public void |
315 |
actionPerformed(ActionEvent e) { updateState(); } |
316 |
|
317 |
public void |
318 |
itemStateChanged(ItemEvent e) { |
319 |
checkFlat.setEnabled(checkScanSubdirs.isSelected()); |
320 |
} |
321 |
} |
322 |
} |