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