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