Advertisement
Bidderlyn

before this computer dies

Aug 2nd, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.11 KB | None | 0 0
  1. ///////////////////////////////// SESSION HANDLER
  2.  
  3. package flightmanager;
  4.  
  5. import model.UserModel;
  6.  
  7. public class SessionHandler {
  8.     /*
  9.      * Create a session for user after loading all data from the database
  10.      */
  11.    
  12.     public SQLHandler sql;
  13.     private UserModel user;
  14.    
  15.  
  16.     public SessionHandler(String name)
  17.     {
  18.         this.sql = new SQLHandler();
  19.        
  20.         //TODO: load mysql data
  21.         user.setUserName(name);
  22.         user.setIsAdmin(1);
  23.     }
  24.     public String getUserName()
  25.     {
  26.         return user.getUserName();
  27.     }
  28.    
  29.     public int isadmin()
  30.     {
  31.         return user.getIsAdmin();
  32.     }
  33. }
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. /////////////////////////////////////// LOGIN
  48.  
  49. package flightmanager;
  50.  
  51. import java.awt.EventQueue;
  52. import java.awt.event.ActionEvent;
  53. import java.awt.event.ActionListener;
  54.  
  55. import javax.swing.JFrame;
  56. import javax.swing.JPanel;
  57. import java.awt.BorderLayout;
  58. import javax.swing.JSplitPane;
  59. import javax.swing.JTabbedPane;
  60. import javax.swing.JTextField;
  61. import javax.swing.JTextArea;
  62. import javax.swing.JLabel;
  63. import javax.swing.JOptionPane;
  64. import javax.swing.SwingConstants;
  65. import javax.swing.JPasswordField;
  66. import java.awt.Color;
  67. import javax.swing.JButton;
  68.  
  69. public class Login {
  70.  
  71.     private JFrame frame;
  72.     private JTextField textField;
  73.     private JPasswordField passwordField;
  74.     public static SessionHandler session;
  75.     /**
  76.      * Launch the application.
  77.      */
  78.     public static void main(String[] args) {
  79.         EventQueue.invokeLater(new Runnable() {
  80.             public void run() {
  81.                 try {
  82.                     Login window = new Login();
  83.                     window.frame.setVisible(true);
  84.                 } catch (Exception e) {
  85.                     e.printStackTrace();
  86.                 }
  87.             }
  88.         });
  89.     }
  90.  
  91.     /**
  92.      * Create the application.
  93.      */
  94.     public Login() {
  95.         initialize();
  96.     }
  97.    
  98.  
  99.     /**
  100.      * Initialize the contents of the frame.
  101.      */
  102.     private void initialize() {
  103.         frame = new JFrame();
  104.         frame.setAutoRequestFocus(false);
  105.         frame.setBounds(100, 100, 450, 300);
  106.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  107.         frame.getContentPane().setLayout(null);
  108.        
  109.         textField = new JTextField();
  110.         textField.setBounds(108, 88, 96, 20);
  111.         frame.getContentPane().add(textField);
  112.         textField.setColumns(10);
  113.        
  114.         JPanel panel = new JPanel();
  115.         panel.setBackground(Color.DARK_GRAY);
  116.         panel.setBounds(214, 0, 220, 261);
  117.         frame.getContentPane().add(panel);
  118.        
  119.         JLabel lblNewLabel = new JLabel("ש×� משתמש:");
  120.         lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
  121.         lblNewLabel.setBounds(108, 73, 96, 14);
  122.         frame.getContentPane().add(lblNewLabel);
  123.        
  124.         JLabel label = new JLabel("סיסמ×�:");
  125.         label.setHorizontalAlignment(SwingConstants.RIGHT);
  126.         label.setBounds(156, 119, 48, 14);
  127.         frame.getContentPane().add(label);
  128.        
  129.         passwordField = new JPasswordField();
  130.         passwordField.setBounds(108, 136, 96, 20);
  131.         frame.getContentPane().add(passwordField);
  132.        
  133.         JButton loginButton = new JButton("התחברות");
  134.         loginButton.setBounds(10, 197, 89, 23);
  135.         frame.getContentPane().add(loginButton);
  136.         loginButton.addActionListener(new ActionListener() {
  137.  
  138.             @Override
  139.             public void actionPerformed(ActionEvent e) {
  140.                 //get username and password fields
  141.                 String username = textField.getText().toString();
  142.                 String password = passwordField.getText().toString();
  143.                 //verify login
  144.                 if(!username.equals("admin") || !password.equals("admin"))
  145.                 {
  146.                     JOptionPane.showMessageDialog(null, "ש×� משתמש/סיסמ×� שגויי×�!");
  147.                     passwordField.setText("");
  148.                 }
  149.                 else
  150.                 {
  151.                     JOptionPane.showMessageDialog(null, "!התחברת");
  152.                     session = new SessionHandler(username);
  153.                     MainDisplay s = new MainDisplay();
  154.                     frame.setVisible(false);
  155.                     MainDisplay main_display = new MainDisplay();
  156.                     main_display.frame.setVisible(true);
  157.                 }
  158.             }
  159.         });
  160.     }
  161. }
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. //////////// USER MODEL
  180.  
  181. package model;
  182.  
  183. import javax.persistence.Entity;
  184. import javax.persistence.Id;
  185.  
  186. @Entity
  187. public class UserModel {
  188.  
  189.     // data table model for a user
  190.     @Id
  191.     private int userId; // primary key field.
  192.     private String userName; // Must Have field
  193.     private String password; // Must have field. Needs authentication TODO
  194.     private String email; // Must have field. Email
  195.     private int isAdmin; // 0 = no, 1 = yes. tinyInt type.
  196.    
  197.     public UserModel(String _userName, String _password)
  198.     {
  199.         userName = _userName;
  200.         password = _password;
  201.     }
  202.    
  203.     public int getUserId() {
  204.         return userId;
  205.     }
  206.     public void setUserId(int userId) {
  207.         this.userId = userId;
  208.     }
  209.     public String getUserName() {
  210.         return userName;
  211.     }
  212.     public void setUserName(String userName) {
  213.         this.userName = userName;
  214.     }
  215.     public String getPassword() {
  216.         return password;
  217.     }
  218.     public void setPassword(String password) {
  219.         this.password = password;
  220.     }
  221.     public String getEmail() {
  222.         return email;
  223.     }
  224.     public void setEmail(String email) {
  225.         this.email = email;
  226.     }
  227.  
  228.     public int getIsAdmin() {
  229.         return isAdmin;
  230.     }
  231.  
  232.     public void setIsAdmin(int isAdmin) {
  233.         this.isAdmin = isAdmin;
  234.     }
  235.  
  236.    
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement