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