1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2010 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.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 |
import java.io.File; |
34 |
|
35 |
import javax.swing.JButton; |
36 |
import javax.swing.JComboBox; |
37 |
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 |
import net.sf.juife.event.TaskEvent; |
47 |
import net.sf.juife.event.TaskListener; |
48 |
|
49 |
import org.jsampler.CC; |
50 |
import org.jsampler.OrchestraInstrument; |
51 |
import org.jsampler.JSPrefs; |
52 |
|
53 |
import org.jsampler.task.Global; |
54 |
|
55 |
import org.linuxsampler.lscp.Instrument; |
56 |
|
57 |
import static org.jsampler.view.std.StdI18n.i18n; |
58 |
import static org.linuxsampler.lscp.Parser.*; |
59 |
|
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 |
private final JComboBox cbPath = new JComboBox(); |
77 |
private final JComboBox cbIndex = new JComboBox(); |
78 |
|
79 |
private OrchestraInstrument instrument; |
80 |
|
81 |
|
82 |
/** |
83 |
* Creates a new instance of <code>JSAddOrEditInstrumentDlg</code>. |
84 |
*/ |
85 |
public JSAddOrEditInstrumentDlg() { this(new OrchestraInstrument()); } |
86 |
|
87 |
/** |
88 |
* Creates a new instance of <code>JSAddOrEditInstrumentDlg</code>. |
89 |
* |
90 |
* @param instr The instrument to modify. |
91 |
*/ |
92 |
public JSAddOrEditInstrumentDlg(OrchestraInstrument instr) { |
93 |
super(CC.getMainFrame(), i18n.getLabel("JSAddOrEditInstrumentDlg.title")); |
94 |
setResizable(true); |
95 |
|
96 |
instrument = instr; |
97 |
|
98 |
cbPath.setEditable(true); |
99 |
String[] files = preferences().getStringListProperty("recentInstrumentFiles"); |
100 |
for(String s : files) cbPath.addItem(s); |
101 |
cbPath.setSelectedItem(null); |
102 |
|
103 |
cbPath.setMinimumSize ( |
104 |
new Dimension(200, cbPath.getMinimumSize().height) |
105 |
); |
106 |
|
107 |
cbPath.setPreferredSize ( |
108 |
new Dimension(200, cbPath.getPreferredSize().height) |
109 |
); |
110 |
|
111 |
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 |
gridbag.setConstraints(cbPath, c); |
165 |
p.add(cbPath); |
166 |
|
167 |
for(int i = 0; i < 101; i++) cbIndex.addItem(i); |
168 |
|
169 |
c.gridx = 1; |
170 |
c.gridy = 3; |
171 |
c.gridwidth = 2; |
172 |
gridbag.setConstraints(cbIndex, c); |
173 |
p.add(cbIndex); |
174 |
|
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 |
cbPath.addActionListener(new ActionListener() { |
187 |
public void |
188 |
actionPerformed(ActionEvent e) { |
189 |
updateState(); |
190 |
updateFileInstruments(); |
191 |
} |
192 |
}); |
193 |
|
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 |
|
203 |
updateInfo(); |
204 |
updateState(); |
205 |
|
206 |
setMinimumSize(getPreferredSize()); |
207 |
} |
208 |
|
209 |
protected JSPrefs |
210 |
preferences() { return CC.getViewConfig().preferences(); } |
211 |
|
212 |
private void |
213 |
updateInfo() { |
214 |
tfName.setText(getInstrument().getName()); |
215 |
tfDesc.setText(getInstrument().getDescription()); |
216 |
cbPath.setSelectedItem(getInstrument().getFilePath()); |
217 |
cbIndex.setSelectedIndex(getInstrument().getInstrumentIndex()); |
218 |
} |
219 |
|
220 |
private void |
221 |
updateState() { |
222 |
boolean b = true; |
223 |
if(tfName.getText().length() == 0) b = false; |
224 |
Object o = cbPath.getSelectedItem(); |
225 |
if(o == null || o.toString().length() == 0) b = false; |
226 |
o = cbIndex.getSelectedItem(); |
227 |
if(o == null || o.toString().length() == 0) b = false; |
228 |
|
229 |
btnOk.setEnabled(b); |
230 |
} |
231 |
|
232 |
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 |
@Override |
269 |
protected void |
270 |
onOk() { |
271 |
if(!btnOk.isEnabled()) return; |
272 |
|
273 |
instrument.setName(tfName.getText()); |
274 |
instrument.setDescription(tfDesc.getText()); |
275 |
instrument.setFilePath(cbPath.getSelectedItem().toString()); |
276 |
int idx = cbIndex.getSelectedIndex(); |
277 |
instrument.setInstrumentIndex(idx); |
278 |
|
279 |
StdUtils.updateRecentElements("recentInstrumentFiles", instrument.getFilePath()); |
280 |
|
281 |
setVisible(false); |
282 |
setCancelled(false); |
283 |
} |
284 |
|
285 |
@Override |
286 |
protected void |
287 |
onCancel() { setVisible(false); } |
288 |
|
289 |
/** |
290 |
* Gets the created/modified orchestra. |
291 |
* @return The created/modified orchestra. |
292 |
*/ |
293 |
public OrchestraInstrument |
294 |
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 |
@Override |
305 |
public void |
306 |
insertUpdate(DocumentEvent e) { updateState(); } |
307 |
|
308 |
@Override |
309 |
public void |
310 |
removeUpdate(DocumentEvent e) { updateState(); } |
311 |
|
312 |
@Override |
313 |
public void |
314 |
changedUpdate(DocumentEvent e) { updateState(); } |
315 |
|
316 |
// ActionListener |
317 |
@Override |
318 |
public void |
319 |
actionPerformed(ActionEvent e) { |
320 |
File f = StdUtils.showOpenInstrumentFileChooser(JSAddOrEditInstrumentDlg.this); |
321 |
if(f == null) return; |
322 |
|
323 |
String path = f.getAbsolutePath(); |
324 |
if(java.io.File.separatorChar == '\\') { |
325 |
path = path.replace('\\', '/'); |
326 |
} |
327 |
path = toEscapedString(path); |
328 |
cbPath.setSelectedItem(path); |
329 |
} |
330 |
} |
331 |
} |