diff --git a/JavaDesktopApp/src/com/philips/lighting/Controller.java b/JavaDesktopApp/src/com/philips/lighting/Controller.java index 56a0395..61293d6 100644 --- a/JavaDesktopApp/src/com/philips/lighting/Controller.java +++ b/JavaDesktopApp/src/com/philips/lighting/Controller.java @@ -9,6 +9,7 @@ import com.philips.lighting.gui.AccessPointList; import com.philips.lighting.gui.DesktopView; import com.philips.lighting.gui.LightColoursFrame; +import com.philips.lighting.gui.LightSwitchOnFrame; import com.philips.lighting.gui.PushLinkFrame; import com.philips.lighting.hue.sdk.PHAccessPoint; import com.philips.lighting.hue.sdk.PHBridgeSearchManager; @@ -29,6 +30,7 @@ public class Controller { private PushLinkFrame pushLinkDialog; private LightColoursFrame lightColoursFrame; + private LightSwitchOnFrame lightEnableFrame; private static final int MAX_HUE=65535; private Controller instance; @@ -88,6 +90,7 @@ public void onBridgeConnected(PHBridge bridge, String username) { // Enable the Buttons/Controls to change the hue bulbs.s desktopView.getRandomLightsButton().setEnabled(true); desktopView.getSetLightsButton().setEnabled(true); + desktopView.getSwitchOffLightsButton().setEnabled(true); } @@ -170,6 +173,14 @@ public void showControlLightsWindow() { lightColoursFrame.setVisible(true); } + public void showControlSwitchOffLightsWindow() { + if (lightEnableFrame == null) { + lightEnableFrame = new LightSwitchOnFrame(); + } + lightEnableFrame.setLocationRelativeTo(null); // Centre window + lightEnableFrame.setVisible(true); + } + /** * Connect to the last known access point. * This method is triggered by the Connect to Bridge button but it can equally be used to automatically connect to a bridge. diff --git a/JavaDesktopApp/src/com/philips/lighting/gui/DesktopView.java b/JavaDesktopApp/src/com/philips/lighting/gui/DesktopView.java index f325eaf..512e231 100644 --- a/JavaDesktopApp/src/com/philips/lighting/gui/DesktopView.java +++ b/JavaDesktopApp/src/com/philips/lighting/gui/DesktopView.java @@ -31,6 +31,7 @@ public class DesktopView extends JFrame { private Controller controller; private JButton setLightsButton; private JButton randomLightsButton; + private JButton switchOffLightsButton; private JButton findBridgesButton; private JButton connectToLastBridgeButton; private JProgressBar findingBridgeProgressBar; @@ -100,6 +101,16 @@ public void actionPerformed(ActionEvent arg0) { } }); + switchOffLightsButton= new JButton("Switch on/off lights"); + switchOffLightsButton.setEnabled(false); + switchOffLightsButton.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent arg0) { + controller.showControlSwitchOffLightsWindow(); + } + }); + randomLightsButton = new JButton("Randomize Lights"); randomLightsButton.setEnabled(false); randomLightsButton.addActionListener(new ActionListener() { @@ -113,7 +124,7 @@ public void actionPerformed(ActionEvent arg0) { double border = 10; double size[][] = {{border, 160, 20, 300, 20, 160}, // Columns - {border, 26, 10, 26, 26, 26,6,26}}; // Rows + {border, 26, 10, 26, 26, 26,6,26,6,26}}; // Rows mainPanel.setLayout (new TableLayout(size)); @@ -131,6 +142,7 @@ public void actionPerformed(ActionEvent arg0) { mainPanel.add(randomLightsButton, " 5, 5"); mainPanel.add(setLightsButton, " 5, 7"); + mainPanel.add(switchOffLightsButton, " 5, 9"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setPreferredSize(new Dimension(700,270)); @@ -158,7 +170,16 @@ public JButton getRandomLightsButton() { return randomLightsButton; } - public JButton getFindBridgesButton() { + + public JButton getSwitchOffLightsButton() { + return switchOffLightsButton; + } + + public void setSwitchOffLightsButton(JButton switchOffLightsButton) { + this.switchOffLightsButton = switchOffLightsButton; + } + + public JButton getFindBridgesButton() { return findBridgesButton; } diff --git a/JavaDesktopApp/src/com/philips/lighting/gui/LightSwitchOnFrame.java b/JavaDesktopApp/src/com/philips/lighting/gui/LightSwitchOnFrame.java new file mode 100644 index 0000000..792a067 --- /dev/null +++ b/JavaDesktopApp/src/com/philips/lighting/gui/LightSwitchOnFrame.java @@ -0,0 +1,118 @@ +package com.philips.lighting.gui; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Container; +import java.awt.Dimension; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.List; + +import javax.swing.BorderFactory; +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JList; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.border.Border; + +import com.philips.lighting.hue.sdk.PHHueSDK; +import com.philips.lighting.model.PHBridge; +import com.philips.lighting.model.PHLight; +import com.philips.lighting.model.PHLightState; +/** + * LightColoursFrame.java + * An example showing how to change Bulb Colours using a JColorChooser. + * + */ +public class LightSwitchOnFrame extends JFrame { + + private static final long serialVersionUID = -3830092035262367974L; + private PHHueSDK phHueSDK; + + private JList lightIdentifiersList; + private List allLights; + + public LightSwitchOnFrame() { + super("Bulb On/Off Switcher"); + + // The the HueSDK singleton. + phHueSDK = PHHueSDK.getInstance(); + + Container content = getContentPane(); + + // Get the selected bridge. + PHBridge bridge = phHueSDK.getSelectedBridge(); + + // To get lights use the Resource Cache. + allLights = bridge.getResourceCache().getAllLights(); + + DefaultListModel sampleModel = new DefaultListModel(); + + for (PHLight light : allLights) { + sampleModel.addElement(light.getIdentifier() + " " + light.getName() ); + } + + lightIdentifiersList = new JList(sampleModel); + lightIdentifiersList.setVisibleRowCount(4); + lightIdentifiersList.setSelectedIndex(0); + + JScrollPane listPane = new JScrollPane(lightIdentifiersList); + listPane.setPreferredSize(new Dimension(300,100)); + + JPanel listPanel = new JPanel(); + listPanel.setBackground(Color.white); + + Border listPanelBorder = BorderFactory.createTitledBorder("My Lights"); + listPanel.setBorder(listPanelBorder); + listPanel.add(listPane); + content.add(listPanel, BorderLayout.CENTER); + + JButton switchOnButton = new JButton("Switch on bulb"); + switchOnButton.addActionListener(new BulbOffChanger(true)); + JButton switchOffButton = new JButton("Switch off bulb"); + switchOffButton.addActionListener(new BulbOffChanger(false)); + + Border buttonPanelBorder = BorderFactory.createTitledBorder("Change Selected"); + JPanel buttonPanel = new JPanel(); + buttonPanel.setBackground(Color.white); + buttonPanel.setBorder(buttonPanelBorder); + buttonPanel.add(switchOnButton); + buttonPanel.add(switchOffButton); + + content.add(buttonPanel, BorderLayout.SOUTH); + setPreferredSize(new Dimension(400,250)); + pack(); + setVisible(true); + } + + private class BulbOffChanger implements ActionListener { + private boolean switchOn; + + public BulbOffChanger() { + } + + public BulbOffChanger(boolean switchOn) { + this.switchOn = switchOn; + + } + + public void actionPerformed(ActionEvent event) { + + int selectedBulb = lightIdentifiersList.getSelectedIndex(); + if (selectedBulb !=-1) { + int selectedIndex= lightIdentifiersList.getSelectedIndex(); + String lightIdentifer = allLights.get(selectedIndex).getIdentifier(); + + PHLightState lightState = new PHLightState(); + lightState.setOn(switchOn); + + phHueSDK.getSelectedBridge().updateLightState(lightIdentifer, lightState, null); // null is passed here as we are not interested in the response from the Bridge. + + + } + } + } + +} \ No newline at end of file