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 |
|
27 |
import java.awt.event.ActionEvent; |
28 |
import java.awt.event.ActionListener; |
29 |
|
30 |
import javax.swing.AbstractAction; |
31 |
import javax.swing.Action; |
32 |
import javax.swing.BorderFactory; |
33 |
import javax.swing.Box; |
34 |
import javax.swing.BoxLayout; |
35 |
import javax.swing.JComboBox; |
36 |
import javax.swing.JPanel; |
37 |
import javax.swing.JScrollPane; |
38 |
import javax.swing.JToolBar; |
39 |
|
40 |
import javax.swing.event.TreeSelectionEvent; |
41 |
import javax.swing.event.TreeSelectionListener; |
42 |
|
43 |
import net.sf.juife.LinkButton; |
44 |
import net.sf.juife.NavigationPage; |
45 |
|
46 |
import org.jsampler.CC; |
47 |
import org.jsampler.MidiInstrument; |
48 |
import org.jsampler.MidiInstrumentMap; |
49 |
|
50 |
import org.jsampler.event.ListEvent; |
51 |
import org.jsampler.event.ListListener; |
52 |
import org.jsampler.event.MidiInstrumentMapEvent; |
53 |
import org.jsampler.event.MidiInstrumentMapListener; |
54 |
|
55 |
import org.linuxsampler.lscp.MidiInstrumentInfo; |
56 |
|
57 |
import static org.jsampler.view.classic.ClassicI18n.i18n; |
58 |
|
59 |
|
60 |
/** |
61 |
* |
62 |
* @author Grigor Iliev |
63 |
*/ |
64 |
public class MidiInstrumentMapsPage extends NavigationPage { |
65 |
private final JToolBar tbMaps = new JToolBar(); |
66 |
private final JToolBar tbInstruments = new JToolBar(); |
67 |
|
68 |
private final EditMap actionEditMap = new EditMap(); |
69 |
private final RemoveMap actionRemoveMap = new RemoveMap(); |
70 |
private final EditInstrument actionEditInstrument = new EditInstrument(); |
71 |
private final RemoveInstrument removeInstrument = new RemoveInstrument(); |
72 |
|
73 |
private final ToolbarButton btnAddMap = new ToolbarButton(A4n.addMidiInstrumentMap); |
74 |
private final ToolbarButton btnEditMap = new ToolbarButton(actionEditMap); |
75 |
private final ToolbarButton btnRemoveMap = new ToolbarButton(actionRemoveMap); |
76 |
private final ToolbarButton btnExportMaps = new ToolbarButton(A4n.exportMidiInstrumentMaps); |
77 |
//private final ToolbarButton btnCloseMapBar = new ToolbarButton(); |
78 |
|
79 |
private final ToolbarButton btnAddInstrument = |
80 |
new ToolbarButton(A4n.addMidiInstrumentWizard); |
81 |
|
82 |
private final ToolbarButton btnEditInstrument = new ToolbarButton(actionEditInstrument); |
83 |
private final ToolbarButton btnRemoveInstrument = new ToolbarButton(removeInstrument); |
84 |
//private final ToolbarButton btnCloseInstrumentBar = new ToolbarButton(); |
85 |
|
86 |
private final JComboBox cbMaps = new JComboBox(); |
87 |
private final MidiInstrumentTree midiInstrumentTree = new MidiInstrumentTree(); |
88 |
|
89 |
/** Creates a new instance of MidiInstrumentMapsPage */ |
90 |
public |
91 |
MidiInstrumentMapsPage() { |
92 |
setTitle(i18n.getLabel("MidiInstrumentMapsPage.title")); |
93 |
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
94 |
|
95 |
/*btnCloseMapBar.setIcon(Res.iconClose8); |
96 |
btnCloseMapBar.setMargin(new java.awt.Insets(0, 0, 0, 0)); |
97 |
btnCloseMapBar.addActionListener(new ActionListener() { |
98 |
public void |
99 |
actionPerformed(ActionEvent e) { |
100 |
tbMaps.setVisible(false); |
101 |
validate(); |
102 |
repaint(); |
103 |
} |
104 |
}); |
105 |
|
106 |
|
107 |
btnCloseInstrumentBar.setIcon(Res.iconClose8); |
108 |
btnCloseInstrumentBar.setMargin(new java.awt.Insets(0, 0, 0, 0)); |
109 |
btnCloseInstrumentBar.addActionListener(new ActionListener() { |
110 |
public void |
111 |
actionPerformed(ActionEvent e) { |
112 |
tbInstruments.setVisible(false); |
113 |
validate(); |
114 |
repaint(); |
115 |
} |
116 |
});*/ |
117 |
|
118 |
|
119 |
tbMaps.add(btnAddMap); |
120 |
tbMaps.add(btnEditMap); |
121 |
tbMaps.add(btnRemoveMap); |
122 |
//tbMaps.addSeparator(); |
123 |
//tbMaps.add(btnExportMaps); |
124 |
|
125 |
/*tbMaps.add(Box.createHorizontalGlue()); |
126 |
tbMaps.add(btnCloseMapBar);*/ |
127 |
|
128 |
Dimension d = new Dimension(Short.MAX_VALUE, tbMaps.getPreferredSize().height); |
129 |
tbMaps.setMaximumSize(d); |
130 |
tbMaps.setFloatable(false); |
131 |
tbMaps.setAlignmentX(LEFT_ALIGNMENT); |
132 |
|
133 |
add(tbMaps); |
134 |
|
135 |
JPanel p = new JPanel(); |
136 |
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
137 |
p.setAlignmentX(LEFT_ALIGNMENT); |
138 |
p.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); |
139 |
|
140 |
cbMaps.addActionListener(new ActionListener() { |
141 |
public void |
142 |
actionPerformed(ActionEvent e) { mapChanged(); } |
143 |
}); |
144 |
|
145 |
for(MidiInstrumentMap m : CC.getSamplerModel().getMidiInstrumentMaps()) { |
146 |
cbMaps.addItem(m); |
147 |
m.addMidiInstrumentMapListener(getHandler()); |
148 |
} |
149 |
|
150 |
cbMaps.setEnabled(cbMaps.getItemCount() > 0); |
151 |
|
152 |
d = new Dimension(Short.MAX_VALUE, cbMaps.getPreferredSize().height); |
153 |
cbMaps.setMaximumSize(d); |
154 |
|
155 |
CC.getSamplerModel().addMidiInstrumentMapListListener(getHandler()); |
156 |
|
157 |
p.add(cbMaps); |
158 |
p.add(Box.createRigidArea(new Dimension(0, 12))); |
159 |
add(p); |
160 |
|
161 |
tbInstruments.add(btnAddInstrument); |
162 |
tbInstruments.add(btnEditInstrument); |
163 |
tbInstruments.add(btnRemoveInstrument); |
164 |
|
165 |
/*tbInstruments.add(Box.createHorizontalGlue()); |
166 |
tbInstruments.add(btnCloseInstrumentBar); */ |
167 |
|
168 |
d = new Dimension(Short.MAX_VALUE, tbInstruments.getPreferredSize().height); |
169 |
tbInstruments.setMaximumSize(d); |
170 |
tbInstruments.setFloatable(false); |
171 |
tbInstruments.setAlignmentX(LEFT_ALIGNMENT); |
172 |
|
173 |
add(tbInstruments); |
174 |
|
175 |
p = new JPanel(); |
176 |
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
177 |
p.setAlignmentX(LEFT_ALIGNMENT); |
178 |
p.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); |
179 |
|
180 |
p.add(new JScrollPane(midiInstrumentTree)); |
181 |
|
182 |
add(p); |
183 |
|
184 |
boolean b = cbMaps.getItemCount() != 0; |
185 |
actionEditMap.setEnabled(b); |
186 |
actionRemoveMap.setEnabled(b); |
187 |
A4n.removeMidiInstrumentMap.setEnabled(b); |
188 |
|
189 |
midiInstrumentTree.addTreeSelectionListener(getHandler()); |
190 |
removeInstrument.setEnabled(midiInstrumentTree.getSelectionCount() > 0); |
191 |
actionEditInstrument.setEnabled(midiInstrumentTree.getSelectedInstrument() != null); |
192 |
} |
193 |
|
194 |
private void |
195 |
mapChanged() { |
196 |
MidiInstrumentMap map = (MidiInstrumentMap)cbMaps.getSelectedItem(); |
197 |
midiInstrumentTree.setMidiInstrumentMap(map); |
198 |
|
199 |
boolean b = cbMaps.getItemCount() != 0; |
200 |
actionEditMap.setEnabled(b); |
201 |
actionRemoveMap.setEnabled(b); |
202 |
A4n.removeMidiInstrumentMap.setEnabled(b); |
203 |
} |
204 |
|
205 |
|
206 |
private class EditMap extends AbstractAction { |
207 |
EditMap() { |
208 |
super(i18n.getLabel("MidiInstrumentMapsPage.editMap")); |
209 |
|
210 |
String s = i18n.getLabel("MidiInstrumentMapsPage.editMap.tt"); |
211 |
putValue(SHORT_DESCRIPTION, s); |
212 |
putValue(Action.SMALL_ICON, Res.iconEdit16); |
213 |
} |
214 |
|
215 |
public void |
216 |
actionPerformed(ActionEvent e) { |
217 |
MidiInstrumentMap map = (MidiInstrumentMap)cbMaps.getSelectedItem(); |
218 |
int id = map.getMapId(); |
219 |
AddMidiInstrumentMapDlg dlg = new AddMidiInstrumentMapDlg(); |
220 |
dlg.setTitle(i18n.getLabel("MidiInstrumentMapsPage.editMap")); |
221 |
dlg.setMapName(map.getName()); |
222 |
dlg.setVisible(true); |
223 |
if(dlg.isCancelled()) return; |
224 |
|
225 |
map.setName(dlg.getMapName()); |
226 |
CC.getSamplerModel().setBackendMidiInstrumentMapName(id, dlg.getMapName()); |
227 |
} |
228 |
} |
229 |
|
230 |
private class RemoveMap extends AbstractAction { |
231 |
RemoveMap() { |
232 |
super(i18n.getMenuLabel("actions.midiInstruments.removeMap")); |
233 |
|
234 |
String s = i18n.getMenuLabel("actions.midiInstruments.removeMap.tt"); |
235 |
putValue(SHORT_DESCRIPTION, s); |
236 |
putValue(Action.SMALL_ICON, Res.iconDelete16); |
237 |
} |
238 |
|
239 |
public void |
240 |
actionPerformed(ActionEvent e) { |
241 |
MidiInstrumentMap map = (MidiInstrumentMap)cbMaps.getSelectedItem(); |
242 |
RemoveMidiInstrumentMapDlg dlg = new RemoveMidiInstrumentMapDlg(map); |
243 |
dlg.setVisible(true); |
244 |
if(dlg.isCancelled()) return; |
245 |
int id = dlg.getSelectedMap().getMapId(); |
246 |
CC.getSamplerModel().removeBackendMidiInstrumentMap(id); |
247 |
} |
248 |
} |
249 |
|
250 |
private class EditInstrument extends AbstractAction { |
251 |
EditInstrument() { |
252 |
super(i18n.getLabel("MidiInstrumentMapsPage.EditInstrument")); |
253 |
|
254 |
String s = i18n.getLabel("MidiInstrumentMapsPage.EditInstrument.tt"); |
255 |
putValue(SHORT_DESCRIPTION, s); |
256 |
putValue(Action.SMALL_ICON, Res.iconEdit16); |
257 |
} |
258 |
|
259 |
public void |
260 |
actionPerformed(ActionEvent e) { |
261 |
MidiInstrument i = midiInstrumentTree.getSelectedInstrument(); |
262 |
EditMidiInstrumentDlg dlg = new EditMidiInstrumentDlg(i.getInfo()); |
263 |
dlg.setVisible(true); |
264 |
|
265 |
if(dlg.isCancelled()) return; |
266 |
|
267 |
MidiInstrumentInfo info = dlg.getInstrument(); |
268 |
CC.getSamplerModel().mapBackendMidiInstrument ( |
269 |
info.getMapId(), info.getMidiBank(), info.getMidiProgram(), info |
270 |
); |
271 |
} |
272 |
} |
273 |
|
274 |
private class RemoveInstrument extends AbstractAction { |
275 |
RemoveInstrument() { |
276 |
String s = i18n.getLabel("MidiInstrumentMapsPage.RemoveInstrument.tt"); |
277 |
putValue(SHORT_DESCRIPTION, s); |
278 |
putValue(Action.SMALL_ICON, Res.iconDelete16); |
279 |
} |
280 |
|
281 |
public void |
282 |
actionPerformed(ActionEvent e) { |
283 |
midiInstrumentTree.removeSelectedInstrumentOrBank(); |
284 |
} |
285 |
} |
286 |
|
287 |
private final Handler handler = new Handler(); |
288 |
|
289 |
private Handler |
290 |
getHandler() { return handler; } |
291 |
|
292 |
private class Handler implements ListListener<MidiInstrumentMap>, |
293 |
MidiInstrumentMapListener, TreeSelectionListener { |
294 |
|
295 |
/** Invoked when a new map is added. */ |
296 |
public void |
297 |
entryAdded(ListEvent<MidiInstrumentMap> e) { |
298 |
MidiInstrumentMap map = e.getEntry(); |
299 |
if(cbMaps.getItemCount() == 0) cbMaps.setEnabled(true); |
300 |
cbMaps.addItem(map); |
301 |
cbMaps.setSelectedItem(map); |
302 |
map.addMidiInstrumentMapListener(getHandler()); |
303 |
} |
304 |
|
305 |
/** Invoked when a map is removed. */ |
306 |
public void |
307 |
entryRemoved(ListEvent<MidiInstrumentMap> e) { |
308 |
cbMaps.removeItem(e.getEntry()); |
309 |
if(cbMaps.getItemCount() == 0) cbMaps.setEnabled(false); |
310 |
e.getEntry().removeMidiInstrumentMapListener(getHandler()); |
311 |
} |
312 |
|
313 |
/** Invoked when the name of MIDI instrument map is changed. */ |
314 |
public void |
315 |
nameChanged(MidiInstrumentMapEvent e) { |
316 |
cbMaps.repaint(); |
317 |
} |
318 |
|
319 |
/** Invoked when an instrument is added to a MIDI instrument map. */ |
320 |
public void instrumentAdded(MidiInstrumentMapEvent e) { } |
321 |
|
322 |
/** Invoked when an instrument is removed from a MIDI instrument map. */ |
323 |
public void instrumentRemoved(MidiInstrumentMapEvent e) { } |
324 |
|
325 |
public void |
326 |
valueChanged(TreeSelectionEvent e) { |
327 |
removeInstrument.setEnabled(midiInstrumentTree.getSelectionCount() > 0); |
328 |
boolean b = midiInstrumentTree.getSelectedInstrument() != null; |
329 |
actionEditInstrument.setEnabled(b); |
330 |
} |
331 |
} |
332 |
} |