1 |
/* |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
* |
4 |
* Copyright (C) 2005-2007 Grigor Iliev <grigor@grigoriliev.com> |
* Copyright (C) 2005-2008 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
* |
6 |
* This file is part of JSampler. |
* This file is part of JSampler. |
7 |
* |
* |
22 |
|
|
23 |
package org.jsampler.view.std; |
package org.jsampler.view.std; |
24 |
|
|
25 |
|
import java.awt.Dialog; |
26 |
import java.awt.Dimension; |
import java.awt.Dimension; |
27 |
|
import java.awt.Frame; |
28 |
import java.awt.GridBagConstraints; |
import java.awt.GridBagConstraints; |
29 |
import java.awt.GridBagLayout; |
import java.awt.GridBagLayout; |
30 |
import java.awt.Insets; |
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; |
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; |
import javax.swing.JLabel; |
42 |
import javax.swing.JOptionPane; |
import javax.swing.JOptionPane; |
43 |
import javax.swing.JPanel; |
import javax.swing.JPanel; |
44 |
|
import javax.swing.JScrollPane; |
45 |
import javax.swing.JTextField; |
import javax.swing.JTextField; |
46 |
|
|
47 |
|
import javax.swing.event.ListSelectionEvent; |
48 |
|
import javax.swing.event.ListSelectionListener; |
49 |
|
|
50 |
|
import net.sf.juife.JuifeUtils; |
51 |
|
|
52 |
import org.jsampler.CC; |
import org.jsampler.CC; |
53 |
|
import org.jsampler.HF; |
54 |
|
import org.jsampler.JSPrefs; |
55 |
import org.jsampler.Prefs; |
import org.jsampler.Prefs; |
56 |
|
|
57 |
import org.jsampler.task.SetServerAddress; |
import org.jsampler.task.SetServerAddress; |
58 |
|
|
59 |
|
import org.jsampler.view.ServerTable; |
60 |
|
import org.jsampler.view.ServerTableModel; |
61 |
|
|
62 |
import static org.jsampler.view.std.StdI18n.i18n; |
import static org.jsampler.view.std.StdI18n.i18n; |
63 |
|
import static org.jsampler.view.std.StdPrefs.*; |
64 |
|
|
65 |
|
|
66 |
/** |
/** |
68 |
* @author Grigor Iliev |
* @author Grigor Iliev |
69 |
*/ |
*/ |
70 |
public class JSConnectionPropsPane extends JPanel { |
public class JSConnectionPropsPane extends JPanel { |
71 |
private final JLabel lAddress = new JLabel(i18n.getLabel("JSConnectionPropsPane.address")); |
private final JCheckBox checkManualSelect = |
72 |
private final JLabel lPort = new JLabel(i18n.getLabel("JSConnectionPropsPane.port")); |
new JCheckBox(i18n.getLabel("JSConnectionPropsPane.checkManualSelect")); |
73 |
private final JTextField tfAddress = new JTextField(); |
|
74 |
private final JTextField tfPort = new JTextField(); |
private final ServerListPane serverListPane; |
75 |
|
|
76 |
|
|
77 |
/** Creates a new instance of <code>JSConnectionPropsPane</code> */ |
/** Creates a new instance of <code>JSConnectionPropsPane</code> */ |
78 |
public |
public |
79 |
JSConnectionPropsPane() { |
JSConnectionPropsPane() { |
80 |
GridBagLayout gridbag = new GridBagLayout(); |
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
|
GridBagConstraints c = new GridBagConstraints(); |
|
|
|
|
|
setLayout(gridbag); |
|
81 |
|
|
82 |
// Set preferred size for username & password fields |
boolean b = preferences().getBoolProperty(MANUAL_SERVER_SELECT_ON_STARTUP); |
83 |
int w1 = (int) tfAddress.getMinimumSize().getWidth(); |
checkManualSelect.setSelected(b); |
|
int h1 = (int) tfAddress.getMinimumSize().getHeight(); |
|
|
Dimension d = new Dimension(w1 > 150 ? w1 : 150, h1); |
|
|
tfAddress.setMinimumSize(d); |
|
|
tfAddress.setPreferredSize(d); |
|
|
|
|
|
w1 = (int) tfPort.getMinimumSize().getWidth(); |
|
|
h1 = (int) tfPort.getMinimumSize().getHeight(); |
|
|
d = new Dimension(w1 > 150 ? w1 : 150, h1); |
|
|
tfPort.setMinimumSize(d); |
|
|
tfPort.setPreferredSize(d); |
|
|
|
|
|
c.fill = GridBagConstraints.NONE; |
|
|
|
|
|
c.gridx = 0; |
|
|
c.gridy = 0; |
|
|
c.anchor = GridBagConstraints.EAST; |
|
|
c.insets = new Insets(3, 3, 3, 3); |
|
|
gridbag.setConstraints(lAddress, c); |
|
|
add(lAddress); |
|
|
|
|
|
c.gridx = 0; |
|
|
c.gridy = 1; |
|
|
gridbag.setConstraints(lPort, c); |
|
|
add(lPort); |
|
|
|
|
|
c.fill = GridBagConstraints.HORIZONTAL; |
|
|
c.gridx = 1; |
|
|
c.gridy = 0; |
|
|
c.weightx = 1.0; |
|
|
c.anchor = GridBagConstraints.WEST; |
|
|
gridbag.setConstraints(tfAddress, c); |
|
|
add(tfAddress); |
|
|
|
|
|
c.gridx = 1; |
|
|
c.gridy = 1; |
|
|
gridbag.setConstraints(tfPort, c); |
|
|
add(tfPort); |
|
84 |
|
|
85 |
String s = i18n.getLabel("JSConnectionPropsPane.title"); |
checkManualSelect.setAlignmentX(LEFT_ALIGNMENT); |
86 |
setBorder(BorderFactory.createTitledBorder(s)); |
add(checkManualSelect); |
87 |
setMaximumSize(new Dimension(Short.MAX_VALUE, getPreferredSize().height)); |
add(Box.createRigidArea(new Dimension(0, 6))); |
88 |
|
serverListPane = createServerListPane(); |
89 |
|
add(serverListPane); |
90 |
|
setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); |
91 |
} |
} |
92 |
|
|
93 |
public String |
private ServerListPane |
94 |
getLSAddress() { return tfAddress.getText(); } |
createServerListPane() { |
95 |
|
ServerListPane p = new ServerListPane(); |
96 |
public void |
p.setPreferredSize(new Dimension(200, 200)); |
97 |
setLSAddress(String address) { tfAddress.setText(address); } |
p.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); |
98 |
|
|
99 |
public String |
String s = i18n.getLabel("JSConnectionPropsPane.title"); |
100 |
getLSPort() { return tfPort.getText(); } |
p.setBorder(BorderFactory.createTitledBorder(s)); |
101 |
|
p.setAlignmentX(LEFT_ALIGNMENT); |
102 |
|
|
103 |
|
return p; |
104 |
|
} |
105 |
|
|
106 |
public void |
private static JSPrefs |
107 |
setLSPort(String port) { tfPort.setText(port); } |
preferences() { return CC.getViewConfig().preferences(); } |
108 |
|
|
109 |
public void |
public void |
110 |
apply() { |
apply() { |
111 |
Prefs.setLSAddress(getLSAddress()); |
boolean b = checkManualSelect.isSelected(); |
112 |
|
preferences().setBoolProperty(MANUAL_SERVER_SELECT_ON_STARTUP, b); |
113 |
|
|
114 |
|
int i = serverListPane.serverTable.getSelectedRow(); |
115 |
|
if(i != -1) preferences().setIntProperty(SERVER_INDEX, i); |
116 |
|
} |
117 |
|
|
118 |
|
public static class ServerListPane extends JPanel { |
119 |
|
protected final ServerTable serverTable; |
120 |
|
|
121 |
|
private final JButton btnAdd = new JButton(i18n.getButtonLabel("add")); |
122 |
|
private final JButton btnRemove = new JButton(i18n.getButtonLabel("remove")); |
123 |
|
|
124 |
|
private final JButton btnMoveUp = |
125 |
|
new JButton(i18n.getButtonLabel("JSConnectionPropsPane.btnMoveUp")); |
126 |
|
|
127 |
|
private final JButton btnMoveDown = |
128 |
|
new JButton(i18n.getButtonLabel("JSConnectionPropsPane.btnMoveDown")); |
129 |
|
|
130 |
|
public |
131 |
|
ServerListPane() { |
132 |
|
setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); |
133 |
|
|
134 |
|
ServerTableModel model = new ServerTableModel(CC.getServerList()); |
135 |
|
serverTable = new ServerTable(model); |
136 |
|
|
137 |
|
add(new JScrollPane(serverTable)); |
138 |
|
|
139 |
|
int h = btnAdd.getPreferredSize().height; |
140 |
|
btnAdd.setMaximumSize(new Dimension(Short.MAX_VALUE, h)); |
141 |
|
|
142 |
|
h = btnRemove.getPreferredSize().height; |
143 |
|
btnRemove.setMaximumSize(new Dimension(Short.MAX_VALUE, h)); |
144 |
|
|
145 |
|
h = btnMoveUp.getPreferredSize().height; |
146 |
|
btnMoveUp.setMaximumSize(new Dimension(Short.MAX_VALUE, h)); |
147 |
|
|
148 |
|
h = btnMoveDown.getPreferredSize().height; |
149 |
|
btnMoveDown.setMaximumSize(new Dimension(Short.MAX_VALUE, h)); |
150 |
|
|
151 |
|
JPanel p = new JPanel(); |
152 |
|
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
153 |
|
p.add(btnAdd); |
154 |
|
p.add(Box.createRigidArea(new Dimension(0, 5))); |
155 |
|
p.add(btnRemove); |
156 |
|
p.add(Box.createRigidArea(new Dimension(0, 17))); |
157 |
|
p.add(btnMoveUp); |
158 |
|
p.add(Box.createRigidArea(new Dimension(0, 5))); |
159 |
|
p.add(btnMoveDown); |
160 |
|
p.add(Box.createGlue()); |
161 |
|
|
162 |
|
p.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0)); |
163 |
|
|
164 |
|
add(p); |
165 |
|
|
166 |
|
installListeners(); |
167 |
|
|
168 |
|
int i = preferences().getIntProperty(JSPrefs.SERVER_INDEX); |
169 |
|
int size = CC.getServerList().getServerCount(); |
170 |
|
if(size > 0) { |
171 |
|
if(i < 0 || i >= size) { |
172 |
|
serverTable.getSelectionModel().setSelectionInterval(0, 0); |
173 |
|
} else { |
174 |
|
serverTable.getSelectionModel().setSelectionInterval(i, i); |
175 |
|
} |
176 |
|
} |
177 |
|
} |
178 |
|
|
179 |
boolean b = true; |
private void |
180 |
String s = getLSPort(); |
installListeners() { |
181 |
try { |
btnAdd.addActionListener(new ActionListener() { |
182 |
if(s.length() > 0) { |
public void |
183 |
int port = Integer.parseInt(s); |
actionPerformed(ActionEvent e) { addServer(); } |
184 |
if(port > 0 && port < 0xffff) |
}); |
185 |
Prefs.setLSPort(port); |
|
186 |
else b = false; |
btnRemove.addActionListener(new ActionListener() { |
187 |
} else Prefs.setLSPort(-1); // -1 resets to default value |
public void |
188 |
} catch(NumberFormatException x) { |
actionPerformed(ActionEvent e) { |
189 |
b = false; |
removeServer(); |
190 |
|
} |
191 |
|
}); |
192 |
|
|
193 |
|
btnMoveUp.addActionListener(new ActionListener() { |
194 |
|
public void |
195 |
|
actionPerformed(ActionEvent e) { |
196 |
|
serverTable.moveSelectedServerUp(); |
197 |
|
} |
198 |
|
}); |
199 |
|
|
200 |
|
btnMoveDown.addActionListener(new ActionListener() { |
201 |
|
public void |
202 |
|
actionPerformed(ActionEvent e) { |
203 |
|
serverTable.moveSelectedServerDown(); |
204 |
|
} |
205 |
|
}); |
206 |
|
|
207 |
|
ServerSelectionHandler l = new ServerSelectionHandler(); |
208 |
|
serverTable.getSelectionModel().addListSelectionListener(l); |
209 |
} |
} |
210 |
|
|
211 |
if(!b) { |
private void |
212 |
JOptionPane.showMessageDialog ( |
addServer() { |
213 |
this, |
int i = serverTable.getSelectedRow(); |
214 |
i18n.getError("JSConnectionPropsPane.invalidPort", s), |
|
215 |
i18n.getError("error"), |
JSAddServerDlg dlg; |
216 |
JOptionPane.ERROR_MESSAGE |
Window w = JuifeUtils.getWindow(this); |
217 |
); |
if(w instanceof Dialog) dlg = new JSAddServerDlg((Dialog)w); |
218 |
|
else if(w instanceof Frame) dlg = new JSAddServerDlg((Frame)w); |
219 |
|
else dlg = new JSAddServerDlg(CC.getMainFrame()); |
220 |
|
|
221 |
return; |
dlg.setVisible(true); |
222 |
|
if(dlg.isCancelled()) return; |
223 |
|
|
224 |
|
serverTable.getSelectionModel().setSelectionInterval(i, i); |
225 |
} |
} |
226 |
|
|
227 |
CC.getTaskQueue().add ( |
private void |
228 |
new SetServerAddress(Prefs.getLSAddress(), Prefs.getLSPort()) |
removeServer() { |
229 |
); |
if(CC.getServerList().getServerCount() < 2) { |
230 |
|
Window w = JuifeUtils.getWindow(this); |
231 |
|
String s = i18n.getError("JSConnectionPropsPane.cantRemove"); |
232 |
|
if(w instanceof Dialog) HF.showErrorMessage(s, (Dialog)w); |
233 |
|
else if(w instanceof Frame) HF.showErrorMessage(s, (Frame)w); |
234 |
|
return; |
235 |
|
} |
236 |
|
|
237 |
|
serverTable.removeSelectedServer(); |
238 |
|
} |
239 |
|
|
240 |
|
private class ServerSelectionHandler implements ListSelectionListener { |
241 |
|
public void |
242 |
|
valueChanged(ListSelectionEvent e) { |
243 |
|
if(e.getValueIsAdjusting()) return; |
244 |
|
|
245 |
|
if(serverTable.getSelectedServer() == null) { |
246 |
|
btnMoveUp.setEnabled(false); |
247 |
|
btnMoveDown.setEnabled(false); |
248 |
|
return; |
249 |
|
} |
250 |
|
|
251 |
|
int idx = serverTable.getSelectedRow(); |
252 |
|
btnMoveUp.setEnabled(idx != 0); |
253 |
|
btnMoveDown.setEnabled(idx != serverTable.getRowCount() - 1); |
254 |
|
} |
255 |
|
} |
256 |
} |
} |
257 |
} |
} |