-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdminPanel.java
491 lines (424 loc) · 20.5 KB
/
AdminPanel.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/**
*
* @author jmcgr
*/
import java.util.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
public class AdminPanel implements ActionListener, TreeModelListener, TreeSelectionListener{
public static List<Composite> listOfUsersAndGroups = new ArrayList<>();
public static Map<String, User> mapOfUsers = new HashMap<>();
public static Map<String, Group> mapOfGroups = new HashMap<>();
static ComponentVisitor visitor = new ComponentVisitor();
int panelHeight = 50,
panelWidth = 300,
buttonHeight = 50,
buttonWidth = 300,
longButtonWidth = 600,
topLabelHeight = 20,
topLabelWidth = 600,
compositePanelHeight = 100,
compositePanelWidth = 600,
treeHeight = 400,
treeWidth = 300;
JFrame adminPanel;
JTree groupTree;
//top right panel group
JPanel topRightGroupPanel;
//user ID panel
JPanel userIdPanel;
JLabel userIdLabel;
JTextArea userIdTextArea;
JLabel userDirectionLabel;
//add user Button
JButton addUserButton;
//group ID panel
JPanel groupIdPanel;
JLabel groupIdLabel;
JTextArea groupIdTextArea;
JLabel groupDirectionLabel;
//add group button
JButton addGroupButton;
//open user view button
JButton oUserViewButton;
//show user total button
JButton sUserTotalButton;
//show group total button
JButton sGroupTotalButton;
//show messages total button
JButton sMessagesTotalButton;
//show positive percentage button
JButton sPositivePercentageButton;
//Id verification button
JButton verificationButton;
//last update button
JButton lastUpdateButton;
//Mid panel
JPanel midPanel;
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultTreeModel treeModel = new DefaultTreeModel(root);
private SimpleDateFormat formattedTime = new SimpleDateFormat("MMM dd, yyyy HH:mm");
protected AdminPanel(){
//group tree
treeModel.addTreeModelListener(this);
groupTree = new JTree(treeModel);
groupTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
groupTree.setEditable(true);
groupTree.setShowsRootHandles(true);
groupTree.setBorder(BorderFactory.createLineBorder(Color.BLACK));
groupTree.setBounds(0, 0, treeWidth, treeHeight);
groupTree.setBackground(new Color(80,128,128));
JScrollPane treeView = new JScrollPane(groupTree);
treeView.setBounds(0, 0, treeWidth, treeHeight);
//user ID panel
userIdPanel = new JPanel();
userIdLabel = new JLabel("User ID");
userDirectionLabel = new JLabel("Enter new ID and click 'Add User'");
userIdTextArea = new JTextArea(1,20);
userIdPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
userIdPanel.setBackground(new Color(0,128,128));
userIdPanel.add(userIdLabel);
userIdPanel.add(userIdTextArea);
userIdPanel.add(userDirectionLabel);
userIdPanel.setBounds(300,20,panelWidth,panelHeight);
//add user Button
addUserButton = new JButton("Add User");
addUserButton.addActionListener(this);
addUserButton.setBounds(600,20,buttonWidth,buttonHeight);
addUserButton.setBackground(new Color(0,128,128));
addUserButton.setBorder(BorderFactory.createLineBorder(Color.BLACK));
addUserButton.setFont(new Font("Comic Sans", Font.BOLD, 20));
//group ID panel
groupIdPanel = new JPanel();
groupIdLabel = new JLabel("Group ID");
groupDirectionLabel = new JLabel("Enter new ID and click 'Add Group'");
groupIdTextArea = new JTextArea(1,20);
groupIdPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
groupIdPanel.add(groupIdLabel);
groupIdPanel.add(groupIdTextArea);
groupIdPanel.add(groupDirectionLabel);
groupIdPanel.setBackground(new Color(0,128,128));
//add group button
addGroupButton = new JButton("Add Group");
addGroupButton.addActionListener(this);
addGroupButton.setBounds(600,20,buttonWidth,buttonHeight);
addGroupButton.setBackground(new Color(0,128,128));
addGroupButton.setBorder(BorderFactory.createLineBorder(Color.BLACK));
addGroupButton.setFont(new Font("Comic Sans", Font.BOLD, 20));
//open user view button
oUserViewButton = new JButton("Open User View");
oUserViewButton.addActionListener(this);
oUserViewButton.setBounds(300,120,longButtonWidth,buttonHeight);
oUserViewButton.setBackground(new Color(0,128,128));
oUserViewButton.setBorder(BorderFactory.createLineBorder(Color.BLACK));
oUserViewButton.setFont(new Font("Comic Sans", Font.BOLD, 20));
//ID verification button
verificationButton = new JButton("ID Verification");
verificationButton.addActionListener(this);
verificationButton.setBounds(300,120,buttonWidth,buttonHeight);
verificationButton.setBackground(new Color(0,128,128));
verificationButton.setBorder(BorderFactory.createLineBorder(Color.BLACK));
verificationButton.setFont(new Font("Comic Sans", Font.BOLD, 20));
lastUpdateButton = new JButton("Last Update");
lastUpdateButton.addActionListener(this);
lastUpdateButton.setBounds(300,120,buttonWidth,buttonHeight);
lastUpdateButton.setBackground(new Color(0,128,128));
lastUpdateButton.setBorder(BorderFactory.createLineBorder(Color.BLACK));
lastUpdateButton.setFont(new Font("Comic Sans", Font.BOLD, 20));
//show user total button
sUserTotalButton = new JButton("Show User Total");
sUserTotalButton.addActionListener(this);
sUserTotalButton.setBounds(300,280,buttonWidth,buttonHeight);
sUserTotalButton.setBackground(new Color(0,128,128));
sUserTotalButton.setBorder(BorderFactory.createLineBorder(Color.BLACK));
sUserTotalButton.setFont(new Font("Comic Sans", Font.BOLD, 20));
//show group total button
sGroupTotalButton = new JButton("Show Group Total");
sGroupTotalButton.addActionListener(this);
sGroupTotalButton.setBounds(600,280,buttonWidth,buttonHeight);
sGroupTotalButton.setBackground(new Color(0,128,128));
sGroupTotalButton.setBorder(BorderFactory.createLineBorder(Color.BLACK));
sGroupTotalButton.setFont(new Font("Comic Sans", Font.BOLD, 20));
//show messages total button
sMessagesTotalButton = new JButton("Show Messages Total");
sMessagesTotalButton.addActionListener(this);
sMessagesTotalButton.setBounds(300,330,buttonWidth,buttonHeight);
sMessagesTotalButton.setBackground(new Color(0,128,128));
sMessagesTotalButton.setBorder(BorderFactory.createLineBorder(Color.BLACK));
sMessagesTotalButton.setFont(new Font("Comic Sans", Font.BOLD, 20));
//show positive percentage button
sPositivePercentageButton = new JButton("Show Positive Percentage");
sPositivePercentageButton.addActionListener(this);
sPositivePercentageButton.setBounds(600,330,buttonWidth,buttonHeight);
sPositivePercentageButton.setBackground(new Color(0,128,128));
sPositivePercentageButton.setBorder(BorderFactory.createLineBorder(Color.BLACK));
sPositivePercentageButton.setFont(new Font("Comic Sans", Font.BOLD, 20));
//set top right corner panel
topRightGroupPanel = new JPanel();
topRightGroupPanel.setLayout(new GridLayout(2,2));
topRightGroupPanel.setBounds(300,20,compositePanelWidth,compositePanelHeight);
topRightGroupPanel.add(userIdPanel);
topRightGroupPanel.add(addUserButton);
topRightGroupPanel.add(groupIdPanel);
topRightGroupPanel.add(addGroupButton);
//set mid panel
midPanel = new JPanel();
midPanel.setLayout(new GridLayout(1,2));
midPanel.setBounds(300,170,compositePanelWidth,compositePanelHeight - 50);
midPanel.add(verificationButton);
midPanel.add(lastUpdateButton);
//set bottom right corner panel
JPanel bottomRightPanel = new JPanel();
bottomRightPanel.setLayout(new GridLayout(2,2));
bottomRightPanel.setBounds(300,263,compositePanelWidth,compositePanelHeight);
bottomRightPanel.add(sUserTotalButton);
bottomRightPanel.add(sGroupTotalButton);
bottomRightPanel.add(sMessagesTotalButton);
bottomRightPanel.add(sPositivePercentageButton);
JLabel topLabel = new JLabel("Admin Operations");
topLabel.setForeground(Color.BLACK);
topLabel.setBackground(Color.white);
topLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
topLabel.setBounds(300,0,topLabelWidth,topLabelHeight);
topLabel.setHorizontalAlignment(JLabel.CENTER);
topLabel.setOpaque(true);
//create admin basic frame
adminPanel = new JFrame();
adminPanel.setLayout(null);
adminPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
adminPanel.setResizable(false);
adminPanel.setTitle("Admin Control Panel");
adminPanel.setSize(900,400);
adminPanel.getContentPane().setBackground(new Color(0x123456));
adminPanel.setVisible(true);
adminPanel.add(treeView);
adminPanel.add(topLabel);
adminPanel.add(topRightGroupPanel);
adminPanel.add(oUserViewButton);
adminPanel.add(midPanel);
adminPanel.add(bottomRightPanel);
}
public List getUGList(){
return this.listOfUsersAndGroups;
}
//SINGLETON
protected static AdminPanel instance = null;
public static AdminPanel getInstance(){
if(instance == null){
synchronized(AdminPanel.class){
if(instance == null){
instance = new AdminPanel();
}
}
}
return instance;
}
public double posMessages(){
double percentage = 0;
double pos = 0;
for(int i = 0; i < UserPanel.messageFeed.size(); i++){
if(UserPanel.messageFeed.get(i).contains("good")){
pos++;
}
}
percentage = (pos / UserPanel.messageFeed.size()) * 100;
return percentage;
}
@Override
public void actionPerformed(ActionEvent e) {
//add user button
if(e.getSource() == addUserButton){
User newUser = new User(userIdTextArea.getText());
if(this.mapOfUsers.containsKey(newUser.getID())){
JOptionPane.showMessageDialog(null, "That user already exists");
}
else{
TreePath path = groupTree.getSelectionPath();
if(path != null){
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) path.getLastPathComponent();
if(selectedNode.getAllowsChildren() == false){
JOptionPane.showMessageDialog(null, "Users cannot contain groups or other users. "
+ "Your selection has been added to \"Root\".");
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(this.userIdTextArea.getText(), false);
this.root.add(newNode);
((DefaultTreeModel) groupTree.getModel()).reload(root);
this.mapOfUsers.put(userIdTextArea.getText(), newUser);
newUser.accept(visitor);
}
else{
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(this.userIdTextArea.getText(), false);
selectedNode.add(newNode);
((DefaultTreeModel) groupTree.getModel()).reload(selectedNode);
this.mapOfUsers.put(userIdTextArea.getText(), newUser);
newUser.accept(visitor);
}
}
else if(path == null){
DefaultMutableTreeNode selectedNode = root;
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(this.userIdTextArea.getText(), false);
selectedNode.add(newNode);
((DefaultTreeModel) groupTree.getModel()).reload(selectedNode);
this.mapOfUsers.put(userIdTextArea.getText(), newUser);
newUser.accept(visitor);
JOptionPane.showMessageDialog(null, "No Group has been selected. "
+ newUser.getID() + " has been added to \"Root\".");
}
newUser.setCreationTime(System.currentTimeMillis());
}
this.userIdTextArea.setText("");
}
//add group button
if(e.getSource() == addGroupButton){
Group newGroup = new Group(groupIdTextArea.getText());
if(this.mapOfGroups.containsKey(newGroup.getID())){
JOptionPane.showMessageDialog(null, "That group already exists");
}
else{
TreePath path = groupTree.getSelectionPath();
if(path != null){
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) path.getLastPathComponent();
if(selectedNode.getAllowsChildren() == false){
JOptionPane.showMessageDialog(null, "Users cannot contain groups or other users. Your selection has been added to Root");
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(this.groupIdTextArea.getText(), true);
this.root.add(newNode);
((DefaultTreeModel) groupTree.getModel()).reload(root);
this.mapOfGroups.put(newGroup.getID(), newGroup);
visitor.visitGroups(newGroup);
}
else if(selectedNode.getAllowsChildren() == true){
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(this.groupIdTextArea.getText(), true);
selectedNode.add(newNode);
((DefaultTreeModel) groupTree.getModel()).reload(selectedNode);
this.mapOfGroups.put(newGroup.getID(), newGroup);
visitor.visitGroups(newGroup);
}
}
else if(path == null){
DefaultMutableTreeNode selectedNode = root;
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(this.groupIdTextArea.getText(), true);
selectedNode.add(newNode);
((DefaultTreeModel) groupTree.getModel()).reload(selectedNode);
this.mapOfGroups.put(newGroup.getID(), newGroup);
visitor.visitGroups(newGroup);
JOptionPane.showMessageDialog(null, "No existing group has been selected. Your new group has been added to \"Root\"");
}
newGroup.setCreationTime(System.currentTimeMillis());
}
this.groupIdTextArea.setText("");
}
//open user view button
if(e.getSource() == oUserViewButton && groupTree.getSelectionPath() != null){
TreePath path = groupTree.getSelectionPath();
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) path.getLastPathComponent();
if(selectedNode.getAllowsChildren() == true){
JOptionPane.showMessageDialog(null, "This is not a user.");
}
else{
new UserPanel(selectedNode.toString(), this.mapOfUsers.get(selectedNode.toString()));
}
}
//show user total button
if(e.getSource() == sUserTotalButton){
JOptionPane.showMessageDialog(null, "Total Users: " + this.visitor.getUserTotal());
}
//show group total button
if(e.getSource() == sGroupTotalButton){
JOptionPane.showMessageDialog(null, "Total Groups: " + this.visitor.getGroupTotal());
}
//show messages total button
if(e.getSource() == sMessagesTotalButton){
JOptionPane.showMessageDialog(null, "Total Messages: " + this.visitor.getMessageTotal());
}
//show positive percentage button
if(e.getSource() == sPositivePercentageButton){
JOptionPane.showMessageDialog(null, String.format("Percentage of Positive Messages: %.2f%%", this.posMessages()));
}
//ID verification button
if(e.getSource() == verificationButton){
if(!this.mapOfUsers.isEmpty() || !this.mapOfGroups.isEmpty()){
boolean isVerified = true;
for(Map.Entry<String,User> mapElement : mapOfUsers.entrySet()){
if(mapElement.getKey().contains(" ")){
isVerified = false;
}
}
for(Map.Entry<String,Group> mapElement : mapOfGroups.entrySet()){
if(mapElement.getKey().contains(" ")){
isVerified = false;
}
}
if(isVerified == false){
JOptionPane.showMessageDialog(null, "Not Verified");
}
else{
JOptionPane.showMessageDialog(null, "Verified");
}
}
}
if(e.getSource() == lastUpdateButton){
long latestTime = 0;
String latestUser = "";
for(Map.Entry<String,User> mapElement : mapOfUsers.entrySet()){
if(mapElement.getValue().getUpdateTime() > latestTime){
latestTime = mapElement.getValue().getUpdateTime();
latestUser = mapElement.getValue().getID();
}
}
Date formattedDate = new Date(latestTime);
String newString = this.formattedTime.format(formattedDate);
JOptionPane.showMessageDialog(null,
"Last Update: " + latestUser + ": " + newString);
}
}
@Override
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)groupTree.getLastSelectedPathComponent();
if(node == null){
return;
}
Object nodeInfo = node.getUserObject();
if (node.isRoot()) {
System.out.println();
}
else {
System.out.println(nodeInfo.toString());
}
}
@Override
public void treeNodesChanged(TreeModelEvent e) {
System.out.println();
}
@Override
public void treeNodesInserted(TreeModelEvent e) {
System.out.println();
}
@Override
public void treeNodesRemoved(TreeModelEvent e) {
System.out.println();
}
@Override
public void treeStructureChanged(TreeModelEvent e) {
System.out.println();
}
}