001package headfirst.command.party;
002
003//
004// This is the invoker
005//
006public class RemoteControl {
007        Command[] onCommands;
008        Command[] offCommands;
009        Command undoCommand;
010
011        public RemoteControl() {
012                onCommands = new Command[7];
013                offCommands = new Command[7];
014
015                Command noCommand = new NoCommand();
016                for(int i=0;i<7;i++) {
017                        onCommands[i] = noCommand;
018                        offCommands[i] = noCommand;
019                }
020                undoCommand = noCommand;
021        }
022
023        public void setCommand(int slot, Command onCommand, Command offCommand) {
024                onCommands[slot] = onCommand;
025                offCommands[slot] = offCommand;
026        }
027
028        public void onButtonWasPushed(int slot) {
029                onCommands[slot].execute();
030                undoCommand = onCommands[slot];
031        }
032
033        public void offButtonWasPushed(int slot) {
034                offCommands[slot].execute();
035                undoCommand = offCommands[slot];
036        }
037
038        public void undoButtonWasPushed() {
039                undoCommand.undo();
040        }
041
042        public String toString() {
043                StringBuilder stringBuff = new StringBuilder();
044                stringBuff.append("\n------ Remote Control -------\n");
045                for (int i = 0; i < onCommands.length; i++) {
046                        stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName()
047                                        + "    " + offCommands[i].getClass().getName() + "\n");
048                }
049                stringBuff.append("[undo] " + undoCommand.getClass().getName() + "\n");
050                return stringBuff.toString();
051        }
052}