Crossfire JXClient, Trunk  R20561
BindCommand.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.commands;
23 
30 import org.jetbrains.annotations.NotNull;
31 
36 public class BindCommand extends AbstractCommand {
37 
41  @NotNull
43 
47  @NotNull
49 
56  public BindCommand(@NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final CommandCallback commandCallback, @NotNull final GUICommandFactory guiCommandFactory) {
57  super("bind", crossfireServerConnection);
58  this.commandCallback = commandCallback;
59  this.guiCommandFactory = guiCommandFactory;
60  }
61 
65  @Override
66  public boolean allArguments() {
67  return true;
68  }
69 
73  @Override
74  public void execute(@NotNull final String args) {
75  final String commandList;
76  final boolean perCharacterBinding;
77  if (args.equals("-c")) {
78  perCharacterBinding = true;
79  commandList = "";
80  } else if (args.startsWith("-c ")) {
81  perCharacterBinding = true;
82  commandList = StringUtils.trimLeading(args.substring(3));
83  } else {
84  perCharacterBinding = false;
85  commandList = args;
86  }
87 
88  if (commandList.isEmpty()) {
89  drawInfoError("Which command do you want to bind?");
90  return;
91  }
92 
93  final CommandList commandList2 = new CommandList(CommandListType.AND);
94  commandList2.add(guiCommandFactory.createCommand(commandList));
95  if (!commandCallback.createKeyBinding(perCharacterBinding, commandList2)) {
96  drawInfoError("Cannot use bind -c since no character is logged in.");
97  //noinspection UnnecessaryReturnStatement
98  return;
99  }
100  }
101 
102 }
boolean allArguments()
Returns whether all remaining commands should be included as arguments.whether all remaining commands...
Abstract base class for Command implementations.
Utility class for string manipulation.
void drawInfoError(@NotNull final String message)
Displays an error message.
final CrossfireServerConnection crossfireServerConnection
The connection instance.
boolean createKeyBinding(boolean perCharacter, @NotNull CommandList commandList)
Adds a key binding.
void add(@NotNull final GUICommand guiCommand)
Adds a command to the end of this command list.
GUICommand createCommand(@NotNull final String commandString)
Creates a new GUICommand instance from string representation.
Interface that defines callback functions needed by commands.
Factory for creating GUICommand instances from string representation.
final GUICommandFactory guiCommandFactory
The GUICommandFactory for creating commands.
final CommandCallback commandCallback
The CommandCallback to use.
AND
List is executed if all entries can execute.
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
BindCommand(@NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final CommandCallback commandCallback, @NotNull final GUICommandFactory guiCommandFactory)
Creates a new instance.
static String trimLeading(@NotNull final CharSequence str)
Removes leading whitespace from a string.
void execute(@NotNull final String args)
Executes the command with the given arguments.the command arguments