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 21 package base.jdbs.ui.panel; 22 23 import java.awt.BorderLayout; 24 import java.awt.GridLayout; 25 import java.awt.event.ActionEvent; 26 import java.awt.event.ActionListener; 27 import java.io.File; 28 29 import javax.swing.ImageIcon; 30 import javax.swing.JButton; 31 import javax.swing.JComboBox; 32 import javax.swing.JLabel; 33 import javax.swing.JPanel; 34 import javax.swing.JTextField; 35 import javax.swing.border.TitledBorder; 36 37 import org.apache.log4j.Logger; 38 39 import ui.dialog.UserDialog; 40 import base.jdbs.ConfigurationManager; 41 import base.jdbs.network.NetworkManager; 42 import base.util.FileUtil; 43 44 /*** 45 * @author skunk 46 * 47 */ 48 @SuppressWarnings("serial") 49 public class UserPanel extends JPanel { 50 51 private static final transient Logger logger = Logger 52 .getLogger(UserPanel.class.getName()); 53 54 private ImageIcon[] flagImageIcon; 55 56 private String[] flagName; 57 58 private JPanel infoPanel; 59 60 private JComboBox locationComboBox; 61 62 private JLabel flagLocationLabel; 63 64 private JPanel peerNamePanel; 65 66 private JTextField peerNameTextField; 67 68 private JButton userInfoButton; 69 70 public UserPanel() { 71 initialize(); 72 } 73 74 protected void initialize() { 75 this.setLayout(new GridLayout(2, 1)); 76 this.setBorder(new TitledBorder("User")); 77 this.add(this.getPeerNamePanel()); 78 this.add(this.getInfoPanel()); 79 } 80 81 /*** 82 * @return Returns the locationComboBox. 83 */ 84 protected JComboBox getLocationComboBox() { 85 if (this.locationComboBox == null) { 86 this.locationComboBox = new JComboBox(); 87 indexFlags(); 88 for (int i = 0; i < flagName.length; i++) { 89 this.locationComboBox.addItem(flagName[i]); 90 if (flagName[i].equalsIgnoreCase(ConfigurationManager 91 .getInstance().getUserLocation())) { 92 this.locationComboBox.setSelectedIndex(i); 93 this.getFlagLocationLabel().setIcon(flagImageIcon[i]); 94 } 95 } 96 97 this.locationComboBox.addActionListener(new ActionListener() { 98 99 public void actionPerformed(ActionEvent arg0) { 100 getFlagLocationLabel().setIcon( 101 flagImageIcon[locationComboBox.getSelectedIndex()]); 102 ConfigurationManager.getInstance().setUserLocation( 103 flagName[locationComboBox.getSelectedIndex()]); 104 } 105 }); 106 107 } 108 return locationComboBox; 109 } 110 111 protected void indexFlags() { 112 File[] icon = FileUtil.allFileContent(new File("lib/gif-1.0/gif16")); 113 flagImageIcon = new ImageIcon[icon.length]; 114 flagName = new String[icon.length]; 115 for (int i = 0; i < icon.length; i++) { 116 logger.debug(icon[i].getAbsolutePath()); 117 flagImageIcon[i] = new ImageIcon(icon[i].getAbsolutePath()); 118 flagName[i] = icon[i].getName().replaceAll(".gif", "") 119 .toUpperCase(); 120 } 121 } 122 123 /*** 124 * @return Returns the infoPanel. 125 */ 126 protected JPanel getInfoPanel() { 127 if (this.infoPanel == null) { 128 this.infoPanel = new JPanel(); 129 this.infoPanel.setLayout(new BorderLayout()); 130 this.infoPanel.setBorder(new TitledBorder("General User Info")); 131 JPanel locationPanel = new JPanel(); 132 locationPanel.setBorder(new TitledBorder("Location")); 133 locationPanel.setLayout(new GridLayout(1, 2)); 134 locationPanel.add(this.getFlagLocationLabel()); 135 locationPanel.add(this.getLocationComboBox()); 136 137 this.infoPanel.add(locationPanel, BorderLayout.CENTER); 138 this.infoPanel.add(this.getUserInfoButton(), BorderLayout.SOUTH); 139 } 140 return infoPanel; 141 } 142 143 /*** 144 * @return Returns the flagLocationLabel. 145 */ 146 protected JLabel getFlagLocationLabel() { 147 if (this.flagLocationLabel == null) { 148 this.flagLocationLabel = new JLabel("Selected Location"); 149 this.flagLocationLabel.setVerticalTextPosition(JLabel.NORTH); 150 } 151 return flagLocationLabel; 152 } 153 154 /*** 155 * @return Returns the peerNamePanel. 156 */ 157 protected JPanel getPeerNamePanel() { 158 if (this.peerNamePanel == null) { 159 this.peerNamePanel = new JPanel(); 160 this.peerNamePanel.setBorder(new TitledBorder("Peer Name")); 161 this.peerNamePanel.setLayout(new BorderLayout()); 162 this.peerNamePanel.add(this.getPeerNameTextField(), 163 BorderLayout.CENTER); 164 } 165 return peerNamePanel; 166 } 167 168 /*** 169 * @return Returns the peerNameTextField. 170 */ 171 protected JTextField getPeerNameTextField() { 172 if (this.peerNameTextField == null) { 173 this.peerNameTextField = new JTextField(); 174 this.peerNameTextField.setEditable(false); 175 this.peerNameTextField.setText(NetworkManager.getInstance().getJdbsPeerGroup().getPeerName()); 176 } 177 return peerNameTextField; 178 } 179 180 /*** 181 * @return Returns the userInfoButton. 182 */ 183 protected JButton getUserInfoButton() { 184 if (this.userInfoButton == null) { 185 this.userInfoButton = new JButton("User Data"); 186 this.userInfoButton.addActionListener(new ActionListener() { 187 188 public void actionPerformed(ActionEvent arg0) { 189 UserDialog userDialog = new UserDialog(ConfigurationManager 190 .getInstance().getUser()); 191 userDialog.setModal(true); 192 userDialog.setVisible(true); 193 } 194 }); 195 } 196 return userInfoButton; 197 } 198 }