1 /********************************************************************************
2 * InternetCafe is a software solution that helps the management of Cybercafes
3 * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4 * Copyright (C) 2006 Guido Angelo Ingenito
5
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *******************************************************************************/
20 package ui.panel;
21
22 import java.awt.BorderLayout;
23 import java.awt.Color;
24 import java.awt.GridLayout;
25 import java.net.Inet4Address;
26 import java.net.InetAddress;
27 import java.net.UnknownHostException;
28
29 import javax.swing.JCheckBox;
30 import javax.swing.JComboBox;
31 import javax.swing.JPanel;
32 import javax.swing.JTextField;
33 import javax.swing.border.EtchedBorder;
34 import javax.swing.border.TitledBorder;
35
36 import org.apache.log4j.Logger;
37
38 import ui.Messages;
39 import base.Control;
40 import base.network.Workstation;
41 import base.network.WorkstationType;
42
43 @SuppressWarnings("serial")
44 public class WorkstationPanel extends JPanel {
45
46 private static final transient Logger logger = Logger
47 .getLogger(WorkstationPanel.class.getName());
48
49 private JPanel namePanel;
50
51 private JTextField nameTextField;
52
53 private JPanel locationPanel;
54
55 private JTextField locationTextField;
56
57 private JPanel descriptionPanel;
58
59 private JTextField descriptionTextField;
60
61 private JPanel typePanel;
62
63 private JComboBox typeComboBox;
64
65 private JPanel addressPanel;
66
67 private JTextField addressTextField;
68
69 private JPanel portPanel;
70
71 private JTextField portTextField;
72
73 private JPanel inUsePanel;
74
75 private JCheckBox inUseCheckBox;
76
77 private Workstation workstation = null;
78
79 public WorkstationPanel(Workstation workstation) {
80 this.workstation = workstation;
81 initialize();
82 }
83
84 protected void initialize() {
85 this.setBorder(new EtchedBorder());
86 this.setLayout(new GridLayout(7, 1));
87 this.add(getNamePanel(), null);
88 this.add(getLocationPanel(), null);
89 this.add(getDescriptionPanel(), null);
90 this.add(getTypePanel(), null);
91 this.add(getAddressPanel(), null);
92 this.add(getPortPanel(), null);
93 this.add(getInUsePanel(), null);
94 }
95
96 /***
97 * @return Returns the addressPanel.
98 */
99 protected JPanel getAddressPanel() {
100 if (addressPanel == null) {
101 addressPanel = new JPanel();
102 addressPanel.setLayout(new BorderLayout());
103 TitledBorder titledBorder = new TitledBorder(Messages.getString("common.address"));
104 addressPanel.setBorder(titledBorder);
105 addressPanel.add(getAddressTextField(), BorderLayout.CENTER);
106 }
107 return addressPanel;
108 }
109
110 /***
111 * @return Returns the addressTextField.
112 */
113 protected JTextField getAddressTextField() {
114 if (addressTextField == null) {
115 addressTextField = new JTextField();
116 try {
117 addressTextField.setText((workstation == null || workstation
118 .getAddress() == null) ? InetAddress.getLocalHost()
119 .getHostAddress().toString() : workstation.getAddress()
120 .getHostAddress().toString());
121 } catch (UnknownHostException e) {
122 logger.error(e.getMessage());
123 e.printStackTrace();
124 }
125 }
126 return addressTextField;
127 }
128
129 /***
130 * @return Returns the descriptionPanel.
131 */
132 protected JPanel getDescriptionPanel() {
133 if (descriptionPanel == null) {
134 descriptionPanel = new JPanel();
135 descriptionPanel.setLayout(new BorderLayout());
136 TitledBorder titledBorder = new TitledBorder(Messages.getString("common.description"));
137 descriptionPanel.setBorder(titledBorder);
138 descriptionPanel
139 .add(getDescriptionTextField(), BorderLayout.CENTER);
140 }
141 return descriptionPanel;
142 }
143
144 /***
145 * @return Returns the descriptionTextField.
146 */
147 protected JTextField getDescriptionTextField() {
148 if (descriptionTextField == null) {
149 descriptionTextField = new JTextField();
150 descriptionTextField.setText(workstation == null ? "" : workstation
151 .getDescription());
152 }
153 return descriptionTextField;
154 }
155
156 /***
157 * @return Returns the locationPanel.
158 */
159 protected JPanel getLocationPanel() {
160 if (locationPanel == null) {
161 locationPanel = new JPanel();
162 locationPanel.setLayout(new BorderLayout());
163 TitledBorder titledBorder = new TitledBorder(Messages.getString("common.location"));
164 locationPanel.setBorder(titledBorder);
165 locationPanel.add(getLocationTextField(), BorderLayout.CENTER);
166 }
167 return locationPanel;
168 }
169
170 /***
171 * @return Returns the locationTextField.
172 */
173 protected JTextField getLocationTextField() {
174 if (locationTextField == null) {
175 locationTextField = new JTextField();
176 locationTextField.setText(workstation == null ? "" : workstation
177 .getLocation());
178 }
179 return locationTextField;
180 }
181
182 /***
183 * @return Returns the inUsePanel.
184 */
185 protected JPanel getInUsePanel() {
186 if (inUsePanel == null) {
187 inUsePanel = new JPanel();
188 inUsePanel.setLayout(new BorderLayout());
189 TitledBorder titledBorder = new TitledBorder(Messages.getString("common.inuse"));
190 inUsePanel.setBorder(titledBorder);
191
192 inUsePanel.add(getInUseCheckBox(), BorderLayout.CENTER);
193 }
194 return inUsePanel;
195 }
196
197 /***
198 * @return Returns the inUseCheckBox.
199 */
200 protected JCheckBox getInUseCheckBox() {
201 if (inUseCheckBox == null) {
202 inUseCheckBox = new JCheckBox();
203 inUseCheckBox.setSelected(workstation == null ? false : workstation
204 .isInUse());
205 }
206 return inUseCheckBox;
207 }
208
209 /***
210 * @return Returns the namePanel.
211 */
212 protected JPanel getNamePanel() {
213 if (namePanel == null) {
214 namePanel = new JPanel();
215 namePanel.setLayout(new BorderLayout());
216 TitledBorder titledBorder = new TitledBorder(Messages.getString("common.name"));
217 namePanel.setBorder(titledBorder);
218 namePanel.add(getNameTextField(), BorderLayout.CENTER);
219 }
220 return namePanel;
221 }
222
223 /***
224 * @return Returns the nameTextField.
225 */
226 protected JTextField getNameTextField() {
227 if (nameTextField == null) {
228 nameTextField = new JTextField();
229 nameTextField.setText(workstation == null ? "" : workstation
230 .getName());
231 }
232 return nameTextField;
233 }
234
235 /***
236 * @return Returns the portPanel.
237 */
238 protected JPanel getPortPanel() {
239 if (portPanel == null) {
240 portPanel = new JPanel();
241 portPanel.setLayout(new BorderLayout());
242 TitledBorder titledBorder = new TitledBorder(Messages.getString("common.port"));
243 portPanel.setBorder(titledBorder);
244 portPanel.add(getPortTextField(), BorderLayout.CENTER);
245 }
246 return portPanel;
247 }
248
249 /***
250 * @return Returns the portTextField.
251 */
252 protected JTextField getPortTextField() {
253 if (portTextField == null) {
254 portTextField = new JTextField();
255 portTextField.setText(workstation == null ? "" : "" + workstation.getPort());
256 }
257 return portTextField;
258 }
259
260 /***
261 * @return Returns the typePanel.
262 */
263 protected JPanel getTypePanel() {
264 if (typePanel == null) {
265 typePanel = new JPanel();
266 typePanel.setLayout(new BorderLayout());
267 TitledBorder titledBorder = new TitledBorder(Messages.getString("common.type"));
268 typePanel.setBorder(titledBorder);
269 typePanel.add(getTypeComboBox(), BorderLayout.CENTER);
270 }
271 return typePanel;
272 }
273
274 /***
275 * @return Returns the nameTextField.
276 */
277 protected JComboBox getTypeComboBox() {
278 if (typeComboBox == null) {
279 typeComboBox = new JComboBox();
280 for (int i = 0; i < WorkstationType.WORKSTATION_TYPE.length; i++)
281 typeComboBox.addItem(WorkstationType.WORKSTATION_TYPE[i]);
282 typeComboBox
283 .setSelectedItem(workstation == null ? WorkstationType.WORKSTATION_TYPE[WorkstationType.WINDOWS]
284 : workstation.getType());
285
286 }
287 return typeComboBox;
288 }
289
290 /***
291 * Resets to the default values all the fields contained in this panel.
292 */
293 public void clearWorkstationData() {
294 this.getDescriptionTextField().setText("");
295 this.getNameTextField().setText("");
296 }
297
298 public String getWorkstationName() {
299 return this.getNameTextField().getText();
300 }
301
302 public String getWorkstationLocation() {
303 return this.getLocationTextField().getText();
304 }
305
306 public String getWorkstationType() {
307 return (String) getTypeComboBox().getSelectedItem();
308 }
309
310 public String getWorkstationDescription() {
311 return this.getDescriptionTextField().getText();
312 }
313
314 public boolean getWorkstationInUse() {
315 return this.getInUseCheckBox().isSelected();
316 }
317
318 public Inet4Address getWorkstationAddress() {
319 Inet4Address result = null;
320 try {
321 String ipAddress = this.getAddressTextField().getText();
322 if (Control.isValidIPAddress(ipAddress)) {
323 result = (Inet4Address) Inet4Address.getByName(ipAddress);
324 setAddressError(false);
325 }
326 } catch (UnknownHostException e) {
327 setAddressError(true);
328 logger.error(e.getMessage());
329 e.printStackTrace();
330 }
331 return result;
332 }
333
334 public int getWorkstationPort() {
335 int result = 0;
336
337 if (Control.isValidIntegerOnlyString(this.getPortTextField().getText()))
338 result = new Integer(this.getPortTextField().getText());
339 else
340 result = workstation != null ? workstation.getPort() : 9000;
341 return result;
342 }
343
344 public void setNameError(boolean isError) {
345 if (isError)
346 ((TitledBorder) this.getNamePanel().getBorder())
347 .setTitleColor(Color.RED);
348 else
349 ((TitledBorder) this.getNamePanel().getBorder())
350 .setTitleColor(new TitledBorder("").getTitleColor());
351 this.getNamePanel().updateUI();
352 }
353
354 public void setPortError(boolean isError) {
355 if (isError)
356 ((TitledBorder) this.getPortPanel().getBorder())
357 .setTitleColor(Color.RED);
358 else
359 ((TitledBorder) this.getPortPanel().getBorder())
360 .setTitleColor(new TitledBorder("").getTitleColor());
361 this.getPortPanel().updateUI();
362 }
363
364 public void setAddressError(boolean isError) {
365 if (isError)
366 ((TitledBorder) this.getAddressPanel().getBorder())
367 .setTitleColor(Color.RED);
368 else
369 ((TitledBorder) this.getAddressPanel().getBorder())
370 .setTitleColor(new TitledBorder("").getTitleColor());
371 this.getAddressPanel().updateUI();
372 }
373
374 }