Versión del proyecto de Apuestas tras la iteración 1
[ISBets21MAUBRY] / eclipse-workspace / ISBets21BRYMAUJONUNA / src / main / java / gui / LoginGUI.java
1 package gui;
2
3 import java.awt.EventQueue;
4 import java.awt.Frame;
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;
10
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;
21
22 import businessLogic.BLFacade;
23 import businessLogic.BLFacadeImplementation;
24 import domain.User;
25 import exceptions.IncorrectPassException;
26 import exceptions.UserDoesNotExistException;
27 import java.awt.Font;
28
29 public class LoginGUI extends JFrame {
30
31         private JPanel contentPane;
32         private JTextField textUser;
33         private JPasswordField textPass;
34         private static BLFacade facade;
35         private static User user;
36
37         private JLabel lblUser = new JLabel("Usuario:");
38         private JLabel lblPass = new JLabel("Contraseña:");
39
40         private JButton buttonLogin = new JButton("Login");
41         private JButton buttonRegister = new JButton("Registrarse");
42
43         
44         public LoginGUI() {
45                 super();
46                 
47                 addWindowListener(new WindowAdapter() {
48                         @Override
49                         public void windowClosing(WindowEvent e) {
50                                 try {
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");
55                                 }
56                                 System.exit(1);
57                         }
58                 });
59
60                 initialize();
61                 //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
62         }
63         
64         
65         
66         public void initialize() {
67                 setTitle("Login");
68
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);
75
76                 lblUser.setHorizontalAlignment(SwingConstants.RIGHT);
77                 lblUser.setBounds(39, 47, 100, 14);
78                 contentPane.add(lblUser);
79
80                 lblPass.setHorizontalAlignment(SwingConstants.RIGHT);
81                 lblPass.setBounds(63, 87, 76, 14);
82                 contentPane.add(lblPass);
83
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);
89
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);
95
96                 buttonLogin.addActionListener(new ActionListener() {
97                         public void actionPerformed(ActionEvent arg0) {
98                                 try {
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);
104                                         }
105                                         else {
106                                         User user = facade.login(userName, pass);
107                                         System.out.println("LOGIN CORRECTO");
108                                         System.out.println(user.getUserName() + " " + user.getUserPass());
109
110                                         if(user.getType()==0) {
111                                                 Frame gui = new MainGUI();
112                                                 gui.setAlwaysOnTop(true);
113                                                 gui.setVisible(true);
114                                         }else {
115                                                 Frame gui = new MainAdminGUI();
116                                                 gui.setAlwaysOnTop(true);
117                                                 gui.setVisible(true);
118                                         }
119
120                                         dispose();
121                                         }
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);
128                                 } finally {
129                                         textPass.setText("");
130                                         textUser.setText("");
131                                 }
132                         }
133                 });
134
135                 
136                 buttonLogin.setBounds(97, 134, 89, 23);
137                 contentPane.add(buttonLogin);
138                 
139                 buttonRegister.addActionListener(new ActionListener() {
140                         public void actionPerformed(ActionEvent e) {
141                                 
142                                 Frame reg = new RegisterGUI();
143                                 reg.setAlwaysOnTop(true);
144                                 reg.setVisible(true);
145                                 dispose();
146                         }
147                 });
148
149                 buttonRegister.setBounds(220, 134, 135, 23);
150                 contentPane.add(buttonRegister);
151                 
152         }
153
154         public static void setBusinessLogic(BLFacade pfacade) {
155                 facade = pfacade;
156         }
157
158         public static BLFacade getBusinessLogic() {
159                 return facade;
160         }
161
162         public void setUser(User user) {
163                 LoginGUI.user = user;
164         }
165
166         public static User getUser() {
167                 return user;
168         }
169 }