/[svn]/jsampler/trunk/src/org/jsampler/view/classic/MidiInstrumentMapsPage.java
ViewVC logotype

Contents of /jsampler/trunk/src/org/jsampler/view/classic/MidiInstrumentMapsPage.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2288 - (show annotations) (download)
Wed Nov 23 21:19:44 2011 UTC (12 years, 4 months ago) by iliev
File size: 9532 byte(s)
* Added option to select a sampler engine in Add/Edit Instrument dialog
* Moved all Swing dependent code outside the JSampler core

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

  ViewVC Help
Powered by ViewVC