package class8_1_RemoteControl_AnonInner; import class8_1_RemoteControl_AnonInner.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, 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 Deque stack = new LinkedList(); 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(this.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(); } /** * This method creates a command to undo things. * This command actually manipulates the internal * stacks of this class as needed to perform * the operation. * @return the undo command. */ private Command getUndoCommand() { return new Command() { @Override public boolean execute() { boolean status; Command lastCommand = stack.pollFirst(); if(null != lastCommand) { status = lastCommand.unexecute(); } else { status = false; } return false; // dilemma: If we return true, (e.g. status) // THIS gets pushed onto the UNDO stack // If we return "false", the command "fails" } @Override public boolean unexecute() { return false; //To change body of implemented methods use File | Settings | File Templates. } @Override public String getCommandName() { return "Undo"; } }; } /** * 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(null == command) { System.out.println("Errro!!!!"); } boolean status; status = command.execute(); if(status) { stack.push(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 }