Crossfire JXClient, Trunk
Shortcuts.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.shortcuts;
24 
30 import java.nio.file.Path;
31 import java.util.Collections;
32 import java.util.EnumMap;
33 import java.util.Iterator;
34 import java.util.Map;
35 import java.util.Map.Entry;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
38 
43 public class Shortcuts implements Iterable<Shortcut> {
44 
49  @NotNull
50  private final Map<ShortcutSlot, Shortcut> shortcuts = new EnumMap<>(ShortcutSlot.class);
51 
55  @Nullable
56  private Path file;
57 
61  @NotNull
63 
67  @NotNull
69 
73  @NotNull
74  @SuppressWarnings("FieldCanBeLocal")
76 
77  @Override
78  public void spellAdded(final int index) {
79  for (final Entry<ShortcutSlot, Shortcut> e : shortcuts.entrySet()) {
80  final Shortcut shortcut = e.getValue();
81  if (shortcut != null) {
82  setShortcutString(e.getKey(), shortcut.getCommand(), false);
83  }
84  }
85  }
86 
87  @Override
88  public void spellRemoved(final int index) {
89  for (final Entry<ShortcutSlot, Shortcut> e : shortcuts.entrySet()) {
90  final Shortcut shortcut = e.getValue();
91  if (shortcut != null) {
92  setShortcutString(e.getKey(), shortcut.getCommand(), false);
93  }
94  }
95  }
96 
97  };
98 
103  public Shortcuts(@NotNull final SpellsManager spellsManager) {
104  this.spellsManager = spellsManager;
105  for (final ShortcutSlot shortcutSlot : ShortcutSlot.values()) {
106  shortcuts.put(shortcutSlot, null);
107  }
108  this.spellsManager.addCrossfireSpellChangedListener(spellsManagerListener);
109  }
110 
114  public void clearShortcuts() {
115  for (final ShortcutSlot shortcutSlot : ShortcutSlot.values()) {
116  final Shortcut shortcut = shortcuts.get(shortcutSlot);
117  if (shortcut != null) {
118  for (ShortcutsListener listener : listeners) {
119  listener.shortcutRemoved(shortcutSlot, shortcut);
120  }
121  shortcut.dispose();
122  shortcuts.put(shortcutSlot, null);
123  }
124  }
125  }
126 
132  @Nullable
133  public Shortcut getShortcut(@NotNull final ShortcutSlot shortcutSlot) {
134  return shortcuts.get(shortcutSlot);
135  }
136 
143  public void setShortcutString(@NotNull final ShortcutSlot shortcutSlot, @NotNull final String command, final boolean saveChanges) {
144  final String command2 = command.trim();
145  @Nullable final Spell spell;
146  if (command2.isEmpty()) {
147  spell = null;
148  } else if (command2.startsWith("cast ")) {
149  spell = spellsManager.getSpell(command2.substring(5).trim());
150  } else if (command2.startsWith("invoke ")) {
151  spell = spellsManager.getSpell(command2.substring(7).trim());
152  } else {
153  spell = null;
154  }
155 
156  final Shortcut oldShortcut = shortcuts.get(shortcutSlot);
157  if (oldShortcut != null) {
158  for (ShortcutsListener listener : listeners) {
159  listener.shortcutRemoved(shortcutSlot, oldShortcut);
160  }
161  oldShortcut.dispose();
162  }
163  final Shortcut newShortcut = command2.isEmpty() ? null : new Shortcut(command2, spell);
164  shortcuts.put(shortcutSlot, newShortcut);
165  if (newShortcut != null) {
166  for (ShortcutsListener listener : listeners) {
167  listener.shortcutAdded(shortcutSlot, newShortcut);
168  }
169  }
170 
171  if (saveChanges && file != null) {
172  ShortcutsLoader.saveShortcuts(file, Collections.unmodifiableMap(shortcuts).values());
173  }
174  }
175 
180  public void addShortcutsListener(@NotNull final ShortcutsListener listener) {
181  listeners.add(listener);
182  }
183 
188  public void removeShortcutsListener(@NotNull final ShortcutsListener listener) {
189  listeners.remove(listener);
190  }
191 
196  @Nullable
197  public Path getFile() {
198  return file;
199  }
200 
205  public void setFile(@Nullable final Path file) {
206  this.file = file;
207  }
208 
209  @NotNull
210  @Override
211  public Iterator<Shortcut> iterator() {
212  return shortcuts.values().iterator();
213  }
214 
215 }
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.setFile
void setFile(@Nullable final Path file)
Definition: Shortcuts.java:205
com.realtime.crossfire.jxclient.shortcuts.Shortcuts.clearShortcuts
void clearShortcuts()
Definition: Shortcuts.java:114
com.realtime.crossfire.jxclient.shortcuts.Shortcuts
Definition: Shortcuts.java:43
com.realtime.crossfire.jxclient.shortcuts.Shortcuts.shortcuts
final Map< ShortcutSlot, Shortcut > shortcuts
Definition: Shortcuts.java:50
com.realtime.crossfire.jxclient.window
Definition: DialogStateParser.java:23
com.realtime.crossfire.jxclient.shortcuts.Shortcuts.spellsManagerListener
final SpellsManagerListener spellsManagerListener
Definition: Shortcuts.java:75
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.util.EventListenerList2
Definition: EventListenerList2.java:37
com.realtime.crossfire.jxclient.window.ShortcutsLoader.saveShortcuts
static void saveShortcuts(@NotNull final Path file, @NotNull final Iterable< Shortcut > shortcuts)
Definition: ShortcutsLoader.java:90
com.realtime.crossfire.jxclient.shortcuts.Shortcuts.addShortcutsListener
void addShortcutsListener(@NotNull final ShortcutsListener listener)
Definition: Shortcuts.java:180
com.realtime.crossfire.jxclient.shortcuts.Shortcuts.removeShortcutsListener
void removeShortcutsListener(@NotNull final ShortcutsListener listener)
Definition: Shortcuts.java:188
com.realtime.crossfire.jxclient.spells.Spell
Definition: Spell.java:37
com.realtime.crossfire.jxclient.spells.SpellsManager
Definition: SpellsManager.java:50
com.realtime.crossfire.jxclient.shortcuts.Shortcuts.file
Path file
Definition: Shortcuts.java:56
com.realtime.crossfire.jxclient.util
Definition: Codec.java:23
com.realtime.crossfire.jxclient.window.ShortcutsLoader
Definition: ShortcutsLoader.java:42
com.realtime.crossfire.jxclient.shortcuts.Shortcuts.iterator
Iterator< Shortcut > iterator()
Definition: Shortcuts.java:211
com.realtime.crossfire.jxclient.spells.SpellsManager.getSpell
Spell getSpell(@NotNull final String spellName)
Definition: SpellsManager.java:307
com.realtime.crossfire.jxclient.shortcuts.ShortcutsListener
Definition: ShortcutsListener.java:32
com.realtime.crossfire
com.realtime.crossfire.jxclient.shortcuts.Shortcut.getCommand
String getCommand()
Definition: Shortcut.java:76
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.shortcuts.Shortcuts.spellsManager
final SpellsManager spellsManager
Definition: Shortcuts.java:68
com.realtime.crossfire.jxclient.spells.SpellsManagerListener
Definition: SpellsManagerListener.java:31
com.realtime.crossfire.jxclient.shortcuts.Shortcuts.listeners
final EventListenerList2< ShortcutsListener > listeners
Definition: Shortcuts.java:62
com.realtime.crossfire.jxclient.shortcuts.Shortcuts.getFile
Path getFile()
Definition: Shortcuts.java:197
com.realtime.crossfire.jxclient.shortcuts.Shortcut.dispose
void dispose()
Definition: Shortcut.java:93
com.realtime.crossfire.jxclient.spells
Definition: Spell.java:23
com.realtime.crossfire.jxclient.shortcuts.Shortcuts.Shortcuts
Shortcuts(@NotNull final SpellsManager spellsManager)
Definition: Shortcuts.java:103