package class8_Lab_RemoteControl_Invoker; import class8_Lab_RemoteControl_Invoker.other.Command; import class8_Lab_RemoteControl_Invoker.other.Invoker; 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, yoder * */ 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.add(robot.getTidyCommand()); commands.add(robot.getScrubCommand()); commands.add(car.getStartCommand()); commands.add(car.getStopCommand()); commands.add(car2.getStartCommand()); commands.add(car2.getStopCommand()); commands.add(invoker.getUndoCommand()); 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); if(invoker.invoke(command)) { 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 }