1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2009 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 |
|
95 |
instrument = instr; |
96 |
|
97 |
cbPath.setEditable(true); |
98 |
String[] files = preferences().getStringListProperty("recentInstrumentFiles"); |
99 |
for(String s : files) cbPath.addItem(s); |
100 |
cbPath.setSelectedItem(null); |
101 |
|
102 |
cbPath.setPreferredSize ( |
103 |
new Dimension(200, cbPath.getPreferredSize().height) |
104 |
); |
105 |
|
106 |
JPanel p = new JPanel(); |
107 |
|
108 |
GridBagLayout gridbag = new GridBagLayout(); |
109 |
GridBagConstraints c = new GridBagConstraints(); |
110 |
|
111 |
p.setLayout(gridbag); |
112 |
|
113 |
c.fill = GridBagConstraints.NONE; |
114 |
|
115 |
c.gridx = 0; |
116 |
c.gridy = 0; |
117 |
c.anchor = GridBagConstraints.EAST; |
118 |
c.insets = new Insets(3, 3, 3, 3); |
119 |
gridbag.setConstraints(lName, c); |
120 |
p.add(lName); |
121 |
|
122 |
c.gridx = 0; |
123 |
c.gridy = 1; |
124 |
gridbag.setConstraints(lDesc, c); |
125 |
p.add(lDesc); |
126 |
|
127 |
c.gridx = 0; |
128 |
c.gridy = 2; |
129 |
gridbag.setConstraints(lPath, c); |
130 |
p.add(lPath); |
131 |
|
132 |
c.gridx = 2; |
133 |
c.gridy = 2; |
134 |
gridbag.setConstraints(btnBrowse, c); |
135 |
p.add(btnBrowse); |
136 |
|
137 |
c.gridx = 0; |
138 |
c.gridy = 3; |
139 |
gridbag.setConstraints(lIndex, c); |
140 |
p.add(lIndex); |
141 |
|
142 |
c.fill = GridBagConstraints.HORIZONTAL; |
143 |
c.gridx = 1; |
144 |
c.gridy = 0; |
145 |
c.weightx = 1.0; |
146 |
c.gridwidth = 2; |
147 |
c.anchor = GridBagConstraints.WEST; |
148 |
gridbag.setConstraints(tfName, c); |
149 |
p.add(tfName); |
150 |
|
151 |
c.gridx = 1; |
152 |
c.gridy = 1; |
153 |
gridbag.setConstraints(tfDesc, c); |
154 |
p.add(tfDesc); |
155 |
|
156 |
c.gridx = 1; |
157 |
c.gridy = 2; |
158 |
c.gridwidth = 1; |
159 |
gridbag.setConstraints(cbPath, c); |
160 |
p.add(cbPath); |
161 |
|
162 |
for(int i = 0; i < 101; i++) cbIndex.addItem(i); |
163 |
|
164 |
c.gridx = 1; |
165 |
c.gridy = 3; |
166 |
c.gridwidth = 2; |
167 |
gridbag.setConstraints(cbIndex, c); |
168 |
p.add(cbIndex); |
169 |
|
170 |
setMainPane(p); |
171 |
|
172 |
int w = getPreferredSize().width; |
173 |
Dimension d = new Dimension(w > 500 ? w : 500, getPreferredSize().height); |
174 |
setPreferredSize(d); |
175 |
pack(); |
176 |
|
177 |
setLocationRelativeTo(getOwner()); |
178 |
|
179 |
tfName.getDocument().addDocumentListener(getHandler()); |
180 |
btnBrowse.addActionListener(getHandler()); |
181 |
cbPath.addActionListener(new ActionListener() { |
182 |
public void |
183 |
actionPerformed(ActionEvent e) { |
184 |
updateState(); |
185 |
updateFileInstruments(); |
186 |
} |
187 |
}); |
188 |
|
189 |
cbIndex.addActionListener(new ActionListener() { |
190 |
public void |
191 |
actionPerformed(ActionEvent e) { |
192 |
Object o = cbIndex.getSelectedItem(); |
193 |
if(o == null) return; |
194 |
if(o.toString().length() > 5) tfName.setText(o.toString()); |
195 |
} |
196 |
}); |
197 |
|
198 |
updateInfo(); |
199 |
updateState(); |
200 |
} |
201 |
|
202 |
protected JSPrefs |
203 |
preferences() { return CC.getViewConfig().preferences(); } |
204 |
|
205 |
private void |
206 |
updateInfo() { |
207 |
tfName.setText(getInstrument().getName()); |
208 |
tfDesc.setText(getInstrument().getDescription()); |
209 |
cbPath.setSelectedItem(getInstrument().getFilePath()); |
210 |
cbIndex.setSelectedIndex(getInstrument().getInstrumentIndex()); |
211 |
} |
212 |
|
213 |
private void |
214 |
updateState() { |
215 |
boolean b = true; |
216 |
if(tfName.getText().length() == 0) b = false; |
217 |
Object o = cbPath.getSelectedItem(); |
218 |
if(o == null || o.toString().length() == 0) b = false; |
219 |
o = cbIndex.getSelectedItem(); |
220 |
if(o == null || o.toString().length() == 0) b = false; |
221 |
|
222 |
btnOk.setEnabled(b); |
223 |
} |
224 |
|
225 |
private boolean init = true; |
226 |
|
227 |
private void |
228 |
updateFileInstruments() { |
229 |
Object o = cbPath.getSelectedItem(); |
230 |
if(o == null) return; |
231 |
String s = o.toString(); |
232 |
final Global.GetFileInstruments t = new Global.GetFileInstruments(s); |
233 |
|
234 |
t.addTaskListener(new TaskListener() { |
235 |
public void |
236 |
taskPerformed(TaskEvent e) { |
237 |
Instrument[] instrs = t.getResult(); |
238 |
if(instrs == null) { |
239 |
cbIndex.removeAllItems(); |
240 |
for(int i = 0; i < 101; i++) cbIndex.addItem(i); |
241 |
return; |
242 |
} else { |
243 |
|
244 |
cbIndex.removeAllItems(); |
245 |
for(int i = 0; i < instrs.length; i++) { |
246 |
cbIndex.addItem(i + " - " + instrs[i].getName()); |
247 |
} |
248 |
} |
249 |
|
250 |
if(init) { |
251 |
int i = getInstrument().getInstrumentIndex(); |
252 |
cbIndex.setSelectedIndex(i); |
253 |
init = false; |
254 |
} |
255 |
} |
256 |
}); |
257 |
|
258 |
CC.getTaskQueue().add(t); |
259 |
} |
260 |
|
261 |
@Override |
262 |
protected void |
263 |
onOk() { |
264 |
if(!btnOk.isEnabled()) return; |
265 |
|
266 |
instrument.setName(tfName.getText()); |
267 |
instrument.setDescription(tfDesc.getText()); |
268 |
instrument.setFilePath(cbPath.getSelectedItem().toString()); |
269 |
int idx = cbIndex.getSelectedIndex(); |
270 |
instrument.setInstrumentIndex(idx); |
271 |
|
272 |
StdUtils.updateRecentElements("recentInstrumentFiles", instrument.getFilePath()); |
273 |
|
274 |
setVisible(false); |
275 |
setCancelled(false); |
276 |
} |
277 |
|
278 |
@Override |
279 |
protected void |
280 |
onCancel() { setVisible(false); } |
281 |
|
282 |
/** |
283 |
* Gets the created/modified orchestra. |
284 |
* @return The created/modified orchestra. |
285 |
*/ |
286 |
public OrchestraInstrument |
287 |
getInstrument() { return instrument; } |
288 |
|
289 |
|
290 |
private final Handler eventHandler = new Handler(); |
291 |
|
292 |
private Handler |
293 |
getHandler() { return eventHandler; } |
294 |
|
295 |
private class Handler implements DocumentListener, ActionListener { |
296 |
// DocumentListener |
297 |
@Override |
298 |
public void |
299 |
insertUpdate(DocumentEvent e) { updateState(); } |
300 |
|
301 |
@Override |
302 |
public void |
303 |
removeUpdate(DocumentEvent e) { updateState(); } |
304 |
|
305 |
@Override |
306 |
public void |
307 |
changedUpdate(DocumentEvent e) { updateState(); } |
308 |
|
309 |
// ActionListener |
310 |
@Override |
311 |
public void |
312 |
actionPerformed(ActionEvent e) { |
313 |
File f = StdUtils.showOpenInstrumentFileChooser(JSAddOrEditInstrumentDlg.this); |
314 |
if(f == null) return; |
315 |
|
316 |
String path = f.getAbsolutePath(); |
317 |
if(java.io.File.separatorChar == '\\') { |
318 |
path = path.replace('\\', '/'); |
319 |
} |
320 |
path = toEscapedString(path); |
321 |
cbPath.setSelectedItem(path); |
322 |
} |
323 |
} |
324 |
} |