Crossfire JXClient, Trunk
ShortcutCommand.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.commands;
24 
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32 
38 public class ShortcutCommand extends AbstractCommand {
39 
43  @NotNull
44  private final Shortcuts shortcuts;
45 
53  super("shortcut", crossfireServerConnection);
54  this.shortcuts = shortcuts;
55  }
56 
57  @Override
58  public boolean allArguments() {
59  return true;
60  }
61 
62  @Override
63  public void execute(@NotNull final String args) {
64  if (args.isEmpty()) {
65  listSlots();
66  } else if (args.startsWith("-d ")) {
67  deleteSlot(StringUtils.trimLeading(args.substring(3)));
68  } else {
69  updateSlot(args);
70  }
71  }
72 
76  private void listSlots() {
77  for (final ShortcutSlot shortcutSlot : ShortcutSlot.values()) {
78  final Shortcut shortcut = shortcuts.getShortcut(shortcutSlot);
79  final String text = shortcut == null ? "(empty)" : shortcut.getCommand();
80  drawInfo(shortcutSlot+": "+text);
81  }
82  }
83 
88  private void deleteSlot(@NotNull final String args) {
89  final ShortcutSlot shortcutSlot = parseKey(args);
90  if (shortcutSlot != null) {
91  shortcuts.setShortcutString(shortcutSlot, "", true);
92  }
93  }
94 
99  private void updateSlot(@NotNull final String args) {
100  final String[] tmp = args.split(" ", 2);
101  final ShortcutSlot shortcutSlot = parseKey(tmp[0]);
102  if (shortcutSlot != null) {
103  shortcuts.setShortcutString(shortcutSlot, tmp[1], true);
104  }
105  }
106 
107  @NotNull
108  @Override
109  public String getHelp() {
110  //noinspection StringBufferReplaceableByString
111  final StringBuilder sb = new StringBuilder();
112  sb.append("Updates the shortcut bar\n");
113  sb.append("\n");
114  sb.append("Usage: shortcut\n");
115  sb.append("Usage: shortcut <key> <command>\n");
116  sb.append("Usage: shortcut -d <key>\n");
117  sb.append("\n");
118  sb.append("Without argument shows the contents of the shortcut bar. ");
119  sb.append("Otherwise replaces the slot for <key> with <command>. ");
120  sb.append("<key> may be F1, F2, ..., F12. Other keys are not supported.\n");
121  sb.append("-d removes the command from the given slot.\n");
122  return sb.toString();
123  }
124 
130  @Nullable
131  private ShortcutSlot parseKey(@NotNull final String args) {
132  try {
133  return ShortcutSlot.valueOf(args);
134  } catch (final IllegalArgumentException ignored) {
135  drawInfoError("unknown key '"+args+"'");
136  return null;
137  }
138  }
139 
140 }
com.realtime.crossfire.jxclient.shortcuts.Shortcuts.getShortcut
Shortcut getShortcut(@NotNull final ShortcutSlot shortcutSlot)
Definition: Shortcuts.java:133
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.shortcuts.Shortcuts
Definition: Shortcuts.java:43
com.realtime.crossfire.jxclient.server
com.realtime.crossfire.jxclient.util.StringUtils.trimLeading
static String trimLeading(@NotNull final CharSequence str)
Definition: StringUtils.java:54
com.realtime.crossfire.jxclient.shortcuts.Shortcuts.setShortcutString
void setShortcutString(@NotNull final ShortcutSlot shortcutSlot, @NotNull final String command, final boolean saveChanges)
Definition: Shortcuts.java:143
com.realtime.crossfire.jxclient.commands.ShortcutCommand
Definition: ShortcutCommand.java:38
com.realtime.crossfire.jxclient.commands.ShortcutCommand.listSlots
void listSlots()
Definition: ShortcutCommand.java:76
com.realtime.crossfire.jxclient.commands.ShortcutCommand.ShortcutCommand
ShortcutCommand(@NotNull final Shortcuts shortcuts, @NotNull final CrossfireServerConnection crossfireServerConnection)
Definition: ShortcutCommand.java:52
com.realtime.crossfire.jxclient.commands.AbstractCommand.crossfireServerConnection
final CrossfireServerConnection crossfireServerConnection
Definition: AbstractCommand.java:45
com.realtime.crossfire.jxclient.commands.AbstractCommand.drawInfoError
void drawInfoError(@NotNull final String message)
Definition: AbstractCommand.java:69
com.realtime.crossfire.jxclient.commands.ShortcutCommand.shortcuts
final Shortcuts shortcuts
Definition: ShortcutCommand.java:44
com.realtime.crossfire.jxclient.commands.ShortcutCommand.getHelp
String getHelp()
Definition: ShortcutCommand.java:109
com.realtime.crossfire.jxclient.commands.ShortcutCommand.updateSlot
void updateSlot(@NotNull final String args)
Definition: ShortcutCommand.java:99
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection
Definition: CrossfireServerConnection.java:37
com.realtime.crossfire.jxclient.util.StringUtils
Definition: StringUtils.java:34
com.realtime.crossfire.jxclient.util
Definition: Codec.java:23
com.realtime.crossfire.jxclient.server.crossfire
Definition: AbstractCrossfireServerConnection.java:23
com.realtime.crossfire.jxclient.shortcuts
Definition: Shortcut.java:23
com.realtime.crossfire
com.realtime.crossfire.jxclient.shortcuts.Shortcut.getCommand
String getCommand()
Definition: Shortcut.java:76
com.realtime.crossfire.jxclient.commands.AbstractCommand.drawInfo
void drawInfo(@NotNull final String message)
Definition: AbstractCommand.java:61
com.realtime
com.realtime.crossfire.jxclient.shortcuts.ShortcutSlot
Definition: ShortcutSlot.java:8
com
com.realtime.crossfire.jxclient.shortcuts.Shortcut
Definition: Shortcut.java:35
com.realtime.crossfire.jxclient.commands.ShortcutCommand.allArguments
boolean allArguments()
Definition: ShortcutCommand.java:58
com.realtime.crossfire.jxclient.commands.AbstractCommand
Definition: AbstractCommand.java:33
com.realtime.crossfire.jxclient.commands.ShortcutCommand.parseKey
ShortcutSlot parseKey(@NotNull final String args)
Definition: ShortcutCommand.java:131
com.realtime.crossfire.jxclient.commands.ShortcutCommand.deleteSlot
void deleteSlot(@NotNull final String args)
Definition: ShortcutCommand.java:88
com.realtime.crossfire.jxclient.commands.ShortcutCommand.execute
void execute(@NotNull final String args)
Definition: ShortcutCommand.java:63