1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2007 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.Dimension; |
26 |
import java.awt.GridBagConstraints; |
27 |
import java.awt.GridBagLayout; |
28 |
import java.awt.Insets; |
29 |
|
30 |
import javax.swing.BorderFactory; |
31 |
import javax.swing.JLabel; |
32 |
import javax.swing.JOptionPane; |
33 |
import javax.swing.JPanel; |
34 |
import javax.swing.JTextField; |
35 |
|
36 |
import org.jsampler.CC; |
37 |
import org.jsampler.Prefs; |
38 |
|
39 |
import org.jsampler.task.SetServerAddress; |
40 |
|
41 |
import static org.jsampler.view.std.StdI18n.i18n; |
42 |
|
43 |
|
44 |
/** |
45 |
* |
46 |
* @author Grigor Iliev |
47 |
*/ |
48 |
public class JSConnectionPropsPane extends JPanel { |
49 |
private final JLabel lAddress = new JLabel(i18n.getLabel("JSConnectionPropsPane.address")); |
50 |
private final JLabel lPort = new JLabel(i18n.getLabel("JSConnectionPropsPane.port")); |
51 |
private final JTextField tfAddress = new JTextField(); |
52 |
private final JTextField tfPort = new JTextField(); |
53 |
|
54 |
|
55 |
/** Creates a new instance of <code>JSConnectionPropsPane</code> */ |
56 |
public |
57 |
JSConnectionPropsPane() { |
58 |
GridBagLayout gridbag = new GridBagLayout(); |
59 |
GridBagConstraints c = new GridBagConstraints(); |
60 |
|
61 |
setLayout(gridbag); |
62 |
|
63 |
// Set preferred size for username & password fields |
64 |
int w1 = (int) tfAddress.getMinimumSize().getWidth(); |
65 |
int h1 = (int) tfAddress.getMinimumSize().getHeight(); |
66 |
Dimension d = new Dimension(w1 > 150 ? w1 : 150, h1); |
67 |
tfAddress.setMinimumSize(d); |
68 |
tfAddress.setPreferredSize(d); |
69 |
|
70 |
w1 = (int) tfPort.getMinimumSize().getWidth(); |
71 |
h1 = (int) tfPort.getMinimumSize().getHeight(); |
72 |
d = new Dimension(w1 > 150 ? w1 : 150, h1); |
73 |
tfPort.setMinimumSize(d); |
74 |
tfPort.setPreferredSize(d); |
75 |
|
76 |
c.fill = GridBagConstraints.NONE; |
77 |
|
78 |
c.gridx = 0; |
79 |
c.gridy = 0; |
80 |
c.anchor = GridBagConstraints.EAST; |
81 |
c.insets = new Insets(3, 3, 3, 3); |
82 |
gridbag.setConstraints(lAddress, c); |
83 |
add(lAddress); |
84 |
|
85 |
c.gridx = 0; |
86 |
c.gridy = 1; |
87 |
gridbag.setConstraints(lPort, c); |
88 |
add(lPort); |
89 |
|
90 |
c.fill = GridBagConstraints.HORIZONTAL; |
91 |
c.gridx = 1; |
92 |
c.gridy = 0; |
93 |
c.weightx = 1.0; |
94 |
c.anchor = GridBagConstraints.WEST; |
95 |
gridbag.setConstraints(tfAddress, c); |
96 |
add(tfAddress); |
97 |
|
98 |
c.gridx = 1; |
99 |
c.gridy = 1; |
100 |
gridbag.setConstraints(tfPort, c); |
101 |
add(tfPort); |
102 |
|
103 |
String s = i18n.getLabel("JSConnectionPropsPane.title"); |
104 |
setBorder(BorderFactory.createTitledBorder(s)); |
105 |
setMaximumSize(new Dimension(Short.MAX_VALUE, getPreferredSize().height)); |
106 |
} |
107 |
|
108 |
public String |
109 |
getLSAddress() { return tfAddress.getText(); } |
110 |
|
111 |
public void |
112 |
setLSAddress(String address) { tfAddress.setText(address); } |
113 |
|
114 |
public String |
115 |
getLSPort() { return tfPort.getText(); } |
116 |
|
117 |
public void |
118 |
setLSPort(String port) { tfPort.setText(port); } |
119 |
|
120 |
public void |
121 |
apply() { |
122 |
Prefs.setLSAddress(getLSAddress()); |
123 |
|
124 |
boolean b = true; |
125 |
String s = getLSPort(); |
126 |
try { |
127 |
if(s.length() > 0) { |
128 |
int port = Integer.parseInt(s); |
129 |
if(port > 0 && port < 0xffff) |
130 |
Prefs.setLSPort(port); |
131 |
else b = false; |
132 |
} else Prefs.setLSPort(-1); // -1 resets to default value |
133 |
} catch(NumberFormatException x) { |
134 |
b = false; |
135 |
} |
136 |
|
137 |
if(!b) { |
138 |
JOptionPane.showMessageDialog ( |
139 |
this, |
140 |
i18n.getError("JSConnectionPropsPane.invalidPort", s), |
141 |
i18n.getError("error"), |
142 |
JOptionPane.ERROR_MESSAGE |
143 |
); |
144 |
|
145 |
return; |
146 |
} |
147 |
|
148 |
CC.getTaskQueue().add ( |
149 |
new SetServerAddress(Prefs.getLSAddress(), Prefs.getLSPort()) |
150 |
); |
151 |
} |
152 |
} |