-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.java
More file actions
1197 lines (1048 loc) · 53 KB
/
Copy pathMain.java
File metadata and controls
1197 lines (1048 loc) · 53 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
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.awt.*;
import javax.swing.*;
import java.net.URI;
import java.net.URLEncoder;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import java.net.InetSocketAddress;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import javax.swing.border.*;
import java.awt.event.*;
import java.util.HashMap;
public class Main extends JFrame {
// Components
// Removed manual phone input; SOS uses saved contacts
private JButton sosButton;
private JLabel statusLabel;
private JPanel contentPanel;
private CardLayout cardLayout;
private HashMap<String, JPanel> panels;
private ContactManager contactManager;
private Database database;
private UserManager userManager;
private UserManager.User currentUser;
private JList<ContactModel> contactsList;
// Navigation items
private final String[] NAV_ITEMS = {"Emergency", "Contacts", "Profile", "Settings", "Help"};
private final String[] NAV_ICONS = {"🆘", "👥", "👤", "⚙️", "❓"};
// Data
private double latitude = 0.0;
private double longitude = 0.0;
// Settings
// Template: include user's full name when available. The app will append the location link after this text.
private String defaultMessage = "Hello, EMERGENCY... I am <name>. I need help... My location:";
private boolean autoLocation = true;
public Main() {
// Load custom font and apply modern style
loadAndApplyCustomFont();
applyModernStyle();
// Initialize components
panels = new HashMap<>();
cardLayout = new CardLayout();
try {
database = new Database("db.properties");
userManager = new UserManager(database);
currentUser = userManager.getCurrentSessionUser().orElse(null);
} catch (RuntimeException ex) {
JOptionPane.showMessageDialog(null,
"Database not configured or unreachable.\n" +
ex.getMessage() + "\n\n" +
"Open db.properties in the project folder and set db.url, db.user, db.password,\n" +
"then run the app again.",
"Database Configuration Required",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
return;
}
// Instantiate contact manager lazily after we know the current user
if (currentUser != null) {
contactManager = new ContactManager(database, currentUser.email);
} else {
contactManager = new ContactManager(database, ""); // temporary, will be replaced after login
}
contactsList = new JList<>(contactManager.getContactsListModel());
// Set up the frame
setTitle("Save Our Ship");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(900, 600));
getContentPane().setBackground(AppStyles.BACKGROUND_COLOR);
// Create main content area
contentPanel = new JPanel(cardLayout);
contentPanel.setBackground(AppStyles.BACKGROUND_COLOR);
// Create sidebar
JPanel sidebarPanel = new JPanel();
sidebarPanel.setPreferredSize(new Dimension(200, 0));
sidebarPanel.setBackground(AppStyles.SIDEBAR_COLOR);
sidebarPanel.setLayout(new BoxLayout(sidebarPanel, BoxLayout.Y_AXIS));
sidebarPanel.setBorder(new EmptyBorder(15, 0, 15, 0));
// Add navigation items to sidebar
for (int i = 0; i < NAV_ITEMS.length; i++) {
JPanel navItem = createNavItem(NAV_ITEMS[i], NAV_ICONS[i]);
sidebarPanel.add(navItem);
sidebarPanel.add(Box.createRigidArea(new Dimension(0, 10)));
}
// Create main layout
setLayout(new BorderLayout(0, 0));
add(sidebarPanel, BorderLayout.WEST);
add(contentPanel, BorderLayout.CENTER);
// Create all content panels
createEmergencyPanel();
createContactsPanel();
createSettingsPanel();
createHelpPanel();
createProfilePanel();
// If no session, show login/register dialog first
if (currentUser == null) {
showAuthDialog();
}
// Show default panel
cardLayout.show(contentPanel, "Emergency");
// Update location
updateLocation();
// Pack and center the frame
pack();
setLocationRelativeTo(null);
}
private void sendSOS() {
// Send SOS to all saved contacts via WhatsApp Web links
try {
// Prepare message by replacing placeholder with user's full name if available
String template = defaultMessage;
if (currentUser != null && currentUser.fullName != null && !currentUser.fullName.isBlank()) {
template = template.replace("<name>", currentUser.fullName);
} else {
template = template.replace("<name>", "");
}
// Ensure we append the location URL after the template text
String message = template + " " + String.format("My location: https://www.google.com/maps?q=%f,%f", latitude, longitude);
DefaultListModel<ContactModel> model = contactManager.getContactsListModel();
if (model.getSize() == 0) {
statusLabel.setText("No saved contacts to send SOS to");
return;
}
final Desktop desktop;
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
desktop = Desktop.getDesktop();
} else {
desktop = null;
}
// Show confirmation dialog
int contactCount = model.getSize();
int confirm = JOptionPane.showConfirmDialog(
this,
"This will open " + contactCount + " WhatsApp tab(s).\nYou will need to manually click 'Send' for each contact.\n\nContinue?",
"Send Emergency SOS",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE
);
if (confirm != JOptionPane.YES_OPTION) {
statusLabel.setText("SOS cancelled");
return;
}
statusLabel.setText("Opening WhatsApp for " + contactCount + " contact(s)...");
// Open WhatsApp tabs with delay for better browser handling
new Thread(() -> {
java.util.List<String> invalidNumbers = new java.util.ArrayList<>();
int opened = 0;
for (int i = 0; i < model.getSize(); i++) {
ContactModel cm = model.getElementAt(i);
String rawPhone = cm.getPhoneNumber().trim();
if (rawPhone.isEmpty()) continue;
String normalized = normalizePhone(rawPhone);
if (normalized == null) {
invalidNumbers.add(cm.getName() + " (" + rawPhone + ")");
continue;
}
try {
String encoded = URLEncoder.encode(message, "UTF-8");
boolean openedThis = false;
// First try whatsapp protocol (WhatsApp Desktop)
try {
String desktopUri = "whatsapp://send?phone=" + normalized + "&text=" + encoded;
if (desktop != null) {
desktop.browse(new URI(desktopUri));
openedThis = true;
}
} catch (Exception ex1) {
// If on Windows, try shell start as another fallback (may open registered app)
try {
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("win")) {
String cmd = "cmd /c start \"\" \"whatsapp://send?phone=" + normalized + "&text=" + encoded + "\"";
Runtime.getRuntime().exec(cmd);
openedThis = true;
}
} catch (Exception ex2) {
// ignore and fall back to web
}
}
// Final fallback to web.whatsapp.com
if (!openedThis) {
try {
String webUri = "https://web.whatsapp.com/send?phone=" + normalized + "&text=" + encoded;
if (desktop != null) {
desktop.browse(new URI(webUri));
openedThis = true;
}
} catch (Exception ex3) {
// failed
openedThis = false;
}
}
if (openedThis) {
opened++;
if (i < model.getSize() - 1) Thread.sleep(900);
} else {
invalidNumbers.add(cm.getName() + " (" + rawPhone + ") - couldn't open");
}
} catch (Exception ex) {
invalidNumbers.add(cm.getName() + " (" + rawPhone + ") - couldn't open");
}
}
final int sentCount = opened;
SwingUtilities.invokeLater(() -> {
String statusMsg = "Opened WhatsApp tabs for " + sentCount + " contact(s).";
if (!invalidNumbers.isEmpty()) {
statusMsg += " Skipped " + invalidNumbers.size() + " invalid/failed contact(s).";
}
statusLabel.setText(statusMsg + " Click Send in each tab!");
if (!invalidNumbers.isEmpty()) {
// Show details and guidance to fix phone numbers
StringBuilder sb = new StringBuilder();
sb.append("The following contacts have invalid or problematic numbers:\n\n");
for (String s : invalidNumbers) sb.append("- ").append(s).append("\n");
sb.append("\nPlease edit these contacts and ensure phone numbers include the country code and only digits (e.g., 919876543210).\n");
JOptionPane.showMessageDialog(Main.this, sb.toString(), "Invalid Contacts", JOptionPane.WARNING_MESSAGE);
}
});
}).start();
} catch (Exception e) {
statusLabel.setText("Error sending SOS: " + e.getMessage());
e.printStackTrace();
}
}
private void updateLocation() {
new Thread(() -> {
try {
// First try browser/device geolocation (asks user permission in the browser)
boolean got = tryBrowserGeolocation();
if (!got) {
// Fallback to IP-based geolocation
URL url = URI.create("http://ip-api.com/json/").toURL();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
String jsonResponse = response.toString();
String[] parts = jsonResponse.split(",");
for (String part : parts) {
if (part.contains("\"lat\":")) {
latitude = Double.parseDouble(part.split(":")[1].trim());
} else if (part.contains("\"lon\":")) {
longitude = Double.parseDouble(part.split(":")[1].trim());
}
}
}
if (autoLocation && currentUser != null) {
database.saveLocation(currentUser.email, latitude, longitude);
}
} catch (Exception e) {
SwingUtilities.invokeLater(() -> statusLabel.setText("Could not get location"));
}
}).start();
}
/**
* Launches a tiny local HTTP server and serves a page that requests navigator.geolocation
* If the user allows, the page POSTs coordinates back to /coords which this method waits for.
* Returns true if coordinates were obtained and set into latitude/longitude.
*/
private boolean tryBrowserGeolocation() {
if (!Desktop.isDesktopSupported()) return false;
try {
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
final double[] coords = new double[2];
final CountDownLatch latch = new CountDownLatch(1);
// Handler for root: serves HTML + JS that requests geolocation and posts to /coords
server.createContext("/", exchange -> {
String page = "<!doctype html><html><head><meta charset=\"utf-8\"><title>Share location</title></head><body>"
+ "<h3>Please allow location access in the browser to share your location with the app.</h3>"
+ "<script>function postCoords(lat,lon){fetch('/coords',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({lat:lat,lon:lon})}).then(()=>{document.body.innerHTML='<p>Location sent. You can close this tab.</p>';}).catch(()=>{document.body.innerHTML='<p>Failed to send.</p>';});}"
+ "if(navigator.geolocation){navigator.geolocation.getCurrentPosition(function(p){postCoords(p.coords.latitude,p.coords.longitude);},function(e){document.body.innerHTML='<p>Permission denied or unavailable.</p>';});}else{document.body.innerHTML='<p>Geolocation not supported.</p>';}</script></body></html>";
byte[] bytes = page.getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().set("Content-Type", "text/html; charset=utf-8");
exchange.sendResponseHeaders(200, bytes.length);
try (OutputStream os = exchange.getResponseBody()) { os.write(bytes); }
});
// Handler to receive coords
server.createContext("/coords", exchange -> {
if (!"POST".equalsIgnoreCase(exchange.getRequestMethod())) {
exchange.sendResponseHeaders(405, -1);
return;
}
String body;
try (BufferedReader br = new BufferedReader(new InputStreamReader(exchange.getRequestBody(), StandardCharsets.UTF_8))) {
body = br.lines().collect(Collectors.joining("\n"));
}
// simple parse
try {
String s = body.replaceAll("[{}\" ]", "");
String[] parts = s.split(",");
double lat = Double.NaN, lon = Double.NaN;
for (String p : parts) {
if (p.startsWith("lat:")) lat = Double.parseDouble(p.substring(4));
if (p.startsWith("lon:")) lon = Double.parseDouble(p.substring(4));
}
if (!Double.isNaN(lat) && !Double.isNaN(lon)) {
coords[0] = lat; coords[1] = lon;
latch.countDown();
}
} catch (Exception ex) {
// ignore
}
String resp = "OK";
byte[] rb = resp.getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(200, rb.length);
try (OutputStream os = exchange.getResponseBody()) { os.write(rb); }
});
server.setExecutor(Executors.newCachedThreadPool());
server.start();
int port = server.getAddress().getPort();
String url = "http://localhost:" + port + "/";
// Open browser to request permission
Desktop.getDesktop().browse(new URI(url));
// Wait up to 20 seconds for user to allow and for page to POST coords
boolean ok = latch.await(20, TimeUnit.SECONDS);
if (ok) {
latitude = coords[0];
longitude = coords[1];
}
server.stop(0);
return ok;
} catch (Exception e) {
return false;
}
}
// Circular button implementation
private static class CircularButton extends JButton {
public CircularButton(String text) {
super(text);
setContentAreaFilled(false);
setFocusPainted(false);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int size = Math.min(getWidth(), getHeight());
int x = (getWidth() - size) / 2;
int y = (getHeight() - size) / 2;
// Drop shadow
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.25f));
g2.setColor(Color.BLACK);
g2.fillOval(x + 4, y + 6, size - 2, size - 2);
// Button body with gradient for 3D effect
g2.setComposite(AlphaComposite.SrcOver);
Color base = getModel().isArmed() ? getBackground().darker() : getBackground();
GradientPaint gp = new GradientPaint(x, y, base.brighter(), x, y + size, base.darker());
g2.setPaint(gp);
g2.fillOval(x, y, size, size);
// Inner highlight ring
g2.setPaint(new GradientPaint(x, y, new Color(255, 255, 255, 70), x, y + size, new Color(255, 255, 255, 0)));
g2.fillOval(x + 4, y + 4, size - 8, size - 8);
// Rim/border
g2.setColor(new Color(0, 0, 0, 60));
g2.setStroke(new BasicStroke(2f));
g2.drawOval(x, y, size, size);
// Text
g2.setColor(getForeground());
g2.setFont(getFont());
FontMetrics fm = g2.getFontMetrics();
String text = getText();
int tx = (getWidth() - fm.stringWidth(text)) / 2;
int ty = (getHeight() + fm.getAscent()) / 2 - 4;
g2.drawString(text, tx, ty);
g2.dispose();
}
@Override
public boolean contains(int x, int y) {
int size = Math.min(getWidth(), getHeight());
int cx = getWidth()/2;
int cy = getHeight()/2;
double dx = x - cx;
double dy = y - cy;
return dx*dx + dy*dy <= (size/2.0)*(size/2.0);
}
}
private JPanel createNavItem(String text, String icon) {
JPanel item = new JPanel();
item.setLayout(new BoxLayout(item, BoxLayout.X_AXIS));
item.setBackground(AppStyles.SIDEBAR_COLOR);
item.setBorder(new EmptyBorder(10, 15, 10, 15));
item.setMaximumSize(new Dimension(200, 40));
JLabel textLabel = new JLabel(text);
textLabel.setFont(AppStyles.getAppFont(Font.PLAIN, 14f));
textLabel.setForeground(AppStyles.TEXT_COLOR);
// item.add(iconLabel);
item.add(Box.createRigidArea(new Dimension(10, 0)));
item.add(textLabel);
item.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
item.setBackground(AppStyles.SIDEBAR_HOVER);
}
@Override
public void mouseExited(MouseEvent e) {
item.setBackground(AppStyles.SIDEBAR_COLOR);
}
@Override
public void mouseClicked(MouseEvent e) {
cardLayout.show(contentPanel, text);
}
});
return item;
}
private void createEmergencyPanel() {
JPanel panel = new JPanel(new BorderLayout(15, 15));
// Emergency gradient background
panel.setBackground(new Color(245, 245, 250));
panel.setBorder(new EmptyBorder(20, 20, 20, 20));
// Create SOS button
sosButton = new CircularButton("SOS");
sosButton.setFont(AppStyles.getAppFont(Font.BOLD, 28f));
sosButton.setBackground(new Color(220, 53, 69)); // Emergency red
sosButton.setForeground(Color.WHITE);
sosButton.setFocusPainted(false);
sosButton.setBorder(new EmptyBorder(20, 20, 20, 20));
sosButton.setPreferredSize(new Dimension(180, 180));
sosButton.setMaximumSize(new Dimension(180, 180));
sosButton.setMinimumSize(new Dimension(180, 180));
sosButton.setContentAreaFilled(false);
sosButton.setOpaque(false);
// Add hover effect
sosButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
sosButton.setBackground(new Color(200, 35, 51));
}
@Override
public void mouseExited(MouseEvent e) {
sosButton.setBackground(new Color(220, 53, 69));
}
});
sosButton.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) { sendSOS(); }
});
// Center panel with GridBagLayout for perfect centering
JPanel centerPanel = new JPanel(new GridBagLayout());
centerPanel.setBackground(new Color(245, 245, 250));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
centerPanel.add(sosButton, gbc);
// Create status section
statusLabel = new JLabel("Ready to send emergency message", SwingConstants.CENTER);
statusLabel.setFont(getAppFont(Font.PLAIN, 14f));
statusLabel.setForeground(new Color(100, 100, 100));
panel.add(centerPanel, BorderLayout.CENTER);
panel.add(statusLabel, BorderLayout.SOUTH);
panels.put("Emergency", panel);
contentPanel.add(panel, "Emergency");
}
private void createContactsPanel() {
JPanel panel = new JPanel(new BorderLayout(15, 15));
panel.setBackground(new Color(245, 245, 250));
panel.setBorder(new EmptyBorder(20, 20, 20, 20));
// Title
JLabel titleLabel = new JLabel("Emergency Contacts", SwingConstants.CENTER);
titleLabel.setFont(AppStyles.getAppFont(Font.BOLD, 26f));
titleLabel.setForeground(new Color(220, 53, 69));
// Create buttons panel
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonsPanel.setBackground(AppStyles.BACKGROUND_COLOR);
// Contacts List
// Use contactManager's model
contactsList = new JList<>(contactManager.getContactsListModel());
contactsList.setFont(getAppFont(Font.PLAIN, 14f));
contactsList.setCellRenderer(new ListCellRenderer<ContactModel>() {
@Override
public Component getListCellRendererComponent(JList<? extends ContactModel> list, ContactModel contact, int index, boolean isSelected, boolean cellHasFocus) {
JPanel p = new JPanel(new BorderLayout(10, 0));
p.setBorder(new EmptyBorder(8, 8, 8, 8));
p.setBackground(isSelected ? AppStyles.SELECTED_BACKGROUND_COLOR : AppStyles.BACKGROUND_COLOR);
// Contact info panel (left side)
JPanel infoPanel = new JPanel(new BorderLayout());
infoPanel.setOpaque(false);
JLabel nameLabel = new JLabel(contact.getName());
nameLabel.setFont(getAppFont(Font.BOLD, 14f));
JLabel phoneLabel = new JLabel(contact.getPhoneNumber());
phoneLabel.setFont(getAppFont(Font.PLAIN, 12f));
phoneLabel.setForeground(AppStyles.SECONDARY_TEXT_COLOR);
infoPanel.add(nameLabel, BorderLayout.NORTH);
infoPanel.add(phoneLabel, BorderLayout.SOUTH);
p.add(infoPanel, BorderLayout.CENTER);
// Edit and Delete buttons (right side)
JPanel btnPanel = new JPanel();
btnPanel.setOpaque(false);
btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.X_AXIS));
JLabel editLabel = new JLabel("Edit");
editLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
editLabel.setForeground(new Color(29, 155, 240));
editLabel.setBorder(new EmptyBorder(0, 0, 0, 10));
JLabel deleteLabel = new JLabel("Delete");
deleteLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
deleteLabel.setForeground(AppStyles.DARK_RED);
btnPanel.add(editLabel);
btnPanel.add(deleteLabel);
p.add(btnPanel, BorderLayout.EAST);
return p;
}
});
contactsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Mouse listener for Edit/Delete
contactsList.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int index = contactsList.locationToIndex(e.getPoint());
if (index != -1) {
ContactModel cm = contactManager.getContactsListModel().getElementAt(index);
Rectangle bounds = contactsList.getCellBounds(index, index);
int btnWidth = 90; // ~width for "EditDelete"
int editWidth = 40; // ~width for "Edit"
int xInCell = e.getX() - (int)bounds.getX();
if (xInCell > bounds.getWidth() - btnWidth && xInCell < bounds.getWidth() - (btnWidth - editWidth)) {
// Edit button clicked
showEditContactDialog(cm);
} else if (xInCell > bounds.getWidth() - (btnWidth - editWidth)) {
// Delete button clicked
int confirm = JOptionPane.showConfirmDialog(Main.this, "Delete this contact?", "Confirm Delete", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
contactManager.deleteContact(cm);
}
} else {
// Non-button click: navigate to Emergency panel
cardLayout.show(contentPanel, "Emergency");
statusLabel.setText("Ready to send SOS to all saved contacts");
}
}
}
});
JScrollPane scrollPane = new JScrollPane(contactsList);
scrollPane.setBorder(BorderFactory.createLineBorder(AppStyles.ACCENT_COLOR));
// Add Contact Panel
JPanel addContactPanel = new JPanel(new GridBagLayout());
addContactPanel.setBackground(Color.WHITE);
addContactPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(220, 220, 220), 1),
new EmptyBorder(15, 15, 15, 15)
));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
JTextField nameField = new JTextField(15);
nameField.setFont(new Font("Segoe UI", Font.PLAIN, 13));
JTextField phoneField = new JTextField(15);
phoneField.setFont(new Font("Segoe UI", Font.PLAIN, 13));
JLabel phoneHint = new JLabel("(Include country code, e.g., +919876543210)");
phoneHint.setFont(new Font("Segoe UI", Font.ITALIC, 11));
phoneHint.setForeground(new Color(120, 120, 120));
JButton addButton = new JButton("Add Contact");
addButton.setBackground(new Color(40, 167, 69));
addButton.setForeground(Color.WHITE);
addButton.setFont(new Font("Segoe UI", Font.BOLD, 13));
addButton.setFocusPainted(false);
addButton.setBorder(new EmptyBorder(10, 16, 10, 16));
gbc.gridx = 0; gbc.gridy = 0;
addContactPanel.add(new JLabel("Name:"), gbc);
gbc.gridx = 1;
addContactPanel.add(nameField, gbc);
gbc.gridx = 0; gbc.gridy = 1;
addContactPanel.add(new JLabel("Phone:"), gbc);
gbc.gridx = 1;
addContactPanel.add(phoneField, gbc);
gbc.gridx = 1; gbc.gridy = 2;
gbc.gridwidth = 1;
addContactPanel.add(phoneHint, gbc);
gbc.gridx = 0; gbc.gridy = 3;
gbc.gridwidth = 2;
addContactPanel.add(addButton, gbc);
addButton.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
String name = nameField.getText().trim();
String phone = phoneField.getText().trim();
if (!name.isEmpty() && !phone.isEmpty()) {
contactManager.addContact(name, phone);
nameField.setText("");
phoneField.setText("");
}
}
});
// Layout
JPanel northPanel = new JPanel(new BorderLayout(0, 15));
northPanel.setBackground(new Color(245, 245, 250));
northPanel.add(titleLabel, BorderLayout.NORTH);
northPanel.add(addContactPanel, BorderLayout.CENTER);
panel.add(northPanel, BorderLayout.NORTH);
// Create split panel for list and buttons
JPanel listPanel = new JPanel(new BorderLayout());
listPanel.setBackground(new Color(245, 245, 250));
listPanel.add(buttonsPanel, BorderLayout.NORTH);
listPanel.add(scrollPane, BorderLayout.CENTER);
panel.add(listPanel, BorderLayout.CENTER);
panels.put("Contacts", panel);
contentPanel.add(panel, "Contacts");
}
private void showEditContactDialog(ContactModel contact) {
JTextField nameField = new JTextField(contact.getName(), 15);
JTextField phoneField = new JTextField(contact.getPhoneNumber(), 15);
JPanel panel = new JPanel(new GridBagLayout());
panel.setBackground(Color.WHITE);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0; gbc.gridy = 0; panel.add(new JLabel("Name:"), gbc);
gbc.gridx = 1; panel.add(nameField, gbc);
gbc.gridx = 0; gbc.gridy = 1; panel.add(new JLabel("Phone:"), gbc);
gbc.gridx = 1; panel.add(phoneField, gbc);
int result = JOptionPane.showConfirmDialog(this, panel, "Edit Contact", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
String newName = nameField.getText().trim();
String newPhone = phoneField.getText().trim();
if (!newName.isEmpty() && !newPhone.isEmpty()) {
contactManager.editContact(contact, newName, newPhone);
}
}
}
// --- Utility and UI Setup Methods ---
private void loadAndApplyCustomFont() {
try {
// Set modern fonts for the entire application
UIManager.put("Label.font", new Font("Segoe UI", Font.PLAIN, 13));
UIManager.put("Button.font", new Font("Segoe UI", Font.PLAIN, 13));
UIManager.put("TextField.font", new Font("Segoe UI", Font.PLAIN, 13));
UIManager.put("TextArea.font", new Font("Segoe UI", Font.PLAIN, 13));
UIManager.put("CheckBox.font", new Font("Segoe UI", Font.PLAIN, 13));
UIManager.put("ComboBox.font", new Font("Segoe UI", Font.PLAIN, 13));
UIManager.put("List.font", new Font("Segoe UI", Font.PLAIN, 13));
} catch (Exception e) {
// Fallback to default if Segoe UI is not available
}
}
private void applyModernStyle() {
// Set modern look and feel
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
// Use default if system look and feel fails
}
}
private void createSettingsPanel() {
JPanel panel = new JPanel(new BorderLayout(15, 15));
panel.setBackground(new Color(245, 245, 250));
panel.setBorder(new EmptyBorder(20, 20, 20, 20));
// Title
JLabel titleLabel = new JLabel("Settings", SwingConstants.CENTER);
titleLabel.setFont(AppStyles.getAppFont(Font.BOLD, 26f));
titleLabel.setForeground(new Color(220, 53, 69));
// Settings Panel
JPanel settingsPanel = new JPanel(new GridBagLayout());
settingsPanel.setBackground(Color.WHITE);
settingsPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(220, 220, 220), 1),
new EmptyBorder(20, 20, 20, 20)
));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
gbc.fill = GridBagConstraints.HORIZONTAL;
// Default Message Setting
JLabel messageLabel = new JLabel("Default Emergency Message:");
messageLabel.setFont(AppStyles.getAppFont(Font.BOLD, 14f));
messageLabel.setForeground(new Color(60, 60, 60));
JTextField messageField = new JTextField(defaultMessage);
messageField.setFont(AppStyles.getAppFont(Font.PLAIN, 14f));
messageField.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(200, 200, 200), 1),
new EmptyBorder(8, 10, 8, 10)
));
// Auto Location Setting
JLabel locationLabel = new JLabel("Auto-detect Location:");
locationLabel.setFont(AppStyles.getAppFont(Font.BOLD, 14f));
locationLabel.setForeground(new Color(60, 60, 60));
JCheckBox locationCheck = new JCheckBox("", autoLocation);
locationCheck.setBackground(Color.WHITE);
// Save Button
JButton saveButton = new JButton("Save Settings");
saveButton.setBackground(Color.WHITE);
saveButton.setForeground(Color.BLACK);
saveButton.setFont(AppStyles.getAppFont(Font.BOLD, 14f));
saveButton.setFocusPainted(false);
saveButton.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(220, 220, 220), 1),
new EmptyBorder(10, 20, 10, 20)
));
// Add components
gbc.gridx = 0; gbc.gridy = 0;
settingsPanel.add(messageLabel, gbc);
gbc.gridy = 1;
settingsPanel.add(messageField, gbc);
gbc.gridy = 2;
settingsPanel.add(locationLabel, gbc);
gbc.gridy = 3;
settingsPanel.add(locationCheck, gbc);
gbc.gridy = 4;
gbc.insets = new Insets(20, 10, 10, 10);
settingsPanel.add(saveButton, gbc);
saveButton.addActionListener(e -> {
defaultMessage = messageField.getText();
autoLocation = locationCheck.isSelected();
JOptionPane.showMessageDialog(Main.this, "Settings saved successfully!", "Settings", JOptionPane.INFORMATION_MESSAGE);
});
panel.add(titleLabel, BorderLayout.NORTH);
panel.add(settingsPanel, BorderLayout.CENTER);
panels.put("Settings", panel);
contentPanel.add(panel, "Settings");
}
private void createHelpPanel() {
JPanel panel = new JPanel(new BorderLayout(15, 15));
panel.setBackground(new Color(245, 245, 250));
panel.setBorder(new EmptyBorder(20, 20, 20, 20));
// Title
JLabel titleLabel = new JLabel("Help & Information", SwingConstants.CENTER);
titleLabel.setFont(AppStyles.getAppFont(Font.BOLD, 26f));
titleLabel.setForeground(new Color(220, 53, 69));
// Help Content
JTextPane helpText = new JTextPane();
helpText.setEditable(false);
helpText.setContentType("text/html");
helpText.setText(
"<html><body style='font-family: Segoe UI; font-size: 14px; padding: 10px;'>" +
"<h2>How to Use the Emergency Assistant</h2>" +
"<p><b>Sending an SOS:</b><br>" +
"1. Save your emergency contacts in the Contacts tab (with country code, e.g., +919876543210)<br>" +
"2. Click the SOS button on the Emergency tab<br>" +
"3. WhatsApp Web will open separate tabs for each contact with pre-filled message<br>" +
"4. <b style='color: #dc3545;'>IMPORTANT: You must manually click 'Send' in each WhatsApp tab</b></p>" +
"<p><b>Managing Contacts:</b><br>" +
"1. Go to the Contacts tab<br>" +
"2. Add emergency contacts with their names and phone numbers<br>" +
"3. Click Edit to modify a contact's details<br>" +
"4. Click Delete to remove a contact</p>" +
"<p><b>Settings:</b><br>" +
"• Customize your default emergency message<br>" +
"• Toggle automatic location detection (IP-based, may not be accurate)</p>" +
"<p><b style='color: #dc3545;'>⚠️ Important Limitations:</b><br>" +
"• <b>Manual Send Required:</b> WhatsApp blocks automatic message sending for security. You must click Send for each contact.<br>" +
"• <b>Location Accuracy:</b> Location is detected via IP address and may be off by several kilometers. Not accurate enough for precise emergencies.<br>" +
"• <b>Multiple Tabs:</b> One browser tab opens per contact, which can be overwhelming with many contacts.<br>" +
"• <b>Desktop Only:</b> This is a desktop app. For real emergency use with automatic SMS and GPS, consider a mobile app.</p>" +
"<p><b>Recommendations:</b><br>" +
"• Always test the system with family members first<br>" +
"• Keep 3-5 most important emergency contacts (to reduce manual effort)<br>" +
"• Verify your location before an emergency occurs<br>" +
"• For real emergencies, always call official emergency services (911, 112, etc.)</p>" +
"</body></html>"
);
JScrollPane scrollPane = new JScrollPane(helpText);
scrollPane.setBorder(BorderFactory.createLineBorder(AppStyles.ACCENT_COLOR));
panel.add(titleLabel, BorderLayout.NORTH);
panel.add(scrollPane, BorderLayout.CENTER);
panels.put("Help", panel);
contentPanel.add(panel, "Help");
}
private void createProfilePanel() {
JPanel panel = new JPanel(new BorderLayout(15, 15));
panel.setBackground(new Color(245, 245, 250));
panel.setBorder(new EmptyBorder(20, 20, 20, 20));
JLabel titleLabel = new JLabel("Profile", SwingConstants.CENTER);
titleLabel.setFont(AppStyles.getAppFont(Font.BOLD, 26f));
titleLabel.setForeground(new Color(220, 53, 69));
JPanel info = new JPanel(new GridLayout(0, 1, 10, 10));
info.setBackground(Color.WHITE);
info.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(220, 220, 220), 1),
new EmptyBorder(20, 20, 20, 20)
));
JLabel fullName = new JLabel("Full Name: ");
JLabel idType = new JLabel("ID Type: ");
JLabel idNumber = new JLabel("ID Number: ");
JLabel email = new JLabel("Email: ");
JLabel phone = new JLabel("Phone: ");
fullName.setFont(AppStyles.getAppFont(Font.BOLD, 16f));
fullName.setForeground(new Color(50, 50, 50));
idType.setFont(AppStyles.getAppFont(Font.PLAIN, 14f));
idType.setForeground(new Color(80, 80, 80));
idNumber.setFont(AppStyles.getAppFont(Font.PLAIN, 14f));
idNumber.setForeground(new Color(80, 80, 80));
email.setFont(AppStyles.getAppFont(Font.PLAIN, 14f));
email.setForeground(new Color(80, 80, 80));
phone.setFont(AppStyles.getAppFont(Font.PLAIN, 14f));
phone.setForeground(new Color(80, 80, 80));
info.add(fullName);
info.add(idType);
info.add(idNumber);
info.add(email);
info.add(phone);
// Logout button
JButton logoutBtn = new JButton("Logout");
logoutBtn.setBackground(Color.WHITE);
logoutBtn.setForeground(Color.BLACK);
logoutBtn.setFont(AppStyles.getAppFont(Font.BOLD, 14f));
logoutBtn.setFocusPainted(false);
logoutBtn.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(220, 220, 220), 1),
new EmptyBorder(10, 20, 10, 20)
));
logoutBtn.addActionListener(e -> {
// Clear session and return to auth dialog
userManager.clearSession();
currentUser = null;
// Reset contact manager to an empty model until login
contactManager = new ContactManager(database, "");
contactsList.setModel(contactManager.getContactsListModel());
showAuthDialog();
});
JPanel south = new JPanel(new FlowLayout(FlowLayout.CENTER));
south.setBackground(new Color(245, 245, 250));
south.setBorder(new EmptyBorder(15, 0, 0, 0));
south.add(logoutBtn);
panel.add(titleLabel, BorderLayout.NORTH);
panel.add(info, BorderLayout.CENTER);
panel.add(south, BorderLayout.SOUTH);
// Update with current user data if available
if (currentUser != null) {
fullName.setText("Full Name: " + currentUser.fullName);
idType.setText("ID Type: " + currentUser.idType);
idNumber.setText("ID Number: " + currentUser.idNumber);
email.setText("Email: " + currentUser.email);
phone.setText("Phone: " + currentUser.phone);
}
panels.put("Profile", panel);
contentPanel.add(panel, "Profile");
}
private void showAuthDialog() {
JDialog dlg = new JDialog(this, "Welcome", true);
dlg.setSize(400, 300);
dlg.setLocationRelativeTo(this);
JPanel p = new JPanel(new BorderLayout(10, 10));
p.setBorder(new EmptyBorder(10,10,10,10));
dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dlg.addWindowListener(new WindowAdapter() {
@Override public void windowClosing(WindowEvent e) { System.exit(0); }
});
JLabel welcome = new JLabel("Please login or register", SwingConstants.CENTER);
welcome.setFont(AppStyles.getAppFont(Font.BOLD, 18f));
p.add(welcome, BorderLayout.NORTH);
JPanel buttons = new JPanel(new FlowLayout());
JButton loginBtn = new JButton("Login");
loginBtn.setFont(new Font("Segoe UI", Font.BOLD, 14));
JButton regBtn = new JButton("Register");
regBtn.setFont(new Font("Segoe UI", Font.BOLD, 14));
buttons.add(loginBtn);
buttons.add(regBtn);
p.add(buttons, BorderLayout.CENTER);
loginBtn.addActionListener(e -> {
dlg.dispose();
showLoginDialog();
});
regBtn.addActionListener(e -> {
dlg.dispose();
showRegisterDialog();
});
dlg.setContentPane(p);
dlg.setVisible(true);
}
private void showLoginDialog() {
JDialog dlg = new JDialog(this, "Login", true);
dlg.setSize(480, 300);
dlg.setLocationRelativeTo(this);
JPanel p = new JPanel(new GridBagLayout());
p.setBorder(new EmptyBorder(10,10,10,10));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(8,8,8,8);
gbc.fill = GridBagConstraints.HORIZONTAL;
dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dlg.addWindowListener(new WindowAdapter() {