1 package com.calipso.reportgenerator.userinterface; 2 3 import com.calipso.reportgenerator.common.IReportManager; 4 import com.calipso.reportgenerator.common.LanguageTraslator; 5 import com.calipso.reportgenerator.common.InfoException; 6 import com.calipso.reportgenerator.client.ReportManagerService; 7 8 import javax.swing.*; 9 import java.awt.*; 10 import java.awt.event.ActionListener ; 11 import java.awt.event.ActionEvent ; 12 13 16 17 public class UserLoginFrame extends JDialog implements ActionListener { 18 19 protected JPasswordField pfUser; 20 private static JTextField tfName; 21 private JButton btAccept; 22 private JButton btCancel; 23 private String defaultUserName; 24 private boolean hasCanceled; 25 26 34 public UserLoginFrame(Frame owner, String title, boolean modal, String defaultUserName) throws HeadlessException { 35 super(owner, title, modal); 36 Image icon = ReportManagerService.getConfiguration().getImage("icon"); 37 if(icon != null && owner!=null) { 38 owner.setIconImage(icon); 39 } 40 this.defaultUserName = defaultUserName; 41 hasCanceled = false; 42 createComponents(); 43 } 44 45 48 protected void createComponents() { 49 getContentPane().setLayout(new BorderLayout()); 50 getContentPane().add(createNorthPanel(), BorderLayout.CENTER); 51 getContentPane().add(createSouthPanel(), BorderLayout.SOUTH); 52 java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 53 setLocation((screenSize.width - 250) / 2, (screenSize.height - 200) / 2); 54 setSize(new Dimension(250, 110)); 55 setVisible(true); 56 } 57 58 62 protected JComponent createSouthPanel() { 63 FlowLayout layout = new FlowLayout(FlowLayout.RIGHT); 64 JPanel pnlSouth = new JPanel(layout); 65 btAccept = new JButton("Aceptar"); 66 btAccept.addActionListener(this); 67 btCancel = new JButton("Cancelar"); 68 btCancel.addActionListener(this); 69 pnlSouth.add(btAccept); 70 pnlSouth.add(btCancel); 71 return pnlSouth; 72 } 73 74 78 protected JComponent createNorthPanel() { 79 JPanel pnlNorth = new JPanel(new GridLayout(2, 2)); 80 JLabel label = new JLabel(LanguageTraslator.traslate("297")); 81 tfName = new JTextField(defaultUserName); 82 pnlNorth.add(label); 83 pnlNorth.add(tfName); 84 label = new JLabel(LanguageTraslator.traslate("298")); 85 pfUser = new JPasswordField(); 86 pnlNorth.add(label); 87 pnlNorth.add(pfUser); 88 return pnlNorth; 89 } 90 91 96 public boolean getHasCanceled() { 97 return hasCanceled; 98 } 99 100 104 public static String getUserName() { 105 return tfName.getText().trim(); 106 } 107 108 112 public JPasswordField getPfUser() { 113 return pfUser; 114 } 115 116 120 protected JButton getBtAccept() { 121 return btAccept; 122 } 123 124 128 public String getUserPasswd() { 129 return new String (pfUser.getPassword()); 130 } 131 132 141 public static boolean login(String defaultUserName, String tittle, IReportManager service) throws InfoException { 142 boolean result = false; 143 UserLoginFrame loginFrame = new UserLoginFrame(new JFrame(), tittle, true, defaultUserName); 144 if(!loginFrame.getHasCanceled() && !loginFrame.getUserPasswd().equals("")) { 145 String userName = loginFrame.getUserName(); 146 String userPassword = loginFrame.getUserPasswd(); 147 result = service.validateUser(userName, userPassword, ReportManagerService.getConfiguration().getUsersRepositoryPath()); 148 if(result==false){ 149 int option = JOptionPane.showConfirmDialog(null, LanguageTraslator.traslate("329")); 150 if(option == 0) { 151 String rootPasswd = UserPasswordFrame.getRootPassword(); 152 if(!rootPasswd.equals("")) { 153 String name = JOptionPane.showInputDialog(LanguageTraslator.traslate("397")); 154 if(name.trim().length()<3){ 155 throw new InfoException(LanguageTraslator.traslate("399")); 156 } 157 String company = JOptionPane.showInputDialog(LanguageTraslator.traslate("398")); 158 if(company.trim().length()<3){ 159 throw new InfoException(LanguageTraslator.traslate("399")); 160 } 161 String rol = JOptionPane.showInputDialog(LanguageTraslator.traslate("405"), "ANY"); 162 if(rol.trim().length()<3){ 163 throw new InfoException(LanguageTraslator.traslate("399")); 164 } 165 result = service.addNewUser(rootPasswd, userName, userPassword, ReportManagerService.getConfiguration().getUsersRepositoryPath()); 166 if(result){ 167 service.addUserData(userName, name, company, ReportManagerService.getConfiguration().getUserDataRepositoryPath()); 168 service.addUserRol(userName, rol, ReportManagerService.getConfiguration().getRolsRepositoryPath()); 169 } 170 } 171 } 172 173 } 174 } 175 return result; 176 } 177 public void actionPerformed(ActionEvent e) { 178 if(e.getSource() == btAccept) { 179 dispose(); 180 } else { 181 hasCanceled = true; 182 dispose(); 183 } 184 } 185 } 186 | Popular Tags |