From a8f8b7452e2a4efabc90d9c041942f26bd6d399a Mon Sep 17 00:00:00 2001
From: joalhelk <joalhelk@jyu.fi>
Date: Tue, 11 Jan 2022 13:46:15 +0200
Subject: [PATCH] added logout method

---
 .../com/joelhelkala/watcherGui/User/User.java |  6 +++
 .../watcherGui/frames/WelcomePage.java        | 48 ++++++++++++++-----
 2 files changed, 42 insertions(+), 12 deletions(-)

diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/User/User.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/User/User.java
index 3e331be..2ea3fba 100644
--- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/User/User.java
+++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/User/User.java
@@ -37,4 +37,10 @@ public class User {
 	public static String getToken() {
 		return token;
 	}
+	
+	public static void clear() {
+		User.name = null;
+		User.email = null;
+		User.token = null;
+	}
 }
diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java
index db2fcda..2c60e4e 100644
--- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java
+++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java
@@ -33,6 +33,8 @@ public class WelcomePage implements MouseListener {
 	private final JLabel friendsLabel;
 	private final JLabel helpLabel;
 	private final JLabel settingsLabel;
+	private final JLabel logoutLabel;
+	private final JFrame frame;
 	
 	public WelcomePage(){
 		Map<String, String> temperatures = HttpRequests.getNodeData(1);
@@ -78,7 +80,7 @@ public class WelcomePage implements MouseListener {
 		chart.setBounds(15, 10, width - leftPanelWidth - 30, cardPanelHeight - 20);
 		chartPanel.add(chart);
 		
-		JFrame frame = new JFrame();
+		frame = new JFrame();
 		frame.getContentPane().setBackground( new Color(128, 127, 137) );
 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 		frame.getContentPane().setLayout(null);
@@ -149,6 +151,18 @@ public class WelcomePage implements MouseListener {
 		settingsLabel.addMouseListener(this);
 		settingsLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
 		leftPanel.add(settingsLabel);
+		
+		logoutLabel = new JLabel("Logout");
+		logoutLabel.setVerticalAlignment(SwingConstants.BOTTOM);
+		logoutLabel.setHorizontalAlignment(SwingConstants.CENTER);
+		logoutLabel.setBounds(884, 6, 48, 20);
+		logoutLabel.setForeground(Color.WHITE);
+		logoutLabel.setFont(new Font("Apple SD Gothic Neo", Font.PLAIN, 16));
+		logoutLabel.addMouseListener(this);
+		bottomPanel.setLayout(null);
+		logoutLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
+		bottomPanel.add(logoutLabel);
+		
 		frame.getContentPane().add(cardPanel);
 		frame.getContentPane().add(chartPanel);
 		frame.getContentPane().add(bottomPanel);
@@ -165,21 +179,13 @@ public class WelcomePage implements MouseListener {
 	@Override
 	public void mouseEntered(MouseEvent arg0) {
 		Object target = arg0.getSource();
-		if (target == nodeDataLabel) nodeDataLabel.setForeground(lightgray);
-		else if (target == nodeSettingsLabel) nodeSettingsLabel.setForeground(lightgray);
-		else if (target == friendsLabel) friendsLabel.setForeground(lightgray);
-		else if (target == helpLabel) helpLabel.setForeground(lightgray);
-		else if (target == settingsLabel) settingsLabel.setForeground(lightgray);
+		((JComponent) target).setForeground(lightgray);
 	}
 
 	@Override
 	public void mouseExited(MouseEvent arg0) {
 		Object target = arg0.getSource();
-		if (target == nodeDataLabel) nodeDataLabel.setForeground(Color.WHITE);
-		else if (target == nodeSettingsLabel) nodeSettingsLabel.setForeground(Color.WHITE);
-		else if (target == friendsLabel) friendsLabel.setForeground(Color.WHITE);
-		else if (target == helpLabel) helpLabel.setForeground(Color.WHITE);
-		else if (target == settingsLabel) settingsLabel.setForeground(Color.WHITE);
+		((JComponent) target).setForeground(Color.WHITE);
 	}
 
 	@Override
@@ -190,7 +196,25 @@ public class WelcomePage implements MouseListener {
 
 	@Override
 	public void mouseReleased(MouseEvent arg0) {
-		// TODO Auto-generated method stub
+		if(arg0.getSource() == logoutLabel) {
+			handleLogout();
+		}
 		
 	}
+	
+	// Logs the user out and clears user data
+	// Closes current window and opens loginpage
+	private void handleLogout() {
+		String message = "Are you sure you want to logout?";
+		String title = "Logout?";
+		int result = JOptionPane.showConfirmDialog(frame, message, title,
+	               JOptionPane.YES_NO_OPTION,
+	               JOptionPane.QUESTION_MESSAGE);
+		
+		if(result == JOptionPane.YES_OPTION) {
+			User.clear();
+			frame.dispose();
+			LoginPage login = new LoginPage();
+		}
+	}
 }
\ No newline at end of file
-- 
GitLab