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; |
24 |
|
25 |
import java.awt.event.ActionEvent; |
26 |
import java.awt.event.KeyEvent; |
27 |
import java.awt.event.MouseAdapter; |
28 |
import java.awt.event.MouseEvent; |
29 |
|
30 |
import javax.swing.AbstractAction; |
31 |
import javax.swing.Action; |
32 |
import javax.swing.JComponent; |
33 |
import javax.swing.JTable; |
34 |
import javax.swing.ListSelectionModel; |
35 |
import javax.swing.KeyStroke; |
36 |
|
37 |
import org.jsampler.DefaultOrchestraListModel; |
38 |
import org.jsampler.OrchestraModel; |
39 |
|
40 |
import javax.swing.table.TableCellRenderer; |
41 |
|
42 |
import static javax.swing.KeyStroke.*; |
43 |
|
44 |
|
45 |
/** |
46 |
* A table for representing orchestras. |
47 |
* @author Grigor Iliev |
48 |
*/ |
49 |
public class OrchestraTable extends JTable { |
50 |
|
51 |
/** Creates a new instance of <code>OrchestraTable</code>. */ |
52 |
public |
53 |
OrchestraTable() { |
54 |
this(new OrchestraTableModel(new DefaultOrchestraListModel())); |
55 |
} |
56 |
|
57 |
/** |
58 |
* Creates a new instance of <code>OrchestraTable</code> using the specified data model. |
59 |
* @param dataModel The data model to be represented by this table. |
60 |
*/ |
61 |
public |
62 |
OrchestraTable(OrchestraTableModel dataModel) { |
63 |
super(dataModel); |
64 |
|
65 |
setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
66 |
setFillsViewportHeight(true); |
67 |
installKeyboardListeners(); |
68 |
|
69 |
addMouseListener(new MouseAdapter() { |
70 |
public void |
71 |
mouseClicked(MouseEvent e) { |
72 |
if(e.getButton() != e.BUTTON1) return; |
73 |
int r = rowAtPoint(e.getPoint()); |
74 |
if(r == -1) { |
75 |
clearSelection(); |
76 |
return; |
77 |
} |
78 |
} |
79 |
}); |
80 |
} |
81 |
|
82 |
private void |
83 |
installKeyboardListeners() { |
84 |
KeyStroke k = getKeyStroke(KeyEvent.VK_ESCAPE, 0); |
85 |
getInputMap(JComponent.WHEN_FOCUSED).put(k, Actions.CLEAR_SELECTION); |
86 |
getActionMap().put(Actions.CLEAR_SELECTION, new Actions(Actions.CLEAR_SELECTION)); |
87 |
|
88 |
k = getKeyStroke(KeyEvent.VK_UP, KeyEvent.ALT_MASK | KeyEvent.SHIFT_MASK); |
89 |
getInputMap(JComponent.WHEN_FOCUSED).put(k, Actions.MOVE_ON_TOP); |
90 |
getActionMap().put(Actions.MOVE_ON_TOP, new Actions(Actions.MOVE_ON_TOP)); |
91 |
|
92 |
k = getKeyStroke(KeyEvent.VK_UP, KeyEvent.ALT_MASK); |
93 |
getInputMap(JComponent.WHEN_FOCUSED).put(k, Actions.MOVE_UP); |
94 |
getActionMap().put(Actions.MOVE_UP, new Actions(Actions.MOVE_UP)); |
95 |
|
96 |
k = getKeyStroke(KeyEvent.VK_DOWN, KeyEvent.ALT_MASK); |
97 |
getInputMap(JComponent.WHEN_FOCUSED).put(k, Actions.MOVE_DOWN); |
98 |
getActionMap().put(Actions.MOVE_DOWN, new Actions(Actions.MOVE_DOWN)); |
99 |
|
100 |
k = getKeyStroke(KeyEvent.VK_DOWN, KeyEvent.ALT_MASK | KeyEvent.SHIFT_MASK); |
101 |
getInputMap(JComponent.WHEN_FOCUSED).put(k, Actions.MOVE_AT_BOTTOM); |
102 |
getActionMap().put(Actions.MOVE_AT_BOTTOM, new Actions(Actions.MOVE_AT_BOTTOM)); |
103 |
} |
104 |
|
105 |
/** |
106 |
* Gets the <code>OrchestraTableModel</code> that |
107 |
* provides the data displayed by this <code>OrchestraTable</code>. |
108 |
* @return The <code>OrchestraTableModel</code> that |
109 |
* provides the data displayed by this <code>OrchestraTable</code>. |
110 |
*/ |
111 |
public OrchestraTableModel |
112 |
getModel() { return (OrchestraTableModel) super.getModel(); } |
113 |
|
114 |
/** |
115 |
* Sets the data model for this table to <code>dataModel</code>. |
116 |
* @param dataModel The new data source for this table. |
117 |
*/ |
118 |
public void |
119 |
setModel(OrchestraTableModel dataModel) { |
120 |
super.setModel(dataModel); |
121 |
} |
122 |
|
123 |
/** |
124 |
* Gets the selected orchestra. |
125 |
* @return The selected orchestra, or <code>null</code> if no orchestra is selected. |
126 |
*/ |
127 |
public OrchestraModel |
128 |
getSelectedOrchestra() { |
129 |
int i = getSelectedRow(); |
130 |
if(i == -1) return null; |
131 |
return getModel().getOrchestraListModel().getOrchestra(i); |
132 |
} |
133 |
|
134 |
/** |
135 |
* Selects the specified orchestra. If <code>orchestra</code> is |
136 |
* <code>null</code> or is not in the table the current selection is cleared. |
137 |
* @param orchestra The orchestra to select. |
138 |
*/ |
139 |
public void |
140 |
setSelectedOrchestra(OrchestraModel orchestra) { |
141 |
int i = getModel().getOrchestraListModel().getOrchestraIndex(orchestra); |
142 |
if(i < 0) { |
143 |
clearSelection(); |
144 |
return; |
145 |
} |
146 |
|
147 |
setRowSelectionInterval(i, i); |
148 |
} |
149 |
|
150 |
/** |
151 |
* Returns an appropriate renderer for the cell specified by |
152 |
* <code>row</code> and <code>column</code>. |
153 |
* @param row The row of the cell to render, where 0 is the first row. |
154 |
* @param column The column of the cell to render, where 0 is the first column. |
155 |
*/ |
156 |
public TableCellRenderer |
157 |
getCellRenderer(int row, int column) { |
158 |
TableCellRenderer r = super.getCellRenderer(row, column); |
159 |
if(r instanceof JComponent) { |
160 |
String s; |
161 |
s = getModel().getOrchestraListModel().getOrchestra(row).getDescription(); |
162 |
if(s != null && s.length() == 0) s = null; |
163 |
((JComponent)r).setToolTipText(s); |
164 |
} |
165 |
|
166 |
return r; |
167 |
} |
168 |
|
169 |
private class Actions extends AbstractAction { |
170 |
private static final String CLEAR_SELECTION = "clearSelection"; |
171 |
private static final String MOVE_ON_TOP = "moveOrchestraOnTop"; |
172 |
private static final String MOVE_UP = "moveOrchestraUp"; |
173 |
private static final String MOVE_DOWN = "moveOrchestraDown"; |
174 |
private static final String MOVE_AT_BOTTOM = "moveOrchestraAtBottom"; |
175 |
|
176 |
Actions(String name) { super(name); } |
177 |
|
178 |
public void |
179 |
actionPerformed(ActionEvent e) { |
180 |
String key = getValue(Action.NAME).toString(); |
181 |
|
182 |
if(key == CLEAR_SELECTION) { |
183 |
clearSelection(); |
184 |
} else if(key == MOVE_ON_TOP) { |
185 |
OrchestraModel om = getSelectedOrchestra(); |
186 |
getModel().getOrchestraListModel().moveOrchestraOnTop(om); |
187 |
setSelectedOrchestra(om); |
188 |
} else if(key == MOVE_UP) { |
189 |
OrchestraModel om = getSelectedOrchestra(); |
190 |
getModel().getOrchestraListModel().moveOrchestraUp(om); |
191 |
setSelectedOrchestra(om); |
192 |
} else if(key == MOVE_DOWN) { |
193 |
OrchestraModel om = getSelectedOrchestra(); |
194 |
getModel().getOrchestraListModel().moveOrchestraDown(om); |
195 |
setSelectedOrchestra(om); |
196 |
} else if(key == MOVE_AT_BOTTOM) { |
197 |
OrchestraModel om = getSelectedOrchestra(); |
198 |
getModel().getOrchestraListModel().moveOrchestraAtBottom(om); |
199 |
setSelectedOrchestra(om); |
200 |
} |
201 |
} |
202 |
} |
203 |
} |