From fe17aa5e317374c11c32af60bac7e8a15906cc3d Mon Sep 17 00:00:00 2001
From: joalhelk <joalhelk@jyu.fi>
Date: Tue, 18 Jan 2022 00:51:54 +0200
Subject: [PATCH] change data according to combobox selection

---
 .../joelhelkala/watcherGui/Nodes/Nodes.java   | 10 +++
 .../watcherGui/frames/WelcomePage.java        | 73 +++++++++++--------
 .../frames/subframes/NodeSettingsFrame.java   |  1 +
 3 files changed, 54 insertions(+), 30 deletions(-)

diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Nodes/Nodes.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Nodes/Nodes.java
index 9b50256..1562a3a 100644
--- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Nodes/Nodes.java
+++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Nodes/Nodes.java
@@ -4,6 +4,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import com.joelhelkala.watcherGui.Nodes.Node.Node;
+import com.joelhelkala.watcherGui.httpRequests.HttpRequests;
 
 public class Nodes {
 	private static List<Node> nodes = new ArrayList<Node>();
@@ -27,6 +28,10 @@ public class Nodes {
 	public static Node get(int index) {
 		return nodes.get(index);
 	}
+	
+	public static List<Node> getAll() {
+		return nodes;
+	}
 
 	public static Node findByLocation(String location) {
 		for(Node n : nodes) {
@@ -34,4 +39,9 @@ public class Nodes {
 		}
 		return null;
 	}
+	
+	public static void updateNodes() {
+		nodes.clear();
+		AddNodes(HttpRequests.getNodes());
+	}
 }
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 c643474..f8ee298 100644
--- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java
+++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java
@@ -3,27 +3,20 @@ package com.joelhelkala.watcherGui.frames;
 import java.awt.*;
 import javax.swing.*;
 
-import org.json.JSONArray;
-
 import java.awt.event.ActionListener;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.ArrayList;
+import java.util.List;
 
 import com.joelhelkala.watcherGui.Nodes.Nodes;
 import com.joelhelkala.watcherGui.Nodes.Node.Node;
 import com.joelhelkala.watcherGui.User.User;
-import com.joelhelkala.watcherGui.frames.panels.LineChartPanel;
-import com.joelhelkala.watcherGui.frames.panels.ProgressBarCirclePanel;
-import com.joelhelkala.watcherGui.frames.panels.TemperaturePanel;
 import com.joelhelkala.watcherGui.frames.subframes.FriendsFrame;
 import com.joelhelkala.watcherGui.frames.subframes.NodeDataFrame;
 import com.joelhelkala.watcherGui.frames.subframes.NodeSettingsFrame;
 import com.joelhelkala.watcherGui.httpRequests.HttpRequests;
 
-import javafx.event.ActionEvent;
-
 /*
  * Main page when the user logs in
  */
@@ -40,15 +33,15 @@ public class WelcomePage implements MouseListener, ActionListener {
 	private static final Color dark = new Color(33, 33, 33);
 	
 	private final CardLayout cl = new CardLayout();
-	private JComboBox comboBox = new JComboBox();
+	private static JComboBox comboBox = new JComboBox();
 	
 	private JPanel stagePanel;
-	private final JLabel nodeDataLabel;
-	private final JLabel nodeSettingsLabel;
-	private final JLabel friendsLabel;
-	private final JLabel helpLabel;
-	private final JLabel settingsLabel;
-	private final JLabel logoutLabel;
+	private final JLabel nodeDataLabel 		= new JLabel("Node data");
+	private final JLabel nodeSettingsLabel 	= new JLabel("Node settings");
+	private final JLabel friendsLabel 		= new JLabel("Friends");
+	private final JLabel helpLabel 			= new JLabel("Help");
+	private final JLabel settingsLabel 		= new JLabel("Settings");
+	private final JLabel logoutLabel 		= new JLabel("Logout");
 	private static JFrame frame;
 	
 	private static JLabel chosen_label;
@@ -57,7 +50,7 @@ public class WelcomePage implements MouseListener, ActionListener {
 	private static NodeSettingsFrame nodeSettingFrame = new NodeSettingsFrame(width-leftPanelWidth, height-topPanelHeight-bottomPanelHeight);
 	
 	public WelcomePage(){
-		Nodes.AddNodes(HttpRequests.getNodes());
+		Nodes.updateNodes();
 		
 		frame = new JFrame();
 		frame.getContentPane().setBackground( Color.MAGENTA );
@@ -77,6 +70,15 @@ public class WelcomePage implements MouseListener, ActionListener {
 			comboBox.addItem(Nodes.get(i).getLocation());			
 		}
 		
+		String selected = (String) comboBox.getSelectedItem();
+		Node node = Nodes.findByLocation(selected);
+		
+		if(node == null) return;
+		
+		node.updateData(HttpRequests.getNodeData(node.getId()));
+		nodeDataFrame.updateData(node);
+		nodeSettingFrame.updateInformation(node);
+		
 		comboBox.addActionListener(this);
 		comboBox.setBounds(500, 10, 200, 50);
 		topPanel.add(comboBox);
@@ -105,7 +107,6 @@ public class WelcomePage implements MouseListener, ActionListener {
 		frame.getContentPane().add(leftPanel);
 		leftPanel.setLayout(null);
 		
-		nodeDataLabel = new JLabel("Node data");
 		nodeDataLabel.setOpaque(true);
 		nodeDataLabel.setHorizontalAlignment(SwingConstants.CENTER);
 		nodeDataLabel.setFont(new Font("Apple SD Gothic Neo", Font.PLAIN, 16));
@@ -119,7 +120,6 @@ public class WelcomePage implements MouseListener, ActionListener {
 		chosen_label = nodeDataLabel;
 		leftPanel.add(nodeDataLabel);
 		
-		nodeSettingsLabel = new JLabel("Node settings");
 		nodeSettingsLabel.setOpaque(true);
 		nodeSettingsLabel.setBackground(dark);
 		nodeSettingsLabel.setHorizontalAlignment(SwingConstants.CENTER);
@@ -130,7 +130,6 @@ public class WelcomePage implements MouseListener, ActionListener {
 		nodeSettingsLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
 		leftPanel.add(nodeSettingsLabel);
 		
-		friendsLabel = new JLabel("Friends");
 		friendsLabel.setOpaque(true);
 		friendsLabel.setBackground(dark);
 		friendsLabel.setHorizontalAlignment(SwingConstants.CENTER);
@@ -141,7 +140,6 @@ public class WelcomePage implements MouseListener, ActionListener {
 		friendsLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
 		leftPanel.add(friendsLabel);
 		
-		helpLabel = new JLabel("Help");
 		helpLabel.setHorizontalAlignment(SwingConstants.CENTER);
 		helpLabel.setForeground(Color.WHITE);
 		helpLabel.setFont(new Font("Apple SD Gothic Neo", Font.PLAIN, 16));
@@ -150,7 +148,6 @@ public class WelcomePage implements MouseListener, ActionListener {
 		helpLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
 		leftPanel.add(helpLabel);
 		
-		settingsLabel = new JLabel("Settings");
 		settingsLabel.setHorizontalAlignment(SwingConstants.CENTER);
 		settingsLabel.setForeground(Color.WHITE);
 		settingsLabel.setFont(new Font("Apple SD Gothic Neo", Font.PLAIN, 16));
@@ -163,7 +160,7 @@ public class WelcomePage implements MouseListener, ActionListener {
 		JPanel bottomPanel = new JPanel();
 		bottomPanel.setBackground(gray);
 		bottomPanel.setBounds(leftPanelWidth, height-height/11, width, height/11);
-		logoutLabel = new JLabel("Logout");
+
 		logoutLabel.setVerticalAlignment(SwingConstants.BOTTOM);
 		logoutLabel.setHorizontalAlignment(SwingConstants.CENTER);
 		logoutLabel.setBounds(884, 6, 48, 20);
@@ -296,6 +293,28 @@ public class WelcomePage implements MouseListener, ActionListener {
 		return result == JOptionPane.YES_OPTION;
 	}
 	
+	public static void updateComboBox() {
+		Nodes.updateNodes();
+		List<String> node_names = new ArrayList<String>();
+		
+		for(Node n : Nodes.getAll()) {
+			node_names.add(n.getLocation());
+		}
+		
+		comboBox.setModel( new DefaultComboBoxModel(node_names.toArray()) );
+		updateDataByName((String)comboBox.getSelectedItem());
+	}
+	
+	private static void updateDataByName(String node_name) {
+		Node node = Nodes.findByLocation(node_name);
+		
+		if(node == null) return;
+		
+		node.updateData(HttpRequests.getNodeData(node.getId()));
+		nodeDataFrame.updateData(node);
+		nodeSettingFrame.updateInformation(node);
+	}
+	
 	/*
 	 * This is prompted when user selects a item from node combobox
 	 * Find the node object based on name and then fetch the data for that node from the server
@@ -304,12 +323,6 @@ public class WelcomePage implements MouseListener, ActionListener {
 	@Override
 	public void actionPerformed(java.awt.event.ActionEvent e) {
 		String node_name = ((JComboBox)e.getSource()).getSelectedItem().toString();
-		Node node = Nodes.findByLocation(node_name);
-		
-		if(node == null) return;
-		
-		node.updateData(HttpRequests.getNodeData(node.getId()));
-		nodeDataFrame.updateData(node);
-		nodeSettingFrame.updateInformation(node);
+		updateDataByName(node_name);
 	}
 }
\ No newline at end of file
diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeSettingsFrame.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeSettingsFrame.java
index 91a4e4a..065027c 100644
--- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeSettingsFrame.java
+++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeSettingsFrame.java
@@ -96,6 +96,7 @@ public class NodeSettingsFrame extends JPanel implements ActionListener{
 												Float.parseFloat(lonField.getText()),
 												locationField.getText(),
 												current_node.getId()));
+		WelcomePage.updateComboBox();
 	}
 	
 	/*
-- 
GitLab