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 3e331be59e64bc5b5f542cd985763309a796b689..2ea3fba3fde06c2bc9cf18755660ec11c92ddb39 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 db2fcdae9f5253d2738b45545a633fea0a45c165..2c60e4ee67de89abcf8f39f20e552201b2938dd1 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