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