Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //GUI Launcher
- userlogin = new JUserPanel(register); //Controller class
- GUIController gc = new GUIController(content, userlogin); //content is actually useless, guicontroller is below.
- KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(gc);
- this.setGlassPane(userlogin);
- userlogin.setVisible(true);
- userlogin.setOpaque(true);
- userlogin.validate();
- userlogin.repaint();
- //GUIController
- package Presenter;
- import java.awt.Container;
- import java.awt.KeyEventPostProcessor;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.util.Observable;
- import java.util.Observer;
- import View.JUserPanel;
- /** Controls the GUI */
- public class GUIController implements KeyEventPostProcessor{
- private Container content;
- private JUserPanel glasspanel;
- public GUIController(Container content, JUserPanel userlogin) {
- this.content = content;
- this.glasspanel = userlogin;
- }
- @Override
- public boolean postProcessKeyEvent(KeyEvent e) {
- char mychar = 'm';
- if(e.isAltDown()){
- if(e.getKeyChar()==mychar){
- //content
- glasspanel.setOpaque(true);
- glasspanel.setNeedToRedispatch(false);
- glasspanel.setVisible(true);
- glasspanel.validate();
- glasspanel.repaint();
- }
- }
- return true;
- }
- }
- //GlassPane
- package View;
- public class JUserPanel extends JComponent implements MouseListener,
- MouseMotionListener, FocusListener {
- // trigger for redispatching (allows external control)
- boolean needToRedispatch = false;
- private JButton loginBut;
- private JLabel usernamelabel;
- private JLabel passwordlabel;
- private JLabel errorlabel;
- private JTextField username;
- private JPasswordField password;
- private Register model;
- public JUserPanel(Register register) {
- this.model = register;
- setLayout(new GridBagLayout());
- setBackground(Color.white);
- setVisible(true);
- setOpaque(true);
- needToRedispatch = false;
- GridBagConstraints c = new GridBagConstraints();
- c.fill = GridBagConstraints.HORIZONTAL;
- // c.weightx = ;
- c.gridx = 1;
- c.gridy = 1;
- addMouseListener(this);
- addMouseMotionListener(this);
- addFocusListener(this);
- JPanel loginpanel = new JPanel();
- loginpanel.setPreferredSize(new Dimension(500,300));
- loginpanel.setBackground(Color.white);
- add(loginpanel, c);
- GridLayout grid = new GridLayout(0,2);
- grid.setHgap(20);
- grid.setVgap(30);
- loginpanel.setLayout(grid);
- loginBut = new JButton("Login");
- loginBut.setFont(new Font(loginBut.getFont().getFontName(), loginBut.getFont().getStyle(), 25));
- usernamelabel = new JLabel("UserID");
- usernamelabel.setFont(new Font(usernamelabel.getFont().getFontName(), usernamelabel.getFont().getStyle(), 25));
- passwordlabel = new JLabel("Password");
- passwordlabel.setFont(new Font(passwordlabel.getFont().getFontName(), passwordlabel.getFont().getStyle(), 25));
- errorlabel = new JLabel();
- errorlabel.setFont(new Font(errorlabel.getFont().getFontName(), errorlabel.getFont().getStyle(), 25));
- username = new JTextField();
- username.setFont(new Font(username.getFont().getFontName(), username.getFont().getStyle(), 25));
- password = new JPasswordField();
- password.setFont(new Font(password.getFont().getFontName(), password.getFont().getStyle(), 25));
- loginpanel.add(usernamelabel);
- loginpanel.add(username);
- loginpanel.add(passwordlabel);
- loginpanel.add(password);
- loginpanel.add(new JLabel(""));
- loginpanel.add(loginBut);
- c.gridy = 2;
- add(errorlabel, c);
- ButtonMouseListen bml = new ButtonMouseListen();
- loginBut.addMouseListener(bml);
- }
- public void setVisible(boolean v) {
- // Make sure we grab the focus so that key events don't go astray.
- if (v){
- requestFocus();
- }
- setNeedToRedispatch(false);
- super.setVisible(v);
- }
- // Once we have focus, keep it if we're visible
- public void focusLost(FocusEvent fe) {
- if (isVisible())
- requestFocus();
- }
- public void focusGained(FocusEvent fe) {
- }
- // We only need to redispatch if we're not visible, but having full control
- // over this might prove handy.
- public void setNeedToRedispatch(boolean need) {
- needToRedispatch = need;
- }
- /*
- * (Based on code from the Java Tutorial) We must forward at least the mouse
- * drags that started with mouse presses over the check box. Otherwise, when
- * the user presses the check box then drags off, the check box isn't
- * disarmed -- it keeps its dark gray background or whatever its L&F uses to
- * indicate that the button is currently being pressed.
- */
- public void mouseDragged(MouseEvent e) {
- if (needToRedispatch)
- redispatchMouseEvent(e);
- }
- public void mouseMoved(MouseEvent e) {
- if (needToRedispatch)
- redispatchMouseEvent(e);
- }
- public void mouseClicked(MouseEvent e) {
- if (needToRedispatch)
- redispatchMouseEvent(e);
- }
- public void mouseEntered(MouseEvent e) {
- if (needToRedispatch)
- redispatchMouseEvent(e);
- }
- public void mouseExited(MouseEvent e) {
- if (needToRedispatch)
- redispatchMouseEvent(e);
- }
- public void mousePressed(MouseEvent e) {
- if (needToRedispatch)
- redispatchMouseEvent(e);
- }
- public void mouseReleased(MouseEvent e) {
- if (needToRedispatch) {
- redispatchMouseEvent(e);
- }
- }
- private void redispatchMouseEvent(MouseEvent e) {
- }
- @Override
- protected void paintComponent(Graphics g)
- {
- g.setColor( getBackground() );
- g.fillRect(0, 0, getSize().width, getSize().height);
- }
- private class ButtonMouseListen implements MouseListener {
- public ButtonMouseListen() {
- }
- /**
- * determines what button was clicked and does something
- */
- public void mouseClicked(MouseEvent e) {
- if(username.getText().equals("") || password.getText().equals("")){
- errorlabel.setText("Please Check That You Filled in the Fields");
- return;
- }
- int uid = -1;
- try{
- uid = Integer.parseInt(username.getText());
- }
- catch(Exception e2){
- errorlabel.setText("User does not exist");
- return;
- }
- Session session = HibernateUtil.getSessionFactory().openSession();
- User u = (User)session.get(User.class, uid);
- if(u == null){
- errorlabel.setText("User does not exist");
- return;
- }
- if(u.getPassword().equals(password.getText())){
- model.setUser(u);
- username.setText("");
- password.setText("");
- errorlabel.setText("");
- setNeedToRedispatch(true);
- setVisible(false);
- // setOpaque(false);
- }
- else{
- errorlabel.setText("Incorrect Password");
- return;
- }
- }
- @Override
- public void mouseEntered(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseExited(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mousePressed(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseReleased(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement