import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class TestFrames extends JFrame{
private JButton jSubmit;
private JButton jNew;
private JTextField jUser;
private JPasswordField jPass;
private JLabel jlText = new JLabel("ID Number:");
private JLabel jlPass = new JLabel("Password:");
private JPanel jAll;
private String password="";
private static PrintWriter writer;
private static BufferedReader reader;
public static JFrame frame = new JFrame();
private Socket socket;
private static InputStreamReader inputstreamreader;
private static Salary obj;
private String id;
private JPanel jnew;
private JPanel jCenter;
private JPanel jLeft;
private boolean newUser = false;
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
TestFrames t= new TestFrames();
}
public TestFrames() throws IOException{
try{
connectServer();
setUser();
}catch(UnknownHostException unhe){
System.out.println("UnknownHostException: " + unhe.getMessage());
}catch(InterruptedIOException intioe){
System.out.println("Timeout while attempting to establish socket connection.");
}catch(IOException ioe){
System.out.println("IOException: " + ioe.getMessage());
}
}
private void connectServer() throws IOException, IOException {
// TODO Auto-generated method stub
socket = new Socket("localhost", 8000);
inputstreamreader = new InputStreamReader(socket.getInputStream());
reader = new BufferedReader(inputstreamreader);
writer = new PrintWriter(socket.getOutputStream(),true);
}
public void setUser (){
jSubmit = new JButton("Submit");
jNew = new JButton("New User");
jUser = new JTextField();
jPass = new JPasswordField(10);
jlText.setLabelFor(jUser);
jlPass.setLabelFor(jPass);
jAll = new JPanel(new GridLayout(0,2));
JPanel jpbutton = new JPanel(new FlowLayout(FlowLayout.CENTER));
jLeft = new JPanel(new GridLayout(2,0));
jCenter = new JPanel(new GridLayout(2,2));
jpbutton.add(jSubmit);
jnew = new JPanel(new FlowLayout(FlowLayout.CENTER));
jnew.add(jNew);
jSubmit.addActionListener(new ActionListener() {
@Override
public void actionPerformed (ActionEvent e) {
// TODO Auto-generated method stub
id = jUser.getText();
char[] pass = jPass.getPassword();
for(int i=0;i<pass.length;i++){
password = password + pass[i];
}
if(!id.matches("\\d{9}")){
JOptionPane.showMessageDialog(null, "Please enter correct ID of 9 Digits");
jUser.requestFocusInWindow();
}
else{
if(newUser)
writer.println("1");
else
writer.println("2");
writer.println(id);
if(newUser){
//JOptionPane.showMessageDialog(null,"?FS");
//check file already on server;
String ServerAnswer;
try {
ServerAnswer = reader.readLine();
System.out.println(ServerAnswer);
if(ServerAnswer.equals("OK"))
{
String correct = reader.readLine();
System.out.println(correct);
if(pass(correct,password)){
writer.println("Continue");
changes();
frame.dispose();
}
else
{
JOptionPane.showMessageDialog(null, "Wrong user name or Password");
writer.println("Wait");
password = "";
}
}
else
if(ServerAnswer.equals("New"))
{
writer.println(password);
obj = new Salary(writer,frame);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else{
String ServerAnswer;
try {
ServerAnswer = reader.readLine();
System.out.println(ServerAnswer);
if(ServerAnswer.equals("Wrong ID user not Exsists"))
{
JOptionPane.showMessageDialog(null, "Wrong ID user not Exsists");
//System.exit(1);
}
else
{
String correct = reader.readLine();
System.out.println(correct);
if(pass(correct,password)){
writer.println("Continue");
changes();
frame.dispose();
}
else
{
JOptionPane.showMessageDialog(null, "Wrong user name or Password");
writer.println("Wait");
password = "";
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
});
jNew.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
newUser=true;
frame.getContentPane().removeAll();
jLeft.setBorder(new TitledBorder("New User Registration"));
frame.add(jLeft);
frame.setTitle("New User");
frame.setSize(300, 150);
frame.setLocationRelativeTo(frame);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setVisible(true);
System.out.println(newUser);
}
});
jCenter.add(jlText);
jCenter.add(jUser);
jCenter.add(jlPass);
jCenter.add(jPass);
jLeft.setBorder(new TitledBorder("User Exsists"));
jLeft.add(jCenter);
jLeft.add(jpbutton);
jAll.add(jLeft);
jAll.add(jnew);
frame.add(jAll);
frame.setTitle("Checker For Worker");
frame.setSize(300, 150);
frame.setLocation(0, 0);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int i = JOptionPane.showConfirmDialog(frame,
"Are you sure to exit?", "Closing dialog",
JOptionPane.YES_NO_OPTION);
if (i == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
}
public static boolean pass(String correct, String pass) throws IOException
{
if(correct.equals(pass))
return true;
else{
return false;
}
}
public static void changes() throws IOException
{
frame.getContentPane().removeAll();
JLabel labelBack = new JLabel("Welcome Back! Let's Continue");
JPanel panelBack = new JPanel(new FlowLayout());
panelBack.add(labelBack);
//JOptionPane.showMessageDialog(null, "Welcome Back! \n Let's Continue");
JLabel labelText = new JLabel ("Were their any changes from the last time you signed in?");
JPanel panelText = new JPanel(new FlowLayout());
panelText.add(labelText);
final JRadioButton yes = new JRadioButton("Yes");
JRadioButton no = new JRadioButton("No");
ButtonGroup group = new ButtonGroup();
group.add(yes);
group.add(no);
JPanel answer = new JPanel(new FlowLayout());
answer.add(yes);
answer.add(no);
JPanel all = new JPanel(new GridLayout(4,0));
all.add(panelBack);
all.add(panelText);
all.add(answer);
final JFrame frame1 = new JFrame("Changes?");
frame1.add(all);
frame1.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame1.setLocationRelativeTo(frame);
frame1.setSize(320,200);
frame1.setVisible(true);
//int change = JOptionPane.showConfirmDialog(null, "Were their any changes from \n the last time you signed in?");
frame1.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int i = JOptionPane.showConfirmDialog(frame1,
"Are you sure to exit?", "Closing dialog",
JOptionPane.YES_NO_OPTION);
if (i == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
no.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
writer.println("1");
try {
obj = new Salary(writer,reader,frame);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// TODO Auto-generated catch block
frame1.dispose();
}
});
yes.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
writer.println("0");
try {
obj = new Salary(writer,frame);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
frame1.dispose();
}
});
}
}