aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ui/LoginPanel.java
blob: bfd47f9c6d44e8f62f774e41060336da20e3f7e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package ui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.JTextArea;
import java.awt.SystemColor;
import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;

import java.awt.event.ActionListener;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.awt.event.ActionEvent;

public class LoginPanel extends JPanel {
	public JTextField textFieldUsername;
	public JTextField textFieldPassword;
	public JButton btnLogIn;
	JTextArea warningTextArea;

	/**
	 * Create the panel.
	 */
	public LoginPanel() {
		setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Username");
		lblNewLabel.setBounds(132, 78, 59, 14);
		add(lblNewLabel);
		
		textFieldUsername = new JTextField();
		textFieldUsername.setColumns(10);
		textFieldUsername.setBounds(205, 75, 100, 20);
		add(textFieldUsername);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(132, 109, 59, 14);
		add(lblPassword);
		
		textFieldPassword = new JTextField();
		textFieldPassword.setColumns(10);
		textFieldPassword.setBounds(205, 106, 100, 20);
		add(textFieldPassword);
		
		warningTextArea = new JTextArea();
		warningTextArea.setWrapStyleWord(true);
		warningTextArea.setLineWrap(true);
		warningTextArea.setToolTipText("");
		warningTextArea.setForeground(Color.RED);
		warningTextArea.setFont(new Font("Monospaced", Font.PLAIN, 10));
		warningTextArea.setEditable(false);
		warningTextArea.setBackground(SystemColor.menu);
		warningTextArea.setBounds(132, 134, 158, 36);
		add(warningTextArea);
		btnLogIn = new JButton("Log In");
		btnLogIn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				MainFrame topFrame=(MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, LoginPanel.this);
//				topFrame.getContentPane().removeAll();
//				topFrame.add(new GamePanel(),BorderLayout.CENTER);
//				SwingUtilities.updateComponentTreeUI(topFrame);
				Login();
			}
		});
		btnLogIn.setBounds(174, 181, 81, 23);
		add(btnLogIn);

	}
	
	public void Login() {
		System.out.println("SEND LOGIN");
		String username=textFieldUsername.getText();
		String password=textFieldPassword.getText();
		if(!(username.trim().length()>0 && password.trim().length()>0))
		{
			warningTextArea.setText("Morate uneti login informacije");
			return;
		}
		String loginMsg="LOGIN:"+username+":"+password;
		ByteBuffer bbLogin=ByteBuffer.wrap(loginMsg.getBytes());
		MainFrame topFrame=(MainFrame) SwingUtilities.getAncestorOfClass(MainFrame.class, LoginPanel.this);
		try {
			SocketChannel client=topFrame.getClient();
			client.write(bbLogin);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}