-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFacePamphlet.java
More file actions
292 lines (265 loc) · 9.64 KB
/
Copy pathFacePamphlet.java
File metadata and controls
292 lines (265 loc) · 9.64 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* File: FacePamphlet.java
* -----------------------
* When it is finished, this program will implement a basic social network
* management system.
*/
import acm.program.*;
import acm.graphics.*;
import acm.util.*;
import java.awt.event.*;
import javax.swing.*;
public class FacePamphlet extends Program
implements FacePamphletConstants {
/**
* This method has the responsibility for initializing the
* interactors in the application, and taking care of any other
* initialization that needs to be performed.
*/
public void init() {
JLabel nameLabel = new JLabel("Name: ");
add(nameLabel, NORTH);
nameInput.addActionListener(this);
nameInput.setActionCommand("Add");
add(nameInput, NORTH);
JButton addButton = new JButton("Add");
add(addButton, NORTH);
JButton deleteButton = new JButton("Delete");
add(deleteButton, NORTH);
JButton lookupButton = new JButton("Lookup");
add(lookupButton, NORTH);
changeStatusInput.addActionListener(this);
changeStatusInput.setActionCommand("Change Status");
add(changeStatusInput, WEST);
JButton changeStatusButton = new JButton("Change Status");
add(changeStatusButton, WEST);
add(new JLabel(EMPTY_LABEL_TEXT), WEST);
changePicInput.addActionListener(this);
changePicInput.setActionCommand("Change Picture");
add(changePicInput, WEST);
JButton changePicButton = new JButton("Change Picture");
add(changePicButton, WEST);
add(new JLabel(EMPTY_LABEL_TEXT), WEST);
addFriendInput.addActionListener(this);
addFriendInput.setActionCommand("Add Friend");
add(addFriendInput, WEST);
JButton addFriendButton = new JButton("Add Friend");
add(addFriendButton, WEST);
add(new JLabel(EMPTY_LABEL_TEXT), WEST);
deleteFriendInput.addActionListener(this);
deleteFriendInput.setActionCommand("Delete Friend");
add(deleteFriendInput, WEST);
JButton deleteFriendButton = new JButton("Delete Friend");
add(deleteFriendButton, WEST);
addActionListeners();
database = new FacePamphletDatabase();
}
public FacePamphlet(){
graph = new FacePamphletCanvas();
add(graph);
}
/**
* This class is responsible for detecting when the buttons are
* clicked or interactors are used, so you will have to add code
* to respond to these actions.
*/
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
// when addButton is clicked
if(cmd.equals("Add")) {
String str = nameInput.getText();
if(!str.equals("")) {
// check whether the database has this profile, if not, create a new profile
if (!database.containsProfile(str)) {
FacePamphletProfile profile = new FacePamphletProfile(str);
database.addProfile(profile);
currentProfile = profile;
String msg = "New profile created";
graph.displayProfile(currentProfile);
graph.showMessage(msg);
} else {
currentProfile = database.getProfile(str);
String msg ="Profile for "+str+" already exists!";
graph.displayProfile(currentProfile);
graph.showMessage(msg);
}
}
//when delete Button is clicked
} else if(cmd.equals("Delete")) {
String str = nameInput.getText();
if(!str.equals("")) {
currentProfile = null;
/*
* check whether the database has this profile
* if yes, then delete it
*/
if (database.containsProfile(str)) {
database.deleteProfile(str);
String msg = "Profile of: "+str+" deleted";
graph.displayProfile(currentProfile);
graph.showMessage(msg);
} else {
String msg = "A profile for "+str+" does not exists";
graph.displayProfile(currentProfile);
graph.showMessage(msg);
}
}
// when lookup button is clicked
} else if(cmd.equals("Lookup")) {
String str = nameInput.getText();
if(!str.equals("")) {
/*
* check whether the database has this profile
* if contains, display it
*/
if (database.containsProfile(str)) {
currentProfile = database.getProfile(str);
String msg = "Displaying "+ str;
graph.displayProfile(currentProfile);
graph.showMessage(msg);
} else {
currentProfile = null;
String msg = "A profile for "+str+" does not exists";
graph.displayProfile(currentProfile);
graph.showMessage(msg);
}
}
//when change status button is clicked
} else if(cmd.equals("Change Status")) {
String str = changeStatusInput.getText();
if (!str.equals("")) {
/*
* check whether there is a current profile
* if yes, then change the status
*/
if (currentProfile!=null) {
currentProfile.setStatus(str);
String msg = "Status updated to"+ str;
graph.displayProfile(currentProfile);
graph.showMessage(msg);
} else{
String msg = "Please select a profile!";
graph.displayProfile(currentProfile);
graph.showMessage(msg);
}
}
//when clicked change picture button
} else if(cmd.equals("Change Picture")) {
String str = changePicInput.getText();
if (!str.equals("")){
/*
* check whether there is a current profile
* if yes, then load the image(if cannot load, prompt an notice)
* then change the profile image
*/
if(currentProfile!=null){
GImage image = null;
try{
image = new GImage(str);
currentProfile.setImage(image);
String msg = "Picture updated";
graph.displayProfile(currentProfile);
graph.showMessage(msg);
} catch (ErrorException ex){
String msg = "please enter a valid file name!";
graph.displayProfile(currentProfile);
graph.showMessage(msg);
}
} else {
String msg = "Please select a profile!";
graph.displayProfile(currentProfile);
graph.showMessage(msg);
}
}
//when add friend button is clicked
} else if(cmd.equals("Add Friend")) {
String str = addFriendInput.getText();
if(!str.equals("")) {
//check whether has a current profile
if(currentProfile!=null){
//check whether the database contains the name
if(database.containsProfile(str)) {
//check whether the name is current profile itself
if(!str.equals(currentProfile.getName())){
addFriends(str);
String msg = str + " added as a friend";
graph.displayProfile(currentProfile);
graph.showMessage(msg);
}
} else {
String msg = "Please enter a valid name!";
graph.displayProfile(currentProfile);
graph.showMessage(msg);
}
} else {
String msg = "Please select a profile!";
graph.displayProfile(currentProfile);
graph.showMessage(msg);
}
}
//when delete button is clicked
} else if(cmd.equals("Delete Friend")) {
String str = deleteFriendInput.getText();
if(!str.equals("")) {
//check whether has a current profile
if(currentProfile!=null){
////check whether the database contains the name
if(database.containsProfile(str)) {
deleteFriends(str);
String msg = "Deleted "+str + " as a friend";
graph.displayProfile(currentProfile);
graph.showMessage(msg);
} else {
String msg = "Please enter a valid name!";
graph.displayProfile(currentProfile);
graph.showMessage(msg);
}
} else {
String msg = "Please select a profile!";
graph.displayProfile(currentProfile);
graph.showMessage(msg);
}
}
}
}
/**
* actual add friends procedure, currentProfile will add name to his friends list
* and profile with "name" will add current profile to his friends list
* if they are already friends with each other, then prompt an notice
* @param name friends that will be added to currentProfile
*/
private void addFriends(String name) {
if(currentProfile.addFriend(name)) {
database.getProfile(name).addFriend(currentProfile.getName());
println(name+" are friends with "+currentProfile.getName()+" now");
} else {
println(name+" are already friends with "+ currentProfile.getName());
}
}
/**
* actual delete friends procedure, currentProfile will remove name from his friends list
* and profile with "name" will remove current profile from his friends list
* if they are already NOT friends with each other, prompt an notice
* @param name friends that will be added to currentProfile
*/
private void deleteFriends(String name) {
if(currentProfile.removeFriend(name)) {
database.getProfile(name).removeFriend(currentProfile.getName());
println(name+" are NOT friends with "+currentProfile.getName()+" now");
} else {
println(name+" are already NOT friends with "+ currentProfile.getName());
}
}
/*
* all the instance variables
*/
private JTextField nameInput = new JTextField(TEXT_FIELD_SIZE);
private JTextField changeStatusInput = new JTextField(TEXT_FIELD_SIZE);
private JTextField changePicInput = new JTextField(TEXT_FIELD_SIZE);
private JTextField addFriendInput = new JTextField(TEXT_FIELD_SIZE);
private JTextField deleteFriendInput = new JTextField(TEXT_FIELD_SIZE);
private FacePamphletCanvas graph;
private FacePamphletProfile currentProfile = null;
// database of the program
private FacePamphletDatabase database;
}