implements Search Email logic

eature/gui
Obai Albek 2025-06-04 00:57:42 +02:00
parent eea5ccba6b
commit c71bece39b
9 changed files with 240 additions and 152 deletions

View File

@ -5,6 +5,7 @@ import java.util.ArrayList;
import domain.email.Email; import domain.email.Email;
import domain.email.EmailFolder; import domain.email.EmailFolder;
import domain.email.EmailNotFoundException;
import domain.user.*; import domain.user.*;
public class EasyMail { public class EasyMail {
@ -14,15 +15,18 @@ public class EasyMail {
public EasyMail() { public EasyMail() {
this.userManager = new UserManager(); this.userManager = new UserManager();
try { try {
this.currentUser = userManager.addUser ("obai","albek","obai.albek",1,1,"Januar",new char[] {'1','2','3','4','5','6'} , new char[]{'1','2','3','4','5','6'}); this.currentUser = userManager.addUser("obai", "albek", "obai.albek", 1, 1, "Januar",
new char[] { '1', '2', '3', '4', '5', '6' }, new char[] { '1', '2', '3', '4', '5', '6' });
// obai.albek@easymail.de // obai.albek@easymail.de
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public void userRegister(String firstname, String lastName, String email, int year, int day, String monthName,char[] password, char[] passwordConfirmation) throws Exception { public void userRegister(String firstname, String lastName, String email, int year, int day, String monthName,
this.currentUser = userManager.addUser(firstname, lastName, email, year, day, monthName, password,passwordConfirmation); char[] password, char[] passwordConfirmation) throws Exception {
this.currentUser = userManager.addUser(firstname, lastName, email, year, day, monthName, password,
passwordConfirmation);
} }
public boolean userSignIn(String username, char[] password) throws Exception { public boolean userSignIn(String username, char[] password) throws Exception {
@ -39,10 +43,10 @@ public class EasyMail {
} }
public void updateUser(String firstName, String lastName, String username, int year, int day, String monthName, public void updateUser(String firstName, String lastName, String username, int year, int day, String monthName,
char[] password, char[] passwordConfirmation) char[] password, char[] passwordConfirmation) throws Exception {
throws Exception {
this.currentUser = userManager.updateUser(firstName, lastName, username, year, day, monthName, password, passwordConfirmation); this.currentUser = userManager.updateUser(firstName, lastName, username, year, day, monthName, password,
passwordConfirmation);
} }
@ -70,6 +74,14 @@ public class EasyMail {
return sent; return sent;
} }
public String searchEmailInInboxFolder(String subject) throws EmailNotFoundException {
if (subject.trim().isEmpty())
throw new IllegalArgumentException("subject field is required!");
Email email = this.currentUser.getUsermail().getInbox().getEmailBySubject(subject);
return email.showEmails();
}
public String[] sendUserDetails() { public String[] sendUserDetails() {
String[] details = new String[2]; String[] details = new String[2];
String name = this.currentUser.getFirstname() + " " + this.currentUser.getLastname(); String name = this.currentUser.getFirstname() + " " + this.currentUser.getLastname();
@ -84,7 +96,6 @@ public class EasyMail {
return this.currentUser.getUsermail().getUserEmail(); return this.currentUser.getUsermail().getUserEmail();
} }
public ArrayList<String> sendAllEmailsToSentWindow() { public ArrayList<String> sendAllEmailsToSentWindow() {
ArrayList<Email> allEmails = currentUser.getUsermail().getSentFolder().listAllEmails(); ArrayList<Email> allEmails = currentUser.getUsermail().getSentFolder().listAllEmails();
return extractEmails(allEmails, true); // true = showEmailsInSent return extractEmails(allEmails, true); // true = showEmailsInSent
@ -100,7 +111,6 @@ public class EasyMail {
return extractEmails(allEmails, false); return extractEmails(allEmails, false);
} }
private ArrayList<String> extractEmails(ArrayList<Email> emails, boolean isSent) { private ArrayList<String> extractEmails(ArrayList<Email> emails, boolean isSent) {
ArrayList<String> result = new ArrayList<>(); ArrayList<String> result = new ArrayList<>();
for (Email email : emails) { for (Email email : emails) {
@ -113,9 +123,6 @@ public class EasyMail {
return result; return result;
} }
private void validateEmailOperation(String subject) { private void validateEmailOperation(String subject) {
if (subject == null || subject.trim().isEmpty()) { if (subject == null || subject.trim().isEmpty()) {
throw new IllegalArgumentException("Subject field is required!"); throw new IllegalArgumentException("Subject field is required!");
@ -131,8 +138,6 @@ public class EasyMail {
return this.currentUser.getUsermail().getTrashFolder().addEmail(removedEmail); return this.currentUser.getUsermail().getTrashFolder().addEmail(removedEmail);
} }
public boolean removeEmailFromInbox(String subject) throws Exception { public boolean removeEmailFromInbox(String subject) throws Exception {
return moveEmailToTrash(subject, this.currentUser.getUsermail().getInbox()); return moveEmailToTrash(subject, this.currentUser.getUsermail().getInbox());
} }
@ -146,8 +151,6 @@ public class EasyMail {
this.currentUser.getUsermail().getTrashFolder().removeEmail(subject); this.currentUser.getUsermail().getTrashFolder().removeEmail(subject);
} }
} }

View File

@ -61,5 +61,6 @@ public class Inbox implements EmailFolder {
return new ArrayList<>(receivedEmails); return new ArrayList<>(receivedEmails);
} }
} }

View File

@ -2,6 +2,9 @@ package gui;
import javax.swing.*; import javax.swing.*;
import javax.swing.table.DefaultTableModel; import javax.swing.table.DefaultTableModel;
import domain.email.EmailNotFoundException;
import java.awt.*; import java.awt.*;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
@ -10,17 +13,19 @@ import java.util.ArrayList;
public class EasyMailWindow extends TemplateWindow { public class EasyMailWindow extends TemplateWindow {
private DefaultTableModel inboxTableModel; private DefaultTableModel inboxTableModel;
private JTextField searchField;
public EasyMailWindow() { public EasyMailWindow() {
super("EasyMail"); super("EasyMail");
initUI(); initUI();
showWindow();
} }
private void initUI() { private void initUI() {
initNavigationPanel(); initNavigationPanel();
initComposePanel(); initComposePanel();
initTablePanel(); initTablePanel();
getAllInboxEmails(); getAllInboxEmails("");
showUserDetails(); showUserDetails();
} }
@ -52,10 +57,10 @@ public class EasyMailWindow extends TemplateWindow {
} }
}); });
navigationPanel.add(trash); navigationPanel.add(trash);
} }
private void initComposePanel() { private void initComposePanel() {
JPanel composePanel = createPanel(367, 11, 750, 86, new Color(230, 230, 230), true); JPanel composePanel = createPanel(367, 11, 750, 86, new Color(230, 230, 230), true);
contentPane.add(composePanel); contentPane.add(composePanel);
@ -71,6 +76,28 @@ public class EasyMailWindow extends TemplateWindow {
} }
}); });
composePanel.add(writeEmail); composePanel.add(writeEmail);
searchField = new PlaceholderTextField("Search By subject");
searchField.setBounds(500, 30, 150, 40);
composePanel.add(searchField);
JButton searchButton = new JButton("Search");
searchButton.setBounds(660, 30, 80, 40);
composePanel.add(searchButton);
searchButton.addActionListener(e -> handleSearching());
}
public void handleSearching() {
try {
String getSubjct = searchField.getText();
String email = fassade.searchEmailInInboxFolder(getSubjct);
inboxTableModel.setRowCount(0);
getAllInboxEmails(email);
} catch (EmailNotFoundException e) {
this.showError(e.getMessage());
}
} }
private void initTablePanel() { private void initTablePanel() {
@ -109,15 +136,12 @@ public class EasyMailWindow extends TemplateWindow {
emailWindow.setEmailSentListener(() -> { emailWindow.setEmailSentListener(() -> {
inboxTableModel.setRowCount(0); inboxTableModel.setRowCount(0);
getAllInboxEmails(); getAllInboxEmails("");
}); });
} }
public void getAllInboxEmails() { public void getAllInboxEmails(String foundedEmail) {
if (fassade.getUsernameFromCurrentUser() == null) { if (foundedEmail.trim().isEmpty()) {
showError("No user is currently logged in!");
return;
}
inboxTableModel.setRowCount(0); inboxTableModel.setRowCount(0);
ArrayList<String> getEmails = fassade.sendAllEmailsToInboxWindow(); ArrayList<String> getEmails = fassade.sendAllEmailsToInboxWindow();
if (getEmails != null && !getEmails.isEmpty()) if (getEmails != null && !getEmails.isEmpty())
@ -126,6 +150,13 @@ public class EasyMailWindow extends TemplateWindow {
Object[] newEmail = { splitEmail[0], splitEmail[1], splitEmail[2] }; Object[] newEmail = { splitEmail[0], splitEmail[1], splitEmail[2] };
inboxTableModel.addRow(newEmail); inboxTableModel.addRow(newEmail);
} }
}else {
String[] splitEmail = foundedEmail.split(",");
Object[] newEmail = { splitEmail[0], splitEmail[1], splitEmail[2] };
inboxTableModel.addRow(newEmail);
}
} }
} }

View File

@ -117,6 +117,7 @@ public class EditProfileWindow extends TemplateWindow {
if (updateListener != null) { if (updateListener != null) {
updateListener.onUpdateSuccess(); updateListener.onUpdateSuccess();
restInputs();
showInfo("Profile updated successfully!"); showInfo("Profile updated successfully!");
} }

View File

@ -0,0 +1,23 @@
package gui;
import javax.swing.*;
import java.awt.*;
public class PlaceholderTextField extends JTextField {
private String placeholder;
public PlaceholderTextField(String placeholder) {
this.placeholder = placeholder;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (getText().isEmpty() && !(FocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == this)) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(Color.GRAY);
g2.setFont(getFont().deriveFont(Font.ITALIC));
g2.drawString(placeholder, 5, getHeight() / 2 + getFont().getSize() / 2 - 2);
g2.dispose();
}
}
}

View File

@ -10,6 +10,7 @@ import java.util.ArrayList;
public class SentWindow extends TemplateWindow { public class SentWindow extends TemplateWindow {
private DefaultTableModel inboxTableModel; private DefaultTableModel inboxTableModel;
private JTextField searchField;
public SentWindow() { public SentWindow() {
super("Sent - EasyMail"); super("Sent - EasyMail");
@ -28,6 +29,20 @@ public class SentWindow extends TemplateWindow {
contentPane.add(composePanel); contentPane.add(composePanel);
composePanel.setLayout(null); composePanel.setLayout(null);
searchField = new PlaceholderTextField("Search By subject");
searchField.setBounds(500, 30, 150, 40);
composePanel.add(searchField);
JButton searchButton = new JButton("Search");
searchButton.setBounds(660, 30, 80, 40);
composePanel.add(searchButton);
searchButton.addActionListener(e -> handleSearching());
}
public void handleSearching() {
String getSubjct = searchField.getText();
} }
private void initNavigationPanel() { private void initNavigationPanel() {
@ -44,7 +59,7 @@ public class SentWindow extends TemplateWindow {
EasyMailWindow easyMailWindow = new EasyMailWindow(); EasyMailWindow easyMailWindow = new EasyMailWindow();
closeWindow(); closeWindow();
easyMailWindow.showWindow(); easyMailWindow.showWindow();
easyMailWindow.getAllInboxEmails(); easyMailWindow.getAllInboxEmails("");
} }
}); });
navigationPanel.add(inbox); navigationPanel.add(inbox);
@ -89,4 +104,3 @@ public class SentWindow extends TemplateWindow {
} }
} }
} }

View File

@ -29,6 +29,8 @@ public abstract class TemplateWindow extends JFrame {
protected static EasyMail fassade = new EasyMail(); protected static EasyMail fassade = new EasyMail();
protected JLabel fullName, username, editProfile; protected JLabel fullName, username, editProfile;
protected JTable inboxTable; protected JTable inboxTable;
private JPanel profilePanel;
public TemplateWindow(String title) { public TemplateWindow(String title) {
setTitle(title); setTitle(title);
@ -48,12 +50,18 @@ public abstract class TemplateWindow extends JFrame {
protected void showUserDetails() { protected void showUserDetails() {
JPanel profilePanel = createPanel(10, 11, 347, 239, new Color(230, 230, 230), true); if (profilePanel != null)
contentPane.remove(profilePanel);
profilePanel = createPanel(10, 11, 347, 239, new Color(230, 230, 230), true);
contentPane.add(profilePanel); contentPane.add(profilePanel);
profilePanel.setLayout(null); profilePanel.setLayout(null);
JLabel profile = createLabel("Profile", 10, 11, 203, 41, 30); JLabel profile = createLabel("Profile", 10, 11, 203, 41, 30);
profilePanel.add(profile); profilePanel.add(profile);
editProfile = createLabel("Edit profile", 10, 189, 165, 39, 22); editProfile = createLabel("Edit profile", 10, 189, 165, 39, 22);
editProfile.setForeground(Color.BLUE); editProfile.setForeground(Color.BLUE);
profilePanel.add(editProfile); profilePanel.add(editProfile);
@ -61,25 +69,31 @@ public abstract class TemplateWindow extends JFrame {
editProfile.addMouseListener(new MouseAdapter() { editProfile.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
EditProfileWindow editProfile = new EditProfileWindow(); EditProfileWindow editProfileWindow = new EditProfileWindow();
editProfile.setUpdateProfileListener(() -> { editProfileWindow.setUpdateProfileListener(() -> {
showUserDetails(); showUserDetails();
}); });
} }
}); });
// User Details
String[] getDetails = fassade.sendUserDetails(); String[] getDetails = fassade.sendUserDetails();
String fullName = getDetails[0]; String fullName = getDetails[0];
String username = getDetails[1]; String username = getDetails[1];
this.fullName = createLabel("", 10, 63, 327, 41, 20); this.fullName = createLabel("", 10, 63, 327, 41, 20);
this.username = createLabel("", 10, 106, 327, 39, 20); this.username = createLabel("", 10, 106, 327, 39, 20);
this.fullName.setText("Full Name: " + fullName); this.fullName.setText("Full Name: " + fullName);
this.username.setText("Email: " + username); this.username.setText("Email: " + username);
profilePanel.add(this.fullName); profilePanel.add(this.fullName);
profilePanel.add(this.username); profilePanel.add(this.username);
contentPane.revalidate();
contentPane.repaint();
} }
// Neue Methode // Neue Methode
protected JPanel createPanel(int x, int y, int width, int height, Color bgColor, boolean withBorder) { protected JPanel createPanel(int x, int y, int width, int height, Color bgColor, boolean withBorder) {
JPanel panel = new JPanel(); JPanel panel = new JPanel();

View File

@ -44,7 +44,7 @@ public class TrashWindow extends TemplateWindow {
EasyMailWindow easyMailWindow = new EasyMailWindow(); EasyMailWindow easyMailWindow = new EasyMailWindow();
closeWindow(); closeWindow();
easyMailWindow.showWindow(); easyMailWindow.showWindow();
easyMailWindow.getAllInboxEmails(); easyMailWindow.getAllInboxEmails("");
} }
}); });
navigationPanel.add(inbox); navigationPanel.add(inbox);

View File

@ -5,7 +5,8 @@ import gui.*;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
new RegisterWindow(); EasyMailWindow easy = new EasyMailWindow();
} }