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 |
|
32 |
import javax.swing.JLabel; |
33 |
import javax.swing.JPanel; |
34 |
import javax.swing.JTextField; |
35 |
|
36 |
import javax.swing.event.DocumentEvent; |
37 |
import javax.swing.event.DocumentListener; |
38 |
|
39 |
import net.sf.juife.OkCancelDialog; |
40 |
import org.jsampler.HF; |
41 |
|
42 |
import org.jsampler.CC; |
43 |
import org.jsampler.Server; |
44 |
|
45 |
import static org.jsampler.view.std.StdI18n.i18n; |
46 |
|
47 |
/** |
48 |
* |
49 |
* @author Grigor Iliev |
50 |
*/ |
51 |
public class JSAddServerDlg extends OkCancelDialog { |
52 |
private final JLabel lName = new JLabel(i18n.getLabel("JSAddServerDlg.lName")); |
53 |
private final JLabel lDesc = new JLabel(i18n.getLabel("JSAddServerDlg.lDesc")); |
54 |
private final JLabel lAddress = new JLabel(i18n.getLabel("JSAddServerDlg.lAddress")); |
55 |
private final JLabel lPort = new JLabel(i18n.getLabel("JSAddServerDlg.lPort")); |
56 |
|
57 |
private final JTextField tfName = new JTextField(); |
58 |
private final JTextField tfDesc = new JTextField(); |
59 |
private final JTextField tfAddress = new JTextField(); |
60 |
private final JTextField tfPort = new JTextField(); |
61 |
|
62 |
|
63 |
/** |
64 |
* Creates a new instance of <code>JSAddServerDlg</code> |
65 |
* @param owner Specifies the <code>Frame</code> from which this dialog is displayed. |
66 |
*/ |
67 |
public |
68 |
JSAddServerDlg(Frame owner) { |
69 |
super(owner, i18n.getLabel("JSAddServerDlg.title")); |
70 |
|
71 |
initJSAddServerDlg(); |
72 |
} |
73 |
|
74 |
/** |
75 |
* Creates a new instance of <code>JSAddServerDlg</code> |
76 |
* @param owner Specifies the <code>Dialog</code> from which this dialog is displayed. |
77 |
*/ |
78 |
public |
79 |
JSAddServerDlg(Dialog owner) { |
80 |
super(owner, i18n.getLabel("JSAddServerDlg.title")); |
81 |
|
82 |
initJSAddServerDlg(); |
83 |
} |
84 |
|
85 |
private void |
86 |
initJSAddServerDlg() { |
87 |
btnOk.setEnabled(false); |
88 |
|
89 |
JPanel mainPane = new JPanel(); |
90 |
GridBagLayout gridbag = new GridBagLayout(); |
91 |
GridBagConstraints c = new GridBagConstraints(); |
92 |
|
93 |
mainPane.setLayout(gridbag); |
94 |
|
95 |
c.fill = GridBagConstraints.NONE; |
96 |
|
97 |
c.gridx = 0; |
98 |
c.gridy = 0; |
99 |
c.anchor = GridBagConstraints.EAST; |
100 |
c.insets = new Insets(3, 3, 3, 3); |
101 |
gridbag.setConstraints(lName, c); |
102 |
mainPane.add(lName); |
103 |
|
104 |
c.gridx = 0; |
105 |
c.gridy = 1; |
106 |
gridbag.setConstraints(lDesc, c); |
107 |
mainPane.add(lDesc); |
108 |
|
109 |
c.gridx = 0; |
110 |
c.gridy = 2; |
111 |
gridbag.setConstraints(lAddress, c); |
112 |
mainPane.add(lAddress); |
113 |
|
114 |
c.gridx = 0; |
115 |
c.gridy = 3; |
116 |
gridbag.setConstraints(lPort, c); |
117 |
mainPane.add(lPort); |
118 |
|
119 |
c.fill = GridBagConstraints.HORIZONTAL; |
120 |
c.gridx = 1; |
121 |
c.gridy = 0; |
122 |
c.weightx = 1.0; |
123 |
c.anchor = GridBagConstraints.WEST; |
124 |
gridbag.setConstraints(tfName, c); |
125 |
mainPane.add(tfName); |
126 |
|
127 |
c.gridx = 1; |
128 |
c.gridy = 1; |
129 |
gridbag.setConstraints(tfDesc, c); |
130 |
mainPane.add(tfDesc); |
131 |
|
132 |
c.gridx = 1; |
133 |
c.gridy = 2; |
134 |
gridbag.setConstraints(tfAddress, c); |
135 |
mainPane.add(tfAddress); |
136 |
|
137 |
c.gridx = 1; |
138 |
c.gridy = 3; |
139 |
gridbag.setConstraints(tfPort, c); |
140 |
mainPane.add(tfPort); |
141 |
|
142 |
setMainPane(mainPane); |
143 |
setResizable(true); |
144 |
|
145 |
int w = getPreferredSize().width; |
146 |
Dimension d = new Dimension(w > 300 ? w : 300, getPreferredSize().height); |
147 |
setPreferredSize(d); |
148 |
setMinimumSize(d); |
149 |
pack(); |
150 |
|
151 |
setLocationRelativeTo(getOwner()); |
152 |
|
153 |
tfName.getDocument().addDocumentListener(getHandler()); |
154 |
tfAddress.getDocument().addDocumentListener(getHandler()); |
155 |
tfPort.getDocument().addDocumentListener(getHandler()); |
156 |
} |
157 |
|
158 |
protected void |
159 |
onOk() { |
160 |
if(!btnOk.isEnabled()) return; |
161 |
|
162 |
Server server = new Server(); |
163 |
server.setName(tfName.getText()); |
164 |
server.setDescription(tfDesc.getText()); |
165 |
server.setAddress(tfAddress.getText()); |
166 |
|
167 |
int p; |
168 |
String s = tfPort.getText(); |
169 |
try { p = Integer.parseInt(s); } |
170 |
catch(Exception x) { |
171 |
HF.showErrorMessage(i18n.getError("JSAddServerDlg.invalidPort", s), this); |
172 |
return; |
173 |
} |
174 |
|
175 |
if(p < 0 || p > 0xffff) { |
176 |
HF.showErrorMessage(i18n.getError("JSAddServerDlg.invalidPort", s), this); |
177 |
return; |
178 |
} |
179 |
|
180 |
server.setPort(p); |
181 |
CC.getServerList().addServer(server); |
182 |
|
183 |
setVisible(false); |
184 |
setCancelled(false); |
185 |
} |
186 |
|
187 |
private void |
188 |
updateState() { |
189 |
boolean b = true; |
190 |
if(tfName.getText().length() == 0) b = false; |
191 |
if(tfAddress.getText().length() == 0) b = false; |
192 |
if(tfPort.getText().length() == 0) b = false; |
193 |
|
194 |
btnOk.setEnabled(b); |
195 |
} |
196 |
|
197 |
protected void |
198 |
onCancel() { setVisible(false); } |
199 |
|
200 |
private final Handler eventHandler = new Handler(); |
201 |
|
202 |
private Handler |
203 |
getHandler() { return eventHandler; } |
204 |
|
205 |
private class Handler implements DocumentListener { |
206 |
// DocumentListener |
207 |
public void |
208 |
insertUpdate(DocumentEvent e) { updateState(); } |
209 |
|
210 |
public void |
211 |
removeUpdate(DocumentEvent e) { updateState(); } |
212 |
|
213 |
public void |
214 |
changedUpdate(DocumentEvent e) { updateState(); } |
215 |
} |
216 |
} |