Merged the two branches, DB separated
[RRRRHHHH_Code] / ruralHouses / src / gui / LoginGUI.java
index 7e8ec11..cf075b2 100644 (file)
@@ -2,8 +2,15 @@ package gui;
 
 import java.awt.Color;
 import java.awt.Frame;
+import java.awt.KeyEventDispatcher;
+import java.awt.KeyboardFocusManager;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.KeyEvent;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.rmi.Naming;
+import java.rmi.RemoteException;
 
 import javax.swing.JButton;
 import javax.swing.JFrame;
@@ -13,9 +20,10 @@ import javax.swing.JPasswordField;
 import javax.swing.JTextField;
 import javax.swing.SwingConstants;
 
-import businessLogic.LoginManager;
-import businessLogic.LoginManagerInterface;
-import domain.Owner;
+import common.LoginInterface;
+
+import configuration.___IntNames;
+import domain.Account;
 
 public class LoginGUI extends JFrame {
 
@@ -23,21 +31,52 @@ public class LoginGUI extends JFrame {
        private JPanel jContentPane = null;
        private JTextField usernameField;
        private JPasswordField passwordField;
-       private LoginManagerInterface loginManager = new LoginManager();
+       private LoginInterface loginManager = null;
        private JLabel loginFeedback;
-
+       private JButton btnRegister;
+       protected boolean isFocused = false;
        public LoginGUI() {
                super();
                initialize();
        }
 
        private void initialize() {
+               try {
+                       loginManager = (LoginInterface) Naming
+                                       .lookup(___IntNames.LoginManager);
+               } catch (Exception e1) {
+                       System.out.println("Error accessing remote authentication: "
+                                       + e1.toString());
+               }
                this.setSize(449, 293);
                this.setContentPane(getJContentPane());
                this.setTitle("Login");
+               addWindowListener(new WindowAdapter() {
+
+
+                       @Override
+                       public void windowGainedFocus(WindowEvent e) {
+                               isFocused = true;
+                       }
+
+                       @Override
+                       public void windowLostFocus(WindowEvent e) {
+                               isFocused = false;
+                       }
+               });
 
+        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
+
+                       @Override
+                       public boolean dispatchKeyEvent(KeyEvent e) {
+                               if (isFocused && e.getKeyCode() == KeyEvent.VK_ENTER )
+                                       jButton_ActionPerformed();
+                               
+                               return false;
+                       }});
        }
 
+       
        private JPanel getJContentPane() {
                if (jContentPane == null) {
                        jContentPane = new JPanel();
@@ -64,10 +103,10 @@ public class LoginGUI extends JFrame {
                        loginButton.addActionListener(new ActionListener() {
                                public void actionPerformed(ActionEvent arg0) {
 
-                               jButton_ActionPerformed(arg0);  
+                               jButton_ActionPerformed();      
                                }                                       
                        });
-                       loginButton.setBounds(164, 179, 117, 25);
+                       loginButton.setBounds(95, 179, 117, 25);
                        jContentPane.add(loginButton);
                        
                        loginFeedback = new JLabel("");
@@ -75,24 +114,55 @@ public class LoginGUI extends JFrame {
                        loginFeedback.setHorizontalAlignment(SwingConstants.CENTER);
                        loginFeedback.setBounds(83, 216, 269, 25);
                        jContentPane.add(loginFeedback);
+                       
+                       btnRegister = new JButton("Register");
+                       btnRegister.setBounds(222, 180, 113, 23);
+                       btnRegister.addActionListener(new ActionListener() {
+                               public void actionPerformed(ActionEvent arg0) {
+
+                               jButton_ActionPerformed2(arg0); 
+                               }                                       
+                       });
+                       jContentPane.add(btnRegister);
                }
                return jContentPane;
        }
+       private void jButton_ActionPerformed2(ActionEvent e){
+               
+               
+                       this.setVisible(false);
+                       Frame a = new OwnerRegistrationGUI();
+                       a.setVisible(true);
+               
+               
+       }
+
+
        
-       private void jButton_ActionPerformed(ActionEvent e){
-               Owner owner = loginManager.checkCredentials(usernameField.getText(),new String(passwordField.getPassword()));
-               if(owner==null){
-                       loginFeedback.setText("Incorrect username or password");
-               }else{
-                       
-                       
+       private void jButton_ActionPerformed(){
                        
+               Account acc = null;
+               try {
+                       acc = loginManager.checkCredentials(usernameField.getText(),new String(passwordField.getPassword()));
+               } catch (RemoteException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
+               if (acc == null) loginFeedback.setText("Incorrect username or password");
+               else if( acc.getAdmin()) {
+                       this.setVisible(false);
+                       Frame a = new AdminMenuGUI();
+                       a.setVisible(true);
+               } else {
+                       if(acc.getOwner()==null){
+                               loginFeedback.setText("Incorrect username or password");
+                       }else{
                                this.setVisible(false);
-                               Frame a = new OwnerMenuGUI(owner);
-                               a.setVisible(true);
-               }       
-
+                               Frame a = new OwnerMenuGUI(acc.getOwner());
+                               a.setVisible(true);             
+                       }
+               }
+               
        }
-
 }  // @jve:decl-index=0:visual-constraint="222,33"