Iteración 3(Versión sin idiomas)
[ISBets21MAUBRY] / eclipse-workspace / ISBets21MAUBRY / src / main / java / gui / LoginGUI.java
1 package gui;
2
3 import java.awt.Font;
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
10 import javax.swing.JButton;
11 import javax.swing.JFrame;
12 import javax.swing.JLabel;
13 import javax.swing.JOptionPane;
14 import javax.swing.JPanel;
15 import javax.swing.JPasswordField;
16 import javax.swing.JTextField;
17 import javax.swing.SwingConstants;
18 import javax.swing.border.EmptyBorder;
19
20 import businessLogic.BLFacade;
21 import domain.AdminUser;
22 import domain.RegularUser;
23 import java.awt.Color;
24 import java.awt.SystemColor;
25
26 public class LoginGUI extends JFrame {
27
28         private JPanel contentPane;
29         private JTextField textUser;
30         private JPasswordField textPass;
31         private static BLFacade facade;
32         private static RegularUser userlog;
33
34         private JLabel lblUser = new JLabel("Usuario:");
35         private JLabel lblPass = new JLabel("Contraseña:");
36
37         private JButton buttonLogin = new JButton("Login");
38         private JButton buttonRegister = new JButton("Registrarse");
39         private JButton btnInvitado;
40
41         public LoginGUI() {
42                 super();
43
44                 addWindowListener(new WindowAdapter() {
45                         @Override
46                         public void windowClosing(WindowEvent e) {
47                                 try {
48                                         // if (ConfigXML.getInstance().isBusinessLogicLocal()) facade.close();
49                                 } catch (Exception e1) {
50                                         System.out.println(
51                                                         "Error: " + e1.toString() + " , probably problems with Business Logic or Database");
52                                 }
53                                 System.exit(1);
54                         }
55                 });
56
57                 initialize();
58                 // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
59         }
60
61         public void initialize() {
62                 setTitle("Login");
63
64                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
65                 setBounds(100, 100, 460, 300);
66                 contentPane = new JPanel();
67                 contentPane.setBackground(Color.WHITE);
68                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
69                 setContentPane(contentPane);
70                 contentPane.setLayout(null);
71
72                 lblUser.setHorizontalAlignment(SwingConstants.RIGHT);
73                 lblUser.setBounds(65, 66, 100, 14);
74                 contentPane.add(lblUser);
75
76                 lblPass.setHorizontalAlignment(SwingConstants.RIGHT);
77                 lblPass.setBounds(89, 106, 76, 14);
78                 contentPane.add(lblPass);
79
80                 textUser = new JTextField("");
81                 textUser.setForeground(Color.GRAY);
82                 textUser.setFont(new Font("Arial", Font.PLAIN, 16));
83                 textUser.setBounds(175, 57, 135, 30);
84                 contentPane.add(textUser);
85                 textUser.setColumns(10);
86
87                 textPass = new JPasswordField("");
88                 textPass.setForeground(Color.GRAY);
89                 textPass.setFont(new Font("Arial", Font.PLAIN, 16));
90                 textPass.setBounds(175, 97, 135, 30);
91                 contentPane.add(textPass);
92                 textPass.setColumns(10);
93                 buttonLogin.setForeground(Color.WHITE);
94                 buttonLogin.setFont(new Font("Dialog", Font.BOLD, 14));
95                 buttonLogin.setBackground(SystemColor.textHighlight);
96
97                 buttonLogin.addActionListener(new ActionListener() {
98                         @Override
99                         public void actionPerformed(ActionEvent arg0) {
100                                 String pass = String.valueOf(textPass.getPassword());
101                                 String userName = textUser.getText();
102                                 if (pass.equals("") || userName.equals("")) {
103                                         JOptionPane.showMessageDialog(contentPane, "Usuario o contraseña no introducida", "Error",
104                                                         JOptionPane.ERROR_MESSAGE);
105                                 } else {
106                                         boolean b = facade.doLogin(userName, pass);
107                                         if (b) {
108
109                                                 // Check user permissions
110                                                 if (facade.isAdmin(userName, pass)) {
111
112                                                         AdminUser au = facade.getAdminUserByUsername(userName);
113                                                         JFrame a = new MainAdminGUI(au);
114                                                         // a.setLocationRelativeTo(null);
115                                                         a.setVisible(true);
116                                                         textUser.setText("");
117                                                         textPass.setText("");
118
119                                                 } else {
120
121                                                         RegularUser ru = facade.getRegularUserByUsername(userName);
122                                                         JFrame a = new MainGUI(ru);
123                                                         // a.setLocationRelativeTo(null);
124                                                         a.setVisible(true);
125                                                         textUser.setText("");
126                                                         textPass.setText("");
127                                                 }
128                                         } else {
129
130                                                 JOptionPane.showMessageDialog(null, "Usuario y contraseña no coinciden", "LOGIN INFO",
131                                                                 JOptionPane.ERROR_MESSAGE, null);
132                                         }
133                                 }
134                         }
135                 });
136
137                 buttonLogin.setBounds(97, 155, 89, 27);
138                 contentPane.add(buttonLogin);
139                 buttonRegister.setFont(new Font("Dialog", Font.BOLD, 14));
140                 buttonRegister.setForeground(Color.WHITE);
141                 buttonRegister.setBackground(SystemColor.textHighlight);
142
143                 buttonRegister.addActionListener(new ActionListener() {
144                         @Override
145                         public void actionPerformed(ActionEvent e) {
146
147                                 Frame reg = new RegisterGUI();
148                                 reg.setAlwaysOnTop(true);
149                                 reg.setVisible(true);
150                                 // dispose();
151                         }
152                 });
153
154                 buttonRegister.setBounds(220, 155, 135, 27);
155                 contentPane.add(buttonRegister);
156
157                 btnInvitado = new JButton("Invitado");
158                 btnInvitado.addActionListener(new ActionListener() {
159                         public void actionPerformed(ActionEvent arg0) {
160                                 Frame reg = new FindQuestionsInvitadoGUI();
161                                 reg.setAlwaysOnTop(true);
162                                 reg.setVisible(true);
163
164                         }
165                 });
166                 btnInvitado.setForeground(Color.WHITE);
167                 btnInvitado.setFont(new Font("Dialog", Font.BOLD, 14));
168                 btnInvitado.setBackground(SystemColor.textHighlight);
169                 btnInvitado.setBounds(145, 204, 120, 27);
170                 contentPane.add(btnInvitado);
171
172         }
173
174         public static void setBusinessLogic(BLFacade pfacade) {
175                 facade = pfacade;
176         }
177
178         public static BLFacade getBusinessLogic() {
179                 return facade;
180         }
181
182         public void setRegularUserLog(RegularUser user) {
183                 LoginGUI.userlog = user;
184         }
185
186         public static RegularUser getUserLog() {
187                 return userlog;
188         }
189 }