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.std; |
24 |
|
25 |
import java.awt.Dialog; |
26 |
import java.awt.Dimension; |
27 |
import java.awt.Frame; |
28 |
import java.awt.GridBagConstraints; |
29 |
import java.awt.GridBagLayout; |
30 |
import java.awt.Insets; |
31 |
import java.awt.Window; |
32 |
|
33 |
import java.awt.event.ActionEvent; |
34 |
import java.awt.event.ActionListener; |
35 |
|
36 |
import javax.swing.BorderFactory; |
37 |
import javax.swing.Box; |
38 |
import javax.swing.BoxLayout; |
39 |
import javax.swing.JButton; |
40 |
import javax.swing.JCheckBox; |
41 |
import javax.swing.JLabel; |
42 |
import javax.swing.JOptionPane; |
43 |
import javax.swing.JPanel; |
44 |
import javax.swing.JScrollPane; |
45 |
import javax.swing.JSpinner; |
46 |
import javax.swing.JTextField; |
47 |
import javax.swing.SpinnerNumberModel; |
48 |
|
49 |
import javax.swing.event.ListSelectionEvent; |
50 |
import javax.swing.event.ListSelectionListener; |
51 |
|
52 |
import net.sf.juife.JuifeUtils; |
53 |
|
54 |
import org.jsampler.CC; |
55 |
import org.jsampler.HF; |
56 |
import org.jsampler.JSPrefs; |
57 |
import org.jsampler.Prefs; |
58 |
|
59 |
import org.jsampler.task.SetServerAddress; |
60 |
|
61 |
import org.jsampler.view.ServerTable; |
62 |
import org.jsampler.view.ServerTableModel; |
63 |
|
64 |
import static org.jsampler.view.std.StdI18n.i18n; |
65 |
import static org.jsampler.view.std.StdPrefs.*; |
66 |
|
67 |
|
68 |
/** |
69 |
* |
70 |
* @author Grigor Iliev |
71 |
*/ |
72 |
public class JSConnectionPropsPane extends JPanel { |
73 |
private final JCheckBox checkManualSelect = |
74 |
new JCheckBox(i18n.getLabel("JSConnectionPropsPane.checkManualSelect")); |
75 |
|
76 |
private final JLabel lReadTimeout = |
77 |
new JLabel(i18n.getLabel("JSConnectionPropsPane.lReadTimeout")); |
78 |
|
79 |
private final JSpinner spinnerTimeout = new JSpinner(new SpinnerNumberModel(0, 0, 2000, 1)); |
80 |
|
81 |
private final ServerListPane serverListPane; |
82 |
|
83 |
|
84 |
/** Creates a new instance of <code>JSConnectionPropsPane</code> */ |
85 |
public |
86 |
JSConnectionPropsPane() { |
87 |
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
88 |
|
89 |
boolean b = preferences().getBoolProperty(MANUAL_SERVER_SELECT_ON_STARTUP); |
90 |
checkManualSelect.setSelected(b); |
91 |
|
92 |
checkManualSelect.setAlignmentX(LEFT_ALIGNMENT); |
93 |
add(checkManualSelect); |
94 |
add(Box.createRigidArea(new Dimension(0, 6))); |
95 |
|
96 |
JPanel p = new JPanel(); |
97 |
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); |
98 |
|
99 |
p.add(lReadTimeout); |
100 |
p.add(Box.createRigidArea(new Dimension(6, 0))); |
101 |
|
102 |
int i = preferences().getIntProperty(SOCKET_READ_TIMEOUT); |
103 |
spinnerTimeout.setValue(i); |
104 |
p.add(spinnerTimeout); |
105 |
|
106 |
p.setAlignmentX(LEFT_ALIGNMENT); |
107 |
p.setMaximumSize(new Dimension(Short.MAX_VALUE, p.getPreferredSize().height)); |
108 |
add(p); |
109 |
add(Box.createRigidArea(new Dimension(0, 6))); |
110 |
|
111 |
serverListPane = createServerListPane(); |
112 |
add(serverListPane); |
113 |
setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); |
114 |
} |
115 |
|
116 |
private ServerListPane |
117 |
createServerListPane() { |
118 |
ServerListPane p = new ServerListPane(); |
119 |
p.setPreferredSize(new Dimension(200, 200)); |
120 |
p.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); |
121 |
|
122 |
String s = i18n.getLabel("JSConnectionPropsPane.title"); |
123 |
p.setBorder(BorderFactory.createTitledBorder(s)); |
124 |
p.setAlignmentX(LEFT_ALIGNMENT); |
125 |
|
126 |
return p; |
127 |
} |
128 |
|
129 |
private static JSPrefs |
130 |
preferences() { return CC.getViewConfig().preferences(); } |
131 |
|
132 |
/** |
133 |
* Gets the read timeout in seconds. |
134 |
*/ |
135 |
public int |
136 |
getReadTimeout() { return Integer.parseInt(spinnerTimeout.getValue().toString()); } |
137 |
|
138 |
public void |
139 |
apply() { |
140 |
boolean b = checkManualSelect.isSelected(); |
141 |
preferences().setBoolProperty(MANUAL_SERVER_SELECT_ON_STARTUP, b); |
142 |
|
143 |
preferences().setIntProperty(SOCKET_READ_TIMEOUT, getReadTimeout()); |
144 |
|
145 |
CC.setClientReadTimeout(getReadTimeout()); |
146 |
|
147 |
int i = serverListPane.serverTable.getSelectedRow(); |
148 |
if(i != -1) preferences().setIntProperty(SERVER_INDEX, i); |
149 |
} |
150 |
|
151 |
public static class ServerListPane extends JPanel { |
152 |
protected final ServerTable serverTable; |
153 |
|
154 |
private final JButton btnAdd = new JButton(i18n.getButtonLabel("add")); |
155 |
private final JButton btnRemove = new JButton(i18n.getButtonLabel("remove")); |
156 |
|
157 |
private final JButton btnMoveUp = |
158 |
new JButton(i18n.getButtonLabel("JSConnectionPropsPane.btnMoveUp")); |
159 |
|
160 |
private final JButton btnMoveDown = |
161 |
new JButton(i18n.getButtonLabel("JSConnectionPropsPane.btnMoveDown")); |
162 |
|
163 |
public |
164 |
ServerListPane() { |
165 |
setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); |
166 |
|
167 |
ServerTableModel model = new ServerTableModel(CC.getServerList()); |
168 |
serverTable = new ServerTable(model); |
169 |
|
170 |
add(new JScrollPane(serverTable)); |
171 |
|
172 |
int h = btnAdd.getPreferredSize().height; |
173 |
btnAdd.setMaximumSize(new Dimension(Short.MAX_VALUE, h)); |
174 |
|
175 |
h = btnRemove.getPreferredSize().height; |
176 |
btnRemove.setMaximumSize(new Dimension(Short.MAX_VALUE, h)); |
177 |
|
178 |
h = btnMoveUp.getPreferredSize().height; |
179 |
btnMoveUp.setMaximumSize(new Dimension(Short.MAX_VALUE, h)); |
180 |
|
181 |
h = btnMoveDown.getPreferredSize().height; |
182 |
btnMoveDown.setMaximumSize(new Dimension(Short.MAX_VALUE, h)); |
183 |
|
184 |
JPanel p = new JPanel(); |
185 |
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
186 |
p.add(btnAdd); |
187 |
p.add(Box.createRigidArea(new Dimension(0, 5))); |
188 |
p.add(btnRemove); |
189 |
p.add(Box.createRigidArea(new Dimension(0, 17))); |
190 |
p.add(btnMoveUp); |
191 |
p.add(Box.createRigidArea(new Dimension(0, 5))); |
192 |
p.add(btnMoveDown); |
193 |
p.add(Box.createGlue()); |
194 |
|
195 |
p.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0)); |
196 |
|
197 |
add(p); |
198 |
|
199 |
installListeners(); |
200 |
|
201 |
int i = preferences().getIntProperty(JSPrefs.SERVER_INDEX); |
202 |
int size = CC.getServerList().getServerCount(); |
203 |
if(size > 0) { |
204 |
if(i < 0 || i >= size) { |
205 |
serverTable.getSelectionModel().setSelectionInterval(0, 0); |
206 |
} else { |
207 |
serverTable.getSelectionModel().setSelectionInterval(i, i); |
208 |
} |
209 |
} |
210 |
} |
211 |
|
212 |
private void |
213 |
installListeners() { |
214 |
btnAdd.addActionListener(new ActionListener() { |
215 |
public void |
216 |
actionPerformed(ActionEvent e) { addServer(); } |
217 |
}); |
218 |
|
219 |
btnRemove.addActionListener(new ActionListener() { |
220 |
public void |
221 |
actionPerformed(ActionEvent e) { |
222 |
removeServer(); |
223 |
} |
224 |
}); |
225 |
|
226 |
btnMoveUp.addActionListener(new ActionListener() { |
227 |
public void |
228 |
actionPerformed(ActionEvent e) { |
229 |
serverTable.moveSelectedServerUp(); |
230 |
} |
231 |
}); |
232 |
|
233 |
btnMoveDown.addActionListener(new ActionListener() { |
234 |
public void |
235 |
actionPerformed(ActionEvent e) { |
236 |
serverTable.moveSelectedServerDown(); |
237 |
} |
238 |
}); |
239 |
|
240 |
ServerSelectionHandler l = new ServerSelectionHandler(); |
241 |
serverTable.getSelectionModel().addListSelectionListener(l); |
242 |
} |
243 |
|
244 |
private void |
245 |
addServer() { |
246 |
int i = serverTable.getSelectedRow(); |
247 |
|
248 |
JSAddServerDlg dlg; |
249 |
Window w = JuifeUtils.getWindow(this); |
250 |
if(w instanceof Dialog) dlg = new JSAddServerDlg((Dialog)w); |
251 |
else if(w instanceof Frame) dlg = new JSAddServerDlg((Frame)w); |
252 |
else dlg = new JSAddServerDlg(CC.getMainFrame()); |
253 |
|
254 |
dlg.setVisible(true); |
255 |
if(dlg.isCancelled()) return; |
256 |
|
257 |
serverTable.getSelectionModel().setSelectionInterval(i, i); |
258 |
} |
259 |
|
260 |
private void |
261 |
removeServer() { |
262 |
if(CC.getServerList().getServerCount() < 2) { |
263 |
Window w = JuifeUtils.getWindow(this); |
264 |
String s = i18n.getError("JSConnectionPropsPane.cantRemove"); |
265 |
if(w instanceof Dialog) HF.showErrorMessage(s, (Dialog)w); |
266 |
else if(w instanceof Frame) HF.showErrorMessage(s, (Frame)w); |
267 |
return; |
268 |
} |
269 |
|
270 |
serverTable.removeSelectedServer(); |
271 |
} |
272 |
|
273 |
private class ServerSelectionHandler implements ListSelectionListener { |
274 |
public void |
275 |
valueChanged(ListSelectionEvent e) { |
276 |
if(e.getValueIsAdjusting()) return; |
277 |
|
278 |
if(serverTable.getSelectedServer() == null) { |
279 |
btnMoveUp.setEnabled(false); |
280 |
btnMoveDown.setEnabled(false); |
281 |
return; |
282 |
} |
283 |
|
284 |
int idx = serverTable.getSelectedRow(); |
285 |
btnMoveUp.setEnabled(idx != 0); |
286 |
btnMoveDown.setEnabled(idx != serverTable.getRowCount() - 1); |
287 |
} |
288 |
} |
289 |
} |
290 |
} |