1 |
iliev |
1286 |
/* |
2 |
|
|
* JSampler - a java front-end for LinuxSampler |
3 |
|
|
* |
4 |
iliev |
2148 |
* Copyright (C) 2005-2010 Grigor Iliev <grigor@grigoriliev.com> |
5 |
iliev |
1286 |
* |
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.Dimension; |
26 |
|
|
import java.awt.GridBagConstraints; |
27 |
|
|
import java.awt.GridBagLayout; |
28 |
|
|
import java.awt.Insets; |
29 |
|
|
|
30 |
|
|
import java.awt.event.ActionEvent; |
31 |
|
|
import java.awt.event.ActionListener; |
32 |
|
|
|
33 |
iliev |
1871 |
import java.io.File; |
34 |
|
|
|
35 |
iliev |
1286 |
import javax.swing.JButton; |
36 |
iliev |
1308 |
import javax.swing.JComboBox; |
37 |
iliev |
1286 |
import javax.swing.JLabel; |
38 |
|
|
import javax.swing.JPanel; |
39 |
|
|
import javax.swing.JTextField; |
40 |
|
|
|
41 |
|
|
import javax.swing.event.DocumentEvent; |
42 |
|
|
import javax.swing.event.DocumentListener; |
43 |
|
|
|
44 |
|
|
import net.sf.juife.OkCancelDialog; |
45 |
|
|
|
46 |
iliev |
1540 |
import net.sf.juife.event.TaskEvent; |
47 |
|
|
import net.sf.juife.event.TaskListener; |
48 |
|
|
|
49 |
iliev |
1286 |
import org.jsampler.CC; |
50 |
iliev |
1540 |
import org.jsampler.OrchestraInstrument; |
51 |
iliev |
1304 |
import org.jsampler.JSPrefs; |
52 |
iliev |
1286 |
|
53 |
iliev |
1540 |
import org.jsampler.task.Global; |
54 |
|
|
|
55 |
|
|
import org.linuxsampler.lscp.Instrument; |
56 |
|
|
|
57 |
iliev |
1286 |
import static org.jsampler.view.std.StdI18n.i18n; |
58 |
iliev |
1352 |
import static org.linuxsampler.lscp.Parser.*; |
59 |
iliev |
1286 |
|
60 |
|
|
|
61 |
|
|
/** |
62 |
|
|
* |
63 |
|
|
* @author Grigor Iliev |
64 |
|
|
*/ |
65 |
|
|
public class JSAddOrEditInstrumentDlg extends OkCancelDialog { |
66 |
|
|
private final JLabel lName = new JLabel(i18n.getLabel("JSAddOrEditInstrumentDlg.lName")); |
67 |
|
|
private final JLabel lDesc = new JLabel(i18n.getLabel("JSAddOrEditInstrumentDlg.lDesc")); |
68 |
|
|
private final JLabel lPath = new JLabel(i18n.getLabel("JSAddOrEditInstrumentDlg.lPath")); |
69 |
|
|
private final JLabel lIndex = new JLabel(i18n.getLabel("JSAddOrEditInstrumentDlg.lIndex")); |
70 |
|
|
|
71 |
|
|
private final JButton btnBrowse = |
72 |
|
|
new JButton(i18n.getButtonLabel("browse")); |
73 |
|
|
|
74 |
|
|
private final JTextField tfName = new JTextField(); |
75 |
|
|
private final JTextField tfDesc = new JTextField(); |
76 |
iliev |
1308 |
private final JComboBox cbPath = new JComboBox(); |
77 |
iliev |
1540 |
private final JComboBox cbIndex = new JComboBox(); |
78 |
iliev |
1286 |
|
79 |
iliev |
1540 |
private OrchestraInstrument instrument; |
80 |
iliev |
1286 |
|
81 |
|
|
|
82 |
|
|
/** |
83 |
|
|
* Creates a new instance of <code>JSAddOrEditInstrumentDlg</code>. |
84 |
|
|
*/ |
85 |
iliev |
1540 |
public JSAddOrEditInstrumentDlg() { this(new OrchestraInstrument()); } |
86 |
iliev |
1286 |
|
87 |
|
|
/** |
88 |
|
|
* Creates a new instance of <code>JSAddOrEditInstrumentDlg</code>. |
89 |
|
|
* |
90 |
|
|
* @param instr The instrument to modify. |
91 |
|
|
*/ |
92 |
iliev |
1540 |
public JSAddOrEditInstrumentDlg(OrchestraInstrument instr) { |
93 |
iliev |
1286 |
super(CC.getMainFrame(), i18n.getLabel("JSAddOrEditInstrumentDlg.title")); |
94 |
iliev |
2148 |
setResizable(true); |
95 |
iliev |
1286 |
|
96 |
|
|
instrument = instr; |
97 |
|
|
|
98 |
iliev |
1308 |
cbPath.setEditable(true); |
99 |
|
|
String[] files = preferences().getStringListProperty("recentInstrumentFiles"); |
100 |
|
|
for(String s : files) cbPath.addItem(s); |
101 |
|
|
cbPath.setSelectedItem(null); |
102 |
iliev |
2148 |
|
103 |
|
|
cbPath.setMinimumSize ( |
104 |
|
|
new Dimension(200, cbPath.getMinimumSize().height) |
105 |
|
|
); |
106 |
|
|
|
107 |
iliev |
1308 |
cbPath.setPreferredSize ( |
108 |
|
|
new Dimension(200, cbPath.getPreferredSize().height) |
109 |
|
|
); |
110 |
|
|
|
111 |
iliev |
1286 |
JPanel p = new JPanel(); |
112 |
|
|
|
113 |
|
|
GridBagLayout gridbag = new GridBagLayout(); |
114 |
|
|
GridBagConstraints c = new GridBagConstraints(); |
115 |
|
|
|
116 |
|
|
p.setLayout(gridbag); |
117 |
|
|
|
118 |
|
|
c.fill = GridBagConstraints.NONE; |
119 |
|
|
|
120 |
|
|
c.gridx = 0; |
121 |
|
|
c.gridy = 0; |
122 |
|
|
c.anchor = GridBagConstraints.EAST; |
123 |
|
|
c.insets = new Insets(3, 3, 3, 3); |
124 |
|
|
gridbag.setConstraints(lName, c); |
125 |
|
|
p.add(lName); |
126 |
|
|
|
127 |
|
|
c.gridx = 0; |
128 |
|
|
c.gridy = 1; |
129 |
|
|
gridbag.setConstraints(lDesc, c); |
130 |
|
|
p.add(lDesc); |
131 |
|
|
|
132 |
|
|
c.gridx = 0; |
133 |
|
|
c.gridy = 2; |
134 |
|
|
gridbag.setConstraints(lPath, c); |
135 |
|
|
p.add(lPath); |
136 |
|
|
|
137 |
|
|
c.gridx = 2; |
138 |
|
|
c.gridy = 2; |
139 |
|
|
gridbag.setConstraints(btnBrowse, c); |
140 |
|
|
p.add(btnBrowse); |
141 |
|
|
|
142 |
|
|
c.gridx = 0; |
143 |
|
|
c.gridy = 3; |
144 |
|
|
gridbag.setConstraints(lIndex, c); |
145 |
|
|
p.add(lIndex); |
146 |
|
|
|
147 |
|
|
c.fill = GridBagConstraints.HORIZONTAL; |
148 |
|
|
c.gridx = 1; |
149 |
|
|
c.gridy = 0; |
150 |
|
|
c.weightx = 1.0; |
151 |
|
|
c.gridwidth = 2; |
152 |
|
|
c.anchor = GridBagConstraints.WEST; |
153 |
|
|
gridbag.setConstraints(tfName, c); |
154 |
|
|
p.add(tfName); |
155 |
|
|
|
156 |
|
|
c.gridx = 1; |
157 |
|
|
c.gridy = 1; |
158 |
|
|
gridbag.setConstraints(tfDesc, c); |
159 |
|
|
p.add(tfDesc); |
160 |
|
|
|
161 |
|
|
c.gridx = 1; |
162 |
|
|
c.gridy = 2; |
163 |
|
|
c.gridwidth = 1; |
164 |
iliev |
1308 |
gridbag.setConstraints(cbPath, c); |
165 |
|
|
p.add(cbPath); |
166 |
iliev |
1286 |
|
167 |
iliev |
1540 |
for(int i = 0; i < 101; i++) cbIndex.addItem(i); |
168 |
|
|
|
169 |
iliev |
1286 |
c.gridx = 1; |
170 |
|
|
c.gridy = 3; |
171 |
|
|
c.gridwidth = 2; |
172 |
iliev |
1540 |
gridbag.setConstraints(cbIndex, c); |
173 |
|
|
p.add(cbIndex); |
174 |
iliev |
1286 |
|
175 |
|
|
setMainPane(p); |
176 |
|
|
|
177 |
|
|
int w = getPreferredSize().width; |
178 |
|
|
Dimension d = new Dimension(w > 500 ? w : 500, getPreferredSize().height); |
179 |
|
|
setPreferredSize(d); |
180 |
|
|
pack(); |
181 |
|
|
|
182 |
|
|
setLocationRelativeTo(getOwner()); |
183 |
|
|
|
184 |
|
|
tfName.getDocument().addDocumentListener(getHandler()); |
185 |
|
|
btnBrowse.addActionListener(getHandler()); |
186 |
iliev |
1308 |
cbPath.addActionListener(new ActionListener() { |
187 |
|
|
public void |
188 |
iliev |
1540 |
actionPerformed(ActionEvent e) { |
189 |
|
|
updateState(); |
190 |
|
|
updateFileInstruments(); |
191 |
|
|
} |
192 |
iliev |
1308 |
}); |
193 |
iliev |
1871 |
|
194 |
|
|
cbIndex.addActionListener(new ActionListener() { |
195 |
|
|
public void |
196 |
|
|
actionPerformed(ActionEvent e) { |
197 |
|
|
Object o = cbIndex.getSelectedItem(); |
198 |
|
|
if(o == null) return; |
199 |
|
|
if(o.toString().length() > 5) tfName.setText(o.toString()); |
200 |
|
|
} |
201 |
|
|
}); |
202 |
iliev |
1286 |
|
203 |
|
|
updateInfo(); |
204 |
|
|
updateState(); |
205 |
iliev |
2148 |
|
206 |
|
|
setMinimumSize(getPreferredSize()); |
207 |
iliev |
1286 |
} |
208 |
|
|
|
209 |
iliev |
1304 |
protected JSPrefs |
210 |
|
|
preferences() { return CC.getViewConfig().preferences(); } |
211 |
|
|
|
212 |
iliev |
1286 |
private void |
213 |
|
|
updateInfo() { |
214 |
|
|
tfName.setText(getInstrument().getName()); |
215 |
|
|
tfDesc.setText(getInstrument().getDescription()); |
216 |
iliev |
1540 |
cbPath.setSelectedItem(getInstrument().getFilePath()); |
217 |
|
|
cbIndex.setSelectedIndex(getInstrument().getInstrumentIndex()); |
218 |
iliev |
1286 |
} |
219 |
|
|
|
220 |
|
|
private void |
221 |
|
|
updateState() { |
222 |
|
|
boolean b = true; |
223 |
|
|
if(tfName.getText().length() == 0) b = false; |
224 |
iliev |
1308 |
Object o = cbPath.getSelectedItem(); |
225 |
|
|
if(o == null || o.toString().length() == 0) b = false; |
226 |
iliev |
1540 |
o = cbIndex.getSelectedItem(); |
227 |
|
|
if(o == null || o.toString().length() == 0) b = false; |
228 |
iliev |
1286 |
|
229 |
|
|
btnOk.setEnabled(b); |
230 |
|
|
} |
231 |
|
|
|
232 |
iliev |
1540 |
private boolean init = true; |
233 |
|
|
|
234 |
|
|
private void |
235 |
|
|
updateFileInstruments() { |
236 |
|
|
Object o = cbPath.getSelectedItem(); |
237 |
|
|
if(o == null) return; |
238 |
|
|
String s = o.toString(); |
239 |
|
|
final Global.GetFileInstruments t = new Global.GetFileInstruments(s); |
240 |
|
|
|
241 |
|
|
t.addTaskListener(new TaskListener() { |
242 |
|
|
public void |
243 |
|
|
taskPerformed(TaskEvent e) { |
244 |
|
|
Instrument[] instrs = t.getResult(); |
245 |
|
|
if(instrs == null) { |
246 |
|
|
cbIndex.removeAllItems(); |
247 |
|
|
for(int i = 0; i < 101; i++) cbIndex.addItem(i); |
248 |
|
|
return; |
249 |
|
|
} else { |
250 |
|
|
|
251 |
|
|
cbIndex.removeAllItems(); |
252 |
|
|
for(int i = 0; i < instrs.length; i++) { |
253 |
|
|
cbIndex.addItem(i + " - " + instrs[i].getName()); |
254 |
|
|
} |
255 |
|
|
} |
256 |
|
|
|
257 |
|
|
if(init) { |
258 |
|
|
int i = getInstrument().getInstrumentIndex(); |
259 |
|
|
cbIndex.setSelectedIndex(i); |
260 |
|
|
init = false; |
261 |
|
|
} |
262 |
|
|
} |
263 |
|
|
}); |
264 |
|
|
|
265 |
|
|
CC.getTaskQueue().add(t); |
266 |
|
|
} |
267 |
|
|
|
268 |
iliev |
1871 |
@Override |
269 |
iliev |
1286 |
protected void |
270 |
|
|
onOk() { |
271 |
|
|
if(!btnOk.isEnabled()) return; |
272 |
|
|
|
273 |
|
|
instrument.setName(tfName.getText()); |
274 |
|
|
instrument.setDescription(tfDesc.getText()); |
275 |
iliev |
1540 |
instrument.setFilePath(cbPath.getSelectedItem().toString()); |
276 |
|
|
int idx = cbIndex.getSelectedIndex(); |
277 |
iliev |
1286 |
instrument.setInstrumentIndex(idx); |
278 |
|
|
|
279 |
iliev |
1540 |
StdUtils.updateRecentElements("recentInstrumentFiles", instrument.getFilePath()); |
280 |
iliev |
1308 |
|
281 |
iliev |
1286 |
setVisible(false); |
282 |
|
|
setCancelled(false); |
283 |
|
|
} |
284 |
|
|
|
285 |
iliev |
1871 |
@Override |
286 |
iliev |
1286 |
protected void |
287 |
iliev |
1871 |
onCancel() { setVisible(false); } |
288 |
iliev |
1286 |
|
289 |
|
|
/** |
290 |
|
|
* Gets the created/modified orchestra. |
291 |
|
|
* @return The created/modified orchestra. |
292 |
|
|
*/ |
293 |
iliev |
1540 |
public OrchestraInstrument |
294 |
iliev |
1286 |
getInstrument() { return instrument; } |
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 { |
303 |
|
|
// DocumentListener |
304 |
iliev |
1871 |
@Override |
305 |
iliev |
1286 |
public void |
306 |
|
|
insertUpdate(DocumentEvent e) { updateState(); } |
307 |
|
|
|
308 |
iliev |
1871 |
@Override |
309 |
iliev |
1286 |
public void |
310 |
|
|
removeUpdate(DocumentEvent e) { updateState(); } |
311 |
|
|
|
312 |
iliev |
1871 |
@Override |
313 |
iliev |
1286 |
public void |
314 |
|
|
changedUpdate(DocumentEvent e) { updateState(); } |
315 |
|
|
|
316 |
|
|
// ActionListener |
317 |
iliev |
1871 |
@Override |
318 |
iliev |
1286 |
public void |
319 |
|
|
actionPerformed(ActionEvent e) { |
320 |
iliev |
1871 |
File f = StdUtils.showOpenInstrumentFileChooser(JSAddOrEditInstrumentDlg.this); |
321 |
|
|
if(f == null) return; |
322 |
iliev |
1352 |
|
323 |
iliev |
1871 |
String path = f.getAbsolutePath(); |
324 |
iliev |
1480 |
if(java.io.File.separatorChar == '\\') { |
325 |
iliev |
1483 |
path = path.replace('\\', '/'); |
326 |
iliev |
1480 |
} |
327 |
|
|
path = toEscapedString(path); |
328 |
|
|
cbPath.setSelectedItem(path); |
329 |
iliev |
1286 |
} |
330 |
|
|
} |
331 |
|
|
} |