-
Notifications
You must be signed in to change notification settings - Fork 1
/
SignupForm.java
183 lines (158 loc) · 5.73 KB
/
SignupForm.java
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Java program to implement
// a Simple Registration Form
// using Java Swing
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SignupForm
extends JDialog
implements ActionListener {
// Components of the Form
private Container c;
private JLabel title;
private JLabel name;
private JTextField tname;
private JLabel pass;
private JPasswordField tpass;
private JLabel email;
private JTextField temail;
private JCheckBox term;
private JButton sub;
private JButton reset;
private JLabel res;
String entries;
// constructor, to initialize the components
// with default values.
public SignupForm()
{
this.setModal(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setTitle("Sign up Form");
setBounds(250, 200, 600, 400);
// this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setResizable(false);
c = getContentPane();
c.setLayout(null);
title = new JLabel("AirBite Sign Up");
title.setFont(new Font("Arial", Font.PLAIN, 30));
title.setSize(300, 30);
title.setLocation(100, 40);
c.add(title);
name = new JLabel("Name");
name.setFont(new Font("Arial", Font.PLAIN, 20));
name.setSize(100, 20);
name.setLocation(100, 100);
c.add(name);
tname = new JTextField();
tname.setFont(new Font("Arial", Font.PLAIN, 15));
tname.setSize(190, 20);
tname.setLocation(200, 100);
c.add(tname);
pass = new JLabel("Password");
pass.setFont(new Font("Arial", Font.PLAIN, 20));
pass.setSize(100, 20);
pass.setLocation(100, 128);
c.add(pass);
tpass = new JPasswordField();
tpass.setFont(new Font("Arial", Font.PLAIN, 15));
tpass.setSize(190, 20);
tpass.setLocation(200, 128);
c.add(tpass);
email = new JLabel("Email");
email.setFont(new Font("Arial", Font.PLAIN, 20));
email.setSize(100, 20);
email.setLocation(100, 155);
c.add(email);
temail = new JTextField();
temail.setFont(new Font("Arial", Font.PLAIN, 15));
temail.setSize(190, 20);
temail.setLocation(200, 155);
c.add(temail);
term = new JCheckBox("Accept Terms And Conditions.");
term.setFont(new Font("Arial", Font.PLAIN, 15));
term.setSize(250, 20);
term.setLocation(150, 200);
term.addActionListener(this);
c.add(term);
sub = new JButton("Submit");
sub.setFont(new Font("Arial", Font.PLAIN, 15));
sub.setSize(100, 20);
sub.setLocation(150, 250);
sub.addActionListener(this);
sub.setEnabled(false);
c.add(sub);
reset = new JButton("Reset");
reset.setFont(new Font("Arial", Font.PLAIN, 15));
reset.setSize(100, 20);
reset.setLocation(150, 280);
reset.addActionListener(this);
c.add(reset);
setVisible(true);
}
// method actionPerformed()
// to get the action performed
// by the user and act accordingly
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == term) {
if (term.isSelected())
sub.setEnabled(true);
else
sub.setEnabled(false);
}
else if (e.getSource() == sub) {
if (term.isSelected()) {
entries = "register|";
entries += tname.getText();
entries += "|" + temail.getText();
String encryptedEntries = new String(entries);
String p = new String(tpass.getPassword());
try {
encryptedEntries += "|" + Client.encryptStringWithPublicKey(p);
} catch (Exception e1) {
e1.printStackTrace();
}
entries += "|" + p;
System.out.println("SingupForm: Sending to Server : "+ encryptedEntries);
try {
Client.os.write(entries.getBytes());
// get server response
byte[] response = new byte[1024];
Client.is.read(response);
String res = new String(response);
System.out.println("LoginForm: server response " + new String(res));
// display a dialog box if the response is "Login failed"
if(res.contains("failed")) {
System.out.println("signupForm: Registeration Failed");
this.setModal(false);
this.dispose();
JOptionPane.showMessageDialog(this, "Email already Exist", "Sign-up failed", JOptionPane.ERROR_MESSAGE, new ImageIcon("./img/AirBite-64px-round.png"));
}
else{
System.out.println("Signup Form: Signup successful");
this.setModal(false);
this.dispose();
JOptionPane.showMessageDialog(this, "Signup Successful! Welcome "+ tname.getText() );
}
}
catch(Exception ioe){
ioe.printStackTrace();
}
}
}
else if (e.getSource() == reset) {
String def = "";
tname.setText(def);
temail.setText(def);
tpass.setText(def);
entries = null;
term.setSelected(false);
}
}
public static void main(String[] args){
// new SignupForm();
}
public String getEntries(){
return entries;
}
}