Crossfire JXClient, Trunk  R20561
GUICommandFactoryImpl.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.gui.textinput;
23 
28 import java.util.regex.Pattern;
29 import org.jetbrains.annotations.NotNull;
30 
36 public class GUICommandFactoryImpl implements GUICommandFactory {
37 
41  @NotNull
42  private static final Pattern PATTERN_ENCODE = Pattern.compile(".*[- \t]$");
43 
47  @NotNull
48  private static final String TRAILING_ESCAPE = "-";
49 
53  @NotNull
55 
59  @NotNull
61 
65  @NotNull
66  private final Macros macros;
67 
74  public GUICommandFactoryImpl(@NotNull final CommandCallback commandCallback, @NotNull final CommandExecutor commandExecutor, @NotNull final Macros macros) {
75  this.commandCallback = commandCallback;
76  this.commandExecutor = commandExecutor;
77  this.macros = macros;
78  }
79 
85  @Override
86  public GUICommand createCommandDecode(@NotNull final String encodedCommandString) {
87  return createCommand(decode(encodedCommandString));
88  }
89 
95  @NotNull
96  @Override
97  public GUICommand createCommand(@NotNull final String commandString) {
98  if (commandString.equals("-e")) {
99  return new ActivateCommandInputCommand("", commandCallback, macros);
100  }
101  if (commandString.startsWith("-e ")) {
102  return new ActivateCommandInputCommand(StringUtils.trimLeading(commandString.substring(3)), commandCallback, macros);
103  }
104  return new ExecuteCommandCommand(commandExecutor, commandString, macros);
105  }
106 
112  @NotNull
113  @Override
114  public String encode(@NotNull final String command) {
115  return PATTERN_ENCODE.matcher(command).matches() ? command+TRAILING_ESCAPE : command;
116  }
117 
123  @NotNull
124  @Override
125  public String decode(@NotNull final String command) {
126  return command.endsWith(TRAILING_ESCAPE) ? command.substring(0, command.length()-TRAILING_ESCAPE.length()) : command;
127  }
128 
129 }
static final Pattern PATTERN_ENCODE
Pattern matching lines that need a TRAILING_ESCAPE appended.
Utility class for string manipulation.
String decode(@NotNull final String command)
Decodes a key binding if necessary.
String encode(@NotNull final String command)
Encodes a key binding if necessary.
Manages macro expansion in command strings.
Definition: Macros.java:37
final CommandExecutor commandExecutor
The CommandExecutor instance to use.
static final String TRAILING_ESCAPE
Character appended to lines ending with whitespace.
GUICommand createCommandDecode(@NotNull final String encodedCommandString)
Creates a new GUICommand instance from string representation.
Interface that defines callback functions needed by commands.
final CommandCallback commandCallback
The CommandCallback to use.
Factory for creating GUICommand instances from string representation.
Factory for creating GUICommand instances from string representation.
GUICommandFactoryImpl(@NotNull final CommandCallback commandCallback, @NotNull final CommandExecutor commandExecutor, @NotNull final Macros macros)
Creates a new instance.
GUICommand createCommand(@NotNull final String commandString)
Creates a new GUICommand instance from string representation.
static String trimLeading(@NotNull final CharSequence str)
Removes leading whitespace from a string.