00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.gui.commandlist;
00023
00024 import java.util.ArrayList;
00025 import java.util.Collection;
00026 import org.jetbrains.annotations.NotNull;
00027
00032 public class CommandList {
00033
00037 @NotNull
00038 private final CommandListType commandListType;
00039
00043 @NotNull
00044 private final Collection<GUICommand> commandList = new ArrayList<GUICommand>();
00045
00050 public CommandList(@NotNull final CommandListType commandListType) {
00051 this.commandListType = commandListType;
00052 }
00053
00058 public void add(@NotNull final GUICommand guiCommand) {
00059 commandList.add(guiCommand);
00060 }
00061
00066 private boolean canExecute() {
00067 switch (commandListType) {
00068 case AND:
00069 for (final GUICommand command : commandList) {
00070 if (!command.canExecute()) {
00071 return false;
00072 }
00073 }
00074 break;
00075
00076 case OR:
00077 boolean ok = false;
00078 for (final GUICommand command : commandList) {
00079 if (command.canExecute()) {
00080 ok = true;
00081 break;
00082 }
00083 }
00084 if (!ok) {
00085 return false;
00086 }
00087 break;
00088 }
00089
00090 return true;
00091 }
00092
00097 public void execute() {
00098 if (!canExecute()) {
00099 return;
00100 }
00101
00102 for (final GUICommand command : commandList) {
00103 command.execute();
00104 }
00105 }
00106
00111 @NotNull
00112 public String getCommandString() {
00113 final StringBuilder sb = new StringBuilder();
00114 boolean firstCommand = true;
00115 for (final GUICommand guiCommand : commandList) {
00116 if (!(guiCommand instanceof GUICommand2)) {
00117 throw new AssertionError("Cannot encode command of type "+guiCommand.getClass().getName());
00118 }
00119 final String commandString = ((GUICommand2)guiCommand).getCommandString();
00120 if (firstCommand) {
00121 firstCommand = false;
00122 } else {
00123 sb.append(';');
00124 }
00125 sb.append(commandString);
00126 }
00127 return sb.toString();
00128 }
00129
00130 }