Coverage details for ui.panel.WorkstationPanel

LineHitsSource
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  
430@SuppressWarnings("serial") //$NON-NLS-1$
440public class WorkstationPanel extends JPanel {
450 
460    private static final transient Logger logger = Logger
470            .getLogger(WorkstationPanel.class.getName());
480 
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;
760 
770    private Workstation workstation = null;
780 
790    public WorkstationPanel(Workstation workstation) {
800        this.workstation = workstation;
810        initialize();
820    }
830 
840    protected void initialize() {
850        this.setBorder(new EtchedBorder());
860        this.setLayout(new GridLayout(7, 1));
870        this.add(getNamePanel(), null);
880        this.add(getLocationPanel(), null);
890        this.add(getDescriptionPanel(), null);
900        this.add(getTypePanel(), null);
910        this.add(getAddressPanel(), null);
920        this.add(getPortPanel(), null);
930        this.add(getInUsePanel(), null);
940    }
950 
96     /**
97      * @return Returns the addressPanel.
98      */
990    protected JPanel getAddressPanel() {
1000        if (addressPanel == null) {
1010            addressPanel = new JPanel();
1020            addressPanel.setLayout(new BorderLayout());
1030            TitledBorder titledBorder = new TitledBorder(Messages.getString("common.address")); //$NON-NLS-1$
1040            addressPanel.setBorder(titledBorder);
1050            addressPanel.add(getAddressTextField(), BorderLayout.CENTER);
1060        }
1070        return addressPanel;
1080    }
109  
110     /**
111      * @return Returns the addressTextField.
112      */
1130    protected JTextField getAddressTextField() {
1140        if (addressTextField == null) {
1150            addressTextField = new JTextField();
1160            try {
1170                addressTextField.setText((workstation == null || workstation
1180                        .getAddress() == null) ? InetAddress.getLocalHost()
1190                        .getHostAddress().toString() : workstation.getAddress()
1200                        .getHostAddress().toString());
1210            } catch (UnknownHostException e) {
1220                logger.error(e.getMessage());
1230                e.printStackTrace();
1240            }
1250        }
1260        return addressTextField;
1270    }
128  
129     /**
130      * @return Returns the descriptionPanel.
131      */
1320    protected JPanel getDescriptionPanel() {
1330        if (descriptionPanel == null) {
1340            descriptionPanel = new JPanel();
1350            descriptionPanel.setLayout(new BorderLayout());
1360            TitledBorder titledBorder = new TitledBorder(Messages.getString("common.description")); //$NON-NLS-1$
1370            descriptionPanel.setBorder(titledBorder);
1380            descriptionPanel
1390                    .add(getDescriptionTextField(), BorderLayout.CENTER);
1400        }
1410        return descriptionPanel;
1420    }
143  
144     /**
145      * @return Returns the descriptionTextField.
146      */
1470    protected JTextField getDescriptionTextField() {
1480        if (descriptionTextField == null) {
1490            descriptionTextField = new JTextField();
1500            descriptionTextField.setText(workstation == null ? "" : workstation //$NON-NLS-1$
1510                    .getDescription());
1520        }
1530        return descriptionTextField;
1540    }
155  
156     /**
157      * @return Returns the locationPanel.
158      */
1590    protected JPanel getLocationPanel() {
1600        if (locationPanel == null) {
1610            locationPanel = new JPanel();
1620            locationPanel.setLayout(new BorderLayout());
1630            TitledBorder titledBorder = new TitledBorder(Messages.getString("common.location")); //$NON-NLS-1$
1640            locationPanel.setBorder(titledBorder);
1650            locationPanel.add(getLocationTextField(), BorderLayout.CENTER);
1660        }
1670        return locationPanel;
1680    }
169  
170     /**
171      * @return Returns the locationTextField.
172      */
1730    protected JTextField getLocationTextField() {
1740        if (locationTextField == null) {
1750            locationTextField = new JTextField();
1760            locationTextField.setText(workstation == null ? "" : workstation //$NON-NLS-1$
1770                    .getLocation());
1780        }
1790        return locationTextField;
1800    }
181  
182     /**
183      * @return Returns the inUsePanel.
184      */
1850    protected JPanel getInUsePanel() {
1860        if (inUsePanel == null) {
1870            inUsePanel = new JPanel();
1880            inUsePanel.setLayout(new BorderLayout());
1890            TitledBorder titledBorder = new TitledBorder(Messages.getString("common.inuse")); //$NON-NLS-1$
1900            inUsePanel.setBorder(titledBorder);
1910 
1920            inUsePanel.add(getInUseCheckBox(), BorderLayout.CENTER);
1930        }
1940        return inUsePanel;
1950    }
196  
197     /**
198      * @return Returns the inUseCheckBox.
199      */
2000    protected JCheckBox getInUseCheckBox() {
2010        if (inUseCheckBox == null) {
2020            inUseCheckBox = new JCheckBox();
2030            inUseCheckBox.setSelected(workstation == null ? false : workstation
2040                    .isInUse());
2050        }
2060        return inUseCheckBox;
2070    }
208  
209     /**
210      * @return Returns the namePanel.
211      */
2120    protected JPanel getNamePanel() {
2130        if (namePanel == null) {
2140            namePanel = new JPanel();
2150            namePanel.setLayout(new BorderLayout());
2160            TitledBorder titledBorder = new TitledBorder(Messages.getString("common.name")); //$NON-NLS-1$
2170            namePanel.setBorder(titledBorder);
2180            namePanel.add(getNameTextField(), BorderLayout.CENTER);
2190        }
2200        return namePanel;
2210    }
222  
223     /**
224      * @return Returns the nameTextField.
225      */
2260    protected JTextField getNameTextField() {
2270        if (nameTextField == null) {
2280            nameTextField = new JTextField();
2290            nameTextField.setText(workstation == null ? "" : workstation //$NON-NLS-1$
2300                    .getName());
2310        }
2320        return nameTextField;
2330    }
234  
235     /**
236      * @return Returns the portPanel.
237      */
2380    protected JPanel getPortPanel() {
2390        if (portPanel == null) {
2400            portPanel = new JPanel();
2410            portPanel.setLayout(new BorderLayout());
2420            TitledBorder titledBorder = new TitledBorder(Messages.getString("common.port")); //$NON-NLS-1$
2430            portPanel.setBorder(titledBorder);
2440            portPanel.add(getPortTextField(), BorderLayout.CENTER);
2450        }
2460        return portPanel;
2470    }
248  
249     /**
250      * @return Returns the portTextField.
251      */
2520    protected JTextField getPortTextField() {
2530        if (portTextField == null) {
2540            portTextField = new JTextField();
2550            portTextField.setText(workstation == null ? "" : "" + workstation.getPort()); //$NON-NLS-1$ //$NON-NLS-2$
2560        }
2570        return portTextField;
2580    }
259  
260     /**
261      * @return Returns the typePanel.
262      */
2630    protected JPanel getTypePanel() {
2640        if (typePanel == null) {
2650            typePanel = new JPanel();
2660            typePanel.setLayout(new BorderLayout());
2670            TitledBorder titledBorder = new TitledBorder(Messages.getString("common.type")); //$NON-NLS-1$
2680            typePanel.setBorder(titledBorder);
2690            typePanel.add(getTypeComboBox(), BorderLayout.CENTER);
2700        }
2710        return typePanel;
2720    }
273  
274     /**
275      * @return Returns the nameTextField.
276      */
2770    protected JComboBox getTypeComboBox() {
2780        if (typeComboBox == null) {
2790            typeComboBox = new JComboBox();
2800            for (int i = 0; i < WorkstationType.WORKSTATION_TYPE.length; i++)
2810                typeComboBox.addItem(WorkstationType.WORKSTATION_TYPE[i]);
2820            typeComboBox
2830                    .setSelectedItem(workstation == null ? WorkstationType.WORKSTATION_TYPE[WorkstationType.WINDOWS]
2840                            : workstation.getType());
2850 
2860        }
2870        return typeComboBox;
2880    }
289  
290     /**
291      * Resets to the default values all the fields contained in this panel.
292      */
2930    public void clearWorkstationData() {
2940        this.getDescriptionTextField().setText(""); //$NON-NLS-1$
2950        this.getNameTextField().setText(""); //$NON-NLS-1$
2960    }
2970 
2980    public String getWorkstationName() {
2990        return this.getNameTextField().getText();
3000    }
301  
3020    public String getWorkstationLocation() {
3030        return this.getLocationTextField().getText();
3040    }
305  
3060    public String getWorkstationType() {
3070        return (String) getTypeComboBox().getSelectedItem();
3080    }
309  
3100    public String getWorkstationDescription() {
3110        return this.getDescriptionTextField().getText();
3120    }
313  
3140    public boolean getWorkstationInUse() {
3150        return this.getInUseCheckBox().isSelected();
3160    }
317  
3180    public Inet4Address getWorkstationAddress() {
3190        Inet4Address result = null;
3200        try {
3210            String ipAddress = this.getAddressTextField().getText();
3220            if (Control.isValidIPAddress(ipAddress)) {
3230                result = (Inet4Address) Inet4Address.getByName(ipAddress);
3240                setAddressError(false);
3250            }
3260        } catch (UnknownHostException e) {
3270            setAddressError(true);
3280            logger.error(e.getMessage());
3290            e.printStackTrace();
3300        }
3310        return result;
3320    }
333  
3340    public int getWorkstationPort() {
3350        int result = 0;
3360 
3370        if (Control.isValidIntegerOnlyString(this.getPortTextField().getText()))
3380            result = new Integer(this.getPortTextField().getText());
3390        else
3400            result = workstation != null ? workstation.getPort() : 9000;
3410        return result;
3420    }
343  
3440    public void setNameError(boolean isError) {
3450        if (isError)
3460            ((TitledBorder) this.getNamePanel().getBorder())
3470                    .setTitleColor(Color.RED);
3480        else
3490            ((TitledBorder) this.getNamePanel().getBorder())
3500                    .setTitleColor(new TitledBorder("").getTitleColor()); //$NON-NLS-1$
3510        this.getNamePanel().updateUI();
3520    }
3530 
3540    public void setPortError(boolean isError) {
3550        if (isError)
3560            ((TitledBorder) this.getPortPanel().getBorder())
3570                    .setTitleColor(Color.RED);
3580        else
3590            ((TitledBorder) this.getPortPanel().getBorder())
3600                    .setTitleColor(new TitledBorder("").getTitleColor()); //$NON-NLS-1$
3610        this.getPortPanel().updateUI();
3620    }
3630 
3640    public void setAddressError(boolean isError) {
3650        if (isError)
3660            ((TitledBorder) this.getAddressPanel().getBorder())
3670                    .setTitleColor(Color.RED);
3680        else
3690            ((TitledBorder) this.getAddressPanel().getBorder())
3700                    .setTitleColor(new TitledBorder("").getTitleColor()); //$NON-NLS-1$
3710        this.getAddressPanel().updateUI();
3720    }
3730 
374 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.