From 6d11269c55618d0972b4e82b6aed06e522ae1e1f Mon Sep 17 00:00:00 2001 From: danielharbor Date: Fri, 20 Feb 2015 12:21:35 -0800 Subject: [PATCH] mvp --- DetailsPanel.java | 8 ++++ Electronic.java | 17 +++++++++ GUI.java | 18 +++++++++ ItemsPanel.java | 14 +++++++ MainFrame.java | 97 +++++++++++++++++++++++++++++++++++++++++++++++ Microwave.java | 22 +++++++++++ Person.java | 25 ++++++++++++ Room.java | 22 +++++++++++ Simulator.java | 25 ++++++++++++ Thing.java | 27 +++++++++++++ 10 files changed, 275 insertions(+) create mode 100644 DetailsPanel.java create mode 100644 Electronic.java create mode 100644 GUI.java create mode 100644 ItemsPanel.java create mode 100644 MainFrame.java create mode 100644 Microwave.java create mode 100644 Person.java create mode 100644 Room.java create mode 100644 Simulator.java create mode 100644 Thing.java diff --git a/DetailsPanel.java b/DetailsPanel.java new file mode 100644 index 0000000..9c10e6b --- /dev/null +++ b/DetailsPanel.java @@ -0,0 +1,8 @@ +import javax.swing.BorderFactory; +import javax.swing.JPanel; + +public class DetailsPanel extends JPanel { + public DetailsPanel() { + setBorder(BorderFactory.createTitledBorder("Details")); + } +} \ No newline at end of file diff --git a/Electronic.java b/Electronic.java new file mode 100644 index 0000000..7a4c22e --- /dev/null +++ b/Electronic.java @@ -0,0 +1,17 @@ +public abstract class Electronic extends Thing { + private boolean state; + + public Electronic(String name, Room room, boolean state) { + super(name, room); + this.state = state; + } + + public void toggleSwitch() { + state = state ? false : true; +// GUI.refreshGUI(); + } + + public boolean getState() { + return state; + } +} \ No newline at end of file diff --git a/GUI.java b/GUI.java new file mode 100644 index 0000000..e29bea6 --- /dev/null +++ b/GUI.java @@ -0,0 +1,18 @@ +import javax.swing.JFrame; +import javax.swing.SwingUtilities; + +public class GUI { + + public static void main(String[] args) { + + SwingUtilities.invokeLater(new Runnable() { + public void run() { + JFrame frame = new MainFrame("Smart Home"); + frame.setSize(500, 400); + frame.setLocationRelativeTo(null); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setVisible(true); + } + }); + } +} \ No newline at end of file diff --git a/ItemsPanel.java b/ItemsPanel.java new file mode 100644 index 0000000..cd36468 --- /dev/null +++ b/ItemsPanel.java @@ -0,0 +1,14 @@ +import java.awt.Dimension; + +import javax.swing.BorderFactory; +import javax.swing.JPanel; + +public class ItemsPanel extends JPanel { + public ItemsPanel() { + Dimension size = getPreferredSize(); + size.width = 190; + setPreferredSize(size); + setBorder(BorderFactory.createTitledBorder("Items")); + } + +} \ No newline at end of file diff --git a/MainFrame.java b/MainFrame.java new file mode 100644 index 0000000..3f8b8d9 --- /dev/null +++ b/MainFrame.java @@ -0,0 +1,97 @@ +import java.awt.BorderLayout; +import java.awt.Container; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.List; + +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JList; +import javax.swing.JScrollPane; +import javax.swing.JTextArea; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; + +public class MainFrame extends JFrame { + private ItemsPanel itemsPanel; + private DetailsPanel detailsPanel; + JList list; + DefaultListModel listSelectionModel = new DefaultListModel<>(); + JTextArea detailsTextArea; + JButton button; + JScrollPane scroller; + Thing thingBeingDisplayed; + + public MainFrame(String title) { + super(title); + setLayout(new BorderLayout()); + + itemsPanel = new ItemsPanel(); + detailsPanel = new DetailsPanel(); + detailsPanel.setLayout(new BorderLayout()); + detailsTextArea = new JTextArea(); + detailsTextArea.setEditable(false); + detailsTextArea.setLineWrap(true); + + button = new JButton("Click me"); + + for(Thing thing : Simulator.getThings()) { + listSelectionModel.addElement(thing); + } + + //Items list + list = new JList(listSelectionModel); + list.setVisibleRowCount(10); + scroller = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + list.setFixedCellHeight(30); + list.setFixedCellWidth(150); + ListenForList lForList = new ListenForList(); + list.addListSelectionListener(lForList); + + button.setText("Click"); + ListenForButton lForButton = new ListenForButton(); + button.addActionListener(lForButton); + + button.setBorderPainted(false); + itemsPanel.add(list); + itemsPanel.add(scroller); + + detailsPanel.add(detailsTextArea, BorderLayout.CENTER); + + Container c = getContentPane(); + c.add(itemsPanel, BorderLayout.WEST); + c.add(detailsPanel, BorderLayout.CENTER); + } + + public void refreshGUI() { + List detailsInfo = thingBeingDisplayed.provideDetails(); + StringBuilder detailsBuffer = new StringBuilder(); + for(String s : detailsInfo) { + detailsBuffer.append(s); + } + detailsTextArea.setText(detailsBuffer.toString()); + } + + private class ListenForButton implements ActionListener { + + @Override + public void actionPerformed(ActionEvent e) { + if(e.getSource() == button) { + + } + } + } + + private class ListenForList implements ListSelectionListener { + + @Override + public void valueChanged(ListSelectionEvent e) { + if(e.getSource() == list) { + thingBeingDisplayed = list.getSelectedValue(); + refreshGUI(); + } + } + } + +} \ No newline at end of file diff --git a/Microwave.java b/Microwave.java new file mode 100644 index 0000000..26049cd --- /dev/null +++ b/Microwave.java @@ -0,0 +1,22 @@ +import java.util.List; +import java.util.ArrayList; + +public class Microwave extends Electronic { + + public Microwave(String name, Room room, boolean state) { + super(name, room, state); + } + + @Override + public List provideDetails() { + List res = new ArrayList<>(); + res.add("Name: " + this.getName() + "\n"); + res.add("Room: " + this.getRoom().getName() + "\nCurrent State: "); + if(getState()) + res.add("ON"); + else + res.add("OFF"); + return res; + } + +} \ No newline at end of file diff --git a/Person.java b/Person.java new file mode 100644 index 0000000..d9de408 --- /dev/null +++ b/Person.java @@ -0,0 +1,25 @@ +import java.util.List; +import java.util.ArrayList; + +public class Person extends Thing { + private String gender; + + public Person(String name, String gender, Room room) { + super(name, room); + this.gender = gender; + } + + public String gender() { + return gender; + } + + @Override + public List provideDetails() { + List ans = new ArrayList<>(); + ans.add("Name: " + this.getName() + "\n"); + ans.add("Room: " + this.getRoom().getName() + "\n"); + ans.add("Gender: " + this.gender); + return ans; + } + +} \ No newline at end of file diff --git a/Room.java b/Room.java new file mode 100644 index 0000000..9399d03 --- /dev/null +++ b/Room.java @@ -0,0 +1,22 @@ +import java.util.List; +import java.util.ArrayList; + +public class Room extends Thing { + + public Room(String name) { + super(name, null); + } + + @Override + public List provideDetails() { + List ans = new ArrayList<>(); + ans.add(this.getName() + " entities: \n"); + for(Thing o : Simulator.getThings()) { + if(o.getRoom() == this) { // check primitive comparison + ans.add(o.getName() + "\n"); + } + } + return ans; + } + +} \ No newline at end of file diff --git a/Simulator.java b/Simulator.java new file mode 100644 index 0000000..f4aa205 --- /dev/null +++ b/Simulator.java @@ -0,0 +1,25 @@ +import java.util.*; + +public class Simulator { + private static Set thingsInSimulation; + + static { + thingsInSimulation = new HashSet<>(); + Room room1 = new Room("Kitchen"); + Room room2 = new Room("Living Room"); + Person jane = new Person("Jane", "F", room1); + Person john = new Person("John", "M", room2); + Microwave sonyMicrowave = new Microwave("Sony Microwave", room1, true); + Microwave samsungMicrowave = new Microwave("Samsung Microwave", room1, false); + thingsInSimulation.add(jane); + thingsInSimulation.add(john); + thingsInSimulation.add(room1); + thingsInSimulation.add(room2); + thingsInSimulation.add(sonyMicrowave); + thingsInSimulation.add(samsungMicrowave); + } + + public static Set getThings() { + return thingsInSimulation; + } +} \ No newline at end of file diff --git a/Thing.java b/Thing.java new file mode 100644 index 0000000..c11770b --- /dev/null +++ b/Thing.java @@ -0,0 +1,27 @@ +import java.util.List; +import java.util.ArrayList; + +public abstract class Thing { + private String name; + private Room room; + + public Thing(String name, Room room) { + this.name = name; + this.room = room; + } + + public String getName() { + return name; + } + + public abstract List provideDetails(); + + public Room getRoom() { + return room; + } + + @Override + public String toString() { + return name; + } +} \ No newline at end of file