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

Contents of /jsampler/trunk/src/org/jsampler/view/InstrumentTable.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2192 - (show annotations) (download)
Fri Jun 24 21:34:51 2011 UTC (12 years, 10 months ago) by iliev
File size: 8851 byte(s)
* Initial implementation of Sampler Browser
  (choose Window/Sampler Browser) - another way to view/edit
  the sampler configuration (work in progress - for now only
  support for viewing/editing send effects)

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

  ViewVC Help
Powered by ViewVC