1 |
iliev |
913 |
/* |
2 |
|
|
* JSampler - a java front-end for LinuxSampler |
3 |
|
|
* |
4 |
iliev |
1143 |
* Copyright (C) 2005-2006 Grigor Iliev <grigor@grigoriliev.com> |
5 |
iliev |
913 |
* |
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.Instrument; |
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 Instrument |
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(Instrument 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 |
|
|
* @param instr The drag & drop string representation of the instrument. |
194 |
|
|
* @see Instrument#getDnDString |
195 |
|
|
*/ |
196 |
|
|
public void |
197 |
|
|
setInstrument(String instr) { |
198 |
|
|
if(!Instrument.isDnDString(instr)) return; |
199 |
|
|
|
200 |
|
|
Instrument instrument = new Instrument(); |
201 |
|
|
try { instrument.setDnDString(instr); } |
202 |
|
|
catch(Exception x) { |
203 |
|
|
CC.getLogger().log(Level.INFO, HF.getErrorMessage(x), x); |
204 |
|
|
return; |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
int idx; |
208 |
|
|
idx = getModel().getOrchestraModel().getInstrumentIndex(getSelectedInstrument()); |
209 |
|
|
if(idx < 0) getModel().getOrchestraModel().addInstrument(instrument); |
210 |
|
|
else getModel().getOrchestraModel().insertInstrument(instrument, idx); |
211 |
|
|
|
212 |
|
|
setSelectedInstrument(instrument); |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
/** |
216 |
|
|
* Returns an appropriate renderer for the cell specified by |
217 |
|
|
* <code>row</code> and <code>column</code>. |
218 |
|
|
* @param row The row of the cell to render, where 0 is the first row. |
219 |
|
|
* @param column The column of the cell to render, where 0 is the first column. |
220 |
|
|
*/ |
221 |
|
|
public TableCellRenderer |
222 |
|
|
getCellRenderer(int row, int column) { |
223 |
|
|
TableCellRenderer r = super.getCellRenderer(row, column); |
224 |
|
|
if(r instanceof JComponent) { |
225 |
|
|
String s; |
226 |
|
|
s = getModel().getOrchestraModel().getInstrument(row).getDescription(); |
227 |
|
|
if(s != null && s.length() == 0) s = null; |
228 |
|
|
((JComponent)r).setToolTipText(s); |
229 |
|
|
} |
230 |
|
|
|
231 |
|
|
return r; |
232 |
|
|
} |
233 |
|
|
|
234 |
|
|
/** |
235 |
|
|
* Determines whether drag and drop is initiated from this table and hasn't finished yet. |
236 |
|
|
* @return <code>true</code> if drag and drop is performing |
237 |
|
|
* at the moment, <code>false</code> otherwise. |
238 |
|
|
*/ |
239 |
|
|
public boolean |
240 |
|
|
isPerformingDnD() { return performingDnD; } |
241 |
|
|
|
242 |
|
|
private class Actions extends AbstractAction { |
243 |
|
|
private static final String CLEAR_SELECTION = "clearSelection"; |
244 |
|
|
private static final String MOVE_ON_TOP = "moveInstrumentOnTop"; |
245 |
|
|
private static final String MOVE_UP = "moveInstrumentUp"; |
246 |
|
|
private static final String MOVE_DOWN = "moveInstrumentDown"; |
247 |
|
|
private static final String MOVE_AT_BOTTOM = "moveInstrumentAtBottom"; |
248 |
|
|
|
249 |
|
|
Actions(String name) { super(name); } |
250 |
|
|
|
251 |
|
|
public void |
252 |
|
|
actionPerformed(ActionEvent e) { |
253 |
|
|
String key = getValue(Action.NAME).toString(); |
254 |
|
|
|
255 |
|
|
if(key == CLEAR_SELECTION) { |
256 |
|
|
clearSelection(); |
257 |
|
|
} else if(key == MOVE_ON_TOP) { |
258 |
|
|
Instrument instr = getSelectedInstrument(); |
259 |
|
|
getModel().getOrchestraModel().moveInstrumentOnTop(instr); |
260 |
|
|
setSelectedInstrument(instr); |
261 |
|
|
} else if(key == MOVE_UP) { |
262 |
|
|
Instrument instr = getSelectedInstrument(); |
263 |
|
|
getModel().getOrchestraModel().moveInstrumentUp(instr); |
264 |
|
|
setSelectedInstrument(instr); |
265 |
|
|
} else if(key == MOVE_DOWN) { |
266 |
|
|
Instrument instr = getSelectedInstrument(); |
267 |
|
|
getModel().getOrchestraModel().moveInstrumentDown(instr); |
268 |
|
|
setSelectedInstrument(instr); |
269 |
|
|
} else if(key == MOVE_AT_BOTTOM) { |
270 |
|
|
Instrument instr = getSelectedInstrument(); |
271 |
|
|
getModel().getOrchestraModel().moveInstrumentAtBottom(instr); |
272 |
|
|
setSelectedInstrument(instr); |
273 |
|
|
} |
274 |
|
|
} |
275 |
|
|
} |
276 |
|
|
} |