1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2007 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.std; |
24 |
|
25 |
import java.awt.BorderLayout; |
26 |
|
27 |
import java.awt.event.ActionEvent; |
28 |
import java.awt.event.MouseAdapter; |
29 |
import java.awt.event.MouseEvent; |
30 |
|
31 |
import javax.swing.AbstractAction; |
32 |
import javax.swing.Action; |
33 |
import javax.swing.JPanel; |
34 |
import javax.swing.JScrollPane; |
35 |
|
36 |
import javax.swing.event.ListSelectionEvent; |
37 |
import javax.swing.event.ListSelectionListener; |
38 |
|
39 |
import org.jsampler.DefaultOrchestraModel; |
40 |
import org.jsampler.Instrument; |
41 |
import org.jsampler.OrchestraModel; |
42 |
|
43 |
import org.jsampler.view.InstrumentTable; |
44 |
import org.jsampler.view.InstrumentTableModel; |
45 |
|
46 |
import static org.jsampler.view.std.StdI18n.i18n; |
47 |
|
48 |
/** |
49 |
* |
50 |
* @author Grigor Iliev |
51 |
*/ |
52 |
public class JSOrchestraPane extends JPanel { |
53 |
protected final InstrumentTable instrumentTable; |
54 |
|
55 |
protected final Action actionAddInstrument = new AddInstrumentAction(); |
56 |
protected final Action actionEditInstrument = new EditInstrumentAction(); |
57 |
protected final Action actionDeleteInstrument = new DeleteInstrumentAction(); |
58 |
protected final Action actionInstrumentUp = new InstrumentUpAction(); |
59 |
protected final Action actionInstrumentDown = new InstrumentDownAction(); |
60 |
|
61 |
private OrchestraModel orchestra; |
62 |
|
63 |
/** Creates a new instance of <code>JSOrchestraPane</code> */ |
64 |
public |
65 |
JSOrchestraPane() { |
66 |
this(null); |
67 |
} |
68 |
|
69 |
/** Creates a new instance of <code>JSOrchestraPane</code> */ |
70 |
public |
71 |
JSOrchestraPane(OrchestraModel orchestra) { |
72 |
instrumentTable = new InstrumentTable(); |
73 |
setOrchestra(orchestra); |
74 |
|
75 |
setLayout(new BorderLayout()); |
76 |
JScrollPane sp = new JScrollPane(instrumentTable); |
77 |
add(sp); |
78 |
|
79 |
installListeneres(); |
80 |
} |
81 |
|
82 |
private void |
83 |
installListeneres() { |
84 |
InstrumentSelectionHandler l = new InstrumentSelectionHandler(); |
85 |
instrumentTable.getSelectionModel().addListSelectionListener(l); |
86 |
|
87 |
instrumentTable.addMouseListener(new MouseAdapter() { |
88 |
public void |
89 |
mouseClicked(MouseEvent e) { |
90 |
if(e.getClickCount() < 2) return; |
91 |
|
92 |
if(instrumentTable.getSelectedInstrument() == null) return; |
93 |
editInstrument(instrumentTable.getSelectedInstrument()); |
94 |
} |
95 |
}); |
96 |
} |
97 |
|
98 |
public void |
99 |
setOrchestra(OrchestraModel orchestra) { |
100 |
this.orchestra = orchestra; |
101 |
if(orchestra == null) { |
102 |
orchestra = new DefaultOrchestraModel(); |
103 |
actionAddInstrument.setEnabled(false); |
104 |
} else { |
105 |
actionAddInstrument.setEnabled(true); |
106 |
} |
107 |
instrumentTable.getModel().setOrchestraModel(orchestra); |
108 |
} |
109 |
|
110 |
public Instrument |
111 |
getSelectedInstrument() { return instrumentTable.getSelectedInstrument(); } |
112 |
|
113 |
/** |
114 |
* Invoked when the user initiates the creation of new instrument. |
115 |
* @return The instrument to add |
116 |
* or <code>null</code> if the user cancelled the task. |
117 |
*/ |
118 |
public Instrument |
119 |
createInstrument() { |
120 |
JSAddOrEditInstrumentDlg dlg = new JSAddOrEditInstrumentDlg(); |
121 |
dlg.setVisible(true); |
122 |
|
123 |
if(dlg.isCancelled()) return null; |
124 |
return dlg.getInstrument(); |
125 |
} |
126 |
|
127 |
public void |
128 |
editInstrument(Instrument instr) { |
129 |
JSAddOrEditInstrumentDlg dlg = new JSAddOrEditInstrumentDlg(instr); |
130 |
dlg.setVisible(true); |
131 |
} |
132 |
|
133 |
private class InstrumentSelectionHandler implements ListSelectionListener { |
134 |
public void |
135 |
valueChanged(ListSelectionEvent e) { |
136 |
if(e.getValueIsAdjusting()) return; |
137 |
|
138 |
if(instrumentTable.getSelectedInstrument() == null) { |
139 |
actionEditInstrument.setEnabled(false); |
140 |
actionDeleteInstrument.setEnabled(false); |
141 |
actionInstrumentUp.setEnabled(false); |
142 |
actionInstrumentDown.setEnabled(false); |
143 |
return; |
144 |
} |
145 |
|
146 |
actionEditInstrument.setEnabled(true); |
147 |
actionDeleteInstrument.setEnabled(true); |
148 |
|
149 |
int idx = instrumentTable.getSelectedRow(); |
150 |
actionInstrumentUp.setEnabled(idx != 0); |
151 |
actionInstrumentDown.setEnabled(idx != instrumentTable.getRowCount() - 1); |
152 |
} |
153 |
} |
154 |
|
155 |
private class AddInstrumentAction extends AbstractAction { |
156 |
AddInstrumentAction() { |
157 |
super(""); |
158 |
|
159 |
String s = i18n.getLabel("JSOrchestraPane.ttAddInstrument"); |
160 |
putValue(SHORT_DESCRIPTION, s); |
161 |
} |
162 |
|
163 |
public void |
164 |
actionPerformed(ActionEvent e) { |
165 |
Instrument instr = createInstrument(); |
166 |
if(instr == null) return; |
167 |
orchestra.addInstrument(instr); |
168 |
} |
169 |
} |
170 |
|
171 |
private class EditInstrumentAction extends AbstractAction { |
172 |
EditInstrumentAction() { |
173 |
super(""); |
174 |
|
175 |
String s = i18n.getLabel("JSOrchestraPane.ttEditInstrument"); |
176 |
putValue(SHORT_DESCRIPTION, s); |
177 |
|
178 |
setEnabled(false); |
179 |
} |
180 |
|
181 |
public void |
182 |
actionPerformed(ActionEvent e) { |
183 |
editInstrument(instrumentTable.getSelectedInstrument()); |
184 |
} |
185 |
} |
186 |
|
187 |
private class DeleteInstrumentAction extends AbstractAction { |
188 |
DeleteInstrumentAction() { |
189 |
super(""); |
190 |
|
191 |
String s = i18n.getLabel("JSOrchestraPane.ttDeleteInstrument"); |
192 |
putValue(SHORT_DESCRIPTION, s); |
193 |
|
194 |
setEnabled(false); |
195 |
} |
196 |
|
197 |
public void |
198 |
actionPerformed(ActionEvent e) { |
199 |
Instrument instr = instrumentTable.getSelectedInstrument(); |
200 |
if(instr == null) return; |
201 |
orchestra.removeInstrument(instr); |
202 |
} |
203 |
} |
204 |
|
205 |
private class InstrumentUpAction extends AbstractAction { |
206 |
InstrumentUpAction() { |
207 |
super(""); |
208 |
|
209 |
String s = i18n.getLabel("JSOrchestraPane.ttInstrumentUp"); |
210 |
putValue(SHORT_DESCRIPTION, s); |
211 |
|
212 |
setEnabled(false); |
213 |
} |
214 |
|
215 |
public void |
216 |
actionPerformed(ActionEvent e) { |
217 |
Instrument instr = instrumentTable.getSelectedInstrument(); |
218 |
instrumentTable.getModel().getOrchestraModel().moveInstrumentUp(instr); |
219 |
instrumentTable.setSelectedInstrument(instr); |
220 |
} |
221 |
} |
222 |
|
223 |
private class InstrumentDownAction extends AbstractAction { |
224 |
InstrumentDownAction() { |
225 |
super(""); |
226 |
|
227 |
String s = i18n.getLabel("JSOrchestraPane.ttInstrumentDown"); |
228 |
putValue(SHORT_DESCRIPTION, s); |
229 |
|
230 |
setEnabled(false); |
231 |
} |
232 |
|
233 |
public void |
234 |
actionPerformed(ActionEvent e) { |
235 |
Instrument instr = instrumentTable.getSelectedInstrument(); |
236 |
instrumentTable.getModel().getOrchestraModel().moveInstrumentDown(instr); |
237 |
instrumentTable.setSelectedInstrument(instr); |
238 |
} |
239 |
} |
240 |
} |