| 1 package com.calipso.reportgenerator.usermanager; 2 3 import com.calipso.reportgenerator.common.LanguageTraslator; 4 import com.calipso.reportgenerator.common.ReportGeneratorConfiguration; 5 import com.calipso.reportgenerator.common.InfoException; 6 7 8 import javax.swing.*; 9 import java.awt.event.ActionListener ; 10 import java.awt.event.ActionEvent ; 11 import java.awt.*; 12 import java.io.File ; 13 14 21 22 25 26 public class UserManagerLoginFrame extends JDialog implements ActionListener { 27 28 protected JPasswordField pfUser; 29 private static JTextField tfName; 30 private JButton btAccept; 31 private JButton btCancel; 32 private String defaultUserName; 33 private boolean hasCanceled; 34 private File file; 35 private ReportGeneratorConfiguration reportGeneratorConfiguration; 36 private UserManager userManager; 37 38 46 public UserManagerLoginFrame(Frame owner, String title, boolean modal, String defaultUserName,ReportGeneratorConfiguration reportGeneratorConfiguration,UserManager userManager) throws HeadlessException, InfoException { 47 48 super(owner, title, modal); 49 this.reportGeneratorConfiguration=reportGeneratorConfiguration; 50 this.userManager = userManager; 51 file = new File (reportGeneratorConfiguration.getUsersRepositoryPath()); 52 53 Image icon = reportGeneratorConfiguration.getImage("ICON"); 54 if(icon != null && owner!=null) { 55 owner.setIconImage(icon); 56 } 57 this.defaultUserName = defaultUserName; 58 hasCanceled = false; 59 createComponents(); 60 } 61 62 65 protected void createComponents() { 66 getContentPane().setLayout(new BorderLayout()); 67 getContentPane().add(createNorthPanel(), BorderLayout.CENTER); 68 getContentPane().add(createSouthPanel(), BorderLayout.SOUTH); 69 java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 70 setLocation((screenSize.width - 250) / 2, (screenSize.height - 200) / 2); 71 setSize(new Dimension(250, 110)); 72 } 74 75 79 protected JComponent createSouthPanel() { 80 FlowLayout layout = new FlowLayout(FlowLayout.RIGHT); 81 JPanel pnlSouth = new JPanel(layout); 82 btAccept = new JButton(LanguageTraslator.traslate("112")); 83 btAccept.addActionListener(this); 84 btCancel = new JButton(LanguageTraslator.traslate("113")); 85 btCancel.addActionListener(this); 86 pnlSouth.add(btAccept); 87 pnlSouth.add(btCancel); 88 return pnlSouth; 89 } 90 91 95 protected JComponent createNorthPanel() { 96 JPanel pnlNorth = new JPanel(new GridLayout(2, 2)); 97 JLabel label = new JLabel(LanguageTraslator.traslate("232")); 98 tfName = new JTextField(defaultUserName); 99 tfName.setEditable(false); 100 pnlNorth.add(label); 101 pnlNorth.add(tfName); 102 label = new JLabel("Password"); 103 pfUser = new JPasswordField(); 104 pnlNorth.add(label); 105 pnlNorth.add(pfUser); 106 return pnlNorth; 107 } 108 109 114 public boolean getHasCanceled() { 115 return hasCanceled; 116 } 117 118 122 public static String getUserName() { 123 return tfName.getText().trim(); 124 } 125 126 130 public JPasswordField getPfUser() { 131 return pfUser; 132 } 133 134 138 protected JButton getBtAccept() { 139 return btAccept; 140 } 141 142 146 public String getUserPasswd() { 147 return new String (pfUser.getPassword()); 148 } 149 150 156 public boolean login() throws InfoException { 157 boolean result = false; 158 int intentos=0; 159 if(!file.exists()) { 160 throw new InfoException(LanguageTraslator.traslate("19")); 161 } 162 for (int i = 0; i < 3; i++) { 163 setVisible(true); 164 if(this.getHasCanceled()) { 165 return false; 166 } 167 String userName = this.getUserName(); 168 String userPassword = this.getUserPasswd(); 169 result = userManager.validate(this.getUserName(), this.getUserPasswd()); 170 if(result==false){ 171 intentos++; 172 JOptionPane.showMessageDialog(this,LanguageTraslator.traslate("451")); 173 } else { 174 return true; 175 } 176 } 177 if(intentos==3) { 178 JOptionPane.showMessageDialog(this,LanguageTraslator.traslate("300")); 179 } 180 return result; 181 } 182 183 public void actionPerformed(ActionEvent e) { 184 if(e.getSource() == btAccept) { 185 dispose(); 186 } else { 187 hasCanceled = true; 188 dispose(); 189 } 190 } 191 } 192 | Popular Tags |