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