Skip to content
Snippets Groups Projects
Commit 6bab5eee authored by joalhelk's avatar joalhelk
Browse files

add helper class for nodes and change the nodes package structure

parent 084f16a7
No related branches found
No related tags found
No related merge requests found
package com.joelhelkala.watcherGui.Node;
package com.joelhelkala.watcherGui.Nodes.Node;
import java.awt.LayoutManager;
import java.time.LocalDateTime;
......@@ -8,7 +8,7 @@ import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.joelhelkala.watcherGui.Node.NodeData.NodeData;
import com.joelhelkala.watcherGui.Nodes.Node.NodeData.NodeData;
public class Node {
private String description;
......
package com.joelhelkala.watcherGui.Node.NodeData;
package com.joelhelkala.watcherGui.Nodes.Node.NodeData;
import java.time.LocalDateTime;
......
package com.joelhelkala.watcherGui.Nodes;
import java.util.ArrayList;
import java.util.List;
import com.joelhelkala.watcherGui.Nodes.Node.Node;
public class Nodes {
private static List<Node> nodes = new ArrayList<Node>();
// Add a list of nodes
public static void AddNodes(List<Node> list) {
for(Node n : list) {
AddNode(n);
}
}
// Add a node to nodes list
public static void AddNode(Node node) {
nodes.add(node);
}
public static int size() {
return nodes.size();
}
public static Node get(int index) {
return nodes.get(index);
}
public static Node findByLocation(String location) {
for(Node n : nodes) {
if(n.getLocation() == location) return n;
}
return null;
}
}
......@@ -11,7 +11,8 @@ import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.Map;
import com.joelhelkala.watcherGui.Node.Node;
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;
......@@ -51,13 +52,12 @@ public class WelcomePage implements MouseListener, ActionListener {
private final JFrame frame;
private static JLabel chosen_label;
private static Node nodes[];
private static NodeDataFrame nodeDataFrame = new NodeDataFrame(leftPanelWidth, topPanelHeight, width-leftPanelWidth, height-topPanelHeight-bottomPanelHeight);
private static NodeSettingsFrame nodeSettingFrame = new NodeSettingsFrame(leftPanelWidth, topPanelHeight, width-leftPanelWidth, height-topPanelHeight-bottomPanelHeight);
public WelcomePage(){
nodes = HttpRequests.getNodes();
Nodes.AddNodes(HttpRequests.getNodes());
frame = new JFrame();
frame.getContentPane().setBackground( Color.MAGENTA );
......@@ -73,8 +73,8 @@ public class WelcomePage implements MouseListener, ActionListener {
topPanel.setLayout(null);
frame.getContentPane().add(topPanel);
for(int i = 0; i < nodes.length; i++) {
comboBox.addItem(nodes[i].getLocation());
for(int i = 0; i < Nodes.size(); i++) {
comboBox.addItem(Nodes.get(i).getLocation());
}
comboBox.addActionListener(this);
......@@ -291,13 +291,7 @@ 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 = null;
for(Node n : nodes) {
if(n.getLocation() == node_name) {
node = n;
break;
}
}
Node node = Nodes.findByLocation(node_name);
if(node == null) return;
......
......@@ -11,7 +11,7 @@ import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import com.joelhelkala.watcherGui.Node.NodeData.NodeData;
import com.joelhelkala.watcherGui.Nodes.Node.NodeData.NodeData;
import java.awt.BorderLayout;
import java.util.List;
......
......@@ -4,7 +4,7 @@ import java.awt.Color;
import javax.swing.JPanel;
import com.joelhelkala.watcherGui.Node.Node;
import com.joelhelkala.watcherGui.Nodes.Node.Node;
import com.joelhelkala.watcherGui.frames.panels.HumidityPanel;
import com.joelhelkala.watcherGui.frames.panels.LineChartPanel;
import com.joelhelkala.watcherGui.frames.panels.LuminosityPanel;
......
......@@ -2,23 +2,43 @@ package com.joelhelkala.watcherGui.frames.subframes;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.JSplitPane;
import java.awt.GridLayout;
import java.awt.BorderLayout;
public class NodeSettingsFrame extends JPanel {
private static final Color gray = new Color(45, 45, 45);
public NodeSettingsFrame(int x, int y, int width, int height) {
setLayout(new BorderLayout());
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);
JPanel grid = new JPanel();
grid.setLayout(new GridLayout(2,2));
JPanel descPanel = new JPanel();
JPanel locationPanel = new JPanel();
JPanel longPanel = new JPanel();
JPanel latPanel = new JPanel();
JButton reset_btn = new JButton("Reset");
JButton save_btn = new JButton("Save");
grid.add(descPanel);
grid.add(locationPanel);
grid.add(latPanel);
grid.add(longPanel);
add(grid, BorderLayout.CENTER);
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, reset_btn, save_btn);
add(split, BorderLayout.SOUTH);
}
}
\ No newline at end of file
......@@ -9,14 +9,16 @@ import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.joelhelkala.watcherGui.Node.Node;
import com.joelhelkala.watcherGui.Nodes.Node.Node;
import com.joelhelkala.watcherGui.User.User;
/*
......@@ -179,10 +181,10 @@ public class HttpRequests {
/*
* Gets all the nodes available from the server
*/
public static Node[] getNodes() {
public static List<Node> getNodes() {
JSONArray data = basicGetRequest("/node");
int nodes_amount = data.length();
Node nodes[] = new Node[nodes_amount];
List<Node> nodes = new ArrayList<Node>(nodes_amount);
for(int i = 0; i < nodes_amount; i++) {
JSONObject obj = data.getJSONObject(i);
......@@ -191,7 +193,7 @@ public class HttpRequests {
Float latitude = obj.getFloat("latitude");
Float longitude = obj.getFloat("longitude");
Integer id = obj.getInt("id");
nodes[i] = new Node(description, latitude, longitude, location, id);
nodes.add(new Node(description, latitude, longitude, location, id));
}
return nodes;
}
......
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