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