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