From c30d3d3530229aded4b8db67ce4882c235897c71 Mon Sep 17 00:00:00 2001 From: joalhelk <joalhelk@jyu.fi> Date: Wed, 12 Jan 2022 19:18:02 +0200 Subject: [PATCH] changed panels to work with data and improved UI --- .../com/joelhelkala/watcherGui/Node/Node.java | 70 +++++++++++++++---- .../Node/{ => NodeData}/NodeData.java | 21 +++++- .../watcherGui/frames/WelcomePage.java | 33 ++++++--- .../frames/panels/ProgressBarCirclePanel.java | 4 +- .../frames/panels/TemperaturePanel.java | 28 ++++++++ 5 files changed, 129 insertions(+), 27 deletions(-) rename WatcherGui/src/main/java/com/joelhelkala/watcherGui/Node/{ => NodeData}/NodeData.java (55%) 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 7a48e10..90a8e0f 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Node/Node.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Node/Node.java @@ -1,5 +1,6 @@ package com.joelhelkala.watcherGui.Node; +import java.awt.LayoutManager; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @@ -7,6 +8,8 @@ import java.util.List; import org.json.JSONArray; import org.json.JSONObject; +import com.joelhelkala.watcherGui.Node.NodeData.NodeData; + public class Node { private String description; private String location; @@ -23,22 +26,59 @@ public class Node { } public Node(JSONArray arr) { - JSONObject node = arr.getJSONObject(0); - JSONObject parent = node.getJSONObject("parent"); - this.description = parent.getString("description"); - this.location = parent.getString("location"); - this.latitude = parent.getFloat("latitude"); - this.longitude = parent.getFloat("longitude"); - this.data = new ArrayList<NodeData>(); - - for(int i = 0; i < arr.length(); i++) { - JSONObject measurements = arr.getJSONObject(i); - LocalDateTime measured_at = LocalDateTime.parse(measurements.getString("measured_at")); - Integer temperature = measurements.getInt("temperature"); - Integer humidity = measurements.getInt("humidity"); - Integer luminosity = measurements.getInt("luminosity"); - this.data.add(new NodeData(measured_at, temperature, humidity, luminosity)); + if(arr.length() == 0) { + this.description = ""; + this.location = ""; + this.latitude = 0f; + this.longitude = 0f; + this.data = new ArrayList<NodeData>(); + } else { + JSONObject node = arr.getJSONObject(0); + JSONObject parent = node.getJSONObject("parent"); + this.description = parent.getString("description"); + this.location = parent.getString("location"); + this.latitude = parent.getFloat("latitude"); + this.longitude = parent.getFloat("longitude"); + this.data = new ArrayList<NodeData>(); + + for(int i = 0; i < arr.length(); i++) { + JSONObject measurements = arr.getJSONObject(i); + LocalDateTime measured_at = LocalDateTime.parse(measurements.getString("measured_at")); + Integer temperature = measurements.getInt("temperature"); + Integer humidity = measurements.getInt("humidity"); + Integer luminosity = measurements.getInt("luminosity"); + this.data.add(new NodeData(measured_at, temperature, humidity, luminosity)); + } + } + } + + // Gets the temperature reading of the most recent measurement + public Integer getRecentTemperature() { + NodeData data = findRecentData(); + if(data == null) return 0; + return data.getTemperature(); + } + + public Integer getRecentHumidity() { + NodeData data = findRecentData(); + if(data == null) return 0; + return data.getHumidity(); + } + + + public LocalDateTime getRecentDate() { + NodeData data = findRecentData(); + if(data == null) return null; + return data.getTime(); + } + + private NodeData findRecentData() { + if(data.size() == 0) return null; + NodeData newest = data.get(0); + for(int i = 1; i < data.size(); i++) { + if(data.get(i).getTime().isAfter(newest.getTime())) newest = data.get(i); } + return newest; } } diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Node/NodeData.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Node/NodeData/NodeData.java similarity index 55% rename from WatcherGui/src/main/java/com/joelhelkala/watcherGui/Node/NodeData.java rename to WatcherGui/src/main/java/com/joelhelkala/watcherGui/Node/NodeData/NodeData.java index 51fecf5..857d012 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Node/NodeData.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Node/NodeData/NodeData.java @@ -1,4 +1,4 @@ -package com.joelhelkala.watcherGui.Node; +package com.joelhelkala.watcherGui.Node.NodeData; import java.time.LocalDateTime; @@ -8,10 +8,29 @@ public class NodeData { private final Integer humidity; private final Integer luminosity; + public NodeData() { + this.measured_at = null; + this.temperature = null; + this.humidity = null; + this.luminosity = null; + } + public NodeData(LocalDateTime measured_at, Integer temperature, Integer humidity, Integer luminosity) { this.measured_at = measured_at; this.temperature = temperature; this.humidity = humidity; this.luminosity = luminosity; } + + public LocalDateTime getTime() { + return measured_at; + } + + public Integer getTemperature() { + return temperature; + } + + public Integer getHumidity() { + return humidity; + } } 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 1818045..8ea0756 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java @@ -30,7 +30,6 @@ public class WelcomePage implements MouseListener { private static final Color darkwhite = new Color(96, 96, 96); private static final Color dark = new Color(33, 33, 33); - private final JLabel nodeDataLabel; private final JLabel nodeSettingsLabel; private final JLabel friendsLabel; @@ -39,6 +38,8 @@ public class WelcomePage implements MouseListener { private final JLabel logoutLabel; private final JFrame frame; + private static JLabel chosen_label; + public WelcomePage(){ Node node = new Node(HttpRequests.getNodeData(1)); JPanel topPanel = new JPanel(); @@ -58,12 +59,12 @@ public class WelcomePage implements MouseListener { cardPanel.setBounds(leftPanelWidth, topPanelHeight, width-leftPanelWidth, cardPanelHeight); cardPanel.setLayout(null); - JPanel card1 = new TemperaturePanel(); + JPanel card1 = new TemperaturePanel(node); card1.setBackground(lightgray); card1.setBounds(15, 10, cardWidth, cardPanelHeight-20); cardPanel.add(card1); - JPanel card2 = new ProgressBarCirclePanel().makeUI("Humidity"); + JPanel card2 = new ProgressBarCirclePanel().makeUI("Humidity", node.getRecentHumidity()); card2.setBackground(lightgray); card2.setBounds(cardWidth+30, 10, cardWidth, cardPanelHeight-20); cardPanel.add(card2); @@ -104,35 +105,45 @@ public class WelcomePage implements MouseListener { iconLabel.setIcon(LoginPage.resizeIcon(image, 50, 50)); topPanel.add(iconLabel); JLabel titleLabel = new JLabel("WATCHER"); + titleLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 21)); titleLabel.setForeground(Color.yellow); - titleLabel.setBounds(100, 0, 100, 50); + titleLabel.setBounds(99, 12, 100, 50); topPanel.add(titleLabel); 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)); - nodeDataLabel.setBounds(0, 41, 240, 32); + nodeDataLabel.setBounds(0, 33, 240, 53); nodeDataLabel.setForeground(Color.WHITE); nodeDataLabel.addMouseListener(this); nodeDataLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); + + // Set as the chosen sectien + nodeDataLabel.setBackground(gray); + chosen_label = nodeDataLabel; leftPanel.add(nodeDataLabel); nodeSettingsLabel = new JLabel("Node settings"); + nodeSettingsLabel.setOpaque(true); + nodeSettingsLabel.setBackground(dark); nodeSettingsLabel.setHorizontalAlignment(SwingConstants.CENTER); nodeSettingsLabel.setForeground(Color.WHITE); nodeSettingsLabel.setFont(new Font("Apple SD Gothic Neo", Font.PLAIN, 16)); - nodeSettingsLabel.setBounds(0, 110, 240, 32); + nodeSettingsLabel.setBounds(0, 86, 240, 53); nodeSettingsLabel.addMouseListener(this); nodeSettingsLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); leftPanel.add(nodeSettingsLabel); friendsLabel = new JLabel("Friends"); + friendsLabel.setOpaque(true); + friendsLabel.setBackground(dark); friendsLabel.setHorizontalAlignment(SwingConstants.CENTER); friendsLabel.setForeground(Color.WHITE); friendsLabel.setFont(new Font("Apple SD Gothic Neo", Font.PLAIN, 16)); - friendsLabel.setBounds(0, 163, 240, 32); + friendsLabel.setBounds(0, 149, 240, 53); friendsLabel.addMouseListener(this); friendsLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); leftPanel.add(friendsLabel); @@ -182,13 +193,17 @@ public class WelcomePage implements MouseListener { @Override public void mouseEntered(MouseEvent arg0) { Object target = arg0.getSource(); - ((JComponent) target).setForeground(lightgray); + ((JComponent) target).setBackground(darkwhite); } @Override public void mouseExited(MouseEvent arg0) { Object target = arg0.getSource(); - ((JComponent) target).setForeground(Color.WHITE); + if(target == chosen_label) { + ((JComponent) target).setBackground(gray); + return; + } + ((JComponent) target).setBackground(dark); } @Override diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/ProgressBarCirclePanel.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/ProgressBarCirclePanel.java index e825f2d..b94d1e5 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/ProgressBarCirclePanel.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/ProgressBarCirclePanel.java @@ -67,7 +67,7 @@ public class ProgressBarCirclePanel extends BasicProgressBarUI { } // Returns the panel which contains the circle progressbar - public JPanel makeUI(String title) { + public JPanel makeUI(String title, Integer humid) { JProgressBar progress = new JProgressBar(); // use JProgressBar#setUI(...) method progress.setUI(new ProgressBarCirclePanel()); @@ -78,7 +78,7 @@ public class ProgressBarCirclePanel extends BasicProgressBarUI { // Timer for animation (new Timer(50, e -> { - int iv = Math.min(100, progress.getValue() + 1); + int iv = Math.min(humid, progress.getValue() + 1); progress.setValue(iv); })).start(); diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/TemperaturePanel.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/TemperaturePanel.java index f253280..6f35a10 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/TemperaturePanel.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/TemperaturePanel.java @@ -6,10 +6,13 @@ import javax.swing.SwingConstants; import org.json.JSONArray; +import com.joelhelkala.watcherGui.Node.Node; + import java.awt.BorderLayout; import java.awt.Font; import java.text.Format; import java.text.SimpleDateFormat; +import java.time.LocalDateTime; import java.util.Calendar; import java.util.Date; @@ -32,6 +35,31 @@ public class TemperaturePanel extends JPanel { this.add(temp, BorderLayout.CENTER); } + public TemperaturePanel(Node node) { + Integer temp_value = null; + String date = "No value!"; + if(node != null) { + temp_value = node.getRecentTemperature(); + LocalDateTime datetime = node.getRecentDate(); + if (datetime != null) date = datetime.toString(); + } + this.setLayout(new BorderLayout()); + JLabel day = new JLabel(date); + day.setForeground(java.awt.Color.white); + day.setHorizontalAlignment(SwingConstants.CENTER); + this.add(day, BorderLayout.NORTH); + + Character degree = 0xB0; + String temperature = "No value"; + if(temp_value != null) temperature = temp_value + "" + degree; + JLabel temp = new JLabel(temperature); + + temp.setForeground(java.awt.Color.white); + temp.setHorizontalAlignment(SwingConstants.CENTER); + temp.setFont(new Font(null, Font.BOLD, 40)); + this.add(temp, BorderLayout.CENTER); + } + private String getDay() { Calendar cal = Calendar.getInstance(); Format f = new SimpleDateFormat("EEEE"); -- GitLab