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.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.KeyStroke; |
33 |
import javax.swing.ListSelectionModel; |
34 |
|
35 |
import org.jsampler.ServerList; |
36 |
import org.jsampler.Server; |
37 |
|
38 |
import static javax.swing.KeyStroke.*; |
39 |
|
40 |
/** |
41 |
* |
42 |
* @author Grigor Iliev |
43 |
*/ |
44 |
public class ServerTable extends JTable { |
45 |
|
46 |
/** Creates a new instance of <code>ServerTable</code> */ |
47 |
public |
48 |
ServerTable() { |
49 |
this(new ServerTableModel(new ServerList())); |
50 |
} |
51 |
|
52 |
/** |
53 |
* Creates a new instance of <code>ServerTable</code> using the specified data model. |
54 |
* @param dataModel The data model to be represented by this table. |
55 |
*/ |
56 |
public |
57 |
ServerTable(ServerTableModel dataModel) { |
58 |
super(dataModel); |
59 |
|
60 |
setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
61 |
installKeyboardListeners(); |
62 |
} |
63 |
|
64 |
private void |
65 |
installKeyboardListeners() { |
66 |
KeyStroke k = getKeyStroke(KeyEvent.VK_UP, KeyEvent.ALT_MASK | KeyEvent.SHIFT_MASK); |
67 |
getInputMap(JComponent.WHEN_FOCUSED).put(k, Actions.MOVE_ON_TOP); |
68 |
getActionMap().put(Actions.MOVE_ON_TOP, new Actions(Actions.MOVE_ON_TOP)); |
69 |
|
70 |
k = getKeyStroke(KeyEvent.VK_UP, KeyEvent.ALT_MASK); |
71 |
getInputMap(JComponent.WHEN_FOCUSED).put(k, Actions.MOVE_UP); |
72 |
getActionMap().put(Actions.MOVE_UP, new Actions(Actions.MOVE_UP)); |
73 |
|
74 |
k = getKeyStroke(KeyEvent.VK_DOWN, KeyEvent.ALT_MASK); |
75 |
getInputMap(JComponent.WHEN_FOCUSED).put(k, Actions.MOVE_DOWN); |
76 |
getActionMap().put(Actions.MOVE_DOWN, new Actions(Actions.MOVE_DOWN)); |
77 |
|
78 |
k = getKeyStroke(KeyEvent.VK_DOWN, KeyEvent.ALT_MASK | KeyEvent.SHIFT_MASK); |
79 |
getInputMap(JComponent.WHEN_FOCUSED).put(k, Actions.MOVE_AT_BOTTOM); |
80 |
getActionMap().put(Actions.MOVE_AT_BOTTOM, new Actions(Actions.MOVE_AT_BOTTOM)); |
81 |
} |
82 |
|
83 |
/** |
84 |
* Returns the <code>ServerTableModel</code> that |
85 |
* provides the data displayed by this <code>ServerTable</code>. |
86 |
*/ |
87 |
public ServerTableModel |
88 |
getModel() { return (ServerTableModel) super.getModel(); } |
89 |
|
90 |
/** |
91 |
* Returns the selected server, or <code>null</code> if nothing is selected. |
92 |
*/ |
93 |
public Server |
94 |
getSelectedServer() { |
95 |
int i = getSelectedRow(); |
96 |
if(i == -1) return null; |
97 |
return getModel().getServerAt(i); |
98 |
} |
99 |
|
100 |
/** Selects the specified server. */ |
101 |
public void |
102 |
setSelectedServer(Server server) { |
103 |
int i = getModel().getServerList().getServerIndex(server); |
104 |
if(i == -1) return; |
105 |
getSelectionModel().setSelectionInterval(i, i); |
106 |
} |
107 |
|
108 |
/** |
109 |
* Moves the currently selected server one position up. |
110 |
* This method does nothing if the there is no server selected or |
111 |
* if the selected server is already on the top. |
112 |
*/ |
113 |
public void |
114 |
moveSelectedServerUp() { |
115 |
int i = getSelectedRow(); |
116 |
if(i < 1) return; |
117 |
|
118 |
Server s = getSelectedServer(); |
119 |
getModel().getServerList().moveServerUp(s); |
120 |
setSelectedServer(s); |
121 |
} |
122 |
|
123 |
/** |
124 |
* Moves the currently selected server one position down. |
125 |
* This method does nothing if the there is no server selected or |
126 |
* if the selected server is already at the bottom. |
127 |
*/ |
128 |
public void |
129 |
moveSelectedServerDown() { |
130 |
int i = getSelectedRow(); |
131 |
if(i == -1 || i == getModel().getRowCount() - 1) return; |
132 |
|
133 |
Server s = getSelectedServer(); |
134 |
getModel().getServerList().moveServerDown(s); |
135 |
setSelectedServer(s); |
136 |
} |
137 |
|
138 |
/** |
139 |
* Removes the currently selected server. |
140 |
*/ |
141 |
public void |
142 |
removeSelectedServer() { |
143 |
Server s = getSelectedServer(); |
144 |
if(s == null) return; |
145 |
|
146 |
int i = getSelectedRow(); |
147 |
getModel().getServerList().removeServer(s); |
148 |
|
149 |
if(getRowCount() > i) { |
150 |
getSelectionModel().setSelectionInterval(i, i); |
151 |
} else { |
152 |
i = getRowCount() - 1; |
153 |
if(i >= 0) getSelectionModel().setSelectionInterval(i, i); |
154 |
} |
155 |
} |
156 |
|
157 |
private class Actions extends AbstractAction { |
158 |
private static final String MOVE_ON_TOP = "moveServerOnTop"; |
159 |
private static final String MOVE_UP = "moveServerUp"; |
160 |
private static final String MOVE_DOWN = "moveServerDown"; |
161 |
private static final String MOVE_AT_BOTTOM = "moveServerAtBottom"; |
162 |
|
163 |
Actions(String name) { super(name); } |
164 |
|
165 |
public void |
166 |
actionPerformed(ActionEvent e) { |
167 |
String key = getValue(Action.NAME).toString(); |
168 |
|
169 |
if(key == MOVE_ON_TOP) { |
170 |
Server server = getSelectedServer(); |
171 |
getModel().getServerList().moveServerOnTop(server); |
172 |
setSelectedServer(server); |
173 |
} else if(key == MOVE_UP) { |
174 |
moveSelectedServerUp(); |
175 |
} else if(key == MOVE_DOWN) { |
176 |
moveSelectedServerDown(); |
177 |
} else if(key == MOVE_AT_BOTTOM) { |
178 |
Server server = getSelectedServer(); |
179 |
getModel().getServerList().moveServerAtBottom(server); |
180 |
setSelectedServer(server); |
181 |
} |
182 |
} |
183 |
} |
184 |
} |