1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005, 2006 Grigor Kirilov Iliev |
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.datatransfer.DataFlavor; |
28 |
|
29 |
import java.awt.event.ActionEvent; |
30 |
import java.awt.event.ActionListener; |
31 |
import java.awt.event.MouseAdapter; |
32 |
import java.awt.event.MouseEvent; |
33 |
|
34 |
import java.util.logging.Level; |
35 |
|
36 |
import javax.swing.AbstractAction; |
37 |
import javax.swing.Action; |
38 |
import javax.swing.Box; |
39 |
import javax.swing.BoxLayout; |
40 |
import javax.swing.JComponent; |
41 |
import javax.swing.JPanel; |
42 |
import javax.swing.JScrollPane; |
43 |
import javax.swing.JSplitPane; |
44 |
import javax.swing.JToolBar; |
45 |
import javax.swing.ListSelectionModel; |
46 |
import javax.swing.TransferHandler; |
47 |
|
48 |
import javax.swing.event.ListSelectionEvent; |
49 |
import javax.swing.event.ListSelectionListener; |
50 |
|
51 |
import net.sf.juife.JuifeUtils; |
52 |
import net.sf.juife.LinkButton; |
53 |
import net.sf.juife.NavigationPage; |
54 |
|
55 |
import org.jsampler.CC; |
56 |
import org.jsampler.DOMUtils; |
57 |
import org.jsampler.DefaultOrchestraListModel; |
58 |
import org.jsampler.DefaultOrchestraModel; |
59 |
import org.jsampler.HF; |
60 |
import org.jsampler.Instrument; |
61 |
import org.jsampler.OrchestraListModel; |
62 |
import org.jsampler.OrchestraModel; |
63 |
|
64 |
import org.jsampler.view.InstrumentTable; |
65 |
import org.jsampler.view.InstrumentTableModel; |
66 |
import org.jsampler.view.OrchestraTable; |
67 |
import org.jsampler.view.OrchestraTableModel; |
68 |
|
69 |
import static org.jsampler.view.classic.ClassicI18n.i18n; |
70 |
|
71 |
|
72 |
/** |
73 |
* |
74 |
* @author Grigor Iliev |
75 |
*/ |
76 |
public class ManageOrchestrasPage extends NavigationPage { |
77 |
private final JComponent manageOrchestrasPane; |
78 |
|
79 |
// Manage Orchestra |
80 |
private final ToolbarButton btnAddOrchestra = new ToolbarButton(new AddOrchestra()); |
81 |
private final ToolbarButton btnEditOrchestra = new ToolbarButton(new EditOrchestra()); |
82 |
private final ToolbarButton btnDeleteOrchestra = new ToolbarButton(new DeleteOrchestra()); |
83 |
private final ToolbarButton btnOrchestraUp = new ToolbarButton(new OrchestraUp()); |
84 |
private final ToolbarButton btnOrchestraDown = new ToolbarButton(new OrchestraDown()); |
85 |
|
86 |
private OrchestraTable orchestraTable; |
87 |
|
88 |
private final ToolbarButton btnAddInstrument = new ToolbarButton(new AddInstrument()); |
89 |
private final ToolbarButton btnEditInstrument = new ToolbarButton(new EditInstrument()); |
90 |
private final ToolbarButton btnDeleteInstrument = new ToolbarButton(new DeleteInstrument()); |
91 |
private final ToolbarButton btnInstrumentUp = new ToolbarButton(new InstrumentUp()); |
92 |
private final ToolbarButton btnInstrumentDown = new ToolbarButton(new InstrumentDown()); |
93 |
|
94 |
private LinkButton lnkOrchestras = |
95 |
new LinkButton(i18n.getButtonLabel("ManageOrchestrasPage.lnkOrchestras")); |
96 |
|
97 |
private InstrumentTable instrumentTable; |
98 |
private OrchestraModel orchestraModel; |
99 |
|
100 |
|
101 |
/** Creates a new instance of <code>ManageOrchestrasPage</code>. */ |
102 |
public ManageOrchestrasPage() { |
103 |
setTitle(i18n.getLabel("ManageOrchestrasPage.title")); |
104 |
|
105 |
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
106 |
|
107 |
manageOrchestrasPane = createManageOrchestrasPane(); |
108 |
add(manageOrchestrasPane); |
109 |
|
110 |
installListeneres(); |
111 |
} |
112 |
|
113 |
private JComponent |
114 |
createManageOrchestrasPane() { |
115 |
JPanel p = new JPanel(); |
116 |
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
117 |
//p.add(Box.createRigidArea(new Dimension(0, 5))); |
118 |
|
119 |
JToolBar tb = new JToolBar(); |
120 |
tb.add(btnAddOrchestra); |
121 |
tb.add(btnEditOrchestra); |
122 |
tb.add(btnDeleteOrchestra); |
123 |
|
124 |
tb.addSeparator(); |
125 |
|
126 |
tb.add(btnOrchestraUp); |
127 |
tb.add(btnOrchestraDown); |
128 |
|
129 |
tb.setMaximumSize(new Dimension(Short.MAX_VALUE, tb.getPreferredSize().height)); |
130 |
tb.setFloatable(false); |
131 |
tb.setAlignmentX(LEFT_ALIGNMENT); |
132 |
|
133 |
p.add(tb); |
134 |
|
135 |
orchestraTable = new OrchestraTable(new OrchestraTableModel(CC.getOrchestras())); |
136 |
JScrollPane sp = new JScrollPane(orchestraTable); |
137 |
|
138 |
sp.getViewport().addMouseListener(new MouseAdapter() { |
139 |
public void |
140 |
mouseClicked(MouseEvent e) { orchestraTable.clearSelection(); } |
141 |
}); |
142 |
|
143 |
Dimension d; |
144 |
d = new Dimension(sp.getMinimumSize().width, sp.getPreferredSize().height); |
145 |
sp.setPreferredSize(d); |
146 |
|
147 |
sp.setAlignmentX(LEFT_ALIGNMENT); |
148 |
p.add(sp); |
149 |
|
150 |
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); |
151 |
|
152 |
splitPane.setTopComponent(p); |
153 |
|
154 |
p = new JPanel(); |
155 |
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
156 |
//p.add(Box.createRigidArea(new Dimension(0, 5))); |
157 |
|
158 |
tb = new JToolBar(); |
159 |
btnAddInstrument.setEnabled(false); |
160 |
tb.add(btnAddInstrument); |
161 |
tb.add(btnEditInstrument); |
162 |
tb.add(btnDeleteInstrument); |
163 |
|
164 |
tb.addSeparator(); |
165 |
|
166 |
tb.add(btnInstrumentUp); |
167 |
tb.add(btnInstrumentDown); |
168 |
|
169 |
tb.setMaximumSize(new Dimension(Short.MAX_VALUE, tb.getPreferredSize().height)); |
170 |
tb.setFloatable(false); |
171 |
tb.setAlignmentX(JPanel.LEFT_ALIGNMENT); |
172 |
|
173 |
p.add(tb); |
174 |
|
175 |
orchestraModel = new DefaultOrchestraModel(); |
176 |
instrumentTable = new InstrumentTable(new InstrumentTableModel(orchestraModel)); |
177 |
sp = new DnDScrollPane(instrumentTable); |
178 |
sp.setAlignmentX(LEFT_ALIGNMENT); |
179 |
p.add(sp); |
180 |
|
181 |
//p.add(lnkOrchestras); |
182 |
//p.add(Box.createRigidArea(new Dimension(0, 5))); |
183 |
|
184 |
splitPane.setBottomComponent(p); |
185 |
splitPane.setDividerSize(3); |
186 |
splitPane.setDividerLocation(180); |
187 |
|
188 |
return splitPane; |
189 |
} |
190 |
|
191 |
private void |
192 |
installListeneres() { |
193 |
lnkOrchestras.addActionListener(new ActionListener() { |
194 |
public void |
195 |
actionPerformed(ActionEvent e) { |
196 |
LeftPane.getLeftPane().showOrchestrasPage(); |
197 |
} |
198 |
}); |
199 |
|
200 |
OrchestraSelectionHandler l = new OrchestraSelectionHandler(); |
201 |
orchestraTable.getSelectionModel().addListSelectionListener(l); |
202 |
|
203 |
orchestraTable.addMouseListener(new MouseAdapter() { |
204 |
public void |
205 |
mouseClicked(MouseEvent e) { |
206 |
if(e.getClickCount() < 2) return; |
207 |
|
208 |
if(orchestraTable.getSelectedOrchestra() == null) return; |
209 |
btnEditOrchestra.doClick(0); |
210 |
} |
211 |
}); |
212 |
|
213 |
InstrumentSelectionHandler l2 = new InstrumentSelectionHandler(); |
214 |
instrumentTable.getSelectionModel().addListSelectionListener(l2); |
215 |
|
216 |
instrumentTable.addMouseListener(new MouseAdapter() { |
217 |
public void |
218 |
mouseClicked(MouseEvent e) { |
219 |
if(e.getClickCount() < 2) return; |
220 |
|
221 |
if(instrumentTable.getSelectedInstrument() == null) return; |
222 |
btnEditInstrument.doClick(0); |
223 |
} |
224 |
}); |
225 |
} |
226 |
|
227 |
public class DnDScrollPane extends JScrollPane { |
228 |
private InstrumentTable instrumentTable; |
229 |
|
230 |
private |
231 |
DnDScrollPane(InstrumentTable table) { |
232 |
instrumentTable = table; |
233 |
setViewport(new DnDViewPort()); |
234 |
setViewportView(instrumentTable); |
235 |
|
236 |
Dimension d; |
237 |
d = new Dimension(getMinimumSize().width, getPreferredSize().height); |
238 |
setPreferredSize(d); |
239 |
} |
240 |
|
241 |
public class DnDViewPort extends javax.swing.JViewport { |
242 |
private |
243 |
DnDViewPort() { |
244 |
setTransferHandler(new TransferHandler("instrument") { |
245 |
public boolean |
246 |
canImport(JComponent comp, DataFlavor[] flavors) { |
247 |
if(instrumentTable.isPerformingDnD()) return false; |
248 |
if(!btnAddInstrument.isEnabled()) return false; |
249 |
|
250 |
return super.canImport(comp, flavors); |
251 |
} |
252 |
}); |
253 |
|
254 |
addMouseListener(new MouseAdapter() { |
255 |
public void |
256 |
mouseClicked(MouseEvent e) { |
257 |
instrumentTable.clearSelection(); |
258 |
} |
259 |
}); |
260 |
} |
261 |
|
262 |
public String |
263 |
getInstrument() { return null; } |
264 |
|
265 |
public void |
266 |
setInstrument(String instr) { |
267 |
instrumentTable.setSelectedInstrument(null); |
268 |
instrumentTable.setInstrument(instr); |
269 |
} |
270 |
} |
271 |
} |
272 |
|
273 |
private class OrchestraSelectionHandler implements ListSelectionListener { |
274 |
public void |
275 |
valueChanged(ListSelectionEvent e) { |
276 |
if(e.getValueIsAdjusting()) return; |
277 |
|
278 |
if(orchestraTable.getSelectedOrchestra() == null) { |
279 |
btnEditOrchestra.setEnabled(false); |
280 |
btnDeleteOrchestra.setEnabled(false); |
281 |
btnAddInstrument.setEnabled(false); |
282 |
btnOrchestraUp.setEnabled(false); |
283 |
btnOrchestraDown.setEnabled(false); |
284 |
instrumentTable.setModel(new InstrumentTableModel()); |
285 |
return; |
286 |
} |
287 |
|
288 |
btnEditOrchestra.setEnabled(true); |
289 |
btnDeleteOrchestra.setEnabled(true); |
290 |
btnAddInstrument.setEnabled(true); |
291 |
|
292 |
orchestraModel = orchestraTable.getSelectedOrchestra(); |
293 |
int idx = orchestraTable.getSelectedRow(); |
294 |
btnOrchestraUp.setEnabled(idx != 0); |
295 |
btnOrchestraDown.setEnabled(idx != orchestraTable.getRowCount() - 1); |
296 |
|
297 |
instrumentTable.getModel().setOrchestraModel(orchestraModel); |
298 |
} |
299 |
} |
300 |
|
301 |
private class InstrumentSelectionHandler implements ListSelectionListener { |
302 |
public void |
303 |
valueChanged(ListSelectionEvent e) { |
304 |
if(e.getValueIsAdjusting()) return; |
305 |
|
306 |
if(instrumentTable.getSelectedInstrument() == null) { |
307 |
btnEditInstrument.setEnabled(false); |
308 |
btnDeleteInstrument.setEnabled(false); |
309 |
btnInstrumentUp.setEnabled(false); |
310 |
btnInstrumentDown.setEnabled(false); |
311 |
return; |
312 |
} |
313 |
|
314 |
btnEditInstrument.setEnabled(true); |
315 |
btnDeleteInstrument.setEnabled(true); |
316 |
|
317 |
int idx = instrumentTable.getSelectedRow(); |
318 |
btnInstrumentUp.setEnabled(idx != 0); |
319 |
btnInstrumentDown.setEnabled(idx != instrumentTable.getRowCount() - 1); |
320 |
} |
321 |
} |
322 |
|
323 |
private class AddOrchestra extends AbstractAction { |
324 |
AddOrchestra() { |
325 |
super(""); |
326 |
|
327 |
String s = i18n.getLabel("ManageOrchestrasPage.ttAddOrchestra"); |
328 |
putValue(SHORT_DESCRIPTION, s); |
329 |
putValue(Action.SMALL_ICON, Res.iconNew16); |
330 |
} |
331 |
|
332 |
public void |
333 |
actionPerformed(ActionEvent e) { |
334 |
AddOrchestraDlg dlg = new AddOrchestraDlg(); |
335 |
dlg.setVisible(true); |
336 |
|
337 |
if(dlg.isCancelled()) return; |
338 |
|
339 |
OrchestraModel om = orchestraTable.getSelectedOrchestra(); |
340 |
int idx = CC.getOrchestras().getOrchestraIndex(om); |
341 |
if(idx < 0) CC.getOrchestras().addOrchestra(dlg.getOrchestra()); |
342 |
else CC.getOrchestras().insertOrchestra(dlg.getOrchestra(), idx); |
343 |
|
344 |
orchestraTable.setSelectedOrchestra(dlg.getOrchestra()); |
345 |
} |
346 |
} |
347 |
|
348 |
private class EditOrchestra extends AbstractAction { |
349 |
EditOrchestra() { |
350 |
super(""); |
351 |
|
352 |
String s = i18n.getLabel("ManageOrchestrasPage.ttEditOrchestra"); |
353 |
putValue(SHORT_DESCRIPTION, s); |
354 |
putValue(Action.SMALL_ICON, Res.iconEdit16); |
355 |
|
356 |
setEnabled(false); |
357 |
} |
358 |
|
359 |
public void |
360 |
actionPerformed(ActionEvent e) { |
361 |
OrchestraModel m = orchestraTable.getSelectedOrchestra(); |
362 |
AddOrchestraDlg dlg = new AddOrchestraDlg(m); |
363 |
dlg.setVisible(true); |
364 |
} |
365 |
} |
366 |
|
367 |
private class DeleteOrchestra extends AbstractAction { |
368 |
DeleteOrchestra() { |
369 |
super(""); |
370 |
|
371 |
String s = i18n.getLabel("ManageOrchestrasPage.ttDeleteOrchestra"); |
372 |
putValue(SHORT_DESCRIPTION, s); |
373 |
putValue(Action.SMALL_ICON, Res.iconDelete16); |
374 |
|
375 |
setEnabled(false); |
376 |
} |
377 |
|
378 |
public void |
379 |
actionPerformed(ActionEvent e) { |
380 |
OrchestraModel om = orchestraTable.getSelectedOrchestra(); |
381 |
if(om == null) return; |
382 |
if(om.getInstrumentCount() > 0) { |
383 |
String s = i18n.getMessage("ManageOrchestrasPage.removeOrchestra?"); |
384 |
if(!HF.showYesNoDialog(CC.getMainFrame(), s)) return; |
385 |
} |
386 |
|
387 |
CC.getOrchestras().removeOrchestra(om); |
388 |
} |
389 |
} |
390 |
|
391 |
private class OrchestraUp extends AbstractAction { |
392 |
OrchestraUp() { |
393 |
super(""); |
394 |
|
395 |
String s = i18n.getLabel("ManageOrchestrasPage.OrchestraUp"); |
396 |
putValue(SHORT_DESCRIPTION, s); |
397 |
putValue(Action.SMALL_ICON, Res.iconUp16); |
398 |
|
399 |
setEnabled(false); |
400 |
} |
401 |
|
402 |
public void |
403 |
actionPerformed(ActionEvent e) { |
404 |
OrchestraModel om = orchestraTable.getSelectedOrchestra(); |
405 |
CC.getOrchestras().moveOrchestraUp(om); |
406 |
orchestraTable.setSelectedOrchestra(om); |
407 |
} |
408 |
} |
409 |
|
410 |
private class OrchestraDown extends AbstractAction { |
411 |
OrchestraDown() { |
412 |
super(""); |
413 |
|
414 |
String s = i18n.getLabel("ManageOrchestrasPage.OrchestraDown"); |
415 |
putValue(SHORT_DESCRIPTION, s); |
416 |
putValue(Action.SMALL_ICON, Res.iconDown16); |
417 |
|
418 |
setEnabled(false); |
419 |
} |
420 |
|
421 |
public void |
422 |
actionPerformed(ActionEvent e) { |
423 |
OrchestraModel om = orchestraTable.getSelectedOrchestra(); |
424 |
CC.getOrchestras().moveOrchestraDown(om); |
425 |
orchestraTable.setSelectedOrchestra(om); |
426 |
} |
427 |
} |
428 |
|
429 |
private class AddInstrument extends AbstractAction { |
430 |
AddInstrument() { |
431 |
super(""); |
432 |
|
433 |
String s = i18n.getLabel("ManageOrchestrasPage.ttAddInstrument"); |
434 |
putValue(SHORT_DESCRIPTION, s); |
435 |
|
436 |
try { |
437 |
putValue(Action.SMALL_ICON, Res.iconNew16); |
438 |
} catch(Exception x) { |
439 |
CC.getLogger().log(Level.INFO, HF.getErrorMessage(x), x); |
440 |
} |
441 |
} |
442 |
|
443 |
public void |
444 |
actionPerformed(ActionEvent e) { |
445 |
AddInstrumentDlg dlg = new AddInstrumentDlg(); |
446 |
dlg.setVisible(true); |
447 |
|
448 |
if(dlg.isCancelled()) return; |
449 |
|
450 |
orchestraModel.addInstrument(dlg.getInstrument()); |
451 |
} |
452 |
} |
453 |
|
454 |
private class EditInstrument extends AbstractAction { |
455 |
EditInstrument() { |
456 |
super(""); |
457 |
|
458 |
String s = i18n.getLabel("ManageOrchestrasPage.ttEditInstrument"); |
459 |
putValue(SHORT_DESCRIPTION, s); |
460 |
putValue(Action.SMALL_ICON, Res.iconEdit16); |
461 |
|
462 |
setEnabled(false); |
463 |
} |
464 |
|
465 |
public void |
466 |
actionPerformed(ActionEvent e) { |
467 |
Instrument instr = instrumentTable.getSelectedInstrument(); |
468 |
AddInstrumentDlg dlg = new AddInstrumentDlg(instr); |
469 |
dlg.setVisible(true); |
470 |
} |
471 |
} |
472 |
|
473 |
private class DeleteInstrument extends AbstractAction { |
474 |
DeleteInstrument() { |
475 |
super(""); |
476 |
|
477 |
String s = i18n.getLabel("ManageOrchestrasPage.ttDeleteInstrument"); |
478 |
putValue(SHORT_DESCRIPTION, s); |
479 |
putValue(Action.SMALL_ICON, Res.iconDelete16); |
480 |
|
481 |
setEnabled(false); |
482 |
} |
483 |
|
484 |
public void |
485 |
actionPerformed(ActionEvent e) { |
486 |
Instrument instr = instrumentTable.getSelectedInstrument(); |
487 |
if(instr == null) return; |
488 |
orchestraModel.removeInstrument(instr); |
489 |
} |
490 |
} |
491 |
|
492 |
private class InstrumentUp extends AbstractAction { |
493 |
InstrumentUp() { |
494 |
super(""); |
495 |
|
496 |
String s = i18n.getLabel("ManageOrchestrasPage.InstrumentUp"); |
497 |
putValue(SHORT_DESCRIPTION, s); |
498 |
putValue(Action.SMALL_ICON, Res.iconUp16); |
499 |
|
500 |
setEnabled(false); |
501 |
} |
502 |
|
503 |
public void |
504 |
actionPerformed(ActionEvent e) { |
505 |
Instrument instr = instrumentTable.getSelectedInstrument(); |
506 |
instrumentTable.getModel().getOrchestraModel().moveInstrumentUp(instr); |
507 |
instrumentTable.setSelectedInstrument(instr); |
508 |
} |
509 |
} |
510 |
|
511 |
private class InstrumentDown extends AbstractAction { |
512 |
InstrumentDown() { |
513 |
super(""); |
514 |
|
515 |
String s = i18n.getLabel("ManageOrchestrasPage.InstrumentDown"); |
516 |
putValue(SHORT_DESCRIPTION, s); |
517 |
putValue(Action.SMALL_ICON, Res.iconDown16); |
518 |
|
519 |
setEnabled(false); |
520 |
} |
521 |
|
522 |
public void |
523 |
actionPerformed(ActionEvent e) { |
524 |
Instrument instr = instrumentTable.getSelectedInstrument(); |
525 |
instrumentTable.getModel().getOrchestraModel().moveInstrumentDown(instr); |
526 |
instrumentTable.setSelectedInstrument(instr); |
527 |
} |
528 |
} |
529 |
} |