From 286c1f6a78252f7d50e2bea433aeca9ee843bc4e Mon Sep 17 00:00:00 2001 From: joalhelk <joalhelk@jyu.fi> Date: Thu, 13 Jan 2022 15:07:43 +0200 Subject: [PATCH] add combobox for selecting node & improve ui components --- .../com/joelhelkala/watcherGui/Node/Node.java | 4 + .../watcherGui/frames/WelcomePage.java | 85 +++++++++---------- .../frames/subframes/FriendsFrame.java | 16 ++-- .../frames/subframes/NodeDataFrame.java | 1 - .../frames/subframes/NodeSettingsFrame.java | 22 ++--- .../watcherGui/httpRequests/HttpRequests.java | 55 ++++++++---- 6 files changed, 108 insertions(+), 75 deletions(-) diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Node/Node.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Node/Node.java index 90a8e0f..d0b5761 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Node/Node.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Node/Node.java @@ -80,5 +80,9 @@ public class Node { } return newest; } + + public String getLocation() { + return location; + } } 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 3f1c365..cd582aa 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java @@ -5,6 +5,7 @@ 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; @@ -20,7 +21,12 @@ import com.joelhelkala.watcherGui.frames.subframes.NodeDataFrame; import com.joelhelkala.watcherGui.frames.subframes.NodeSettingsFrame; import com.joelhelkala.watcherGui.httpRequests.HttpRequests; -public class WelcomePage implements MouseListener { +import javafx.event.ActionEvent; + +/* + * Main page when the user logs in + */ +public class WelcomePage implements MouseListener, ActionListener { private static final int width = 1200, height = 750; private static final int topPanelHeight = height/10; @@ -33,6 +39,8 @@ public class WelcomePage implements MouseListener { private static final Color dark = new Color(33, 33, 33); private final CardLayout cl = new CardLayout(); + private JComboBox comboBox = new JComboBox(); + private JPanel stagePanel; private final JLabel nodeDataLabel; private final JLabel nodeSettingsLabel; @@ -45,6 +53,8 @@ public class WelcomePage implements MouseListener { private static JLabel chosen_label; public WelcomePage(){ + Node nodes[] = HttpRequests.getNodes(); + frame = new JFrame(); frame.getContentPane().setBackground( Color.MAGENTA ); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -52,20 +62,20 @@ public class WelcomePage implements MouseListener { frame.setSize(width, height); frame.setLocationRelativeTo(null); + // TOP PANEL JPanel topPanel = new JPanel(); topPanel.setBackground(dark); topPanel.setBounds(0,0,width, topPanelHeight); topPanel.setLayout(null); frame.getContentPane().add(topPanel); + for(int i = 0; i < nodes.length; i++) { + comboBox.addItem(nodes[i].getLocation()); + } - JPanel leftPanel = new JPanel(); - leftPanel.setBackground(dark); - leftPanel.setBounds(0, topPanelHeight, leftPanelWidth, height); - - JPanel bottomPanel = new JPanel(); - bottomPanel.setBackground(gray); - bottomPanel.setBounds(leftPanelWidth, height-height/11, width, height/11); + comboBox.addActionListener(this); + comboBox.setBounds(500, 10, 200, 50); + topPanel.add(comboBox); JLabel userLabel = new JLabel("Hello, " + User.getName()); userLabel.setBounds(1060, 32, 71, 16); @@ -82,6 +92,12 @@ public class WelcomePage implements MouseListener { titleLabel.setForeground(Color.yellow); titleLabel.setBounds(99, 12, 100, 50); topPanel.add(titleLabel); + + // LEFT PANEL + JPanel leftPanel = new JPanel(); + leftPanel.setBackground(dark); + leftPanel.setBounds(0, topPanelHeight, leftPanelWidth, height); + frame.getContentPane().add(leftPanel); leftPanel.setLayout(null); @@ -139,6 +155,10 @@ public class WelcomePage implements MouseListener { settingsLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); leftPanel.add(settingsLabel); + // BOTTOM PANEL + 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); @@ -151,7 +171,8 @@ public class WelcomePage implements MouseListener { bottomPanel.add(logoutLabel); frame.getContentPane().add(bottomPanel); - + // Add frames to a Cardlayout panel which can be used to swap between panels + // This is used for navigationbar on the left stagePanel = new JPanel(); stagePanel.setBounds(leftPanelWidth, topPanelHeight, width-leftPanelWidth, height-topPanelHeight-bottomPanelHeight); stagePanel.setBackground(Color.red); @@ -165,43 +186,13 @@ public class WelcomePage implements MouseListener { cl.show(stagePanel, "1"); frame.add(stagePanel); - /* - JPanel cardPanel = new JPanel(); - cardPanel.setBackground(gray); - cardPanel.setBounds(leftPanelWidth, topPanelHeight, width-leftPanelWidth, cardPanelHeight); - cardPanel.setLayout(null); - - JPanel card1 = new TemperaturePanel(node); - card1.setBackground(lightgray); - card1.setBounds(15, 10, cardWidth, cardPanelHeight-20); - cardPanel.add(card1); - - JPanel card2 = new ProgressBarCirclePanel().makeUI("Humidity", node.getRecentHumidity()); - card2.setBackground(lightgray); - card2.setBounds(cardWidth+30, 10, cardWidth, cardPanelHeight-20); - cardPanel.add(card2); - - JPanel card3 = new JPanel(); - card3.setBackground(lightgray); - card3.setBounds(cardWidth*2+45, 10, cardWidth, cardPanelHeight-20); - cardPanel.add(card3); - - JPanel chartPanel = new JPanel(); - chartPanel.setBackground(gray); - chartPanel.setBounds(leftPanelWidth, topPanelHeight+cardPanelHeight, width-leftPanelWidth, cardPanelHeight); - chartPanel.setLayout(null); - - JPanel chart = new LineChartPanel("Temperature"); - chart.setBackground(darkwhite); - chart.setBounds(15, 10, width - leftPanelWidth - 30, cardPanelHeight - 20); - chartPanel.add(chart); - - frame.getContentPane().add(cardPanel); - frame.getContentPane().add(chartPanel); - */ frame.setVisible(true); } + /* + * If mouse is clicked on top of left navigationbar items, + * then swap the scene. If clicked is already active, then do nothing. + */ @Override public void mouseClicked(MouseEvent arg0) { if(arg0.getSource() == nodeSettingsLabel && arg0.getSource() != chosen_label) { @@ -217,6 +208,9 @@ public class WelcomePage implements MouseListener { } + /* + * Sets a navbar item as chosen and changes its colors accordingly + */ private void setActiveScene(JLabel chosen) { nodeSettingsLabel.setBackground(dark); friendsLabel.setBackground(dark); @@ -282,4 +276,9 @@ public class WelcomePage implements MouseListener { LoginPage login = new LoginPage(); } } + + @Override + public void actionPerformed(java.awt.event.ActionEvent e) { + System.out.println(((JComboBox)e.getSource()).getSelectedItem()); + } } \ No newline at end of file diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/FriendsFrame.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/FriendsFrame.java index 22a6252..4b7a2f5 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/FriendsFrame.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/FriendsFrame.java @@ -4,17 +4,21 @@ import java.awt.Color; import javax.swing.JLabel; import javax.swing.JPanel; +import javax.swing.SwingConstants; + +import java.awt.BorderLayout; public class FriendsFrame extends JPanel { + private static final Color gray = new Color(45, 45, 45); public FriendsFrame(int x, int y, int width, int height) { - setLayout(null); + setLayout(new BorderLayout()); setBounds(x,y,width,height); setOpaque(true); - setBackground(Color.BLACK); - JLabel label = new JLabel("testing"); - label.setOpaque(true); - label.setForeground(Color.BLACK); - add(label); + setBackground(gray); + JLabel label = new JLabel("Friends"); + label.setForeground(Color.white); + label.setHorizontalAlignment(SwingConstants.CENTER); + add(label, BorderLayout.NORTH); } } diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeDataFrame.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeDataFrame.java index 9d25591..1bcaa28 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeDataFrame.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeDataFrame.java @@ -2,7 +2,6 @@ package com.joelhelkala.watcherGui.frames.subframes; import java.awt.Color; -import javax.swing.JLabel; import javax.swing.JPanel; import com.joelhelkala.watcherGui.Node.Node; 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 1f89b5a..d510bf4 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 @@ -1,22 +1,24 @@ package com.joelhelkala.watcherGui.frames.subframes; import java.awt.Color; -import java.awt.CardLayout; import javax.swing.JLabel; import javax.swing.JPanel; +import javax.swing.SwingConstants; + +import java.awt.BorderLayout; public class NodeSettingsFrame extends JPanel { - private final CardLayout cl = new CardLayout(); + private static final Color gray = new Color(45, 45, 45); public NodeSettingsFrame(int x, int y, int width, int height) { - this.setLayout(cl); - this.setBounds(x,y,width,height); - this.setOpaque(true); - this.setBackground(Color.WHITE); - JLabel label = new JLabel("testing"); - label.setOpaque(true); - label.setForeground(Color.BLACK); - this.add(label); + setBounds(0,0,width,height); + setOpaque(true); + setBackground(gray); + setLayout(new BorderLayout()); + JLabel title = new JLabel("Node Settings"); + title.setForeground(Color.white); + title.setHorizontalAlignment(SwingConstants.CENTER); + add(title, BorderLayout.NORTH); } } \ No newline at end of file diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java index cac560c..bb0ca26 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java @@ -16,6 +16,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import com.joelhelkala.watcherGui.Node.Node; import com.joelhelkala.watcherGui.User.User; /* @@ -109,7 +110,7 @@ public class HttpRequests { /* * Send a login POST request to server - * TODO: Get a session token from the server which will be used for communicating with endpoints + * Server should return a session token and user information */ public static boolean LoginRequest(String user, String password) { try { @@ -159,9 +160,46 @@ public class HttpRequests { * TODO: Set a parameter that configures the amount of history data will be fetched */ public static JSONArray getNodeData(Integer node_id) { + JSONArray data = basicGetRequest("/nodeData/" + node_id); + return data; + } + + public static JSONArray parse(String responseBody) { + JSONArray body = new JSONArray(responseBody); + for(int i = 0; i < body.length(); i++) { + JSONObject obj = body.getJSONObject(i); + int code = obj.getInt("code"); + int id = obj.getInt("id"); + String user = obj.getString("user"); + System.out.println(code + " " + id + " " + user); + } + return body; + } + + /* + * Gets all the nodes available from the server + */ + public static Node[] getNodes() { + JSONArray data = basicGetRequest("/node"); + int nodes_amount = data.length(); + Node nodes[] = new Node[nodes_amount]; + + for(int i = 0; i < nodes_amount; i++) { + JSONObject obj = data.getJSONObject(i); + String description = obj.getString("description"); + String location = obj.getString("location"); + Float latitude = obj.getFloat("latitude"); + Float longitude = obj.getFloat("longitude"); + nodes[i] = new Node(description, latitude, longitude, location); + } + return nodes; + } + + private static JSONArray basicGetRequest(String uri) { JSONArray data = new JSONArray(); + URL url; try { - URL url = new URL(address + "/nodeData/" + node_id); + url = new URL(address + uri); StringBuilder response = new StringBuilder(); HttpURLConnection con = (HttpURLConnection)url.openConnection(); con.setRequestMethod("GET"); @@ -183,19 +221,6 @@ public class HttpRequests { } catch (IOException e) { e.printStackTrace(); } - return data; } - - public static JSONArray parse(String responseBody) { - JSONArray body = new JSONArray(responseBody); - for(int i = 0; i < body.length(); i++) { - JSONObject obj = body.getJSONObject(i); - int code = obj.getInt("code"); - int id = obj.getInt("id"); - String user = obj.getString("user"); - System.out.println(code + " " + id + " " + user); - } - return body; - } } -- GitLab