Crossfire JXClient, Trunk  R20561
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-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.shortcuts;
23 
28 import java.io.File;
29 import java.util.ArrayList;
30 import java.util.Iterator;
31 import java.util.List;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
34 
39 public class Shortcuts implements Iterable<Shortcut> {
40 
45  @NotNull
46  private final List<Shortcut> shortcuts = new ArrayList<>();
47 
52  private boolean modified;
53 
57  @Nullable
58  private File file;
59 
63  @NotNull
65 
69  @NotNull
70  private final CommandQueue commandQueue;
71 
75  @NotNull
77 
83  public Shortcuts(@NotNull final CommandQueue commandQueue, @NotNull final SpellsManager spellsManager) {
84  this.commandQueue = commandQueue;
85  this.spellsManager = spellsManager;
86  }
87 
91  public void clearShortcuts() {
92  if (shortcuts.isEmpty()) {
93  return;
94  }
95 
96  for (int i = 0; i < shortcuts.size(); i++) {
97  final Shortcut shortcut = shortcuts.get(i);
98  if (shortcut != null) {
99  for (final ShortcutsListener listener : listeners) {
100  listener.shortcutRemoved(i, shortcut);
101  }
102  shortcut.dispose();
103  }
104  }
105  shortcuts.clear();
106  modified = true;
107  }
108 
114  @Nullable
115  private Shortcut getShortcut(final int index) {
116  try {
117  return shortcuts.get(index);
118  } catch (final IndexOutOfBoundsException ignored) {
119  return null;
120  }
121  }
122 
128  public void setShortcut(final int index, @Nullable final Shortcut shortcut) {
129  while (shortcuts.size() <= index) {
130  shortcuts.add(null);
131  }
132 
133  final Shortcut oldShortcut = shortcuts.get(index);
134  if (oldShortcut != null) {
135  for (final ShortcutsListener listener : listeners) {
136  listener.shortcutRemoved(index, oldShortcut);
137  }
138  oldShortcut.dispose();
139  }
140  shortcuts.set(index, shortcut);
141  modified = true;
142  if (shortcut != null) {
143  for (final ShortcutsListener listener : listeners) {
144  listener.shortcutAdded(index, shortcut);
145  }
146  }
147  }
148 
153  public void unsetShortcut(final int index) {
154  setShortcut(index, null);
155  }
156 
164  public void setSpellShortcut(final int index, @NotNull final String spellName, final boolean cast) {
165  final Spell spell = spellsManager.getSpell(spellName);
166  setSpellShortcut(index, spell, cast);
167  }
168 
176  public void setSpellShortcut(final int index, @NotNull final Spell spell, final boolean cast) {
177  final ShortcutSpell shortcutSpell = new ShortcutSpell(commandQueue, spell);
178  shortcutSpell.setCast(cast);
179  setShortcut(index, shortcutSpell);
180  }
181 
187  public void setCommandShortcut(final int index, @NotNull final String command) {
188  if (command.length() <= 0) {
189  System.err.println("shortcut: ignoring empty command");
190  return;
191  }
192 
193  if (command.contains("\n")) {
194  System.err.println("shortcut: ignoring multi-line command");
195  return;
196  }
197 
198  final Shortcut shortcutCommand = new ShortcutCommand(commandQueue, command);
199  setShortcut(index, shortcutCommand);
200  }
201 
206  public void executeShortcut(final int index) {
207  final Shortcut shortcut = getShortcut(index);
208  if (shortcut != null) {
209  shortcut.execute();
210  }
211  }
212 
217  public void addShortcutsListener(@NotNull final ShortcutsListener listener) {
218  listeners.add(listener);
219  }
220 
225  public void removeShortcutsListener(@NotNull final ShortcutsListener listener) {
226  listeners.remove(listener);
227  }
228 
234  public boolean isModified() {
235  return modified;
236  }
237 
241  public void resetModified() {
242  modified = false;
243  }
244 
249  @Nullable
250  public File getFile() {
251  return file;
252  }
253 
258  public void setFile(@Nullable final File file) {
259  this.file = file;
260  }
261 
265  @Override
266  public Iterator<Shortcut> iterator() {
267  return shortcuts.iterator();
268  }
269 
270 }
void setShortcut(final int index, @Nullable final Shortcut shortcut)
Sets a Shortcut.
Definition: Shortcuts.java:128
final EventListenerList2< ShortcutsListener > listeners
The listeners to be notified.
Definition: Shortcuts.java:64
void setCast(final boolean cast)
Sets whether the spell should be "cast" or "invoked".
Shortcuts(@NotNull final CommandQueue commandQueue, @NotNull final SpellsManager spellsManager)
Creates a new instance.
Definition: Shortcuts.java:83
void resetModified()
Resets the modified state.
Definition: Shortcuts.java:241
void setSpellShortcut(final int index, @NotNull final Spell spell, final boolean cast)
Sets a Shortcut to a spell.
Definition: Shortcuts.java:176
final List< Shortcut > shortcuts
The shortcuts.
Definition: Shortcuts.java:46
abstract void execute()
Executes the shortcut.
boolean isModified()
Returns whether the shortcuts have been modified since creation or last call to resetModified().
Definition: Shortcuts.java:234
File getFile()
Returns the backing file.
Definition: Shortcuts.java:250
void clearShortcuts()
Clears all defined shortcuts.
Definition: Shortcuts.java:91
void setSpellShortcut(final int index, @NotNull final String spellName, final boolean cast)
Sets a Shortcut to a spell.
Definition: Shortcuts.java:164
void setCommandShortcut(final int index, @NotNull final String command)
Sets a Shortcut to a command.
Definition: Shortcuts.java:187
boolean modified
Whether the contents of shortcuts have been modified from the last saved state.
Definition: Shortcuts.java:52
void unsetShortcut(final int index)
Unsets a Shortcut.
Definition: Shortcuts.java:153
Describes a Crossfire spell.
Definition: Spell.java:36
Shortcut getShortcut(final int index)
Returns a shortcut.
Definition: Shortcuts.java:115
void add(@NotNull final T listener)
Adds a listener.
Interface for listeners for Shortcut changes.
final SpellsManager spellsManager
The SpellsManager instance to watch.
Definition: Shortcuts.java:76
final CommandQueue commandQueue
The command queue for executing commands.
Definition: Shortcuts.java:70
Abstract base class for shortcut commands.
Definition: Shortcut.java:32
void setFile(@Nullable final File file)
Sets the backing file.
Definition: Shortcuts.java:258
A Shortcut that executes a Crossfire command.
Maintains the pending (ncom) commands sent to the server.
void addShortcutsListener(@NotNull final ShortcutsListener listener)
Adds a ShortcutsListener.
Definition: Shortcuts.java:217
void remove(@NotNull final T listener)
Removes a listener.
void removeShortcutsListener(@NotNull final ShortcutsListener listener)
Removes a ShortcutsListener.
Definition: Shortcuts.java:225
abstract void dispose()
Releases all allocated resources.
Spell getSpell(@NotNull final String spellName)
Returns a Spell instance by spell name.
void executeShortcut(final int index)
Executes a shortcut.
Definition: Shortcuts.java:206