cleaning
[RRRRHHHH_Code] / ruralHouses / src / gui / LoginGUI.java
1 package gui;
2
3 import java.awt.Color;
4 import java.awt.Frame;
5 import java.awt.KeyEventDispatcher;
6 import java.awt.KeyboardFocusManager;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.awt.event.KeyEvent;
10 import java.awt.event.WindowAdapter;
11 import java.awt.event.WindowEvent;
12 import java.rmi.Naming;
13 import java.rmi.RemoteException;
14
15 import javax.swing.JButton;
16 import javax.swing.JFrame;
17 import javax.swing.JLabel;
18 import javax.swing.JPanel;
19 import javax.swing.JPasswordField;
20 import javax.swing.JTextField;
21 import javax.swing.SwingConstants;
22
23 import common.LoginInterface;
24
25 import configuration.___IntNames;
26 import domain.Account;
27
28 public class LoginGUI extends JFrame {
29
30         private static final long serialVersionUID= 1L;
31         private JPanel jContentPane = null;
32         private JTextField usernameField;
33         private JPasswordField passwordField;
34         private LoginInterface loginManager = null;
35         private JLabel loginFeedback;
36         private JButton btnRegister;
37         protected boolean isFocused = false;
38         public LoginGUI() {
39                 super();
40                 initialize();
41         }
42
43         private void initialize() {
44                 try {
45                         loginManager = (LoginInterface) Naming
46                                         .lookup(___IntNames.LoginManager);
47                 } catch (Exception e1) {
48                         System.out.println("Error accessing remote authentication: "
49                                         + e1.toString());
50                 }
51                 this.setSize(449, 293);
52                 this.setContentPane(getJContentPane());
53                 this.setTitle("Login");
54                 addWindowListener(new WindowAdapter() {
55
56
57                         @Override
58                         public void windowGainedFocus(WindowEvent e) {
59                                 isFocused = true;
60                         }
61
62                         @Override
63                         public void windowLostFocus(WindowEvent e) {
64                                 isFocused = false;
65                         }
66                 });
67
68         KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
69
70                         @Override
71                         public boolean dispatchKeyEvent(KeyEvent e) {
72                                 if (isFocused && e.getKeyCode() == KeyEvent.VK_ENTER )
73                                         jButton_ActionPerformed();
74                                 
75                                 return false;
76                         }});
77         }
78
79         
80         private JPanel getJContentPane() {
81                 if (jContentPane == null) {
82                         jContentPane = new JPanel();
83                         jContentPane.setLayout(null);
84                         
85                         JLabel usernameTag = new JLabel("Username:");
86                         usernameTag.setBounds(56, 67, 104, 20);
87                         jContentPane.add(usernameTag);
88                         
89                         JLabel passwordTag = new JLabel("Password:");
90                         passwordTag.setBounds(56, 132, 104, 15);
91                         jContentPane.add(passwordTag);
92                         
93                         usernameField = new JTextField();
94                         usernameField.setBounds(202, 65, 133, 25);
95                         jContentPane.add(usernameField);
96                         usernameField.setColumns(10);
97                         
98                         passwordField = new JPasswordField();
99                         passwordField.setBounds(202, 122, 133, 25);
100                         jContentPane.add(passwordField);
101                         
102                         JButton loginButton = new JButton("Login");
103                         loginButton.addActionListener(new ActionListener() {
104                                 public void actionPerformed(ActionEvent arg0) {
105
106                                 jButton_ActionPerformed();      
107                                 }                                       
108                         });
109                         loginButton.setBounds(95, 179, 117, 25);
110                         jContentPane.add(loginButton);
111                         
112                         loginFeedback = new JLabel("");
113                         loginFeedback.setForeground(Color.RED);
114                         loginFeedback.setHorizontalAlignment(SwingConstants.CENTER);
115                         loginFeedback.setBounds(83, 216, 269, 25);
116                         jContentPane.add(loginFeedback);
117                         
118                         btnRegister = new JButton("Register");
119                         btnRegister.setBounds(222, 180, 113, 23);
120                         btnRegister.addActionListener(new ActionListener() {
121                                 public void actionPerformed(ActionEvent arg0) {
122
123                                 jButton_ActionPerformed2(arg0); 
124                                 }                                       
125                         });
126                         jContentPane.add(btnRegister);
127                 }
128                 return jContentPane;
129         }
130         private void jButton_ActionPerformed2(ActionEvent e){
131                 
132                 
133                         this.setVisible(false);
134                         Frame a = new OwnerRegistrationGUI();
135                         a.setVisible(true);
136                 
137                 
138         }
139
140
141         
142         private void jButton_ActionPerformed(){
143                         
144                 Account acc = null;
145                 try {
146                         acc = loginManager.checkCredentials(usernameField.getText(),new String(passwordField.getPassword()));
147                 } catch (RemoteException e) {
148                         // TODO Auto-generated catch block
149                         e.printStackTrace();
150                 }
151                 if (acc == null) loginFeedback.setText("Incorrect username or password");
152                 else if( acc.getAdmin()) {
153                         this.setVisible(false);
154                         Frame a = new AdminMenuGUI();
155                         a.setVisible(true);
156                 } else {
157                         if(acc.getOwner()==null){
158                                 loginFeedback.setText("Incorrect username or password");
159                         }else{
160                                 this.setVisible(false);
161                                 Frame a = new OwnerMenuGUI(acc.getOwner());
162                                 a.setVisible(true);             
163                         }
164                 }
165                 
166         }
167 }  // @jve:decl-index=0:visual-constraint="222,33"
168