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