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.JFileChooser; |
42 |
import javax.swing.JPanel; |
43 |
import javax.swing.JTextField; |
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.task.InstrumentsDb; |
58 |
|
59 |
import static org.jsampler.view.std.StdI18n.i18n; |
60 |
|
61 |
|
62 |
/** |
63 |
* |
64 |
* @author Grigor Iliev |
65 |
*/ |
66 |
public class JSAddDbInstrumentsFromDirDlg extends OkCancelDialog { |
67 |
private final JTextField tfSource = new JTextField(); |
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 JTextField tfDest = new JTextField(); |
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(tfSource); |
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(tfDest); |
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 |
if(dbDir != null) tfDest.setText(dbDir); |
171 |
|
172 |
btnOk.setEnabled(false); |
173 |
checkScanSubdirs.doClick(0); |
174 |
checkFlat.doClick(0); |
175 |
|
176 |
tfSource.getDocument().addDocumentListener(getHandler()); |
177 |
tfDest.getDocument().addDocumentListener(getHandler()); |
178 |
|
179 |
btnBrowse.addActionListener(new ActionListener() { |
180 |
public void |
181 |
actionPerformed(ActionEvent e) { onBrowse(); } |
182 |
}); |
183 |
|
184 |
checkScanSubdirs.addItemListener(getHandler()); |
185 |
|
186 |
btnBrowseDb.addActionListener(new ActionListener() { |
187 |
public void |
188 |
actionPerformed(ActionEvent e) { |
189 |
JSDbDirectoryChooser dlg; |
190 |
dlg = new JSDbDirectoryChooser(JSAddDbInstrumentsFromDirDlg.this); |
191 |
String s = tfDest.getText(); |
192 |
if(s.length() > 0) dlg.setSelectedDirectory(s); |
193 |
dlg.setVisible(true); |
194 |
if(dlg.isCancelled()) return; |
195 |
tfDest.setText(dlg.getSelectedDirectory()); |
196 |
} |
197 |
}); |
198 |
} |
199 |
|
200 |
private void |
201 |
onBrowse() { |
202 |
JFileChooser fc = new JFileChooser(); |
203 |
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
204 |
int result = fc.showOpenDialog(this); |
205 |
if(result != JFileChooser.APPROVE_OPTION) return; |
206 |
|
207 |
tfSource.setText(fc.getSelectedFile().getPath()); |
208 |
} |
209 |
|
210 |
private void |
211 |
updateState() { |
212 |
boolean b = tfSource.getText().length() != 0 && tfDest.getText().length() != 0; |
213 |
btnOk.setEnabled(b); |
214 |
} |
215 |
|
216 |
protected void |
217 |
onOk() { |
218 |
if(!btnOk.isEnabled()) return; |
219 |
|
220 |
btnOk.setEnabled(false); |
221 |
String dbDir = tfDest.getText(); |
222 |
String fsDir = tfSource.getText(); |
223 |
boolean recursive = checkScanSubdirs.isSelected(); |
224 |
boolean flat = !checkFlat.isSelected(); |
225 |
if(recursive) runTask(new InstrumentsDb.AddInstruments(dbDir, fsDir, flat)); |
226 |
else runTask(new InstrumentsDb.AddInstrumentsNonrecursive(dbDir, fsDir)); |
227 |
|
228 |
//new JSAddDbInstrumentsProgressDlg(null).setVisible(true); |
229 |
} |
230 |
|
231 |
protected void |
232 |
onCancel() { setVisible(false); } |
233 |
|
234 |
private void |
235 |
runTask(final Task<Integer> t) { |
236 |
t.addTaskListener(new TaskListener() { |
237 |
public void |
238 |
taskPerformed(TaskEvent e) { |
239 |
updateState(); |
240 |
if(t.doneWithErrors()) return; |
241 |
showProgress(t.getResult()); |
242 |
} |
243 |
}); |
244 |
|
245 |
CC.getTaskQueue().add(t); |
246 |
} |
247 |
|
248 |
private void |
249 |
showProgress(int jobId) { |
250 |
final JSAddDbInstrumentsProgressDlg dlg; |
251 |
dlg = new JSAddDbInstrumentsProgressDlg(this, jobId); |
252 |
SwingUtilities.invokeLater(new Runnable() { |
253 |
public void |
254 |
run() { |
255 |
dlg.updateStatus(); |
256 |
dlg.setVisible(true); |
257 |
} |
258 |
}); |
259 |
} |
260 |
|
261 |
private final Handler eventHandler = new Handler(); |
262 |
|
263 |
private Handler |
264 |
getHandler() { return eventHandler; } |
265 |
|
266 |
private class Handler implements DocumentListener, ItemListener { |
267 |
// DocumentListener |
268 |
public void |
269 |
insertUpdate(DocumentEvent e) { updateState(); } |
270 |
|
271 |
public void |
272 |
removeUpdate(DocumentEvent e) { updateState(); } |
273 |
|
274 |
public void |
275 |
changedUpdate(DocumentEvent e) { updateState(); } |
276 |
|
277 |
public void |
278 |
itemStateChanged(ItemEvent e) { |
279 |
checkFlat.setEnabled(checkScanSubdirs.isSelected()); |
280 |
} |
281 |
} |
282 |
} |