package class8_1_RemoteControl; import class8_1_RemoteControl.other.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*; /** * The remote control unit which controls the world * @author hornick * */ public class ControlPanelUI extends JFrame { private ControlPanelListener eventListener; // UI event listener private Robot robot; private Car car; private Car car2; private JTextField result; private Map map; private Invoker invoker = new Invoker(); public ControlPanelUI( Car car, Car car2, Robot robot ) { this.robot = robot; this.car = car; this.car2 = car2; // create the containing window and set its size etc setTitle("X10 Controller"); setSize(200, 300); setLocation(400, 400); setResizable(false); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); eventListener = new ControlPanelListener(); // Retrieves the window's contentPane and sets the background color Container contentPane = getContentPane(); contentPane.setBackground(Color.BLACK); JPanel jp = new JPanel(); FlowLayout fl = new FlowLayout(); jp.setLayout(fl); contentPane.add(jp); // create the command buttons ArrayList commands = new ArrayList(); //commands = {"Clean", "Scrub", "Start", "Stop","Start2", "Stop2", "Undo"}; commands.add(new RobotTidyCommand(robot,"Clean")); commands.add(new RobotScrubCommand(robot,"Scrub")); commands.add(car.carStartCommand); commands.add(new CarStopCommand(car,"Stop 1")); commands.add(car2.carStartCommand); commands.add(new CarStopCommand(car2,"Stop 2")); commands.add(new UndoCommand()); map = new HashMap(); for( Command action: commands ) { JButton cb = new JButton(action.getCommandName()); cb.setPreferredSize(new Dimension(150, 25)); cb.setActionCommand(action.getCommandName()); jp.add(cb); cb.addActionListener(eventListener); map.put(action.getCommandName(), action); } result = new JTextField(""); result.setPreferredSize(new Dimension(200, 25)); jp.add(result); contentPane.validate(); contentPane.repaint(); } /** * handle the invocation of various commands * @param action The action command / text on the button that was pressed */ private void handleCommand( String action ) { Command command = map.get(action); boolean status; status = invoker.invoke(command); if(status) { result.setText("command OK"); } else { result.setText("command failed"); } } /** * This is a nested inner class that handles events * for the ControlPanel * @author hornick */ private class ControlPanelListener implements ActionListener { int myThing; /** * Constructor; does nothing useful */ private ControlPanelListener() { } /** * ActionListener event handler for this class * @param event - the ActionEvent object that caused the event */ public void actionPerformed(ActionEvent event) { String action = event.getActionCommand(); handleCommand(action); } } // end of ControlPanel inner class }