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.textinput;
00023
00024 import com.realtime.crossfire.jxclient.gui.commandlist.GUICommand;
00025 import com.realtime.crossfire.jxclient.settings.Macros;
00026 import com.realtime.crossfire.jxclient.util.StringUtils;
00027 import java.util.regex.Pattern;
00028 import org.jetbrains.annotations.NotNull;
00029
00035 public class GUICommandFactory {
00036
00040 @NotNull
00041 private static final Pattern PATTERN_ENCODE = Pattern.compile(".*[- \t]$");
00042
00046 @NotNull
00047 private static final String TRAILING_ESCAPE = "-";
00048
00052 @NotNull
00053 private final CommandCallback commandCallback;
00054
00058 @NotNull
00059 private final CommandExecutor commandExecutor;
00060
00064 @NotNull
00065 private final Macros macros;
00066
00073 public GUICommandFactory(@NotNull final CommandCallback commandCallback, @NotNull final CommandExecutor commandExecutor, @NotNull final Macros macros) {
00074 this.commandCallback = commandCallback;
00075 this.commandExecutor = commandExecutor;
00076 this.macros = macros;
00077 }
00078
00084 public GUICommand createCommandDecode(@NotNull final String encodedCommandString) {
00085 return createCommand(decode(encodedCommandString));
00086 }
00087
00093 @NotNull
00094 public GUICommand createCommand(@NotNull final String commandString) {
00095 if (commandString.equals("-e")) {
00096 return new ActivateCommandInputCommand("", commandCallback, macros);
00097 } else if (commandString.startsWith("-e ")) {
00098 return new ActivateCommandInputCommand(StringUtils.trimLeading(commandString.substring(3)), commandCallback, macros);
00099 } else {
00100 return new ExecuteCommandCommand(commandExecutor, commandString, macros);
00101 }
00102 }
00103
00109 @NotNull
00110 public static String encode(@NotNull final String command) {
00111 return PATTERN_ENCODE.matcher(command).matches() ? command+TRAILING_ESCAPE : command;
00112 }
00113
00119 @NotNull
00120 private static String decode(@NotNull final String command) {
00121 return command.endsWith(TRAILING_ESCAPE) ? command.substring(0, command.length()-TRAILING_ESCAPE.length()) : command;
00122 }
00123
00124 }