import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.util.Collections; import java.util.Date; import java.util.List; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.UIManager; import javax.swing.border.TitledBorder; public class X10AHP extends JFrame { /** * */ private static final long serialVersionUID = 1L; static X10Interface dev; private JPanel rootPanel = null; private JPanel writePanel = null; private JPanel logPanel = null; private JScrollPane readLogScrollPane = null; private JButton writeButton = null; private JButton clearButton = null; private JButton pauseButton = null; private JButton statusButton = null; private JTextArea readLogTextArea = null; private JComboBox houseCodeComboBox = null; private JComboBox deviceCodeComboBox = null; private JComboBox functionCodeComboBox = null; private boolean pause = false; public X10AHP() { super(); dev = new X10Interface(); dev.openUsbDevice(); initialize(); } private void initialize() { this.setTitle("X10 AHP JAVA"); this.setContentPane(getRootPanel()); this.pack(); this.setSize(600, 500); this.setVisible(true); } public void runReader() { String readData = ""; List commandQueue; while(true) { commandQueue = dev.getCommandQueue(); if(!commandQueue.isEmpty()) { dev.write(X10Interface.parseByteArray(commandQueue.get(0))); dev.popCommandQueue(); } readData = X10Interface.translateData(dev.read()); if(readData.length() > 0 && !pause) { addtoLogTextArea(readData + "\n"); } } } private JPanel getRootPanel() { if (rootPanel == null) { rootPanel = new JPanel(); rootPanel.setLayout(new BoxLayout(getRootPanel(), BoxLayout.Y_AXIS)); rootPanel.add(getWritePanel(),null); rootPanel.add(getLogPanel(),null); } return rootPanel; } private JPanel getWritePanel() { if (writePanel == null) { FlowLayout flowLayout = new FlowLayout(); flowLayout.setVgap(1); flowLayout.setAlignment(FlowLayout.LEFT); writePanel = new JPanel(); writePanel.setLayout(flowLayout); writePanel.setBorder(BorderFactory.createTitledBorder(null,"X10 Command", TitledBorder.DEFAULT_JUSTIFICATION,TitledBorder.DEFAULT_POSITION, new Font("Dialog",Font.BOLD,12), new Color(51, 51, 51))); writePanel.add(getWriteButton(),null); writePanel.add(getHouseCodeComboBox(), null); writePanel.add(getDeviceCodeComboBox(), null); writePanel.add(getFunctionCodeComboBox(), null); writePanel.add(getStatusButton(),null); } return writePanel; } private JPanel getLogPanel() { if (logPanel == null) { logPanel = new JPanel(); logPanel.setLayout(new BoxLayout(logPanel,BoxLayout.Y_AXIS)); logPanel.setBorder(BorderFactory.createTitledBorder(null,"Command Log", TitledBorder.DEFAULT_JUSTIFICATION,TitledBorder.DEFAULT_POSITION, new Font("Dialog",Font.BOLD,12), new Color(51, 51, 51))); logPanel.add(getClearButton(),null); logPanel.add(getPauseButton(),null); logPanel.add(getReadLogScrollPane(), null); } return logPanel; } private JScrollPane getReadLogScrollPane() { readLogTextArea = new JTextArea(); readLogTextArea.setSize(180,390); readLogScrollPane = new JScrollPane(readLogTextArea); readLogScrollPane.setPreferredSize(new Dimension(190,400)); return readLogScrollPane; } private JButton getWriteButton() { if (writeButton == null) { writeButton = new JButton(); writeButton.setText("Send Command"); writeButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { addtoLogTextArea(dev.sendCommand(houseCodeComboBox.getSelectedItem().toString(),Integer.parseInt(deviceCodeComboBox.getSelectedItem().toString()),functionCodeComboBox.getSelectedItem().toString())); } }); } return writeButton; } private JButton getStatusButton() { if (statusButton == null) { statusButton = new JButton(); statusButton.setText("cm15a Status"); statusButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { addtoLogTextArea(dev.statusCommand()); } }); } return statusButton; } private JButton getClearButton() { if (clearButton == null) { clearButton = new JButton(); clearButton.setText("Clear Log"); clearButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { readLogTextArea.setText(""); } }); } return clearButton; } private JButton getPauseButton() { if (pauseButton == null) { pauseButton = new JButton(); pauseButton.setText("Pause"); pauseButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { if(pause) { pause = false; pauseButton.setText("Pause"); } else { pause = true; pauseButton.setText("Restart"); } } }); } return pauseButton; } private JComboBox getHouseCodeComboBox() { List houseCodes = X10Interface.getHouseCodes(); // Collections.sort(houseCodes); int pos = 0; if( houseCodeComboBox == null) { houseCodeComboBox = new JComboBox(); while (pos < houseCodes.size()) { houseCodeComboBox.addItem(houseCodes.get(pos)); pos++; } } return houseCodeComboBox; } private JComboBox getDeviceCodeComboBox() { List deviceCodes = X10Interface.getDeviceCodes(); // Collections.sort(deviceCodes); int pos = 0; if( deviceCodeComboBox == null) { deviceCodeComboBox = new JComboBox(); while (pos < deviceCodes.size()) { deviceCodeComboBox.addItem(deviceCodes.get(pos)); pos++; } } return deviceCodeComboBox; } private JComboBox getFunctionCodeComboBox() { List functionCodes = X10Interface.getFunctionCodes(); int pos = 0; if( functionCodeComboBox == null) { functionCodeComboBox = new JComboBox(); while (pos < functionCodes.size()) { functionCodeComboBox.addItem(functionCodes.get(pos)); pos++; } } return functionCodeComboBox; } private void addtoLogTextArea(String theString) { Date date = new Date(); int pos = 0; String[] data = theString.split("\n"); while (pos < data.length) { readLogTextArea.insert("[" + date.toString() + "] " + data[pos] + "\n",0); pos++; } } public static void main(String[] args) { // set LookAndFeel try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } X10AHP app = new X10AHP(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.runReader(); } }