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