Crossfire JXClient, Trunk
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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.gui.textinput;
24 
29 import java.util.regex.Pattern;
30 import org.jetbrains.annotations.NotNull;
31 
37 public class GUICommandFactoryImpl implements GUICommandFactory {
38 
42  @NotNull
43  private static final Pattern PATTERN_ENCODE = Pattern.compile(".*[- \t]$");
44 
48  @NotNull
49  private static final String TRAILING_ESCAPE = "-";
50 
54  @NotNull
56 
60  @NotNull
62 
66  @NotNull
67  private final Macros macros;
68 
75  public GUICommandFactoryImpl(@NotNull final CommandCallback commandCallback, @NotNull final CommandExecutor commandExecutor, @NotNull final Macros macros) {
76  this.commandCallback = commandCallback;
77  this.commandExecutor = commandExecutor;
78  this.macros = macros;
79  }
80 
86  @Override
87  public GUICommand createCommandDecode(@NotNull final String encodedCommandString) {
88  return createCommand(decode(encodedCommandString));
89  }
90 
96  @NotNull
97  @Override
98  public GUICommand createCommand(@NotNull final String commandString) {
99  if (commandString.equals("-e")) {
101  }
102  if (commandString.startsWith("-e ")) {
103  return new ActivateCommandInputCommand(StringUtils.trimLeading(commandString.substring(3)), commandCallback, macros);
104  }
105  return new ExecuteCommandCommand(commandExecutor, commandString, macros);
106  }
107 
113  @NotNull
114  @Override
115  public String encode(@NotNull final String command) {
116  return PATTERN_ENCODE.matcher(command).matches() ? command+TRAILING_ESCAPE : command;
117  }
118 
124  @NotNull
125  @Override
126  public String decode(@NotNull final String command) {
127  return command.endsWith(TRAILING_ESCAPE) ? command.substring(0, command.length()-TRAILING_ESCAPE.length()) : command;
128  }
129 
130 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.gui.textinput.GUICommandFactoryImpl
Definition: GUICommandFactoryImpl.java:37
com.realtime.crossfire.jxclient.gui.textinput.GUICommandFactoryImpl.TRAILING_ESCAPE
static final String TRAILING_ESCAPE
Definition: GUICommandFactoryImpl.java:49
com.realtime.crossfire.jxclient.gui.textinput.GUICommandFactoryImpl.commandExecutor
final CommandExecutor commandExecutor
Definition: GUICommandFactoryImpl.java:61
com.realtime.crossfire.jxclient.util.StringUtils.trimLeading
static String trimLeading(@NotNull final CharSequence str)
Definition: StringUtils.java:54
com.realtime.crossfire.jxclient.gui.textinput.GUICommandFactoryImpl.decode
String decode(@NotNull final String command)
Definition: GUICommandFactoryImpl.java:126
com.realtime.crossfire.jxclient.gui.textinput.ExecuteCommandCommand
Definition: ExecuteCommandCommand.java:34
com.realtime.crossfire.jxclient.gui.commandlist
Definition: CommandList.java:23
com.realtime.crossfire.jxclient.gui.textinput.GUICommandFactoryImpl.macros
final Macros macros
Definition: GUICommandFactoryImpl.java:67
com.realtime.crossfire.jxclient.gui.textinput.CommandCallback
Definition: CommandCallback.java:33
com.realtime.crossfire.jxclient.settings
Definition: CommandHistory.java:23
com.realtime.crossfire.jxclient.gui.textinput.GUICommandFactoryImpl.commandCallback
final CommandCallback commandCallback
Definition: GUICommandFactoryImpl.java:55
com.realtime.crossfire.jxclient.gui.textinput.GUICommandFactoryImpl.createCommandDecode
GUICommand createCommandDecode(@NotNull final String encodedCommandString)
Definition: GUICommandFactoryImpl.java:87
com.realtime.crossfire.jxclient.gui.commandlist.GUICommandFactory
Definition: GUICommandFactory.java:32
com.realtime.crossfire.jxclient.settings.Macros
Definition: Macros.java:38
com.realtime.crossfire.jxclient.util.StringUtils
Definition: StringUtils.java:34
com.realtime.crossfire.jxclient.gui
com.realtime.crossfire.jxclient.gui.textinput.GUICommandFactoryImpl.GUICommandFactoryImpl
GUICommandFactoryImpl(@NotNull final CommandCallback commandCallback, @NotNull final CommandExecutor commandExecutor, @NotNull final Macros macros)
Definition: GUICommandFactoryImpl.java:75
com.realtime.crossfire.jxclient.gui.textinput.GUICommandFactoryImpl.createCommand
GUICommand createCommand(@NotNull final String commandString)
Definition: GUICommandFactoryImpl.java:98
com.realtime.crossfire.jxclient.util
Definition: Codec.java:23
com.realtime.crossfire.jxclient.gui.textinput.CommandExecutor
Definition: CommandExecutor.java:31
com.realtime.crossfire.jxclient.gui.textinput.GUICommandFactoryImpl.encode
String encode(@NotNull final String command)
Definition: GUICommandFactoryImpl.java:115
com.realtime.crossfire
com.realtime
com
com.realtime.crossfire.jxclient.gui.textinput.GUICommandFactoryImpl.PATTERN_ENCODE
static final Pattern PATTERN_ENCODE
Definition: GUICommandFactoryImpl.java:43
com.realtime.crossfire.jxclient.gui.textinput.ActivateCommandInputCommand
Definition: ActivateCommandInputCommand.java:34
com.realtime.crossfire.jxclient.gui.commandlist.GUICommand
Definition: GUICommand.java:29