Advertisement
Guest User

Untitled

a guest
May 1st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.99 KB | None | 0 0
  1. //GUI Launcher
  2.     userlogin = new JUserPanel(register);  //Controller class
  3.     GUIController gc = new GUIController(content, userlogin); //content is actually useless, guicontroller is below.
  4.     KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(gc);
  5.  
  6.     this.setGlassPane(userlogin);  
  7.     userlogin.setVisible(true);
  8.     userlogin.setOpaque(true);
  9.     userlogin.validate();
  10.     userlogin.repaint();
  11.  
  12.  
  13.  
  14. //GUIController
  15.  
  16. package Presenter;
  17.  
  18. import java.awt.Container;
  19. import java.awt.KeyEventPostProcessor;
  20. import java.awt.event.KeyEvent;
  21. import java.awt.event.KeyListener;
  22. import java.util.Observable;
  23. import java.util.Observer;
  24.  
  25. import View.JUserPanel;
  26.  
  27. /** Controls the GUI */
  28. public class GUIController implements  KeyEventPostProcessor{
  29.     private Container content;
  30.     private JUserPanel glasspanel;
  31.    
  32.     public GUIController(Container content, JUserPanel userlogin) {
  33.     this.content = content;
  34.     this.glasspanel = userlogin;
  35.     }
  36.  
  37.     @Override
  38.     public boolean postProcessKeyEvent(KeyEvent e) {
  39.         char mychar = 'm';
  40.         if(e.isAltDown()){
  41.             if(e.getKeyChar()==mychar){
  42.                 //content
  43.                 glasspanel.setOpaque(true);
  44.                 glasspanel.setNeedToRedispatch(false);
  45.                 glasspanel.setVisible(true);
  46.                 glasspanel.validate();
  47.                 glasspanel.repaint();
  48.             }
  49.         }
  50.         return true;
  51.     }
  52. }
  53.  
  54.  
  55. //GlassPane
  56.  
  57. package View;
  58.  
  59. public class JUserPanel extends JComponent implements MouseListener,
  60.         MouseMotionListener, FocusListener {
  61.  
  62.     // trigger for redispatching (allows external control)
  63.     boolean needToRedispatch = false;
  64.     private JButton loginBut;
  65.     private JLabel usernamelabel;
  66.     private JLabel passwordlabel;
  67.     private JLabel errorlabel;
  68.     private JTextField username;
  69.     private JPasswordField password;
  70.     private Register model;
  71.    
  72.    
  73.    
  74.     public JUserPanel(Register register) {
  75.         this.model = register;
  76.        
  77.         setLayout(new GridBagLayout());
  78.         setBackground(Color.white);
  79.         setVisible(true);
  80.         setOpaque(true);
  81.         needToRedispatch = false;
  82.        
  83.     GridBagConstraints c = new GridBagConstraints();
  84.         c.fill = GridBagConstraints.HORIZONTAL;
  85.     //  c.weightx = ;
  86.         c.gridx = 1;
  87.         c.gridy = 1;
  88.        
  89.         addMouseListener(this);
  90.         addMouseMotionListener(this);
  91.         addFocusListener(this);
  92.         JPanel loginpanel = new JPanel();
  93.         loginpanel.setPreferredSize(new Dimension(500,300));
  94.         loginpanel.setBackground(Color.white);
  95.         add(loginpanel, c);
  96.    
  97.         GridLayout grid = new GridLayout(0,2);
  98.         grid.setHgap(20);
  99.         grid.setVgap(30);
  100.         loginpanel.setLayout(grid);
  101.    
  102.    
  103.    
  104.         loginBut = new JButton("Login");
  105.         loginBut.setFont(new Font(loginBut.getFont().getFontName(), loginBut.getFont().getStyle(), 25));
  106.  
  107.         usernamelabel = new JLabel("UserID");
  108.         usernamelabel.setFont(new Font(usernamelabel.getFont().getFontName(), usernamelabel.getFont().getStyle(), 25));
  109.  
  110.         passwordlabel = new JLabel("Password");
  111.         passwordlabel.setFont(new Font(passwordlabel.getFont().getFontName(), passwordlabel.getFont().getStyle(), 25));
  112.  
  113.         errorlabel = new JLabel();
  114.         errorlabel.setFont(new Font(errorlabel.getFont().getFontName(), errorlabel.getFont().getStyle(), 25));
  115.  
  116.         username = new JTextField();
  117.         username.setFont(new Font(username.getFont().getFontName(), username.getFont().getStyle(), 25));
  118.  
  119.         password = new JPasswordField();
  120.         password.setFont(new Font(password.getFont().getFontName(), password.getFont().getStyle(), 25));
  121.  
  122.        
  123.         loginpanel.add(usernamelabel);
  124.         loginpanel.add(username);
  125.         loginpanel.add(passwordlabel);
  126.         loginpanel.add(password);
  127.         loginpanel.add(new JLabel(""));
  128.         loginpanel.add(loginBut);
  129.         c.gridy = 2;
  130.         add(errorlabel, c);
  131.         ButtonMouseListen bml = new ButtonMouseListen();
  132.         loginBut.addMouseListener(bml);
  133.        
  134.     }
  135.  
  136.     public void setVisible(boolean v) {
  137.         // Make sure we grab the focus so that key events don't go astray.
  138.         if (v){
  139.             requestFocus();
  140.         }
  141.         setNeedToRedispatch(false);
  142.         super.setVisible(v);
  143.  
  144.     }
  145.  
  146.     // Once we have focus, keep it if we're visible
  147.     public void focusLost(FocusEvent fe) {
  148.         if (isVisible())
  149.             requestFocus();
  150.     }
  151.  
  152.     public void focusGained(FocusEvent fe) {
  153.     }
  154.  
  155.     // We only need to redispatch if we're not visible, but having full control
  156.     // over this might prove handy.
  157.     public void setNeedToRedispatch(boolean need) {
  158.         needToRedispatch = need;
  159.     }
  160.  
  161.     /*
  162.      * (Based on code from the Java Tutorial) We must forward at least the mouse
  163.      * drags that started with mouse presses over the check box. Otherwise, when
  164.      * the user presses the check box then drags off, the check box isn't
  165.      * disarmed -- it keeps its dark gray background or whatever its L&F uses to
  166.      * indicate that the button is currently being pressed.
  167.      */
  168.     public void mouseDragged(MouseEvent e) {
  169.         if (needToRedispatch)
  170.             redispatchMouseEvent(e);
  171.     }
  172.  
  173.     public void mouseMoved(MouseEvent e) {
  174.         if (needToRedispatch)
  175.             redispatchMouseEvent(e);
  176.     }
  177.  
  178.     public void mouseClicked(MouseEvent e) {
  179.         if (needToRedispatch)
  180.             redispatchMouseEvent(e);
  181.     }
  182.  
  183.     public void mouseEntered(MouseEvent e) {
  184.         if (needToRedispatch)
  185.             redispatchMouseEvent(e);
  186.     }
  187.  
  188.     public void mouseExited(MouseEvent e) {
  189.         if (needToRedispatch)
  190.             redispatchMouseEvent(e);
  191.     }
  192.  
  193.     public void mousePressed(MouseEvent e) {
  194.         if (needToRedispatch)
  195.             redispatchMouseEvent(e);
  196.     }
  197.  
  198.     public void mouseReleased(MouseEvent e) {
  199.         if (needToRedispatch) {
  200.             redispatchMouseEvent(e);
  201.  
  202.         }
  203.     }
  204.  
  205.     private void redispatchMouseEvent(MouseEvent e) {
  206.  
  207.     }
  208.     @Override
  209.     protected void paintComponent(Graphics g)
  210.     {
  211.         g.setColor( getBackground() );
  212.         g.fillRect(0, 0, getSize().width, getSize().height);
  213.     }
  214.  
  215.    
  216.     private class ButtonMouseListen implements MouseListener {
  217.  
  218.         public ButtonMouseListen() {
  219.         }
  220.  
  221.         /**
  222.          * determines what button was clicked and does something
  223.          */
  224.         public void mouseClicked(MouseEvent e) {
  225.             if(username.getText().equals("") || password.getText().equals("")){
  226.                 errorlabel.setText("Please Check That You Filled in the Fields");
  227.                 return;
  228.             }
  229.             int uid = -1;
  230.             try{
  231.                 uid = Integer.parseInt(username.getText());
  232.             }
  233.             catch(Exception e2){
  234.                 errorlabel.setText("User does not exist");
  235.                 return;
  236.             }
  237.             Session session = HibernateUtil.getSessionFactory().openSession();
  238.             User u = (User)session.get(User.class, uid);
  239.             if(u == null){
  240.                 errorlabel.setText("User does not exist");
  241.                 return;
  242.             }
  243.             if(u.getPassword().equals(password.getText())){
  244.                 model.setUser(u);
  245.                 username.setText("");
  246.                 password.setText("");
  247.                 errorlabel.setText("");
  248.                 setNeedToRedispatch(true);
  249.                 setVisible(false);
  250.             //  setOpaque(false);
  251.             }
  252.             else{
  253.                 errorlabel.setText("Incorrect Password");
  254.                 return;
  255.             }
  256.         }
  257.  
  258.         @Override
  259.         public void mouseEntered(MouseEvent arg0) {
  260.             // TODO Auto-generated method stub
  261.  
  262.         }
  263.  
  264.         @Override
  265.         public void mouseExited(MouseEvent arg0) {
  266.             // TODO Auto-generated method stub
  267.  
  268.         }
  269.  
  270.         @Override
  271.         public void mousePressed(MouseEvent arg0) {
  272.             // TODO Auto-generated method stub
  273.  
  274.         }
  275.  
  276.         @Override
  277.         public void mouseReleased(MouseEvent arg0) {
  278.             // TODO Auto-generated method stub
  279.  
  280.         }
  281.  
  282.     }
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement