Skip to content
Snippets Groups Projects
Commit 084f16a7 authored by joalhelk's avatar joalhelk
Browse files

created abstract class for panels and value updates to panels

parent 89c60967
No related branches found
No related tags found
No related merge requests found
Showing with 123 additions and 60 deletions
......@@ -70,6 +70,13 @@ public class Node {
return data.getHumidity();
}
public Integer getRecentLuminosity() {
NodeData data = findRecentData();
if(data == null) return 0;
return data.getLuminosity();
}
public LocalDateTime getRecentDate() {
NodeData data = findRecentData();
......@@ -102,6 +109,5 @@ public class Node {
Integer lux = obj.getInt("luminosity");
data.add(new NodeData(measured_at, temp, humidity, lux));
}
}
}
}
......@@ -33,4 +33,8 @@ public class NodeData {
public Integer getHumidity() {
return humidity;
}
public Integer getLuminosity() {
return luminosity;
}
}
......@@ -114,7 +114,7 @@ public class WelcomePage implements MouseListener, ActionListener {
nodeDataLabel.addMouseListener(this);
nodeDataLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
// Set as the chosen sectien
// Set as the chosen section
nodeDataLabel.setBackground(gray);
chosen_label = nodeDataLabel;
leftPanel.add(nodeDataLabel);
......@@ -175,6 +175,7 @@ public class WelcomePage implements MouseListener, ActionListener {
bottomPanel.add(logoutLabel);
frame.getContentPane().add(bottomPanel);
// MAIN PANEL
// 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();
......
package com.joelhelkala.watcherGui.frames.panels;
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
// Abstract class for displaying the values of temperature, humidity and luminosity
public abstract class AbstractValuePanel extends JPanel {
private JLabel label;
private Integer value;
private Character symbol;
/*
* Default constructor,
*/
public AbstractValuePanel(Character symbol, String title) {
this(null, symbol, title);
}
// Constructor with given value
public AbstractValuePanel(Integer value, Character symbol, String title) {
this.value = value;
this.symbol = symbol;
setLayout(new BorderLayout());
String value_string = "No value";
if(this.value != null) value_string = this.value + "" + this.symbol;
label = new JLabel(value_string);
label.setForeground(java.awt.Color.white);
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font(null, Font.BOLD, 40));
add(label, BorderLayout.CENTER);
JLabel titleLabel = new JLabel(title);
titleLabel.setForeground(java.awt.Color.white);
titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
titleLabel.setFont(new Font(null, Font.BOLD, 20));
add(titleLabel, BorderLayout.SOUTH);
}
// Updates the label with new value
public void updateData(int value) {
label.setText(value + "" + symbol);
}
}
package com.joelhelkala.watcherGui.frames.panels;
public class HumidityPanel extends AbstractValuePanel {
private static final Character symbol = '%';
private static final String title = "Humidity";
// Constructor
public HumidityPanel() {
super(symbol, title);
}
public HumidityPanel(int value) {
super(value, symbol, title);
}
}
package com.joelhelkala.watcherGui.frames.panels;
public class LuminosityPanel extends AbstractValuePanel {
private final static Character symbol = ' ';
private final static String title = "Luminosity";
public LuminosityPanel() {
super(symbol, title);
}
public LuminosityPanel(int value) {
super(value, symbol, title);
}
}
package com.joelhelkala.watcherGui.frames.panels;
import javax.swing.JLabel;
import javax.swing.JPanel;
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.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
public class TemperaturePanel extends JPanel {
private JLabel temp;
private static final Character degree = 0xB0;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
public class TemperaturePanel extends AbstractValuePanel {
private static final Character symbol = 0xB0;
private static final String title = "Temperature";
static DateTimeFormatter formatter = DateTimeFormatter.ISO_WEEK_DATE;
public TemperaturePanel() {
setLayout(new BorderLayout());
JLabel day = new JLabel(getDay());
day.setForeground(java.awt.Color.white);
day.setHorizontalAlignment(SwingConstants.CENTER);
add(day, BorderLayout.NORTH);
String temperature = "-17" + degree;
JLabel temp = new JLabel(temperature);
temp.setForeground(java.awt.Color.white);
temp.setHorizontalAlignment(SwingConstants.CENTER);
temp.setFont(new Font(null, Font.BOLD, 40));
add(temp, BorderLayout.CENTER);
super(symbol, title);
}
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();
}
setLayout(new BorderLayout());
JLabel day = new JLabel(date);
public TemperaturePanel(Integer value, LocalDateTime date) {
super(value, symbol, title);
Integer year = date.getYear();
Integer month = date.getMonthValue();
Integer day_num = date.getDayOfMonth();
Integer hours = date.getHour();
Integer minutes = date.getMinute();
String date_value = "" + day_num + '.' + month + "." + year + " " + hours + ":" + minutes;
JLabel day = new JLabel(date_value);
day.setForeground(java.awt.Color.white);
day.setHorizontalAlignment(SwingConstants.CENTER);
add(day, BorderLayout.NORTH);
String temperature = "No value";
if(temp_value != null) temperature = temp_value + "" + degree;
temp = new JLabel(temperature);
temp.setForeground(java.awt.Color.white);
temp.setHorizontalAlignment(SwingConstants.CENTER);
temp.setFont(new Font(null, Font.BOLD, 40));
add(temp, BorderLayout.CENTER);
}
private String getDay() {
private static String getDay() {
Calendar cal = Calendar.getInstance();
Format f = new SimpleDateFormat("EEEE");
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String date = f.format(new Date()) + " " + sdf.format(cal.getTime());
return date;
}
// Updates the temperature value to most recent node temp
public void updateData(Node node) {
String text = node.getRecentTemperature().toString() + "" + degree;
temp.setText(text);
}
}
......@@ -5,7 +5,9 @@ import java.awt.Color;
import javax.swing.JPanel;
import com.joelhelkala.watcherGui.Node.Node;
import com.joelhelkala.watcherGui.frames.panels.HumidityPanel;
import com.joelhelkala.watcherGui.frames.panels.LineChartPanel;
import com.joelhelkala.watcherGui.frames.panels.LuminosityPanel;
import com.joelhelkala.watcherGui.frames.panels.ProgressBarCirclePanel;
import com.joelhelkala.watcherGui.frames.panels.TemperaturePanel;
import com.joelhelkala.watcherGui.httpRequests.HttpRequests;
......@@ -34,17 +36,19 @@ public class NodeDataFrame extends JPanel {
cardPanel.setBounds(0, 0, width, cardPanelHeight);
cardPanel.setLayout(null);
tempCard = new TemperaturePanel(node);
tempCard = new TemperaturePanel(node.getRecentTemperature(), node.getRecentDate());
tempCard.setBackground(lightgray);
tempCard.setBounds(15, 10, cardWidth, cardPanelHeight-20);
cardPanel.add(tempCard);
humidCard = new ProgressBarCirclePanel().makeUI("Humidity", node.getRecentHumidity());
// Updating this animation was hard so I just changed it to regular
//humidCard = new ProgressBarCirclePanel().makeUI("Humidity", node.getRecentHumidity());
humidCard = new HumidityPanel(node.getRecentHumidity());
humidCard.setBackground(lightgray);
humidCard.setBounds(cardWidth+30, 10, cardWidth, cardPanelHeight-20);
cardPanel.add(humidCard);
JPanel card3 = new JPanel();
JPanel card3 = new LuminosityPanel(node.getRecentLuminosity());
card3.setBackground(lightgray);
card3.setBounds(cardWidth*2+45, 10, cardWidth, cardPanelHeight-20);
cardPanel.add(card3);
......@@ -65,7 +69,7 @@ public class NodeDataFrame extends JPanel {
// Updates the frames data panels with given node information
public void updateData(Node node) {
tempCard.updateData(node);
tempCard.updateData(node.getRecentTemperature());
chartCard.updateData(node.getData());
// TODO: perhaps change the humidity to normal label since the updating is hard
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment