3 import java.awt.EventQueue;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.WindowAdapter;
8 import java.awt.event.WindowEvent;
9 import java.util.ResourceBundle;
11 import javax.persistence.Entity;
12 import javax.swing.JButton;
13 import javax.swing.JFrame;
14 import javax.swing.JLabel;
15 import javax.swing.JOptionPane;
16 import javax.swing.JPanel;
17 import javax.swing.JPasswordField;
18 import javax.swing.JTextField;
19 import javax.swing.SwingConstants;
20 import javax.swing.border.EmptyBorder;
22 import businessLogic.BLFacade;
23 import businessLogic.BLFacadeImplementation;
25 import exceptions.IncorrectPassException;
26 import exceptions.UserDoesNotExistException;
29 public class LoginGUI extends JFrame {
31 private JPanel contentPane;
32 private JTextField textUser;
33 private JPasswordField textPass;
34 private static BLFacade facade;
35 private static User user;
37 private JLabel lblUser = new JLabel("Usuario:");
38 private JLabel lblPass = new JLabel("Contraseña:");
40 private JButton buttonLogin = new JButton("Login");
41 private JButton buttonRegister = new JButton("Registrarse");
47 addWindowListener(new WindowAdapter() {
49 public void windowClosing(WindowEvent e) {
51 //if (ConfigXML.getInstance().isBusinessLogicLocal()) facade.close();
52 } catch (Exception e1) {
53 // TODO Auto-generated catch block
54 System.out.println("Error: "+e1.toString()+" , probably problems with Business Logic or Database");
61 //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
66 public void initialize() {
69 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
70 setBounds(100, 100, 450, 300);
71 contentPane = new JPanel();
72 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
73 setContentPane(contentPane);
74 contentPane.setLayout(null);
76 lblUser.setHorizontalAlignment(SwingConstants.RIGHT);
77 lblUser.setBounds(39, 47, 100, 14);
78 contentPane.add(lblUser);
80 lblPass.setHorizontalAlignment(SwingConstants.RIGHT);
81 lblPass.setBounds(63, 87, 76, 14);
82 contentPane.add(lblPass);
84 textUser = new JTextField();
85 textUser.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
86 textUser.setBounds(149, 45, 131, 20);
87 contentPane.add(textUser);
88 textUser.setColumns(10);
90 textPass = new JPasswordField();
91 textPass.setFont(new Font("Liberation Sans", Font.PLAIN, 13));
92 textPass.setBounds(149, 85, 131, 20);
93 contentPane.add(textPass);
94 textPass.setColumns(10);
96 buttonLogin.addActionListener(new ActionListener() {
97 public void actionPerformed(ActionEvent arg0) {
99 String pass = String.valueOf(textPass.getPassword());
100 String userName = textUser.getText();
101 if (pass.equals("") || userName.equals("")) {
102 JOptionPane.showMessageDialog(contentPane, "Usuario o contraseña no introducida", "Error",
103 JOptionPane.ERROR_MESSAGE);
106 User user = facade.login(userName, pass);
107 System.out.println("LOGIN CORRECTO");
108 System.out.println(user.getUserName() + " " + user.getUserPass());
110 if(user.getType()==0) {
111 Frame gui = new MainGUI();
112 gui.setAlwaysOnTop(true);
113 gui.setVisible(true);
115 Frame gui = new MainAdminGUI();
116 gui.setAlwaysOnTop(true);
117 gui.setVisible(true);
122 } catch (UserDoesNotExistException e) {
123 JOptionPane.showMessageDialog(contentPane, e.getMessage() , "Error", JOptionPane.ERROR_MESSAGE);
124 } catch (IncorrectPassException a) {
125 JOptionPane.showMessageDialog(contentPane, a.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
126 } catch (Exception e) {
127 JOptionPane.showMessageDialog(getContentPane(), e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
129 textPass.setText("");
130 textUser.setText("");
136 buttonLogin.setBounds(97, 134, 89, 23);
137 contentPane.add(buttonLogin);
139 buttonRegister.addActionListener(new ActionListener() {
140 public void actionPerformed(ActionEvent e) {
142 Frame reg = new RegisterGUI();
143 reg.setAlwaysOnTop(true);
144 reg.setVisible(true);
149 buttonRegister.setBounds(220, 134, 135, 23);
150 contentPane.add(buttonRegister);
154 public static void setBusinessLogic(BLFacade pfacade) {
158 public static BLFacade getBusinessLogic() {
162 public void setUser(User user) {
163 LoginGUI.user = user;
166 public static User getUser() {