1 |
iliev |
1286 |
/* |
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 |
|
|
|
33 |
|
|
import javax.swing.BorderFactory; |
34 |
|
|
import javax.swing.Box; |
35 |
|
|
import javax.swing.BoxLayout; |
36 |
|
|
import javax.swing.ButtonGroup; |
37 |
|
|
import javax.swing.Icon; |
38 |
|
|
import javax.swing.JButton; |
39 |
|
|
import javax.swing.JFileChooser; |
40 |
|
|
import javax.swing.JPanel; |
41 |
|
|
import javax.swing.JRadioButton; |
42 |
|
|
import javax.swing.JSpinner; |
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 |
iliev |
1304 |
import org.jsampler.JSPrefs; |
58 |
iliev |
1286 |
import org.jsampler.task.InstrumentsDb; |
59 |
|
|
|
60 |
|
|
import static org.jsampler.view.std.StdI18n.i18n; |
61 |
|
|
|
62 |
|
|
/** |
63 |
|
|
* |
64 |
|
|
* @author Grigor Iliev |
65 |
|
|
*/ |
66 |
|
|
public class JSAddDbInstrumentsFromFileDlg extends OkCancelDialog { |
67 |
|
|
private final JRadioButton rbAllInstruments = |
68 |
|
|
new JRadioButton(i18n.getLabel("JSAddDbInstrumentsFromFileDlg.rbAllInstruments")); |
69 |
|
|
private final JRadioButton rbIndex = |
70 |
|
|
new JRadioButton(i18n.getLabel("JSAddDbInstrumentsFromFileDlg.rbIndex")); |
71 |
|
|
|
72 |
|
|
private final JTextField tfSource = new JTextField(); |
73 |
|
|
private final JSpinner spinnerIndex = new JSpinner(new SpinnerNumberModel(0, 0, 500, 1)); |
74 |
|
|
private JButton btnBrowse; |
75 |
|
|
private final JTextField tfDest = new JTextField(); |
76 |
|
|
private JButton btnBrowseDb; |
77 |
|
|
|
78 |
|
|
/** |
79 |
|
|
* Creates a new instance of <code>JSAddDbInstrumentsFromFileDlg</code> |
80 |
|
|
*/ |
81 |
|
|
public |
82 |
|
|
JSAddDbInstrumentsFromFileDlg(Frame owner, String dbDir, Icon iconBrowse) { |
83 |
|
|
super(owner, i18n.getLabel("JSAddDbInstrumentsFromFileDlg.title")); |
84 |
|
|
|
85 |
|
|
initAddDbInstrumentsFromFileDlg(dbDir, iconBrowse); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
/** |
89 |
|
|
* Creates a new instance of <code>JSAddDbInstrumentsFromFileDlg</code> |
90 |
|
|
*/ |
91 |
|
|
public |
92 |
|
|
JSAddDbInstrumentsFromFileDlg(Dialog owner, String dbDir, Icon iconBrowse) { |
93 |
|
|
super(owner, i18n.getLabel("JSAddDbInstrumentsFromFileDlg.title")); |
94 |
|
|
|
95 |
|
|
initAddDbInstrumentsFromFileDlg(dbDir, iconBrowse); |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
private void |
99 |
|
|
initAddDbInstrumentsFromFileDlg(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 |
|
|
String s = i18n.getButtonLabel("browse"); |
111 |
|
|
btnBrowse.setMargin(new Insets(0, 0, 0, 0)); |
112 |
|
|
btnBrowse.setToolTipText(s); |
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(rbAllInstruments); |
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(rbIndex); |
130 |
|
|
p2.add(Box.createRigidArea(new Dimension(6, 0))); |
131 |
|
|
p2.add(spinnerIndex); |
132 |
|
|
p2.setBorder(BorderFactory.createEmptyBorder(0, 3, 3, 3)); |
133 |
|
|
p2.setMaximumSize(new Dimension(Short.MAX_VALUE, p2.getPreferredSize().height)); |
134 |
|
|
p2.setAlignmentX(LEFT_ALIGNMENT); |
135 |
|
|
|
136 |
|
|
p.add(p2); |
137 |
|
|
|
138 |
|
|
s = i18n.getLabel("JSAddDbInstrumentsFromFileDlg.source"); |
139 |
|
|
p.setBorder(BorderFactory.createTitledBorder(s)); |
140 |
|
|
|
141 |
|
|
JPanel mainPane = new JPanel(); |
142 |
|
|
mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS)); |
143 |
|
|
|
144 |
|
|
mainPane.add(p); |
145 |
|
|
mainPane.add(Box.createRigidArea(new Dimension(0, 6))); |
146 |
|
|
|
147 |
|
|
p = new JPanel(); |
148 |
|
|
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
149 |
|
|
|
150 |
|
|
p2 = new JPanel(); |
151 |
|
|
p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS)); |
152 |
|
|
p2.add(tfDest); |
153 |
|
|
p2.add(Box.createRigidArea(new Dimension(6, 0))); |
154 |
|
|
s = i18n.getButtonLabel("browse"); |
155 |
|
|
btnBrowseDb.setMargin(new Insets(0, 0, 0, 0)); |
156 |
|
|
btnBrowseDb.setToolTipText(s); |
157 |
|
|
p2.add(btnBrowseDb); |
158 |
|
|
p2.setBorder(BorderFactory.createEmptyBorder(0, 3, 3, 3)); |
159 |
|
|
p2.setMaximumSize(new Dimension(Short.MAX_VALUE, p2.getPreferredSize().height)); |
160 |
|
|
p2.setAlignmentX(LEFT_ALIGNMENT); |
161 |
|
|
|
162 |
|
|
p.add(p2); |
163 |
|
|
|
164 |
|
|
s = i18n.getLabel("JSAddDbInstrumentsFromFileDlg.dest"); |
165 |
|
|
p.setBorder(BorderFactory.createTitledBorder(s)); |
166 |
|
|
|
167 |
|
|
mainPane.add(p); |
168 |
|
|
Dimension d = mainPane.getPreferredSize(); |
169 |
|
|
mainPane.setPreferredSize(new Dimension(d.width > 300 ? d.width : 300, d.height)); |
170 |
|
|
|
171 |
|
|
setMainPane(mainPane); |
172 |
|
|
|
173 |
|
|
setMinimumSize(this.getPreferredSize()); |
174 |
|
|
setResizable(true); |
175 |
|
|
|
176 |
|
|
if(dbDir != null) tfDest.setText(dbDir); |
177 |
|
|
|
178 |
|
|
ButtonGroup group = new ButtonGroup(); |
179 |
|
|
group.add(rbAllInstruments); |
180 |
|
|
group.add(rbIndex); |
181 |
|
|
rbAllInstruments.doClick(0); |
182 |
|
|
|
183 |
|
|
btnOk.setEnabled(false); |
184 |
|
|
spinnerIndex.setEnabled(false); |
185 |
|
|
|
186 |
|
|
tfSource.getDocument().addDocumentListener(getHandler()); |
187 |
|
|
tfDest.getDocument().addDocumentListener(getHandler()); |
188 |
|
|
|
189 |
|
|
btnBrowse.addActionListener(new ActionListener() { |
190 |
|
|
public void |
191 |
|
|
actionPerformed(ActionEvent e) { onBrowse(); } |
192 |
|
|
}); |
193 |
|
|
|
194 |
|
|
rbAllInstruments.addActionListener(new ActionListener() { |
195 |
|
|
public void |
196 |
|
|
actionPerformed(ActionEvent e) { |
197 |
|
|
spinnerIndex.setEnabled(rbIndex.isSelected()); |
198 |
|
|
} |
199 |
|
|
}); |
200 |
|
|
|
201 |
|
|
rbIndex.addActionListener(new ActionListener() { |
202 |
|
|
public void |
203 |
|
|
actionPerformed(ActionEvent e) { |
204 |
|
|
spinnerIndex.setEnabled(rbIndex.isSelected()); |
205 |
|
|
} |
206 |
|
|
}); |
207 |
|
|
|
208 |
|
|
btnBrowseDb.addActionListener(new ActionListener() { |
209 |
|
|
public void |
210 |
|
|
actionPerformed(ActionEvent e) { |
211 |
|
|
JSDbDirectoryChooser dlg; |
212 |
|
|
dlg = new JSDbDirectoryChooser(JSAddDbInstrumentsFromFileDlg.this); |
213 |
|
|
String s = tfDest.getText(); |
214 |
|
|
if(s.length() > 0) dlg.setSelectedDirectory(s); |
215 |
|
|
dlg.setVisible(true); |
216 |
|
|
if(dlg.isCancelled()) return; |
217 |
|
|
tfDest.setText(dlg.getSelectedDirectory()); |
218 |
|
|
} |
219 |
|
|
}); |
220 |
|
|
} |
221 |
|
|
|
222 |
iliev |
1304 |
protected JSPrefs |
223 |
|
|
preferences() { return CC.getViewConfig().preferences(); } |
224 |
|
|
|
225 |
iliev |
1286 |
private void |
226 |
|
|
onBrowse() { |
227 |
iliev |
1304 |
String path = preferences().getStringProperty("lastInstrumentLocation"); |
228 |
|
|
JFileChooser fc = new JFileChooser(path); |
229 |
iliev |
1286 |
fc.setFileSelectionMode(JFileChooser.FILES_ONLY); |
230 |
|
|
int result = fc.showOpenDialog(this); |
231 |
|
|
if(result != JFileChooser.APPROVE_OPTION) return; |
232 |
|
|
|
233 |
|
|
tfSource.setText(fc.getSelectedFile().getPath()); |
234 |
iliev |
1304 |
path = fc.getCurrentDirectory().getAbsolutePath(); |
235 |
|
|
preferences().setStringProperty("lastInstrumentLocation", path); |
236 |
iliev |
1286 |
} |
237 |
|
|
|
238 |
|
|
private void |
239 |
|
|
updateState() { |
240 |
|
|
boolean b = tfSource.getText().length() != 0 && tfDest.getText().length() != 0; |
241 |
|
|
btnOk.setEnabled(b); |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
protected void |
245 |
|
|
onOk() { |
246 |
|
|
if(!btnOk.isEnabled()) return; |
247 |
|
|
|
248 |
|
|
btnOk.setEnabled(false); |
249 |
|
|
String dbDir = tfDest.getText(); |
250 |
|
|
String filePath = tfSource.getText(); |
251 |
|
|
int idx = -1; |
252 |
|
|
if(rbIndex.isSelected()) idx = Integer.parseInt(spinnerIndex.getValue().toString()); |
253 |
|
|
|
254 |
|
|
final Task<Integer> t = new InstrumentsDb.AddInstrumentsFromFile(dbDir, filePath, idx); |
255 |
|
|
t.addTaskListener(new TaskListener() { |
256 |
|
|
public void |
257 |
|
|
taskPerformed(TaskEvent e) { |
258 |
|
|
updateState(); |
259 |
|
|
if(t.doneWithErrors()) return; |
260 |
|
|
showProgress(t.getResult()); |
261 |
|
|
} |
262 |
|
|
}); |
263 |
|
|
|
264 |
|
|
CC.getTaskQueue().add(t); |
265 |
|
|
} |
266 |
|
|
|
267 |
|
|
private void |
268 |
|
|
showProgress(int jobId) { |
269 |
|
|
final JSAddDbInstrumentsProgressDlg dlg; |
270 |
|
|
dlg = new JSAddDbInstrumentsProgressDlg(this, jobId); |
271 |
|
|
SwingUtilities.invokeLater(new Runnable() { |
272 |
|
|
public void |
273 |
|
|
run() { dlg.setVisible(true); } |
274 |
|
|
}); |
275 |
|
|
} |
276 |
|
|
|
277 |
|
|
protected void |
278 |
|
|
onCancel() { setVisible(false); } |
279 |
|
|
|
280 |
|
|
private final Handler eventHandler = new Handler(); |
281 |
|
|
|
282 |
|
|
private Handler |
283 |
|
|
getHandler() { return eventHandler; } |
284 |
|
|
|
285 |
|
|
private class Handler implements DocumentListener { |
286 |
|
|
// DocumentListener |
287 |
|
|
public void |
288 |
|
|
insertUpdate(DocumentEvent e) { updateState(); } |
289 |
|
|
|
290 |
|
|
public void |
291 |
|
|
removeUpdate(DocumentEvent e) { updateState(); } |
292 |
|
|
|
293 |
|
|
public void |
294 |
|
|
changedUpdate(DocumentEvent e) { updateState(); } |
295 |
|
|
} |
296 |
|
|
} |