1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2008 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; |
24 |
|
25 |
import java.awt.datatransfer.DataFlavor; |
26 |
import java.awt.datatransfer.Transferable; |
27 |
|
28 |
import java.awt.event.ActionEvent; |
29 |
import java.awt.event.KeyEvent; |
30 |
import java.awt.event.MouseAdapter; |
31 |
import java.awt.event.MouseEvent; |
32 |
|
33 |
import java.util.logging.Level; |
34 |
|
35 |
import javax.swing.AbstractAction; |
36 |
import javax.swing.Action; |
37 |
import javax.swing.JComponent; |
38 |
import javax.swing.JTable; |
39 |
import javax.swing.KeyStroke; |
40 |
import javax.swing.ListSelectionModel; |
41 |
import javax.swing.TransferHandler; |
42 |
|
43 |
import javax.swing.table.TableCellRenderer; |
44 |
|
45 |
import org.jsampler.CC; |
46 |
import org.jsampler.DefaultOrchestraModel; |
47 |
import org.jsampler.HF; |
48 |
import org.jsampler.OrchestraInstrument; |
49 |
|
50 |
import org.jsampler.event.OrchestraAdapter; |
51 |
import org.jsampler.event.OrchestraEvent; |
52 |
|
53 |
import static javax.swing.KeyStroke.*; |
54 |
|
55 |
|
56 |
/** |
57 |
* A table for representing instruments. |
58 |
* @author Grigor Iliev |
59 |
*/ |
60 |
public class InstrumentTable extends JTable { |
61 |
private boolean performingDnD = false; |
62 |
|
63 |
/** Creates a new instance of <code>InstrumentTable</code>. */ |
64 |
public |
65 |
InstrumentTable() { |
66 |
this(new InstrumentTableModel(new DefaultOrchestraModel())); |
67 |
} |
68 |
|
69 |
/** |
70 |
* Creates a new instance of <code>InstrumentTable</code> using the specified data model. |
71 |
* @param dataModel The data model to be represented by this table. |
72 |
*/ |
73 |
public |
74 |
InstrumentTable(InstrumentTableModel dataModel) { |
75 |
super(dataModel); |
76 |
|
77 |
setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
78 |
//setColumnSelectionAllowed(false); |
79 |
//setCellSelectionEnabled(false); |
80 |
//setRowSelectionAllowed(false); |
81 |
|
82 |
addMouseListener(new MouseAdapter() { |
83 |
public void |
84 |
mouseExited(MouseEvent e) { |
85 |
int b1 = e.BUTTON1_DOWN_MASK; |
86 |
if((e.getModifiersEx() & b1) != b1) return; |
87 |
|
88 |
JComponent c = (JComponent)e.getSource(); |
89 |
TransferHandler handler = c.getTransferHandler(); |
90 |
handler.exportAsDrag(c, e, TransferHandler.COPY); |
91 |
performingDnD = true; |
92 |
} |
93 |
}); |
94 |
|
95 |
setTransferHandler(new TransferHandler("instrument") { |
96 |
public boolean |
97 |
canImport(JComponent comp, DataFlavor[] transferFlavors) { |
98 |
if(isPerformingDnD()) return false; |
99 |
return super.canImport(comp, transferFlavors); |
100 |
} |
101 |
|
102 |
protected void |
103 |
exportDone(JComponent source, Transferable data, int action) { |
104 |
performingDnD = false; |
105 |
} |
106 |
}); |
107 |
|
108 |
installKeyboardListeners(); |
109 |
} |
110 |
|
111 |
private void |
112 |
installKeyboardListeners() { |
113 |
KeyStroke k = getKeyStroke(KeyEvent.VK_ESCAPE, 0); |
114 |
getInputMap(JComponent.WHEN_FOCUSED).put(k, Actions.CLEAR_SELECTION); |
115 |
getActionMap().put(Actions.CLEAR_SELECTION, new Actions(Actions.CLEAR_SELECTION)); |
116 |
|
117 |
k = getKeyStroke(KeyEvent.VK_UP, KeyEvent.ALT_MASK | KeyEvent.SHIFT_MASK); |
118 |
getInputMap(JComponent.WHEN_FOCUSED).put(k, Actions.MOVE_ON_TOP); |
119 |
getActionMap().put(Actions.MOVE_ON_TOP, new Actions(Actions.MOVE_ON_TOP)); |
120 |
|
121 |
k = getKeyStroke(KeyEvent.VK_UP, KeyEvent.ALT_MASK); |
122 |
getInputMap(JComponent.WHEN_FOCUSED).put(k, Actions.MOVE_UP); |
123 |
getActionMap().put(Actions.MOVE_UP, new Actions(Actions.MOVE_UP)); |
124 |
|
125 |
k = getKeyStroke(KeyEvent.VK_DOWN, KeyEvent.ALT_MASK); |
126 |
getInputMap(JComponent.WHEN_FOCUSED).put(k, Actions.MOVE_DOWN); |
127 |
getActionMap().put(Actions.MOVE_DOWN, new Actions(Actions.MOVE_DOWN)); |
128 |
|
129 |
k = getKeyStroke(KeyEvent.VK_DOWN, KeyEvent.ALT_MASK | KeyEvent.SHIFT_MASK); |
130 |
getInputMap(JComponent.WHEN_FOCUSED).put(k, Actions.MOVE_AT_BOTTOM); |
131 |
getActionMap().put(Actions.MOVE_AT_BOTTOM, new Actions(Actions.MOVE_AT_BOTTOM)); |
132 |
|
133 |
|
134 |
} |
135 |
|
136 |
/** |
137 |
* Gets the <code>InstrumentTableModel</code> that |
138 |
* provides the data displayed by this <code>InstrumentTable</code>. |
139 |
* @return The <code>InstrumentTableModel</code> that |
140 |
* provides the data displayed by this <code>InstrumentTable</code>. |
141 |
*/ |
142 |
public InstrumentTableModel |
143 |
getModel() { return (InstrumentTableModel) super.getModel(); } |
144 |
|
145 |
/** |
146 |
* Sets the data model for this table to <code>dataModel</code>. |
147 |
* @param dataModel The new data source for this table. |
148 |
*/ |
149 |
public void |
150 |
setModel(InstrumentTableModel dataModel) { super.setModel(dataModel); } |
151 |
|
152 |
/** |
153 |
* Gets the selected instrument. |
154 |
* @return The selected instrument, or <code>null</code> if no instrument is selected. |
155 |
*/ |
156 |
public OrchestraInstrument |
157 |
getSelectedInstrument() { |
158 |
int i = getSelectedRow(); |
159 |
if(i == -1) return null; |
160 |
return getModel().getOrchestraModel().getInstrument(i); |
161 |
} |
162 |
|
163 |
/** |
164 |
* Selects the specified instrument. If <code>instr</code> is |
165 |
* <code>null</code> or is not in the table the current selection is cleared. |
166 |
* @param instr The instrument to select. |
167 |
*/ |
168 |
public void |
169 |
setSelectedInstrument(OrchestraInstrument instr) { |
170 |
int i = getModel().getOrchestraModel().getInstrumentIndex(instr); |
171 |
if(i < 0) { |
172 |
clearSelection(); |
173 |
return; |
174 |
} |
175 |
|
176 |
setRowSelectionInterval(i, i); |
177 |
} |
178 |
|
179 |
/** |
180 |
* Gets the selected instrument. |
181 |
* @return The selected instrument, or <code>null</code> if no instrument is selected. |
182 |
*/ |
183 |
public String |
184 |
getInstrument() { |
185 |
int i = getSelectedRow(); |
186 |
if(i == -1) return null; |
187 |
return getModel().getOrchestraModel().getInstrument(i).getDnDString(); |
188 |
} |
189 |
|
190 |
/** |
191 |
* Creates new instrument using the specified |
192 |
* drag & drop string representation of the instrument. |
193 |
* |
194 |
* @param instr The drag & drop string representation of the instrument. |
195 |
* @see OrchestraInstrument#getDnDString |
196 |
*/ |
197 |
public void |
198 |
setInstrument(String instr) { |
199 |
if(!OrchestraInstrument.isDnDString(instr)) return; |
200 |
|
201 |
OrchestraInstrument instrument = new OrchestraInstrument(); |
202 |
try { instrument.setDnDString(instr); } |
203 |
catch(Exception x) { |
204 |
CC.getLogger().log(Level.INFO, HF.getErrorMessage(x), x); |
205 |
return; |
206 |
} |
207 |
|
208 |
int idx; |
209 |
idx = getModel().getOrchestraModel().getInstrumentIndex(getSelectedInstrument()); |
210 |
if(idx < 0) getModel().getOrchestraModel().addInstrument(instrument); |
211 |
else getModel().getOrchestraModel().insertInstrument(instrument, idx); |
212 |
|
213 |
setSelectedInstrument(instrument); |
214 |
} |
215 |
|
216 |
/** |
217 |
* Returns an appropriate renderer for the cell specified by |
218 |
* <code>row</code> and <code>column</code>. |
219 |
* @param row The row of the cell to render, where 0 is the first row. |
220 |
* @param column The column of the cell to render, where 0 is the first column. |
221 |
*/ |
222 |
public TableCellRenderer |
223 |
getCellRenderer(int row, int column) { |
224 |
TableCellRenderer r = super.getCellRenderer(row, column); |
225 |
if(r instanceof JComponent) { |
226 |
String s; |
227 |
s = getModel().getOrchestraModel().getInstrument(row).getDescription(); |
228 |
if(s != null && s.length() == 0) s = null; |
229 |
((JComponent)r).setToolTipText(s); |
230 |
} |
231 |
|
232 |
return r; |
233 |
} |
234 |
|
235 |
/** |
236 |
* Determines whether drag and drop is initiated from this table and hasn't finished yet. |
237 |
* @return <code>true</code> if drag and drop is performing |
238 |
* at the moment, <code>false</code> otherwise. |
239 |
*/ |
240 |
public boolean |
241 |
isPerformingDnD() { return performingDnD; } |
242 |
|
243 |
private class Actions extends AbstractAction { |
244 |
private static final String CLEAR_SELECTION = "clearSelection"; |
245 |
private static final String MOVE_ON_TOP = "moveInstrumentOnTop"; |
246 |
private static final String MOVE_UP = "moveInstrumentUp"; |
247 |
private static final String MOVE_DOWN = "moveInstrumentDown"; |
248 |
private static final String MOVE_AT_BOTTOM = "moveInstrumentAtBottom"; |
249 |
|
250 |
Actions(String name) { super(name); } |
251 |
|
252 |
public void |
253 |
actionPerformed(ActionEvent e) { |
254 |
String key = getValue(Action.NAME).toString(); |
255 |
|
256 |
if(key == CLEAR_SELECTION) { |
257 |
clearSelection(); |
258 |
} else if(key == MOVE_ON_TOP) { |
259 |
OrchestraInstrument instr = getSelectedInstrument(); |
260 |
getModel().getOrchestraModel().moveInstrumentOnTop(instr); |
261 |
setSelectedInstrument(instr); |
262 |
} else if(key == MOVE_UP) { |
263 |
OrchestraInstrument instr = getSelectedInstrument(); |
264 |
getModel().getOrchestraModel().moveInstrumentUp(instr); |
265 |
setSelectedInstrument(instr); |
266 |
} else if(key == MOVE_DOWN) { |
267 |
OrchestraInstrument instr = getSelectedInstrument(); |
268 |
getModel().getOrchestraModel().moveInstrumentDown(instr); |
269 |
setSelectedInstrument(instr); |
270 |
} else if(key == MOVE_AT_BOTTOM) { |
271 |
OrchestraInstrument instr = getSelectedInstrument(); |
272 |
getModel().getOrchestraModel().moveInstrumentAtBottom(instr); |
273 |
setSelectedInstrument(instr); |
274 |
} |
275 |
} |
276 |
} |
277 |
} |