Crossfire JXClient, Trunk  R20561
SetCommand.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 
29 import org.jetbrains.annotations.NotNull;
30 
35 public class SetCommand extends AbstractCommand {
36 
40  @NotNull
42 
48  public SetCommand(@NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final OptionManager optionManager) {
49  super("set", crossfireServerConnection);
50  this.optionManager = optionManager;
51  }
52 
56  @Override
57  public boolean allArguments() {
58  return false;
59  }
60 
64  @Override
65  public void execute(@NotNull final String args) {
66  final String[] tmp = Patterns.PATTERN_WHITESPACE.split(args, 2);
67  if (tmp.length != 2) {
68  drawInfoError("The set command needs two arguments: set <option> <value>");
69  return;
70  }
71 
72  final String optionName = tmp[0];
73  final String optionArgs = tmp[1];
74  final CheckBoxOption option;
75  try {
76  option = optionManager.getCheckBoxOption(optionName);
77  } catch (final OptionException ex) {
78  drawInfoError(ex.getMessage());
79  return;
80  }
81 
82  final boolean checked;
83  switch (optionArgs) {
84  case "on":
85  checked = true;
86  break;
87 
88  case "off":
89  checked = false;
90  break;
91 
92  default:
93  drawInfoError("The '"+optionArgs+"' for option '"+optionName+"' is invalid. Valid arguments are 'on' or 'off'.");
94  return;
95  }
96 
97  option.setChecked(checked);
98  }
99 
100 }
boolean allArguments()
Returns whether all remaining commands should be included as arguments.whether all remaining commands...
Definition: SetCommand.java:57
Abstract base class for Command implementations.
SetCommand(@NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final OptionManager optionManager)
Creates a new instance.
Definition: SetCommand.java:48
void setChecked(final boolean checked)
Sets the current state.
void drawInfoError(@NotNull final String message)
Displays an error message.
final CrossfireServerConnection crossfireServerConnection
The connection instance.
CheckBoxOption getCheckBoxOption(@NotNull final String optionName)
Returns a check box option.
void execute(@NotNull final String args)
Executes the command with the given arguments.the command arguments
Definition: SetCommand.java:65
static final Pattern PATTERN_WHITESPACE
The pattern to split a command from arguments, and to split arguments.
Definition: Patterns.java:37
Utility class containing useful Patterns.
Definition: Patterns.java:31
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
final OptionManager optionManager
The option manager instance.
Definition: SetCommand.java:41