-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditContact.java
More file actions
213 lines (179 loc) · 6.27 KB
/
EditContact.java
File metadata and controls
213 lines (179 loc) · 6.27 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.StringTokenizer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
class EditContact
{
JFrame window; JPanel panel, btnPanel; JButton saveBtn, dontSaveBtn;
JTextField f[]; JLabel labels[]; int idn; String contactLine;
public EditContact(String contactLine)
{
idn = 0; this.contactLine = contactLine;
window = new JFrame("Edit Contact");
panel = new JPanel(new GridBagLayout());
btnPanel = new JPanel(new GridBagLayout());
window.setSize(500, 500);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f = new JTextField[11];
for(int i = 0; i < f.length; i++)
f[i] = new JTextField(18);
saveBtn = new JButton("Save Changes");
dontSaveBtn = new JButton("Don't Save");
}
public void addComponents()
{
labels = new JLabel[11];
labels[0] = new JLabel(" First Name ");
labels[1] = new JLabel(" Middle Name ");
labels[2] = new JLabel(" Last Name ");
labels[3] = new JLabel(" Email Address ");
labels[4] = new JLabel(" Mobile Number ");
labels[5] = new JLabel(" Home / Landline Number ");
labels[6] = new JLabel(" Notes ");
labels[7] = new JLabel(" Address");
labels[8] = new JLabel(" Pin-Code ");
labels[9] = new JLabel(" City");
labels[10] = new JLabel(" State");
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.gridx = 0; c.gridy = 0;
for(int i = 0; i < f.length; i++,c.gridy--)
panel.add(labels[i], c);
c.gridx = 1; c.gridy = 0;
for(int i = 0; i < f.length; i++, c.gridy--)
panel.add(f[i], c);
panel.add(btnPanel, c);
c.gridx = 0; c.gridy = 0; btnPanel.add(saveBtn, c);
c.gridx = 1; btnPanel.add(dontSaveBtn, c);
window.add(panel);
}
public String removeHash(String x)
{
if(x.trim().equals("#") == true)
return "";
else
return x.trim();
}
public void putDetailsInField()
{
StringTokenizer st = new StringTokenizer(contactLine,"\t");
idn = Integer.parseInt(st.nextToken());
int k = 0;
while(st.hasMoreTokens())
f[k++].setText(removeHash(st.nextToken()));
}
public String getChangedDetails(String x)
{
String p = x + "\t";
for(int i = 0; i < f.length; i++)
p = p + addHashToEmptyField(f[i].getText()) + "\t";
return p;
}
public String addHashToEmptyField(String x)
{
if(x.equals("") == true)
return "#";
return x;
}
public void saveNewDetails()throws IOException
{
FileReader fr = new FileReader("ContactRegister.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("ContactRegister1.txt");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
StringTokenizer st = new StringTokenizer(contactLine, "\t");
int idn2 = Integer.parseInt(st.nextToken());
String s = "";
while((s = br.readLine()) != null)
{
st = new StringTokenizer(s, "\t");
int idn1 = Integer.parseInt(st.nextToken());
if(idn1 != idn2)
pw.println(s);
else
pw.println(getChangedDetails(Integer.toString(idn1)));
}
pw.close(); bw.close(); fw.close(); br.close(); fr.close();
}
public void updateOrigninalDetails()throws IOException
{
FileReader fr = new FileReader("ContactRegister1.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("ContactRegister.txt");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
String s = "";
while((s = br.readLine()) != null)
pw.println(s);
pw.close(); bw.close(); fw.close(); br.close(); fr.close();
}
public void handleEvents()
{
EditionHandlerClass h = new EditionHandlerClass();
saveBtn.addActionListener(h);
dontSaveBtn.addActionListener(h);
}
class EditionHandlerClass implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == saveBtn)
{
try
{
saveNewDetails();
updateOrigninalDetails();
ContactList ob = new ContactList();
ob.callAllMethods();
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
finally
{
window.dispose();
}
}
else if(e.getSource() == dontSaveBtn)
{
window.dispose();
ContactList ob = new ContactList();
try
{
ob.callAllMethods();
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
}
public void callAllMethods()throws IOException
{
addComponents(); putDetailsInField(); handleEvents();
}
public static void main(String args[])
{
//EditContact ob = new EditContact("");
// ob.addComponents(); // ob.putDetailsInField();//ob.handleEvents();
}
}