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