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